Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
823a65991a | |||
f9b997b65c | |||
7c2b146eed | |||
a54683e29a | |||
a855665777 | |||
034287672c | |||
e8c0e61c6b | |||
a64a55af0d |
@ -1,7 +0,0 @@
|
|||||||
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,10 +30,6 @@ await register({
|
|||||||
prefix: '/v1',
|
prefix: '/v1',
|
||||||
name: 'db', // the same name you used for configuring `hapi-sequelize` (options.name)
|
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
|
||||||
|
|
||||||
// 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
|
models: ['cat', 'dog'] // only the cat and dog models will have routes created
|
||||||
// or
|
// or
|
||||||
models: [
|
models: [
|
||||||
@ -49,6 +45,10 @@ await register({
|
|||||||
{model: 'bat', methods: ['list'], config: { ... }},
|
{model: 'bat', methods: ['list'], config: { ... }},
|
||||||
{model: 'bat', methods: ['create']}
|
{model: 'bat', methods: ['create']}
|
||||||
]
|
]
|
||||||
|
models: {
|
||||||
|
bat: {
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
10
package.json
10
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "hapi-sequelize-crud",
|
"name": "@getable/hapi-sequelize-crud",
|
||||||
"version": "2.5.4",
|
"version": "2.5.1",
|
||||||
"description": "Hapi plugin that automatically generates RESTful API for CRUD",
|
"description": "Hapi plugin that automatically generates RESTful API for CRUD",
|
||||||
"main": "build/index.js",
|
"main": "build/index.js",
|
||||||
"config": {
|
"config": {
|
||||||
@ -15,7 +15,7 @@
|
|||||||
"watch": "scripty"
|
"watch": "scripty"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"git": "https://github.com/mdibaiee/hapi-sequelize-crud"
|
"git": "https://github.com/Getable/hapi-sequelize-crud"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"build"
|
"build"
|
||||||
@ -35,11 +35,9 @@
|
|||||||
"scripty": "^1.6.0"
|
"scripty": "^1.6.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"babel": "5.8.3",
|
||||||
"boom": "^3.2.2",
|
"boom": "^3.2.2",
|
||||||
"joi": "7.2.1",
|
"joi": "7.2.1",
|
||||||
"lodash": "4.0.0"
|
"lodash": "4.0.0"
|
||||||
},
|
|
||||||
"optionalDependencies": {
|
|
||||||
"babel-polyfill": "^6.13.0"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
13
src/crud.js
13
src/crud.js
@ -3,7 +3,6 @@ import error from './error';
|
|||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import { parseInclude, parseWhere } from './utils';
|
import { parseInclude, parseWhere } from './utils';
|
||||||
import { notFound } from 'boom';
|
import { notFound } from 'boom';
|
||||||
import * as associations from './associations/index';
|
|
||||||
|
|
||||||
const createAll = ({ server, model, prefix, config }) => {
|
const createAll = ({ server, model, prefix, config }) => {
|
||||||
Object.keys(methods).forEach((method) => {
|
Object.keys(methods).forEach((method) => {
|
||||||
@ -11,8 +10,6 @@ const createAll = ({ server, model, prefix, config }) => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export { associations };
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
The `models` option, becomes `permissions`, and can look like:
|
The `models` option, becomes `permissions`, and can look like:
|
||||||
|
|
||||||
@ -33,14 +30,13 @@ models: {
|
|||||||
|
|
||||||
export default (server, model, { prefix, defaultConfig: config, models: permissions }) => {
|
export default (server, model, { prefix, defaultConfig: config, models: permissions }) => {
|
||||||
const modelName = model._singular;
|
const modelName = model._singular;
|
||||||
|
const permissionsIsArray = Array.isArray(permissions);
|
||||||
|
|
||||||
if (!permissions) {
|
if (!permissions) {
|
||||||
createAll({ server, model, prefix, config });
|
createAll({ server, model, prefix, config });
|
||||||
} else if (!Array.isArray(permissions)) {
|
} else if (permissionsIsArray && permissions.includes(modelName)) {
|
||||||
throw new Error('hapi-sequelize-crud: `models` property must be an array');
|
|
||||||
} else if (permissions.includes(modelName)) {
|
|
||||||
createAll({ server, model, prefix, config });
|
createAll({ server, model, prefix, config });
|
||||||
} else {
|
} else if (permissionsIsArray) {
|
||||||
const permissionOptions = permissions.filter((permission) => {
|
const permissionOptions = permissions.filter((permission) => {
|
||||||
return permission.model === modelName;
|
return permission.model === modelName;
|
||||||
});
|
});
|
||||||
@ -257,6 +253,9 @@ export const update = ({ server, model, prefix, config }) => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
import * as associations from './associations/index';
|
||||||
|
export { associations };
|
||||||
|
|
||||||
const methods = {
|
const methods = {
|
||||||
list, get, scope, create, destroy, destroyAll, destroyScope, update,
|
list, get, scope, create, destroy, destroyAll, destroyScope, update,
|
||||||
};
|
};
|
||||||
|
57
src/error.js
57
src/error.js
@ -1,5 +1,3 @@
|
|||||||
import Boom from 'boom';
|
|
||||||
|
|
||||||
export default (target, key, descriptor) => {
|
export default (target, key, descriptor) => {
|
||||||
const fn = descriptor.value;
|
const fn = descriptor.value;
|
||||||
|
|
||||||
@ -7,59 +5,8 @@ export default (target, key, descriptor) => {
|
|||||||
try {
|
try {
|
||||||
await fn(request, reply);
|
await fn(request, reply);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e.original) {
|
console.error(e);
|
||||||
const { code, detail, hint } = e.original;
|
reply(e);
|
||||||
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) {
|
if (!global._babelPolyfill) {
|
||||||
require('babel-polyfill');
|
require('babel/polyfill');
|
||||||
}
|
}
|
||||||
|
|
||||||
import crud, { associations } from './crud';
|
import crud, { associations } from './crud';
|
||||||
|
Reference in New Issue
Block a user