mirror of
https://github.com/Ratstail91/sineql-demo.git
synced 2026-05-05 23:30:11 +10:00
Updated book routes
This commit is contained in:
@@ -14,17 +14,14 @@ type Weather {
|
||||
String wind_dir
|
||||
}
|
||||
|
||||
scalar Index
|
||||
scalar Date
|
||||
|
||||
type Book {
|
||||
Index index
|
||||
String title
|
||||
Date published
|
||||
}
|
||||
|
||||
type Author {
|
||||
Index index
|
||||
String name
|
||||
Book books
|
||||
}
|
||||
|
||||
@@ -1,36 +1,38 @@
|
||||
const { Book } = require('../database/models');
|
||||
|
||||
//utils
|
||||
const checkDateFormat = date => /(19\d{2}|20\d{2})[-\/.](0[1-9]|1[012])[-\/.](0[1-9]|[12][0-9]|3[01])/.test(date);
|
||||
const checkDateFormat = date => /(19\d{2}|20\d{2})[-\/.](0[1-9]|1[012])[-\/.](0[1-9]|[12][0-9]|3[01])/.test(date) || date == 'NULL';
|
||||
|
||||
const queryBook = async (query, typeGraph) => {
|
||||
//sequelize stuff to find
|
||||
const attributes = [];
|
||||
const where = {};
|
||||
|
||||
//specify fields to find
|
||||
Object.keys(query)
|
||||
.filter(key => key != 'typeName') //filter out this meta-field
|
||||
.forEach(key => {
|
||||
//check published date
|
||||
if (key == 'published' && !checkDateFormat(query[key].match)) {
|
||||
throw 'Wrong date format for published';
|
||||
}
|
||||
|
||||
//all book members should be scalar
|
||||
//all book members queried should be scalar
|
||||
if (query[key].scalar) {
|
||||
//push this key into the attributes list
|
||||
attributes.push(key);
|
||||
}
|
||||
|
||||
//filter the members to a specific value
|
||||
if (query[key].match) {
|
||||
where[key] = query[key].match == 'NULL' ? null : query[key].match;
|
||||
}
|
||||
})
|
||||
;
|
||||
|
||||
//search the database
|
||||
const result = await Book.findAll({
|
||||
attributes: attributes
|
||||
attributes: attributes,
|
||||
where: where,
|
||||
});
|
||||
|
||||
//finally
|
||||
return result;
|
||||
return result.map(book => book.dataValues);
|
||||
};
|
||||
|
||||
const createBook = async (query, typeGraph) => {
|
||||
@@ -45,7 +47,7 @@ const createBook = async (query, typeGraph) => {
|
||||
|
||||
//specify fields to create
|
||||
Object.keys(q)
|
||||
.filter(key => key != 'typeName' && key != 'create') //filter out this meta-field
|
||||
.filter(key => key != 'typeName' && key != 'create') //filter out these meta-fields
|
||||
.forEach(key => {
|
||||
//check published date
|
||||
if (key == 'published' && !checkDateFormat(q[key].create)) {
|
||||
@@ -76,7 +78,7 @@ const createBook = async (query, typeGraph) => {
|
||||
|
||||
//specify fields to return in each record
|
||||
const keys = Object.keys(q)
|
||||
.filter(key => key != 'typeName' && key != 'create') //filter out this meta-field
|
||||
.filter(key => key != 'typeName' && key != 'create') //filter out these meta-fields
|
||||
.forEach(key => {
|
||||
if (q[key].scalar) {
|
||||
returns[idx][key] = results[idx][key];
|
||||
@@ -90,11 +92,96 @@ const createBook = async (query, typeGraph) => {
|
||||
};
|
||||
|
||||
const updateBook = async (query, typeGraph) => {
|
||||
throw 'Not yet implemented';
|
||||
//total updated
|
||||
const totals = [];
|
||||
|
||||
const promises = query.map(async (q, idx) => {
|
||||
//the array of objects to update
|
||||
const updates = {};
|
||||
const where = {};
|
||||
|
||||
//just in case
|
||||
if (!q.update && !q.match) {
|
||||
throw 'Unexpected field (expected an update or match keyword)';
|
||||
}
|
||||
|
||||
//specify fields to update
|
||||
Object.keys(q)
|
||||
.filter(key => key != 'typeName' && key != 'create') //filter out these meta-fields
|
||||
.forEach(key => {
|
||||
//check published date format
|
||||
if (key == 'published') {
|
||||
if (q[key].update && !checkDateFormat(q[key].update)) {
|
||||
throw 'Wrong date format for published';
|
||||
}
|
||||
}
|
||||
|
||||
//all book members should be scalar
|
||||
if (q[key].update) {
|
||||
//push this key into the updates object
|
||||
updates[key] = q[key].update;
|
||||
}
|
||||
|
||||
//filter the members to a specific value
|
||||
if (q[key].match) {
|
||||
where[key] = q[key].match == 'NULL' ? null : q[key].match;
|
||||
}
|
||||
})
|
||||
;
|
||||
|
||||
//update the database
|
||||
const [updated] = await Book.update(updates, { where });
|
||||
|
||||
totals.push(updated);
|
||||
});
|
||||
|
||||
await Promise.all(promises);
|
||||
|
||||
//finally
|
||||
return { totals };
|
||||
};
|
||||
|
||||
const deleteBook = async (query, typeGraph) => {
|
||||
throw 'Not yet implemented';
|
||||
//total updated
|
||||
const totals = [];
|
||||
|
||||
const promises = query.map(async (q, idx) => {
|
||||
//the array of objects to delete
|
||||
const where = {};
|
||||
|
||||
//just in case
|
||||
if (!q.delete) {
|
||||
throw 'Unexpected field (expected match keyword)';
|
||||
}
|
||||
|
||||
//specify fields to delete
|
||||
Object.keys(q)
|
||||
.filter(key => key != 'typeName' && key != 'delete') //filter out these meta-fields
|
||||
.forEach(key => {
|
||||
//check published date format
|
||||
if (key == 'published') {
|
||||
if (q[key].match && !checkDateFormat(q[key].match)) {
|
||||
throw 'Wrong date format for published';
|
||||
}
|
||||
}
|
||||
|
||||
//filter the members to a specific value
|
||||
if (q[key].match) {
|
||||
where[key] = q[key].match == 'NULL' ? null : q[key].match;
|
||||
}
|
||||
})
|
||||
;
|
||||
|
||||
//update the database
|
||||
const deleted = await Book.destroy({ where });
|
||||
|
||||
totals.push(deleted);
|
||||
});
|
||||
|
||||
await Promise.all(promises);
|
||||
|
||||
//finally
|
||||
return { totals };
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
|
||||
Reference in New Issue
Block a user