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:
Generated
+380
-238
File diff suppressed because it is too large
Load Diff
+1
-1
@@ -18,6 +18,6 @@
|
|||||||
"mariadb": "^2.5.6",
|
"mariadb": "^2.5.6",
|
||||||
"node-fetch": "^2.0.0",
|
"node-fetch": "^2.0.0",
|
||||||
"sequelize": "^6.17.0",
|
"sequelize": "^6.17.0",
|
||||||
"sineql": "^1.0.0"
|
"sineql": "^1.0.3"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,17 +14,14 @@ type Weather {
|
|||||||
String wind_dir
|
String wind_dir
|
||||||
}
|
}
|
||||||
|
|
||||||
scalar Index
|
|
||||||
scalar Date
|
scalar Date
|
||||||
|
|
||||||
type Book {
|
type Book {
|
||||||
Index index
|
|
||||||
String title
|
String title
|
||||||
Date published
|
Date published
|
||||||
}
|
}
|
||||||
|
|
||||||
type Author {
|
type Author {
|
||||||
Index index
|
|
||||||
String name
|
String name
|
||||||
Book books
|
Book books
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,36 +1,38 @@
|
|||||||
const { Book } = require('../database/models');
|
const { Book } = require('../database/models');
|
||||||
|
|
||||||
//utils
|
//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) => {
|
const queryBook = async (query, typeGraph) => {
|
||||||
//sequelize stuff to find
|
//sequelize stuff to find
|
||||||
const attributes = [];
|
const attributes = [];
|
||||||
|
const where = {};
|
||||||
|
|
||||||
//specify fields to find
|
//specify fields to find
|
||||||
Object.keys(query)
|
Object.keys(query)
|
||||||
.filter(key => key != 'typeName') //filter out this meta-field
|
.filter(key => key != 'typeName') //filter out this meta-field
|
||||||
.forEach(key => {
|
.forEach(key => {
|
||||||
//check published date
|
//all book members queried should be scalar
|
||||||
if (key == 'published' && !checkDateFormat(query[key].match)) {
|
|
||||||
throw 'Wrong date format for published';
|
|
||||||
}
|
|
||||||
|
|
||||||
//all book members should be scalar
|
|
||||||
if (query[key].scalar) {
|
if (query[key].scalar) {
|
||||||
//push this key into the attributes list
|
//push this key into the attributes list
|
||||||
attributes.push(key);
|
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
|
//search the database
|
||||||
const result = await Book.findAll({
|
const result = await Book.findAll({
|
||||||
attributes: attributes
|
attributes: attributes,
|
||||||
|
where: where,
|
||||||
});
|
});
|
||||||
|
|
||||||
//finally
|
//finally
|
||||||
return result;
|
return result.map(book => book.dataValues);
|
||||||
};
|
};
|
||||||
|
|
||||||
const createBook = async (query, typeGraph) => {
|
const createBook = async (query, typeGraph) => {
|
||||||
@@ -45,7 +47,7 @@ const createBook = async (query, typeGraph) => {
|
|||||||
|
|
||||||
//specify fields to create
|
//specify fields to create
|
||||||
Object.keys(q)
|
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 => {
|
.forEach(key => {
|
||||||
//check published date
|
//check published date
|
||||||
if (key == 'published' && !checkDateFormat(q[key].create)) {
|
if (key == 'published' && !checkDateFormat(q[key].create)) {
|
||||||
@@ -76,7 +78,7 @@ const createBook = async (query, typeGraph) => {
|
|||||||
|
|
||||||
//specify fields to return in each record
|
//specify fields to return in each record
|
||||||
const keys = Object.keys(q)
|
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 => {
|
.forEach(key => {
|
||||||
if (q[key].scalar) {
|
if (q[key].scalar) {
|
||||||
returns[idx][key] = results[idx][key];
|
returns[idx][key] = results[idx][key];
|
||||||
@@ -90,11 +92,96 @@ const createBook = async (query, typeGraph) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const updateBook = 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) => {
|
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 = {
|
module.exports = {
|
||||||
|
|||||||
Reference in New Issue
Block a user