removes the need to pass full colletions to registerCollectionSchema
This commit is contained in:
@@ -7,7 +7,7 @@ import localizationPlugin from '../localization/plugin';
|
||||
import autopopulate from '../mongoose/autopopulate';
|
||||
import passwordResetConfig from '../auth/passwordResets/config';
|
||||
|
||||
const addSchema = (collection, collections, config) => {
|
||||
const addSchema = (collection, config) => {
|
||||
const fields = { ...baseFields };
|
||||
|
||||
if (collection.auth) {
|
||||
@@ -32,11 +32,8 @@ const addSchema = (collection, collections, config) => {
|
||||
}
|
||||
|
||||
return {
|
||||
...collections,
|
||||
[collection.slug]: {
|
||||
config: collection,
|
||||
model: mongoose.model(collection.slug, Schema),
|
||||
},
|
||||
config: collection,
|
||||
model: mongoose.model(collection.slug, Schema),
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -58,13 +58,18 @@ export const fetch = (req, res) => {
|
||||
|
||||
findOne(query, createAutopopulateOptions(query))
|
||||
.then((doc) => {
|
||||
if (doc[req.params.key]) {
|
||||
return res.json(doc[req.params.key]);
|
||||
const globals = { ...doc };
|
||||
delete globals._id;
|
||||
delete globals.id;
|
||||
delete globals.__v;
|
||||
|
||||
if (globals[req.params.key]) {
|
||||
return res.json(globals[req.params.key]);
|
||||
} if (req.params.key) {
|
||||
return res.status(httpStatus.NOT_FOUND)
|
||||
.json({ error: 'not found' });
|
||||
}
|
||||
return res.json(doc);
|
||||
return res.json(globals);
|
||||
})
|
||||
.catch(err => res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ error: err }));
|
||||
};
|
||||
|
||||
@@ -16,6 +16,8 @@ import registerGlobalSchema from './globals/registerSchema';
|
||||
import registerGlobalRoutes from './globals/registerRoutes';
|
||||
|
||||
class Payload {
|
||||
collections = {};
|
||||
|
||||
constructor(options) {
|
||||
this.config = options.config;
|
||||
this.app = options.app;
|
||||
@@ -35,7 +37,8 @@ class Payload {
|
||||
|
||||
registerCollection = (collection) => {
|
||||
validateCollection(collection, this.collections, this.User);
|
||||
this.collections = registerCollectionSchema(collection, this.collections, this.config);
|
||||
const registeredCollection = registerCollectionSchema(collection, this.config);
|
||||
this.collections[collection.slug] = registeredCollection;
|
||||
registerCollectionRoutes(this.collections[collection.slug], this.router);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user