Skip to content
This repository was archived by the owner on Sep 16, 2025. It is now read-only.

Commit 78ca3f6

Browse files
refactor(bootstrap): move build and lifecycle setup to separate files
refactor(settingsService): add clear distinction between model types
1 parent f1ff933 commit 78ca3f6

File tree

9 files changed

+64
-53
lines changed

9 files changed

+64
-53
lines changed

server/bootstrap.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

server/bootstrap/buildSettings.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const { getPluginService } = require('../utils/getPluginService');
2+
const buildSettings = async () => {
3+
const settingsService = getPluginService('settingsService');
4+
const settings = await settingsService.get();
5+
6+
// build settings structure
7+
const normalizedSettings = settingsService.build(settings);
8+
9+
// reset plugin settings
10+
await settingsService.set(normalizedSettings);
11+
12+
return normalizedSettings;
13+
};
14+
15+
module.exports = {
16+
buildSettings,
17+
};

server/bootstrap/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
const { buildSettings } = require('./buildSettings');
4+
const { setupLifecycles } = require('./setupLifecycles');
5+
6+
module.exports = async ({ strapi }) => {
7+
const settings = await buildSettings(strapi);
8+
setupLifecycles(settings);
9+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const _ = require('lodash');
2+
const { getPluginService } = require('../utils/getPluginService');
3+
const setupLifecycles = (settings) => {
4+
// set up lifecycles
5+
const subscribe = {
6+
models: _.map(settings.modelsByUID, (model) => model.uid),
7+
};
8+
9+
['beforeCreate', 'afterCreate', 'beforeUpdate'].forEach((lifecycle) => {
10+
subscribe[lifecycle] = (ctx) => getPluginService('slugService').slugify(ctx);
11+
});
12+
13+
strapi.db.lifecycles.subscribe(subscribe);
14+
};
15+
16+
module.exports = {
17+
setupLifecycles,
18+
};

server/controllers/slug-controller.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ const { hasRequiredModelScopes } = require('../utils/hasRequiredModelScopes');
1010

1111
module.exports = ({ strapi }) => ({
1212
async findSlug(ctx) {
13-
const { models } = getPluginService('settingsService').get();
13+
const { modelsByName } = getPluginService('settingsService').get();
1414
const { modelName, slug } = ctx.request.params;
1515
const { auth } = ctx.state;
1616

1717
isValidFindSlugParams({
1818
modelName,
1919
slug,
20-
models,
20+
modelsByName,
2121
});
2222

23-
const { uid, field, contentType } = models[modelName];
23+
const { uid, field, contentType } = modelsByName[modelName];
2424

2525
await hasRequiredModelScopes(strapi, uid, auth);
2626

server/graphql/types.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ const { sanitizeOutput } = require('../utils/sanitizeOutput');
77
const getCustomTypes = (strapi, nexus) => {
88
const { naming } = getPluginService('utils', 'graphql');
99
const { toEntityResponse } = getPluginService('format', 'graphql').returnTypes;
10-
const { models } = getPluginService('settingsService').get();
10+
const { modelsByUID } = getPluginService('settingsService').get();
1111
const { getEntityResponseName } = naming;
1212

1313
// get all types required for findSlug query
1414
let findSlugTypes = {
1515
response: [],
1616
};
17-
_.forEach(strapi.contentTypes, (value, key) => {
18-
if (models[key]) {
19-
findSlugTypes.response.push(getEntityResponseName(value));
17+
_.forEach(strapi.contentTypes, (contentType, uid) => {
18+
if (modelsByUID[uid]) {
19+
findSlugTypes.response.push(getEntityResponseName(contentType));
2020
}
2121
});
2222

@@ -33,7 +33,7 @@ const getCustomTypes = (strapi, nexus) => {
3333
t.members(...findSlugTypes.response);
3434
},
3535
resolveType: (ctx) => {
36-
return getEntityResponseName(models[ctx.info.resourceUID].contentType);
36+
return getEntityResponseName(modelsByUID[ctx.info.resourceUID].contentType);
3737
},
3838
});
3939

@@ -50,18 +50,18 @@ const getCustomTypes = (strapi, nexus) => {
5050
publicationState: nexus.stringArg('The publication state of the entry'),
5151
},
5252
resolve: async (_parent, args, ctx) => {
53-
const { models } = getPluginService('settingsService').get();
53+
const { modelsByName } = getPluginService('settingsService').get();
5454
const { modelName, slug, publicationState } = args;
5555
const { auth } = ctx.state;
5656

5757
isValidFindSlugParams({
5858
modelName,
5959
slug,
60-
models,
60+
modelsByName,
6161
publicationState,
6262
});
6363

64-
const { uid, field, contentType } = models[modelName];
64+
const { uid, field, contentType } = modelsByName[modelName];
6565

6666
await hasRequiredModelScopes(strapi, uid, auth);
6767

server/services/settings-service.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ module.exports = ({ strapi }) => ({
1313
},
1414
build(settings) {
1515
// build models
16-
settings.models = {};
16+
settings.modelsByUID = {};
17+
settings.modelsByName = {};
1718
_.each(strapi.contentTypes, (contentType, uid) => {
1819
const model = settings.contentTypes[contentType.modelName];
1920
if (!model) {
@@ -30,7 +31,9 @@ module.exports = ({ strapi }) => ({
3031
}
3132

3233
let references = _.isArray(model.references) ? model.references : [model.references];
33-
const hasReferences = references.every((r) => isValidModelField(contentType, r));
34+
const hasReferences = references.every((referenceField) =>
35+
isValidModelField(contentType, referenceField)
36+
);
3437
if (!hasReferences) {
3538
strapi.log.warn(
3639
`[slugify] skipping ${contentType.info.singularName} registration, invalid reference field provided.`
@@ -44,8 +47,8 @@ module.exports = ({ strapi }) => ({
4447
contentType,
4548
references,
4649
};
47-
settings.models[uid] = data;
48-
settings.models[contentType.modelName] = data;
50+
settings.modelsByUID[uid] = data;
51+
settings.modelsByName[contentType.modelName] = data;
4952
});
5053

5154
_.omit(settings, ['contentTypes']);

server/utils/constants.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

server/utils/isValidFindSlugParams.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ const isValidFindSlugParams = (params) => {
66
throw new ValidationError('A model and slug must be provided.');
77
}
88

9-
const { modelName, slug, models, publicationState } = params;
10-
const model = models[modelName];
9+
const { modelName, slug, modelsByName, publicationState } = params;
10+
const model = modelsByName[modelName];
1111

1212
if (!modelName) {
1313
throw new ValidationError('A model name path variable is required.');

0 commit comments

Comments
 (0)