removes the need to pass full colletions to registerCollectionSchema

This commit is contained in:
James
2019-12-23 12:55:28 -05:00
parent dcfa909a35
commit 8f0426fbc8
4 changed files with 16 additions and 10 deletions

View File

@@ -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),
};
};

View File

@@ -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 }));
};

View File

@@ -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);
}