Skip to content

Commit

Permalink
Merge pull request #111 from boazpoolman/feature/fix-import-on-bootstrap
Browse files Browse the repository at this point in the history
Feature/fix import on bootstrap
  • Loading branch information
MSACC authored Oct 21, 2023
2 parents 88a40e7 + 19f5ba1 commit a4b767f
Show file tree
Hide file tree
Showing 9 changed files with 1,239 additions and 945 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"eslint": "eslint --max-warnings=0 './**/*.{js,jsx}'",
"eslint:fix": "eslint --fix './**/*.{js,jsx}'",
"test:unit": "jest --verbose",
"test:integration": "cd playground && node_modules/.bin/jest --verbose",
"test:integration": "cd playground && node_modules/.bin/jest --verbose --forceExit --detectOpenHandles",
"plugin:install": "yarn install && rm -rf node_modules/@strapi/helper-plugin",
"playground:install": "cd playground && yarn install",
"playground:build": "cd playground && yarn build",
Expand Down Expand Up @@ -73,7 +73,7 @@
"eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-plugin-react": "^7.21.5",
"eslint-plugin-react-hooks": "^2.3.0",
"jest": "^29.3.1",
"jest": "^29.7.0",
"jest-cli": "^29.3.1",
"jest-styled-components": "^7.0.2",
"lodash": "^4.17.11",
Expand Down
5 changes: 2 additions & 3 deletions playground/__tests__/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const exec = util.promisify(require('child_process').exec);
jest.setTimeout(20000);

