From b7a2778e89478c9a2fdcdf4f94af0be9abf2bc7d Mon Sep 17 00:00:00 2001 From: Jim Laurie Date: Tue, 24 Apr 2018 12:30:43 +0200 Subject: [PATCH] Change core store setup --- packages/strapi/lib/Strapi.js | 7 +- packages/strapi/lib/core/store.js | 278 +++++++++++++----------------- 2 files changed, 124 insertions(+), 161 deletions(-) diff --git a/packages/strapi/lib/Strapi.js b/packages/strapi/lib/Strapi.js index db99cba8b4..8147444eb6 100755 --- a/packages/strapi/lib/Strapi.js +++ b/packages/strapi/lib/Strapi.js @@ -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); } diff --git a/packages/strapi/lib/core/store.js b/packages/strapi/lib/core/store.js index a5285718fd..e0c0ac2878 100644 --- a/packages/strapi/lib/core/store.js +++ b/packages/strapi/lib/core/store.js @@ -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(); + }); };