Compare commits
	
		
			8 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
|  | 823a65991a | ||
|  | f9b997b65c | ||
|  | 7c2b146eed | ||
|  | a54683e29a | ||
|  | a855665777 | ||
|  | 034287672c | ||
|  | e8c0e61c6b | ||
|  | a64a55af0d | 
							
								
								
									
										32
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										32
									
								
								README.md
									
									
									
									
									
								
							| @@ -29,11 +29,41 @@ await register({ | ||||
|   options: { | ||||
|     prefix: '/v1', | ||||
|     name: 'db', // the same name you used for configuring `hapi-sequelize` (options.name) | ||||
|     defaultConfig: { ... } // passed as `config` to all routes created | ||||
|     defaultConfig: { ... }, // passed as `config` to all routes created | ||||
|     models: ['cat', 'dog'] // only the cat and dog models will have routes created | ||||
|     // or | ||||
|     models: [ | ||||
|       // possible methods: list, get, scope, create, destroy, destroyAll, destroyScope, update | ||||
|       // the cat model only has get and list methods enabled | ||||
|       {model: 'cat', methods: ['get', 'list']}, | ||||
|       // the dog model has all methods enabled | ||||
|       {model: 'dog'}, | ||||
|       // the cow model also has all methods enabled | ||||
|       'cow', | ||||
|       // the bat model as a custom config for the list method, but uses the default config for create. | ||||
|       // `config` if provided, overrides the default config | ||||
|       {model: 'bat', methods: ['list'], config: { ... }}, | ||||
|       {model: 'bat', methods: ['create']} | ||||
|     ] | ||||
|     models: { | ||||
|       bat: { | ||||
|       } | ||||
|     } | ||||
|   } | ||||
| }); | ||||
| ``` | ||||
|  | ||||
| ### Methods | ||||
| * list: get all rows in a table | ||||
| * get: get a single row | ||||
| * scope: reference a [sequelize scope](http://docs.sequelizejs.com/en/latest/api/model/#scopeoptions-model) | ||||
| * create: create a new row | ||||
| * destroy: delete a row | ||||
| * destroyAll: delete all models in the table | ||||
| * destroyScope: use a [sequelize scope](http://docs.sequelizejs.com/en/latest/api/model/#scopeoptions-model) to find rows, then delete them | ||||
| * update: update a row | ||||
|  | ||||
|  | ||||
| Please note that you should register `hapi-sequelize-crud` after defining your | ||||
| associations. | ||||
|  | ||||
|   | ||||
| @@ -1,6 +1,6 @@ | ||||
| { | ||||
|   "name": "hapi-sequelize-crud", | ||||
|   "version": "2.4.0", | ||||
|   "name": "@getable/hapi-sequelize-crud", | ||||
|   "version": "2.5.1", | ||||
|   "description": "Hapi plugin that automatically generates RESTful API for CRUD", | ||||
|   "main": "build/index.js", | ||||
|   "config": { | ||||
| @@ -15,7 +15,7 @@ | ||||
|     "watch": "scripty" | ||||
|   }, | ||||
|   "repository": { | ||||
|     "git": "https://github.com/mdibaiee/hapi-sequelize-crud" | ||||
|     "git": "https://github.com/Getable/hapi-sequelize-crud" | ||||
|   }, | ||||
|   "files": [ | ||||
|     "build" | ||||
|   | ||||
							
								
								
									
										107
									
								
								src/crud.js
									
									
									
									
									
								
							
							
						
						
									
										107
									
								
								src/crud.js
									
									
									
									
									
								
							| @@ -4,24 +4,65 @@ import _ from 'lodash'; | ||||
| import { parseInclude, parseWhere } from './utils'; | ||||
| import { notFound } from 'boom'; | ||||
|  | ||||
| let prefix; | ||||
| let defaultConfig; | ||||
|  | ||||
| export default (server, model, options) => { | ||||
|   prefix = options.prefix; | ||||
|   defaultConfig = options.defaultConfig; | ||||
|  | ||||
|   list(server, model); | ||||
|   get(server, model); | ||||
|   scope(server, model); | ||||
|   create(server, model); | ||||
|   destroy(server, model); | ||||
|   destroyAll(server, model); | ||||
|   destroyScope(server, model); | ||||
|   update(server, model); | ||||
| const createAll = ({ server, model, prefix, config }) => { | ||||
|   Object.keys(methods).forEach((method) => { | ||||
|     methods[method]({ server, model, prefix, config }); | ||||
|   }); | ||||
| }; | ||||
|  | ||||
| export const list = (server, model) => { | ||||
| /* | ||||
| The `models` option, becomes `permissions`, and can look like: | ||||
|  | ||||
| ``` | ||||
| models: ['cat', 'dog'] | ||||
| ``` | ||||
|  | ||||
| or | ||||
|  | ||||
| ``` | ||||
| models: { | ||||
|   cat: ['list', 'get'] | ||||
|   , dog: true // all | ||||
| } | ||||
| ``` | ||||
|  | ||||
| */ | ||||
|  | ||||
| export default (server, model, { prefix, defaultConfig: config, models: permissions }) => { | ||||
|   const modelName = model._singular; | ||||
|   const permissionsIsArray = Array.isArray(permissions); | ||||
|  | ||||
|   if (!permissions) { | ||||
|     createAll({ server, model, prefix, config }); | ||||
|   } else if (permissionsIsArray && permissions.includes(modelName)) { | ||||
|     createAll({ server, model, prefix, config }); | ||||
|   } else if (permissionsIsArray) { | ||||
|     const permissionOptions = permissions.filter((permission) => { | ||||
|       return permission.model === modelName; | ||||
|     }); | ||||
|  | ||||
|     permissionOptions.forEach((permissionOption) => { | ||||
|       if (_.isPlainObject(permissionOption)) { | ||||
|         const permissionConfig = permissionOption.config || config; | ||||
|  | ||||
|         if (permissionOption.methods) { | ||||
|           permissionOption.methods.forEach((method) => { | ||||
|             methods[method]({ | ||||
|               server, | ||||
|               model, | ||||
|               prefix, | ||||
|               config: permissionConfig, | ||||
|             }); | ||||
|           }); | ||||
|         } else { | ||||
|           createAll({ server, model, prefix, config: permissionConfig }); | ||||
|         } | ||||
|       } | ||||
|     }); | ||||
|   } | ||||
| }; | ||||
|  | ||||
| export const list = ({ server, model, prefix, config }) => { | ||||
|   server.route({ | ||||
|     method: 'GET', | ||||
|     path: `${prefix}/${model._plural}`, | ||||
| @@ -40,11 +81,11 @@ export const list = (server, model) => { | ||||
|       reply(list); | ||||
|     }, | ||||
|  | ||||
|     config: defaultConfig, | ||||
|     config, | ||||
|   }); | ||||
| }; | ||||
|  | ||||
| export const get = (server, model) => { | ||||
| export const get = ({ server, model, prefix, config }) => { | ||||
|   server.route({ | ||||
|     method: 'GET', | ||||
|     path: `${prefix}/${model._singular}/{id?}`, | ||||
| @@ -68,11 +109,11 @@ export const get = (server, model) => { | ||||
|           id: joi.any(), | ||||
|         }), | ||||
|       }, | ||||
|     }, defaultConfig), | ||||
|     }, config), | ||||
|   }); | ||||
| }; | ||||
|  | ||||
| export const scope = (server, model) => { | ||||
| export const scope = ({ server, model, prefix, config }) => { | ||||
|   const scopes = Object.keys(model.options.scopes); | ||||
|  | ||||
|   server.route({ | ||||
| @@ -94,11 +135,11 @@ export const scope = (server, model) => { | ||||
|           scope: joi.string().valid(...scopes), | ||||
|         }), | ||||
|       }, | ||||
|     }, defaultConfig), | ||||
|     }, config), | ||||
|   }); | ||||
| }; | ||||
|  | ||||
| export const create = (server, model) => { | ||||
| export const create = ({ server, model, prefix, config }) => { | ||||
|   server.route({ | ||||
|     method: 'POST', | ||||
|     path: `${prefix}/${model._singular}`, | ||||
| @@ -110,11 +151,11 @@ export const create = (server, model) => { | ||||
|       reply(instance); | ||||
|     }, | ||||
|  | ||||
|     config: defaultConfig, | ||||
|     config, | ||||
|   }); | ||||
| }; | ||||
|  | ||||
| export const destroy = (server, model) => { | ||||
| export const destroy = ({ server, model, prefix, config }) => { | ||||
|   server.route({ | ||||
|     method: 'DELETE', | ||||
|     path: `${prefix}/${model._singular}/{id?}`, | ||||
| @@ -131,11 +172,11 @@ export const destroy = (server, model) => { | ||||
|       reply(list.length === 1 ? list[0] : list); | ||||
|     }, | ||||
|  | ||||
|     config: defaultConfig, | ||||
|     config, | ||||
|   }); | ||||
| }; | ||||
|  | ||||
| export const destroyAll = (server, model) => { | ||||
| export const destroyAll = ({ server, model, prefix, config }) => { | ||||
|   server.route({ | ||||
|     method: 'DELETE', | ||||
|     path: `${prefix}/${model._plural}`, | ||||
| @@ -151,11 +192,11 @@ export const destroyAll = (server, model) => { | ||||
|       reply(list.length === 1 ? list[0] : list); | ||||
|     }, | ||||
|  | ||||
|     config: defaultConfig, | ||||
|     config, | ||||
|   }); | ||||
| }; | ||||
|  | ||||
| export const destroyScope = (server, model) => { | ||||
| export const destroyScope = ({ server, model, prefix, config }) => { | ||||
|   const scopes = Object.keys(model.options.scopes); | ||||
|  | ||||
|   server.route({ | ||||
| @@ -179,11 +220,11 @@ export const destroyScope = (server, model) => { | ||||
|           scope: joi.string().valid(...scopes), | ||||
|         }), | ||||
|       }, | ||||
|     }, defaultConfig), | ||||
|     }, config), | ||||
|   }); | ||||
| }; | ||||
|  | ||||
| export const update = (server, model) => { | ||||
| export const update = ({ server, model, prefix, config }) => { | ||||
|   server.route({ | ||||
|     method: 'PUT', | ||||
|     path: `${prefix}/${model._singular}/{id}`, | ||||
| @@ -208,9 +249,13 @@ export const update = (server, model) => { | ||||
|       validate: { | ||||
|         payload: joi.object().required(), | ||||
|       }, | ||||
|     }, defaultConfig), | ||||
|     }, config), | ||||
|   }); | ||||
| }; | ||||
|  | ||||
| import * as associations from './associations/index'; | ||||
| export { associations }; | ||||
|  | ||||
| const methods = { | ||||
|   list, get, scope, create, destroy, destroyAll, destroyScope, update, | ||||
| }; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user