Compare commits
25 Commits
Author | SHA1 | Date | |
---|---|---|---|
506d42f39a | |||
3dfa72ddee | |||
bab2e90cbb | |||
da6b3ce963 | |||
b032be20d1 | |||
79c6a81a3a | |||
517f2b8157 | |||
a9fa790ae9 | |||
ce6f1fedde | |||
db86507ef9 | |||
6ad9df2db1 | |||
17105f66f4 | |||
b18479e02e | |||
0e9cd935b9 | |||
9524e55690 | |||
1752d700f5 | |||
6d289d6d78 | |||
0d6a715511 | |||
e5d72fd034 | |||
a0aeaef3a9 | |||
79b9fc1242 | |||
fb8275abca | |||
098aabfea5 | |||
f95f411a65 | |||
0416986896 |
7
CONTRIBUTING
Normal file
7
CONTRIBUTING
Normal file
@ -0,0 +1,7 @@
|
||||
Commit Message
|
||||
===============
|
||||
Please follow [this convention](http://karma-runner.github.io/1.0/dev/git-commit-msg.html) for git commit message.
|
||||
|
||||
Lint
|
||||
====
|
||||
Please lint your code using `npm run lint` (also `npm run lint -- --fix` to auto-fix).
|
@ -30,6 +30,10 @@ await register({
|
||||
prefix: '/v1',
|
||||
name: 'db', // the same name you used for configuring `hapi-sequelize` (options.name)
|
||||
defaultConfig: { ... }, // passed as `config` to all routes created
|
||||
|
||||
// You can specify which models must have routes defined for using the
|
||||
// `models` property. If you omit this property, all models will have
|
||||
// models defined for them. e.g.
|
||||
models: ['cat', 'dog'] // only the cat and dog models will have routes created
|
||||
// or
|
||||
models: [
|
||||
@ -45,10 +49,6 @@ await register({
|
||||
{model: 'bat', methods: ['list'], config: { ... }},
|
||||
{model: 'bat', methods: ['create']}
|
||||
]
|
||||
models: {
|
||||
bat: {
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
```
|
||||
|
10
package.json
10
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@getable/hapi-sequelize-crud",
|
||||
"version": "2.5.1",
|
||||
"name": "hapi-sequelize-crud",
|
||||
"version": "2.5.4",
|
||||
"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/Getable/hapi-sequelize-crud"
|
||||
"git": "https://github.com/mdibaiee/hapi-sequelize-crud"
|
||||
},
|
||||
"files": [
|
||||
"build"
|
||||
@ -35,9 +35,11 @@
|
||||
"scripty": "^1.6.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"babel": "5.8.3",
|
||||
"boom": "^3.2.2",
|
||||
"joi": "7.2.1",
|
||||
"lodash": "4.0.0"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
"babel-polyfill": "^6.13.0"
|
||||
}
|
||||
}
|
||||
|
13
src/crud.js
13
src/crud.js
@ -3,6 +3,7 @@ import error from './error';
|
||||
import _ from 'lodash';
|
||||
import { parseInclude, parseWhere } from './utils';
|
||||
import { notFound } from 'boom';
|
||||
import * as associations from './associations/index';
|
||||
|
||||
const createAll = ({ server, model, prefix, config }) => {
|
||||
Object.keys(methods).forEach((method) => {
|
||||
@ -10,6 +11,8 @@ const createAll = ({ server, model, prefix, config }) => {
|
||||
});
|
||||
};
|
||||
|
||||
export { associations };
|
||||
|
||||
/*
|
||||
The `models` option, becomes `permissions`, and can look like:
|
||||
|
||||
@ -30,13 +33,14 @@ models: {
|
||||
|
||||
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)) {
|
||||
} else if (!Array.isArray(permissions)) {
|
||||
throw new Error('hapi-sequelize-crud: `models` property must be an array');
|
||||
} else if (permissions.includes(modelName)) {
|
||||
createAll({ server, model, prefix, config });
|
||||
} else if (permissionsIsArray) {
|
||||
} else {
|
||||
const permissionOptions = permissions.filter((permission) => {
|
||||
return permission.model === modelName;
|
||||
});
|
||||
@ -253,9 +257,6 @@ export const update = ({ server, model, prefix, config }) => {
|
||||
});
|
||||
};
|
||||
|
||||
import * as associations from './associations/index';
|
||||
export { associations };
|
||||
|
||||
const methods = {
|
||||
list, get, scope, create, destroy, destroyAll, destroyScope, update,
|
||||
};
|
||||
|
57
src/error.js
57
src/error.js
@ -1,3 +1,5 @@
|
||||
import Boom from 'boom';
|
||||
|
||||
export default (target, key, descriptor) => {
|
||||
const fn = descriptor.value;
|
||||
|
||||
@ -5,8 +7,59 @@ export default (target, key, descriptor) => {
|
||||
try {
|
||||
await fn(request, reply);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
reply(e);
|
||||
if (e.original) {
|
||||
const { code, detail, hint } = e.original;
|
||||
let error;
|
||||
|
||||
// pg error codes https://www.postgresql.org/docs/9.5/static/errcodes-appendix.html
|
||||
if (code && (code.startsWith('22') || code.startsWith('23'))) {
|
||||
error = Boom.wrap(e, 406);
|
||||
} else if (code && (code.startsWith('42'))) {
|
||||
error = Boom.wrap(e, 422);
|
||||
// TODO: we could get better at parse postgres error codes
|
||||
} else {
|
||||
// use a 502 error code since the issue is upstream with postgres, not
|
||||
// this server
|
||||
error = Boom.wrap(e, 502);
|
||||
}
|
||||
|
||||
// detail tends to be more specific information. So, if we have it, use.
|
||||
if (detail) {
|
||||
error.message += `: ${detail}`;
|
||||
error.reformat();
|
||||
}
|
||||
|
||||
// hint might provide useful information about how to fix the problem
|
||||
if (hint) {
|
||||
error.message += ` Hint: ${hint}`;
|
||||
error.reformat();
|
||||
}
|
||||
|
||||
reply(error);
|
||||
} else if (!e.isBoom) {
|
||||
const { message } = e;
|
||||
let err;
|
||||
|
||||
if (e.name === 'SequelizeValidationError')
|
||||
err = Boom.badData(message);
|
||||
else if (e.name === 'SequelizeConnectionTimedOutError')
|
||||
err = Boom.gatewayTimeout(message);
|
||||
else if (e.name === 'SequelizeHostNotReachableError')
|
||||
err = Boom.serverUnavailable(message);
|
||||
else if (e.name === 'SequelizeUniqueConstraintError')
|
||||
err = Boom.conflict(message);
|
||||
else if (e.name === 'SequelizeForeignKeyConstraintError')
|
||||
err = Boom.expectationFailed(message);
|
||||
else if (e.name === 'SequelizeExclusionConstraintError')
|
||||
err = Boom.expectationFailed(message);
|
||||
else if (e.name === 'SequelizeConnectionError')
|
||||
err = Boom.badGateway(message);
|
||||
else err = Boom.badImplementation(message);
|
||||
|
||||
reply(err);
|
||||
} else {
|
||||
reply(e);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
if (!global._babelPolyfill) {
|
||||
require('babel/polyfill');
|
||||
require('babel-polyfill');
|
||||
}
|
||||
|
||||
import crud, { associations } from './crud';
|
||||
|
Reference in New Issue
Block a user