Change core store setup

This commit is contained in:
Jim Laurie 2018-04-24 12:30:43 +02:00
parent b34ac11e10
commit b7a2778e89
2 changed files with 124 additions and 161 deletions

View File

@ -199,8 +199,8 @@ class Strapi extends EventEmitter {
// Usage.
await utils.usage.call(this);
// Init core store manager
await store.pre.call(this);
// Init core store
await store.call(this);
// Initialize hooks and middlewares.
await Promise.all([
@ -208,9 +208,6 @@ class Strapi extends EventEmitter {
initializeHooks.call(this)
]);
// Core store post middleware and hooks init validation.
await store.post.call(this);
// Harmonize plugins configuration.
await plugins.call(this);
}

View File

@ -1,171 +1,137 @@
'use strict';
module.exports = {
pre: function () {
return new Promise((resolve, reject) => {
this.models['core_store'] = {
connection: 'default',
info: {
name: 'core_store',
description: ''
module.exports = function () {
return new Promise((resolve, reject) => {
this.models['core_store'] = {
connection: 'default',
info: {
name: 'core_store',
description: ''
},
attributes: {
key: {
type: 'string'
},
attributes: {
key: {
type: 'string'
},
value: {
type: 'string'
},
type: {
type: 'string'
},
environment: {
type: 'string'
},
tag: {
type: 'string'
value: {
type: 'string'
},
type: {
type: 'string'
},
environment: {
type: 'string'
},
tag: {
type: 'string'
}
},
globalId: 'StrapiConfigs',
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 get = async (params = {}) => {
Object.assign(source, params);
const set = async (params = {}) => {
Object.assign(source, params);
const {
key,
environment = strapi.config.environment,
type = 'core',
name = '',
tag = ''
} = source;
const {
key,
value,
environment = strapi.config.environment,
type,
name,
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}`,
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 where = {
key: `${prefix}_${key}`,
environment,
tag
};
const set = async (params = {}) => {
Object.assign(source, params);
const {
key,
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);
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();
}
};
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();
});
};