From 9e9d8186783f872dfd2791565074c0f1ccad7afc Mon Sep 17 00:00:00 2001 From: ochicf Date: Fri, 10 Aug 2018 11:24:38 +0200 Subject: [PATCH 1/4] collectionName can now be specified as an argument --- packages/vulcan-core/lib/modules/default_mutations.js | 2 +- packages/vulcan-core/lib/modules/default_resolvers.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/vulcan-core/lib/modules/default_mutations.js b/packages/vulcan-core/lib/modules/default_mutations.js index e3ba55f70..df08d5125 100644 --- a/packages/vulcan-core/lib/modules/default_mutations.js +++ b/packages/vulcan-core/lib/modules/default_mutations.js @@ -16,7 +16,7 @@ export function getDefaultMutations (options) { if (typeof arguments[0] === 'object') { // new single-argument API typeName = arguments[0].typeName; - collectionName = getCollectionName(typeName); // TODO: find more reliable way to get type name from collection name? + collectionName = arguments[0].collectionName || getCollectionName(typeName); mutationOptions = arguments[0].options || defaultOptions; } else { // OpenCRUD backwards compatibility diff --git a/packages/vulcan-core/lib/modules/default_resolvers.js b/packages/vulcan-core/lib/modules/default_resolvers.js index 8b5097c23..87316b39c 100644 --- a/packages/vulcan-core/lib/modules/default_resolvers.js +++ b/packages/vulcan-core/lib/modules/default_resolvers.js @@ -18,7 +18,7 @@ export function getDefaultResolvers(options) { if (typeof arguments[0] === 'object') { // new single-argument API typeName = arguments[0].typeName; - collectionName = getCollectionName(typeName); // TODO: find more reliable way to get type name from collection name + collectionName = arguments[0].collectionName || getCollectionName(typeName); resolverOptions = arguments[0].options || defaultOptions; } else { // OpenCRUD backwards compatibility From b55096855e3bcabcfdc2263af9221e98d4d316c1 Mon Sep 17 00:00:00 2001 From: ochicf Date: Fri, 10 Aug 2018 11:24:50 +0200 Subject: [PATCH 2/4] added TODOs --- packages/vulcan-lib/lib/modules/collections.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/vulcan-lib/lib/modules/collections.js b/packages/vulcan-lib/lib/modules/collections.js index 7d95e7575..429f5b2db 100644 --- a/packages/vulcan-lib/lib/modules/collections.js +++ b/packages/vulcan-lib/lib/modules/collections.js @@ -20,8 +20,10 @@ export const Collections = []; export const getCollection = name => Collections.find(({ options: { collectionName }}) => name === collectionName || name === collectionName.toLowerCase()); +// TODO: find more reliable way to get collection name from type name? export const getCollectionName = typeName => Utils.pluralize(typeName); +// TODO: find more reliable way to get type name from collection name? export const getTypeName = collectionName => collectionName.slice(0,-1); /** From b31ff85b8c8247583057a60a564f66b0bbda6c21 Mon Sep 17 00:00:00 2001 From: ochicf Date: Fri, 10 Aug 2018 11:39:20 +0200 Subject: [PATCH 3/4] merge mutation and resolver options This makes possible to only specify a subset of options. As an example, imagine we want to pass a customized `updateCheck` function to `getDefaultMutations`. Before this commit options weren't merged, so all of them had to be specified: ``` getDefaultMutations({ typeName, collectionName, options: { create: true, update: true, upsert: true, delete: true, updateCheck: () => true, }); ``` After the commit: ``` getDefaultMutations({ typeName, collectionName, options: { updateCheck: () => true, }); ``` --- packages/vulcan-core/lib/modules/default_mutations.js | 4 ++-- packages/vulcan-core/lib/modules/default_resolvers.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/vulcan-core/lib/modules/default_mutations.js b/packages/vulcan-core/lib/modules/default_mutations.js index df08d5125..c52737746 100644 --- a/packages/vulcan-core/lib/modules/default_mutations.js +++ b/packages/vulcan-core/lib/modules/default_mutations.js @@ -17,12 +17,12 @@ export function getDefaultMutations (options) { // new single-argument API typeName = arguments[0].typeName; collectionName = arguments[0].collectionName || getCollectionName(typeName); - mutationOptions = arguments[0].options || defaultOptions; + mutationOptions = { ...defaultOptions, ...arguments[0].options }; } else { // OpenCRUD backwards compatibility collectionName = arguments[0]; typeName = getTypeName(collectionName); - mutationOptions = arguments[1] || defaultOptions; + mutationOptions = { ...defaultOptions, ...arguments[1] }; } // register callbacks for documentation purposes diff --git a/packages/vulcan-core/lib/modules/default_resolvers.js b/packages/vulcan-core/lib/modules/default_resolvers.js index 87316b39c..b489aef83 100644 --- a/packages/vulcan-core/lib/modules/default_resolvers.js +++ b/packages/vulcan-core/lib/modules/default_resolvers.js @@ -19,12 +19,12 @@ export function getDefaultResolvers(options) { // new single-argument API typeName = arguments[0].typeName; collectionName = arguments[0].collectionName || getCollectionName(typeName); - resolverOptions = arguments[0].options || defaultOptions; + resolverOptions = { ...defaultOptions, ...arguments[0].options }; } else { // OpenCRUD backwards compatibility collectionName = arguments[0]; typeName = getTypeName(collectionName); - resolverOptions = arguments[1] || defaultOptions; + resolverOptions = { ...defaultOptions, ...arguments[1] }; } return { From eb544faa0738456c0326258939fe3126c295eeaa Mon Sep 17 00:00:00 2001 From: ochicf Date: Fri, 10 Aug 2018 11:51:43 +0200 Subject: [PATCH 4/4] register callbacks only for the created mutations --- .../lib/modules/default_mutations.js | 242 +++++++++--------- 1 file changed, 123 insertions(+), 119 deletions(-) diff --git a/packages/vulcan-core/lib/modules/default_mutations.js b/packages/vulcan-core/lib/modules/default_mutations.js index c52737746..3e8752bed 100644 --- a/packages/vulcan-core/lib/modules/default_mutations.js +++ b/packages/vulcan-core/lib/modules/default_mutations.js @@ -26,7 +26,7 @@ export function getDefaultMutations (options) { } // register callbacks for documentation purposes - registerCollectionCallbacks(typeName); + registerCollectionCallbacks(typeName, mutationOptions); const mutations = {}; @@ -201,124 +201,128 @@ export function getDefaultMutations (options) { return mutations; } -const registerCollectionCallbacks = typeName => { +const registerCollectionCallbacks = (typeName, options) => { typeName = typeName.toLowerCase(); - registerCallback({ - name: `${typeName}.create.validate`, - iterator: { document: 'The document being inserted' }, - properties: [ - { document: 'The document being inserted' }, - { currentUser: 'The current user' }, - { validationErrors: 'An object that can be used to accumulate validation errors' }, - ], - runs: 'sync', - returns: 'document', - description: `Validate a document before insertion (can be skipped when inserting directly on server).`, - }); - registerCallback({ - name: `${typeName}.create.before`, - iterator: { document: 'The document being inserted' }, - properties: [ { currentUser: 'The current user' }], - runs: 'sync', - returns: 'document', - description: `Perform operations on a new document before it's inserted in the database.`, - }); - registerCallback({ - name: `${typeName}.create.after`, - iterator: { document: 'The document being inserted' }, - properties: [{ currentUser: 'The current user' }], - runs: 'sync', - returns: 'document', - description: `Perform operations on a new document after it's inserted in the database but *before* the mutation returns it.`, - }); - registerCallback({ - name: `${typeName}.create.async`, - iterator: { document: 'The document being inserted' }, - properties: [ - { currentUser: 'The current user' }, - { collection: 'The collection the document belongs to' }, - ], - runs: 'async', - returns: null, - description: `Perform operations on a new document after it's inserted in the database asynchronously.`, - }); - - registerCallback({ - name: `${typeName}.update.validate`, - iterator: { data: 'The client data' }, - properties: [ - { document: 'The document being edited' }, - { currentUser: 'The current user' }, - { validationErrors: 'An object that can be used to accumulate validation errors' }, - ], - runs: 'sync', - returns: 'modifier', - description: `Validate a document before update (can be skipped when updating directly on server).`, - }); - registerCallback({ - name: `${typeName}.update.before`, - iterator: { data: 'The client data'}, - properties: [ - { document: 'The document being edited' }, - { currentUser: 'The current user' }, - ], - runs: 'sync', - returns: 'modifier', - description: `Perform operations on a document before it's updated in the database.`, - }); - registerCallback({ - name: `${typeName}.update.after`, - iterator: { newDocument: 'The document after the update'}, - properties: [ - { document: 'The document being edited' }, - { currentUser: 'The current user' }, - ], - runs: 'sync', - returns: 'document', - description: `Perform operations on a document after it's updated in the database but *before* the mutation returns it.`, - }); - registerCallback({ - name: `${typeName}.update.async`, - iterator: { newDocument: 'The document after the edit' }, - properties: [ - { document: 'The document before the edit' }, - { currentUser: 'The current user' }, - { collection: 'The collection the document belongs to' }, - ], - runs: 'async', - returns: null, - description: `Perform operations on a document after it's updated in the database asynchronously.`, - }); - - registerCallback({ - name: `${typeName}.delete.validate`, - iterator: { document: 'The document being removed' }, - properties: [ - { currentUser: 'The current user' }, - { validationErrors: 'An object that can be used to accumulate validation errors' }, - ], - runs: 'sync', - returns: 'document', - description: `Validate a document before removal (can be skipped when removing directly on server).`, - }); - registerCallback({ - name: `${typeName}.delete.before`, - iterator: { document: 'The document being removed' }, - properties: [{ currentUser: 'The current user' }], - runs: 'sync', - returns: null, - description: `Perform operations on a document before it's removed from the database.`, - }); - registerCallback({ - name: `${typeName}.delete.async`, - properties: [ - { document: 'The document being removed' }, - { currentUser: 'The current user' }, - { collection: 'The collection the document belongs to' }, - ], - runs: 'async', - returns: null, - description: `Perform operations on a document after it's removed from the database asynchronously.`, - }); + if (options.create) { + registerCallback({ + name: `${typeName}.create.validate`, + iterator: { document: 'The document being inserted' }, + properties: [ + { document: 'The document being inserted' }, + { currentUser: 'The current user' }, + { validationErrors: 'An object that can be used to accumulate validation errors' }, + ], + runs: 'sync', + returns: 'document', + description: `Validate a document before insertion (can be skipped when inserting directly on server).`, + }); + registerCallback({ + name: `${typeName}.create.before`, + iterator: { document: 'The document being inserted' }, + properties: [{ currentUser: 'The current user' }], + runs: 'sync', + returns: 'document', + description: `Perform operations on a new document before it's inserted in the database.`, + }); + registerCallback({ + name: `${typeName}.create.after`, + iterator: { document: 'The document being inserted' }, + properties: [{ currentUser: 'The current user' }], + runs: 'sync', + returns: 'document', + description: `Perform operations on a new document after it's inserted in the database but *before* the mutation returns it.`, + }); + registerCallback({ + name: `${typeName}.create.async`, + iterator: { document: 'The document being inserted' }, + properties: [ + { currentUser: 'The current user' }, + { collection: 'The collection the document belongs to' }, + ], + runs: 'async', + returns: null, + description: `Perform operations on a new document after it's inserted in the database asynchronously.`, + }); + } + if (options.update) { + registerCallback({ + name: `${typeName}.update.validate`, + iterator: { data: 'The client data' }, + properties: [ + { document: 'The document being edited' }, + { currentUser: 'The current user' }, + { validationErrors: 'An object that can be used to accumulate validation errors' }, + ], + runs: 'sync', + returns: 'modifier', + description: `Validate a document before update (can be skipped when updating directly on server).`, + }); + registerCallback({ + name: `${typeName}.update.before`, + iterator: { data: 'The client data' }, + properties: [ + { document: 'The document being edited' }, + { currentUser: 'The current user' }, + ], + runs: 'sync', + returns: 'modifier', + description: `Perform operations on a document before it's updated in the database.`, + }); + registerCallback({ + name: `${typeName}.update.after`, + iterator: { newDocument: 'The document after the update' }, + properties: [ + { document: 'The document being edited' }, + { currentUser: 'The current user' }, + ], + runs: 'sync', + returns: 'document', + description: `Perform operations on a document after it's updated in the database but *before* the mutation returns it.`, + }); + registerCallback({ + name: `${typeName}.update.async`, + iterator: { newDocument: 'The document after the edit' }, + properties: [ + { document: 'The document before the edit' }, + { currentUser: 'The current user' }, + { collection: 'The collection the document belongs to' }, + ], + runs: 'async', + returns: null, + description: `Perform operations on a document after it's updated in the database asynchronously.`, + }); + } + if (options.delete) { + registerCallback({ + name: `${typeName}.delete.validate`, + iterator: { document: 'The document being removed' }, + properties: [ + { currentUser: 'The current user' }, + { validationErrors: 'An object that can be used to accumulate validation errors' }, + ], + runs: 'sync', + returns: 'document', + description: `Validate a document before removal (can be skipped when removing directly on server).`, + }); + registerCallback({ + name: `${typeName}.delete.before`, + iterator: { document: 'The document being removed' }, + properties: [{ currentUser: 'The current user' }], + runs: 'sync', + returns: null, + description: `Perform operations on a document before it's removed from the database.`, + }); + registerCallback({ + name: `${typeName}.delete.async`, + properties: [ + { document: 'The document being removed' }, + { currentUser: 'The current user' }, + { collection: 'The collection the document belongs to' }, + ], + runs: 'async', + returns: null, + description: `Perform operations on a document after it's removed from the database asynchronously.`, + }); + } };