Tweaked comments, updated fallback

This commit is contained in:
2021-04-06 11:17:37 +10:00
parent 45c012a08e
commit f2994fc52e
2 changed files with 20 additions and 15 deletions

View File

@@ -1,6 +1,6 @@
{ {
"name": "sineql", "name": "sineql",
"version": "0.3.1", "version": "0.4.0",
"description": "A simple to use graphQL clone", "description": "A simple to use graphQL clone",
"main": "source/index.js", "main": "source/index.js",
"scripts": { "scripts": {

View File

@@ -1,38 +1,42 @@
const { Op } = require('sequelize'); const { Op } = require('sequelize');
const { books, authors } = require('../database/models'); const { books, authors } = require('../database/models');
//TODO: 'unique' may be a useful modifier, but not at this stage of development
//The create handlers are supposed to handle inserting new data into a database //The create handlers are supposed to handle inserting new data into a database
//You don't have to create all associated books at the same time as the authors - you can use update later to join them //You don't have to create all associated books at the same time as the authors - you can use update later to join them
//You can use the '[' and ']' symbols to create mutliple elements of data at once //You can use the '[' and ']' symbols to create mutliple elements of data at once
//'create' also counts as a modifier, indicating that a specific value is new to the database, and returning an error if it exists already OR //'create' also counts as a modifier, indicating that a specific value is new to the database, and returning an error if it exists already OR
//'match' is used when an existing value must already exist in the database, and returning an error if it does not OR //'match' is used when an existing value must already exist in the database, and returning an error if it does not
//if no modifiers are specified, 'set' is used as a fallback (query for compounds, create if not found)
/* possible create requests include: /* possible create requests include:
create Author { create Author {
name "Sydney Sheldon" create name "Sydney Sheldon"
create books [ create books [
{ {
title "The Naked Face" create title "The Naked Face"
published 1970 published 1970
} }
{ {
title "A Stranger in the Mirror" create title "A Stranger in the Mirror"
published 1976 published 1976
} }
] ]
} }
create Author { create Author {
name "Sydney Sheldon" create name "Sydney Sheldon"
create books { create books {
title "Bloodline" create title "Bloodline"
published 1977 create published 1977
}
}
create Author {
create name "Sydney Sheldon"
match books {
match title "Rage of Angels"
} }
} }
@@ -43,6 +47,7 @@ create Author {
//Author array //Author array
[{ [{
typeName: 'Author', typeName: 'Author',
create: true,
name: { typeName: 'String', scalar: true, create: 'Sydney Sheldon' } name: { typeName: 'String', scalar: true, create: 'Sydney Sheldon' }
books: [{ books: [{
typeName: 'Book', typeName: 'Book',
@@ -96,7 +101,7 @@ const createHandlers = {
//create the element (with created scalar fields) //create the element (with created scalar fields)
const args = {}; const args = {};
Object.keys(fields).filter(field => fields[field].scalar).forEach(field => args[field] = fields[field].create); Object.keys(fields).filter(field => fields[field].scalar).forEach(field => args[field] = fields[field].create || fields[field].set);
const createdAuthor = await authors.create(args); const createdAuthor = await authors.create(args);
//pass on to the sub-objects (books) //pass on to the sub-objects (books)
@@ -158,7 +163,7 @@ const createHandlers = {
//create the element (with created scalar fields) //create the element (with created scalar fields)
const args = {}; const args = {};
Object.keys(fields).filter(field => fields[field].scalar).forEach(field => args[field] = fields[field].create); Object.keys(fields).filter(field => fields[field].scalar).forEach(field => args[field] = fields[field].create || fields[field].set);
args['authorId'] = authorId; //hacked in args['authorId'] = authorId; //hacked in
await books.create(args); await books.create(args);
} }
@@ -166,7 +171,7 @@ const createHandlers = {
//pulled from query (match existing books) //pulled from query (match existing books)
else if (match) { else if (match) {
//get the names of matched fields //get the names of matched fields
const matchedNames = Object.keys(fields).filter(field => fields[field].match); const matchedNames = Object.keys(fields).filter(field => fields[field].match || fields[field].set);
//short-circuit if querying everything //short-circuit if querying everything
const where = {}; const where = {};
@@ -174,7 +179,7 @@ const createHandlers = {
//build the "where" object //build the "where" object
matchedNames.forEach(mn => { matchedNames.forEach(mn => {
if (fields[mn].match !== true) { if (fields[mn].match !== true) {
where[mn] = { [Op.eq]: fields[mn].match }; where[mn] = { [Op.eq]: fields[mn].match || fields[mn].set };
} }
}); });
} }