feat(operators): ability to use sequelize operators in queries

This commit is contained in:
Mahdi Dibaiee 2016-02-01 16:46:30 +03:30
parent 6cfe877884
commit 5db3bb4eb3
3 changed files with 81 additions and 1 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "hapi-sequelize-crud", "name": "hapi-sequelize-crud",
"version": "1.4.0", "version": "1.5.0",
"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": {

View File

@ -58,6 +58,14 @@ export const list = (server, a, b) => {
let where = _.omit(request.query, 'include'); let where = _.omit(request.query, 'include');
for (const key of Object.keys(where)) {
try {
where[key] = JSON.parse(where[key]);
} catch (e) {
//
}
}
let list = await b.findAll({ let list = await b.findAll({
where, where,
@ -89,6 +97,14 @@ export const scope = (server, a, b) => {
let where = _.omit(request.query, 'include'); let where = _.omit(request.query, 'include');
for (const key of Object.keys(where)) {
try {
where[key] = JSON.parse(where[key]);
} catch (e) {
//
}
}
let list = await b.scope(request.params.scope).findAll({ let list = await b.scope(request.params.scope).findAll({
where, where,
include: include.concat({ include: include.concat({
@ -131,6 +147,14 @@ export const scopeScope = (server, a, b) => {
let where = _.omit(request.query, 'include'); let where = _.omit(request.query, 'include');
for (const key of Object.keys(where)) {
try {
where[key] = JSON.parse(where[key]);
} catch (e) {
//
}
}
let list = await b.scope(request.params.scopeb).findAll({ let list = await b.scope(request.params.scopeb).findAll({
where, where,
include: include.concat({ include: include.concat({
@ -161,6 +185,14 @@ export const destroy = (server, a, b) => {
async handler(request, reply) { async handler(request, reply) {
let where = _.omit(request.query, 'include'); let where = _.omit(request.query, 'include');
for (const key of Object.keys(where)) {
try {
where[key] = JSON.parse(where[key]);
} catch (e) {
//
}
}
let list = await b.findAll({ let list = await b.findAll({
where, where,
include: { include: {
@ -189,6 +221,14 @@ export const destroyScope = (server, a, b) => {
async handler(request, reply) { async handler(request, reply) {
let where = _.omit(request.query, 'include'); let where = _.omit(request.query, 'include');
for (const key of Object.keys(where)) {
try {
where[key] = JSON.parse(where[key]);
} catch (e) {
//
}
}
let list = await b.scope(request.params.scope).findAll({ let list = await b.scope(request.params.scope).findAll({
where, where,

View File

@ -28,6 +28,14 @@ export const list = (server, model) => {
let where = _.omit(request.query, 'include'); let where = _.omit(request.query, 'include');
for (const key of Object.keys(where)) {
try {
where[key] = JSON.parse(where[key]);
} catch (e) {
//
}
}
let list = await model.findAll({ let list = await model.findAll({
where, include where, include
}); });
@ -49,6 +57,14 @@ export const get = (server, model) => {
let where = request.params.id ? { id : request.params.id } : _.omit(request.query, 'include'); let where = request.params.id ? { id : request.params.id } : _.omit(request.query, 'include');
for (const key of Object.keys(where)) {
try {
where[key] = JSON.parse(where[key]);
} catch (e) {
//
}
}
let instance = await model.findOne({ where, include }); let instance = await model.findOne({ where, include });
reply(instance); reply(instance);
@ -77,6 +93,14 @@ export const scope = (server, model) => {
let where = _.omit(request.query, 'include'); let where = _.omit(request.query, 'include');
for (const key of Object.keys(where)) {
try {
where[key] = JSON.parse(where[key]);
} catch (e) {
//
}
}
let list = await model.scope(request.params.scope).findAll({ include, where }); let list = await model.scope(request.params.scope).findAll({ include, where });
reply(list); reply(list);
@ -114,6 +138,14 @@ export const destroy = (server, model) => {
async handler(request, reply) { async handler(request, reply) {
let where = request.params.id ? { id : request.params.id } : request.query; let where = request.params.id ? { id : request.params.id } : request.query;
for (const key of Object.keys(where)) {
try {
where[key] = JSON.parse(where[key]);
} catch (e) {
//
}
}
let list = await model.findAll({ where }); let list = await model.findAll({ where });
await* list.map(instance => instance.destroy()); await* list.map(instance => instance.destroy());
@ -137,6 +169,14 @@ export const destroyScope = (server, model) => {
let where = _.omit(request.query, 'include'); let where = _.omit(request.query, 'include');
for (const key of Object.keys(where)) {
try {
where[key] = JSON.parse(where[key]);
} catch (e) {
//
}
}
let list = await model.scope(request.params.scope).findAll({ include, where }); let list = await model.scope(request.params.scope).findAll({ include, where });
await* list.map(instance => instance.destroy()); await* list.map(instance => instance.destroy());