Added Author query, creation handlers

This commit is contained in:
2023-06-03 22:32:27 +10:00
parent e6b51f5374
commit 71c201cae5
5 changed files with 181 additions and 36 deletions
+7 -1
View File
@@ -1,7 +1,9 @@
const Sequelize = require('sequelize'); const Sequelize = require('sequelize');
const sequelize = require('..'); const sequelize = require('..');
module.exports = sequelize.define('author', { const Book = require("./book");
const Author = sequelize.define('author', {
index: { index: {
type: Sequelize.INTEGER(11), type: Sequelize.INTEGER(11),
allowNull: false, allowNull: false,
@@ -17,3 +19,7 @@ module.exports = sequelize.define('author', {
unique: true, unique: true,
} }
}); });
Author.hasMany(Book);
module.exports = Author;
+3 -1
View File
@@ -1,7 +1,7 @@
const Sequelize = require('sequelize'); const Sequelize = require('sequelize');
const sequelize = require('..'); const sequelize = require('..');
module.exports = sequelize.define('book', { const Book = sequelize.define('book', {
index: { index: {
type: Sequelize.INTEGER(11), type: Sequelize.INTEGER(11),
allowNull: false, allowNull: false,
@@ -23,3 +23,5 @@ module.exports = sequelize.define('book', {
defaultValue: null defaultValue: null
} }
}); });
module.exports = Book;
+145 -4
View File
@@ -1,17 +1,158 @@
const { Author, Book } = require('../database/models');
const { queryBookFields } = require("./book-handlers");
const queryAuthorFields = (query) => {
//subtypes to find
const subtypes = {
"books": {
model: Book,
query: queryBookFields,
}
};
const include = [];
//sequelize stuff to find
const attributes = [];
const where = {};
//specify fields to find
Object.keys(query)
.filter(key => query[key])
.filter(key => key != 'typeName') //filter out this meta-field
.forEach(key => {
//all author members queried should be scalar
if (query[key].scalar) {
//push this key into the attributes list
attributes.push(key);
}
//handle compounds
else {
include.push({
model: subtypes[key].model,
...subtypes[key].query(query[key])
}); //handle compound subtypes
}
//filter the members to a specific value
if (query[key].match) {
where[key] = query[key].match == 'NULL' ? null : query[key].match;
}
})
;
return { attributes, where, include };
};
const queryAuthor = async (query, typeGraph) => { const queryAuthor = async (query, typeGraph) => {
throw 'Not yet implemented'; const authorFields = queryAuthorFields(query);
try {
//search the database
return await Author.findAll({
...authorFields,
raw: true,
});
}
catch(e) {
console.log(e);
throw "Query failed";
}
};
const createAuthorFields = (query) => {
const subtypes = {
"Book": {
model: Book,
query: queryBookFields,
}
};
//hook to monkey wrap
let afterBulkCreate = (instances, options) => null;
//the array of objects to insert
const records = [];
query.forEach((q, idx) => {
//just in case
if (!q.create) {
throw 'Unexpected create == false';
}
//specify fields to create
Object.keys(q)
.filter(key => q[key])
.filter(key => key != 'typeName' && key != 'create') //filter out these meta-fields
.forEach(key => {
//some author members should be scalar
if (q[key].scalar) {
//make sure this exists
records[idx] = records[idx] || {};
//push this key into the inserts list
records[idx][key] = q[key].create;
return;
}
//find each compound type, associate it with the created instances (using a wrapped hook)
q[key].forEach(compoundType => {
const prev = afterBulkCreate; //cache prev impl.
const { where } = subtypes[compoundType.typeName].query(compoundType);
afterBulkCreate = (instances, options) => {
//monkey patch the associations
instances.forEach(instance => {
//BUGFIX: don't want records mixed
if (instance.name != q["name"]?.create) {
return;
}
subtypes[compoundType.typeName].model.update({
authorIndex: instance["index"],
}, {
where: where
});
});
//continue
return prev(instances, options);
}
});
})
;
});
return [records, { afterBulkCreate }];
}; };
const createAuthor = async (query, typeGraph) => { const createAuthor = async (query, typeGraph) => {
throw 'Not yet implemented'; const [authorRecords, authorOptions] = createAuthorFields(query);
try {
Author.afterBulkCreate(authorOptions.afterBulkCreate)
//insert into the database
await Author.bulkCreate(
authorRecords,
authorOptions,
);
}
catch(e) {
console.log(e);
throw "Create failed";
}
return "OK";
}; };
const updateAuthor = async (query, typeGraph) => { const updateAuthor = async (query, typeGraph) => {
throw 'Not yet implemented'; throw 'Author update handler not yet implemented';
}; };
const deleteAuthor = async (query, typeGraph) => { const deleteAuthor = async (query, typeGraph) => {
throw 'Not yet implemented'; throw 'Author delete handler not yet implemented';
}; };
module.exports = { module.exports = {
+6 -3
View File
@@ -1,7 +1,7 @@
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) || date == 'NULL'; 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.toString().toUpperCase() == 'NULL';
const queryBookFields = (query) => { const queryBookFields = (query) => {
//sequelize stuff to find //sequelize stuff to find
@@ -10,6 +10,7 @@ const queryBookFields = (query) => {
//specify fields to find //specify fields to find
Object.keys(query) Object.keys(query)
.filter(key => query[key])
.filter(key => key != 'typeName') //filter out this meta-field .filter(key => key != 'typeName') //filter out this meta-field
.forEach(key => { .forEach(key => {
//all book members queried should be scalar //all book members queried should be scalar
@@ -142,7 +143,7 @@ const updateBookFields = (query) => {
const updateBook = async (query, typeGraph) => { const updateBook = async (query, typeGraph) => {
const { updates, ...opts } = updateBookFields(query); const { updates, ...opts } = updateBookFields(query);
console.log(opts)
try { try {
await Book.bulkCreate( await Book.bulkCreate(
updates, updates,
@@ -182,7 +183,7 @@ const deleteBook = async (query, typeGraph) => {
//filter the members to a specific value //filter the members to a specific value
if (q[key].match) { if (q[key].match) {
where[key] = q[key].match == 'NULL' ? null : q[key].match; where[key] = q[key].match.toString().toUpperCase() == 'NULL' ? null : q[key].match;
} }
}) })
; ;
@@ -208,4 +209,6 @@ module.exports = {
createBook, createBook,
updateBook, updateBook,
deleteBook, deleteBook,
queryBookFields,
}; };
+20 -27
View File
@@ -13,7 +13,7 @@ Content-Type: text/plain
create Book [ create Book [
{ {
create title "The Philosophers Kidney Stone" create title "The Philosopher's Kidney Stone"
} }
{ {
create title "The Chamber Pot of Secrets" create title "The Chamber Pot of Secrets"
@@ -42,32 +42,32 @@ Content-Type: text/plain
update Book [ update Book [
{ {
match title "The Philosophers Kidney Stone" match title "The Philosopher's Kidney Stone"
update published "1969-04-21" update published "1997-06-26"
} }
{ {
match title "The Chamber Pot of Secrets" match title "The Chamber Pot of Secrets"
update published "1969-04-22" update published "1998-07-02"
} }
{ {
match title "The Prisoner of Aunt Kazban" match title "The Prisoner of Aunt Kazban"
update published "1969-04-23" update published "1999-07-08"
} }
{ {
match title "The Goblet of the Fire Cocktail" match title "The Goblet of the Fire Cocktail"
update published "1969-04-24" update published "2000-07-08"
} }
{ {
match title "The Order for Kleenex" match title "The Order for Kleenex"
update published "1969-04-25" update published "2003-06-21"
} }
{ {
match title "The Half-Priced Pharmacy" match title "The Half-Priced Pharmacy"
update published "1969-04-26" update published "2005-07-16"
} }
{ {
match title "Yeah, I Got Nothing" match title "Yeah, I Got Nothing"
update published "1969-04-27" update published "2007-07-21"
} }
] ]
@@ -76,27 +76,20 @@ update Book [
POST http://localhost:4000/sineql HTTP/1.1 POST http://localhost:4000/sineql HTTP/1.1
Content-Type: text/plain Content-Type: text/plain
delete Book [ create Author [
{ {
match title "The Philosophers Kidney Stone" create name "Just Kidding"
match books [
{
match title "Yeah, I Got Nothing"
}
{
match title "The Half-Priced Pharmacy"
}
]
} }
{ {
match title "The Chamber Pot of Secrets" create name "Mark Twain"
}
{
match title "The Prisoner of Aunt Kazban"
}
{
match title "The Goblet of the Fire Cocktail"
}
{
match title "The Order for Kleenex"
}
{
match title "The Half-Priced Pharmacy"
}
{
match title "Yeah, I Got Nothing"
} }
] ]