describe('Test the config-sync CLI', () => {

afterAll(async () => {
// Remove the generated files and the DB.
await exec('rm -rf config/sync');
Expand All @@ -33,8 +32,8 @@ describe('Test the config-sync CLI', () => {
let error;
try {
await exec('yarn cs diff');
} catch(e) {
error = e;
} catch (e) {
error = e;
}
expect(error).toHaveProperty('code', 1);
});
Expand Down
34 changes: 34 additions & 0 deletions playground/__tests__/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const fs = require('fs');
const Strapi = require('@strapi/strapi');

let instance;

async function setupStrapi() {
if (!instance) {
await Strapi().load();
instance = strapi;

await instance.server.mount();
}
return instance;
}

async function cleanupStrapi() {
const dbSettings = strapi.config.get('database.connection');

// close server to release the db-file.
await strapi.server.httpServer.close();

// close the connection to the database before deletion.
await strapi.db.connection.destroy();

// delete test database after all tests have completed.
if (dbSettings && dbSettings.connection && dbSettings.connection.filename) {
const tmpDbFile = dbSettings.connection.filename;
if (fs.existsSync(tmpDbFile)) {
fs.unlinkSync(tmpDbFile);
}
}
}

module.exports = { setupStrapi, cleanupStrapi };
48 changes: 48 additions & 0 deletions playground/__tests__/import-on-boostrap.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const util = require('util');
const exec = util.promisify(require('child_process').exec);

const { setupStrapi, cleanupStrapi } = require('./helpers');

jest.setTimeout(20000);

afterEach(async () => {
// Disable importOnBootstrap
await exec('sed -i "s/importOnBootstrap: true/importOnBootstrap: false/g" config/plugins.js');

await cleanupStrapi();
await exec('rm -rf config/sync');
});

describe('Test the importOnBootstrap feature', () => {
test('Without a database', async () => {
// Do the initial export and remove the database.
await exec('yarn cs export -y');
await exec('rm -rf .tmp');

// Manually change the plugins.js to enable importOnBoostrap.
await exec('sed -i "s/importOnBootstrap: false/importOnBootstrap: true/g" config/plugins.js');

// Start up Strapi to initiate the importOnBootstrap function.
await setupStrapi();

expect(strapi).toBeDefined();
});

test('With a database', async () => {
// Delete any existing database and do an export.
await exec('rm -rf .tmp');
await exec('yarn cs export -y');

// Manually change the plugins.js to enable importOnBoostrap.
await exec('sed -i "s/importOnBootstrap: false/importOnBootstrap: true/g" config/plugins.js');

// Remove a config file to make sure the importOnBoostrap
// function actually attempts to import.
await exec('rm -rf config/sync/admin-role.strapi-editor.json');

// Start up Strapi to initiate the importOnBootstrap function.
await setupStrapi();

expect(strapi).toBeDefined();
});
});
8 changes: 8 additions & 0 deletions playground/config/plugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
'config-sync': {
enabled: true,
config: {
importOnBootstrap: false,
},
},
};
5 changes: 3 additions & 2 deletions playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
"cs": "config-sync"
},
"devDependencies": {
"jest": "^26.0.1",
"jest-cli": "^26.0.1"
"jest": "^29.7.0",
"jest-cli": "^29.7.0",
"supertest": "^6.3.3"
},
"dependencies": {
"@strapi/plugin-i18n": "^4.14.4",
Expand Down
22 changes: 7 additions & 15 deletions server/config/type.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { isEmpty } = require('lodash');
const { logMessage, sanitizeConfig, dynamicSort, noLimit, getCombinedUid, getCombinedUidWhereFilter, getUidParamsFromName } = require('../utils');
const { difference, same } = require('../utils/getArrayDiff');
const queryFallBack = require('../utils/queryFallBack');

const ConfigType = class ConfigType {
constructor({ queryString, configName, uid, jsonFields, relations, components }) {
Expand Down Expand Up @@ -69,11 +70,11 @@ const ConfigType = class ConfigType {
});

await Promise.all(relations.map(async (relation) => {
await strapi.entityService.delete(queryString, relation.id);
await queryFallBack.delete(queryString, relation.id);
}));
}));

await strapi.entityService.delete(this.queryString, existingConfig.id);
await queryFallBack.delete(this.queryString, existingConfig.id);

return;
}
Expand All @@ -86,15 +87,15 @@ const ConfigType = class ConfigType {

// Create entity.
this.relations.map(({ relationName }) => delete query[relationName]);
const newEntity = await strapi.entityService.create(this.queryString, {
const newEntity = await queryFallBack.create(this.queryString, {
data: query,
});

// Create relation entities.
await Promise.all(this.relations.map(async ({ queryString, relationName, parentName }) => {
await Promise.all(configContent[relationName].map(async (relationEntity) => {
const relationQuery = { ...relationEntity, [parentName]: newEntity };
await strapi.entityService.create(queryString, {
await queryFallBack.create(queryString, {
data: relationQuery,
});
}));
Expand All @@ -110,16 +111,7 @@ const ConfigType = class ConfigType {

// Update entity.
this.relations.map(({ relationName }) => delete query[relationName]);

const entity = await queryAPI.findOne({ where: combinedUidWhereFilter });
try {
await strapi.entityService.update(this.queryString, entity.id, {
data: query,
});
} catch (error) {
console.warn(logMessage(`Use Query Engine API instead of Entity Service API for type ${this.configPrefix}`));
await queryAPI.update({ where: combinedUidWhereFilter, data: query });
}
const entity = queryFallBack.update(this.queryString, { where: combinedUidWhereFilter, data: query });

// Delete/create relations.
await Promise.all(this.relations.map(async ({ queryString, relationName, parentName, relationSortFields }) => {
Expand All @@ -145,7 +137,7 @@ const ConfigType = class ConfigType {
}));

await Promise.all(configToAdd.map(async (config) => {
await strapi.entityService.create(queryString, {
await queryFallBack.create(queryString, {
data: { ...config, [parentName]: entity.id },
});
}));
Expand Down
32 changes: 32 additions & 0 deletions server/utils/queryFallBack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const queryFallBack = {
create: async (queryString, options) => {
try {
const newEntity = await strapi.entityService.create(queryString, options);

return newEntity;
} catch (e) {
return strapi.query(queryString).create(options);
}
},
update: async (queryString, options) => {
try {
const entity = await strapi.query(queryString).findOne(options.where);
const updatedEntity = await strapi.entityService.update(queryString, entity.id);

return updatedEntity;
} catch (e) {
return strapi.query(queryString).update(options);
}
},
delete: async (queryString, id) => {
try {
await strapi.entityService.delete(queryString, id);
} catch (e) {
await strapi.query(queryString).delete({
where: { id },
});
}
},
};

module.exports = queryFallBack;
Loading

0 comments on commit a4b767f

Please sign in to comment.