From 8f0426fbc8cbcc1ea99d882298d58382f4e5e21d Mon Sep 17 00:00:00 2001 From: James Date: Mon, 23 Dec 2019 12:55:28 -0500 Subject: [PATCH] removes the need to pass full colletions to registerCollectionSchema --- demo/content-blocks/Quote.js | 1 + src/collections/registerSchema.js | 9 +++------ src/globals/requestHandlers.js | 11 ++++++++--- src/index.js | 5 ++++- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/demo/content-blocks/Quote.js b/demo/content-blocks/Quote.js index a8da97814f..fdbb3a0270 100644 --- a/demo/content-blocks/Quote.js +++ b/demo/content-blocks/Quote.js @@ -4,6 +4,7 @@ module.exports = { singular: 'Quote', plural: 'Quotes', }, + useAsContentBlock: true, policies: { create: (req, res, next) => { return next(); diff --git a/src/collections/registerSchema.js b/src/collections/registerSchema.js index 3953840cea..3f6c032031 100644 --- a/src/collections/registerSchema.js +++ b/src/collections/registerSchema.js @@ -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), }; }; diff --git a/src/globals/requestHandlers.js b/src/globals/requestHandlers.js index a49b68d957..5e43e8a47b 100644 --- a/src/globals/requestHandlers.js +++ b/src/globals/requestHandlers.js @@ -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 })); }; diff --git a/src/index.js b/src/index.js index 1865f34dba..b35eb84030 100644 --- a/src/index.js +++ b/src/index.js @@ -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); }