mirror of
https://github.com/strapi/strapi.git
synced 2025-11-07 21:58:23 +00:00
Change core store setup
This commit is contained in:
parent
b34ac11e10
commit
b7a2778e89
@ -199,8 +199,8 @@ class Strapi extends EventEmitter {
|
|||||||
// Usage.
|
// Usage.
|
||||||
await utils.usage.call(this);
|
await utils.usage.call(this);
|
||||||
|
|
||||||
// Init core store manager
|
// Init core store
|
||||||
await store.pre.call(this);
|
await store.call(this);
|
||||||
|
|
||||||
// Initialize hooks and middlewares.
|
// Initialize hooks and middlewares.
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
@ -208,9 +208,6 @@ class Strapi extends EventEmitter {
|
|||||||
initializeHooks.call(this)
|
initializeHooks.call(this)
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Core store post middleware and hooks init validation.
|
|
||||||
await store.post.call(this);
|
|
||||||
|
|
||||||
// Harmonize plugins configuration.
|
// Harmonize plugins configuration.
|
||||||
await plugins.call(this);
|
await plugins.call(this);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,171 +1,137 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
module.exports = {
|
module.exports = function () {
|
||||||
pre: function () {
|
return new Promise((resolve, reject) => {
|
||||||
return new Promise((resolve, reject) => {
|
this.models['core_store'] = {
|
||||||
this.models['core_store'] = {
|
connection: 'default',
|
||||||
connection: 'default',
|
info: {
|
||||||
info: {
|
name: 'core_store',
|
||||||
name: 'core_store',
|
description: ''
|
||||||
description: ''
|
},
|
||||||
|
attributes: {
|
||||||
|
key: {
|
||||||
|
type: 'string'
|
||||||
},
|
},
|
||||||
attributes: {
|
value: {
|
||||||
key: {
|
type: 'string'
|
||||||
type: 'string'
|
},
|
||||||
},
|
type: {
|
||||||
value: {
|
type: 'string'
|
||||||
type: 'string'
|
},
|
||||||
},
|
environment: {
|
||||||
type: {
|
type: 'string'
|
||||||
type: 'string'
|
},
|
||||||
},
|
tag: {
|
||||||
environment: {
|
type: 'string'
|
||||||
type: 'string'
|
}
|
||||||
},
|
},
|
||||||
tag: {
|
globalId: 'StrapiConfigs',
|
||||||
type: 'string'
|
collectionName: 'core_store'
|
||||||
|
};
|
||||||
|
|
||||||
|
this.store = (source = {}) => {
|
||||||
|
const get = async (params = {}) => {
|
||||||
|
Object.assign(source, params);
|
||||||
|
|
||||||
|
const {
|
||||||
|
key,
|
||||||
|
environment = strapi.config.environment,
|
||||||
|
type = 'core',
|
||||||
|
name = '',
|
||||||
|
tag = ''
|
||||||
|
} = source;
|
||||||
|
|
||||||
|
const prefix = `${type}${name ? `_${name}` : ''}`;
|
||||||
|
|
||||||
|
const findAction = strapi.models['core_store'].orm === 'mongoose' ? 'findOne' : 'forge';
|
||||||
|
|
||||||
|
const where = {
|
||||||
|
key: `${prefix}_${key}`,
|
||||||
|
environment,
|
||||||
|
tag
|
||||||
|
};
|
||||||
|
|
||||||
|
const data = strapi.models['core_store'].orm === 'mongoose'
|
||||||
|
? await strapi.models['core_store'].findOne(where)
|
||||||
|
: await strapi.models['core_store'].forge(where).fetch().then(config => {
|
||||||
|
if (config) {
|
||||||
|
return config.toJSON();
|
||||||
}
|
}
|
||||||
},
|
});
|
||||||
globalId: 'StrapiConfigs',
|
|
||||||
collectionName: 'core_store'
|
if (!data) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data.type === 'object' || data.type === 'array' || data.type === 'boolean') {
|
||||||
|
try {
|
||||||
|
return JSON.parse(data.value);
|
||||||
|
} catch (err) {
|
||||||
|
return new Date(data.value);
|
||||||
|
}
|
||||||
|
} else if (data.type === 'number') {
|
||||||
|
return parseFloat(data.value);
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
this.store = (source = {}) => {
|
const set = async (params = {}) => {
|
||||||
const get = async (params = {}) => {
|
Object.assign(source, params);
|
||||||
Object.assign(source, params);
|
|
||||||
|
|
||||||
const {
|
const {
|
||||||
key,
|
key,
|
||||||
environment = strapi.config.environment,
|
value,
|
||||||
type = 'core',
|
environment = strapi.config.environment,
|
||||||
name = '',
|
type,
|
||||||
tag = ''
|
name,
|
||||||
} = source;
|
tag = ''
|
||||||
|
} = source;
|
||||||
|
|
||||||
const prefix = `${type}${name ? `_${name}` : ''}`;
|
const prefix = `${type}${name ? `_${name}` : ''}`;
|
||||||
|
|
||||||
const findAction = strapi.models['core_store'].orm === 'mongoose' ? 'findOne' : 'forge';
|
const where = {
|
||||||
|
key: `${prefix}_${key}`,
|
||||||
const where = {
|
environment,
|
||||||
key: `${prefix}_${key}`,
|
tag
|
||||||
environment,
|
|
||||||
tag
|
|
||||||
};
|
|
||||||
|
|
||||||
const data = strapi.models['core_store'].orm === 'mongoose'
|
|
||||||
? await strapi.models['core_store'].findOne(where)
|
|
||||||
: await strapi.models['core_store'].forge(where).fetch().then(config => {
|
|
||||||
if (config) {
|
|
||||||
return config.toJSON();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!data) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data.type === 'object' || data.type === 'array' || data.type === 'boolean') {
|
|
||||||
try {
|
|
||||||
return JSON.parse(data.value);
|
|
||||||
} catch (err) {
|
|
||||||
return new Date(data.value);
|
|
||||||
}
|
|
||||||
} else if (data.type === 'number') {
|
|
||||||
return parseFloat(data.value);
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const set = async (params = {}) => {
|
let data = strapi.models['core_store'].orm === 'mongoose'
|
||||||
Object.assign(source, params);
|
? await strapi.models['core_store'].findOne(where)
|
||||||
|
: await strapi.models['core_store'].forge(where).fetch().then(config => {
|
||||||
const {
|
if (config) {
|
||||||
key,
|
return config.toJSON();
|
||||||
value,
|
|
||||||
environment = strapi.config.environment,
|
|
||||||
type,
|
|
||||||
name,
|
|
||||||
tag = ''
|
|
||||||
} = source;
|
|
||||||
|
|
||||||
const prefix = `${type}${name ? `_${name}` : ''}`;
|
|
||||||
|
|
||||||
const where = {
|
|
||||||
key: `${prefix}_${key}`,
|
|
||||||
environment,
|
|
||||||
tag
|
|
||||||
};
|
|
||||||
|
|
||||||
let data = strapi.models['core_store'].orm === 'mongoose'
|
|
||||||
? await strapi.models['core_store'].findOne(where)
|
|
||||||
: await strapi.models['core_store'].forge(where).fetch().then(config => {
|
|
||||||
if (config) {
|
|
||||||
return config.toJSON();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (data) {
|
|
||||||
Object.assign(data, {
|
|
||||||
value: JSON.stringify(value) || value.toString(),
|
|
||||||
type: (typeof value).toString()
|
|
||||||
});
|
|
||||||
|
|
||||||
strapi.models['core_store'].orm === 'mongoose'
|
|
||||||
? await strapi.models['core_store'].update({ _id: data._id }, data, { strict: false })
|
|
||||||
: await strapi.models['core_store'].forge({ id: data.id }).save(data, { patch: true });
|
|
||||||
} else {
|
|
||||||
Object.assign(where, {
|
|
||||||
value: JSON.stringify(value) || value.toString(),
|
|
||||||
type: (typeof value).toString(),
|
|
||||||
tag
|
|
||||||
});
|
|
||||||
|
|
||||||
strapi.models['core_store'].orm === 'mongoose'
|
|
||||||
? await strapi.models['core_store'].create(where)
|
|
||||||
: await strapi.models['core_store'].forge().save(where);
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
return {
|
|
||||||
get,
|
|
||||||
set
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
},
|
|
||||||
post: function () {
|
|
||||||
return new Promise(async (resolve, reject) => {
|
|
||||||
const Model = this.models['core_store'];
|
|
||||||
|
|
||||||
if (Model.orm !== 'bookshelf') {
|
|
||||||
return resolve();
|
|
||||||
}
|
|
||||||
|
|
||||||
const hasTable = await this.connections[Model.connection].schema.hasTable(Model.tableName || Model.collectionName);
|
|
||||||
|
|
||||||
if (!hasTable) {
|
|
||||||
const quote = Model.client === 'pg' ? '"' : '`';
|
|
||||||
|
|
||||||
this.connections[Model.connection].raw(`
|
|
||||||
CREATE TABLE ${quote}${Model.tableName || Model.collectionName}${quote} (
|
|
||||||
id ${Model.client === 'pg' ? 'SERIAL' : 'INT AUTO_INCREMENT'} NOT NULL PRIMARY KEY,
|
|
||||||
${quote}key${quote} text,
|
|
||||||
${quote}value${quote} text,
|
|
||||||
environment text,
|
|
||||||
type text,
|
|
||||||
tag text
|
|
||||||
);
|
|
||||||
|
|
||||||
ALTER TABLE ${quote}${Model.tableName || Model.collectionName}${quote} ADD COLUMN ${quote}parent${quote} integer, ADD FOREIGN KEY (${quote}parent${quote}) REFERENCES ${quote}${Model.tableName || Model.collectionName}${quote}(${quote}id${quote});
|
|
||||||
`).then(() => {
|
|
||||||
resolve();
|
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
resolve();
|
if (data) {
|
||||||
|
Object.assign(data, {
|
||||||
|
value: JSON.stringify(value) || value.toString(),
|
||||||
|
type: (typeof value).toString()
|
||||||
|
});
|
||||||
|
|
||||||
|
strapi.models['core_store'].orm === 'mongoose'
|
||||||
|
? await strapi.models['core_store'].update({ _id: data._id }, data, { strict: false })
|
||||||
|
: await strapi.models['core_store'].forge({ id: data.id }).save(data, { patch: true });
|
||||||
|
} else {
|
||||||
|
Object.assign(where, {
|
||||||
|
value: JSON.stringify(value) || value.toString(),
|
||||||
|
type: (typeof value).toString(),
|
||||||
|
tag
|
||||||
|
});
|
||||||
|
|
||||||
|
strapi.models['core_store'].orm === 'mongoose'
|
||||||
|
? await strapi.models['core_store'].create(where)
|
||||||
|
: await strapi.models['core_store'].forge().save(where);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
get,
|
||||||
|
set
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}
|
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user