From f2994fc52eabaf34f6b3a6e0af38c11f810804bc Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Tue, 6 Apr 2021 11:17:37 +1000 Subject: [PATCH] Tweaked comments, updated fallback --- package.json | 2 +- test/handlers/create-handlers.js | 33 ++++++++++++++++++-------------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index 0757128..6029529 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sineql", - "version": "0.3.1", + "version": "0.4.0", "description": "A simple to use graphQL clone", "main": "source/index.js", "scripts": { diff --git a/test/handlers/create-handlers.js b/test/handlers/create-handlers.js index 0bb47cc..8f47f0a 100644 --- a/test/handlers/create-handlers.js +++ b/test/handlers/create-handlers.js @@ -1,38 +1,42 @@ const { Op } = require('sequelize'); 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 //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 //'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 -//if no modifiers are specified, 'set' is used as a fallback (query for compounds, create if not found) +//'match' is used when an existing value must already exist in the database, and returning an error if it does not /* possible create requests include: create Author { - name "Sydney Sheldon" + create name "Sydney Sheldon" create books [ { - title "The Naked Face" + create title "The Naked Face" published 1970 } { - title "A Stranger in the Mirror" + create title "A Stranger in the Mirror" published 1976 } ] } create Author { - name "Sydney Sheldon" + create name "Sydney Sheldon" create books { - title "Bloodline" - published 1977 + create title "Bloodline" + create published 1977 + } +} + +create Author { + create name "Sydney Sheldon" + match books { + match title "Rage of Angels" } } @@ -43,6 +47,7 @@ create Author { //Author array [{ typeName: 'Author', + create: true, name: { typeName: 'String', scalar: true, create: 'Sydney Sheldon' } books: [{ typeName: 'Book', @@ -96,7 +101,7 @@ const createHandlers = { //create the element (with created scalar fields) 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); //pass on to the sub-objects (books) @@ -158,7 +163,7 @@ const createHandlers = { //create the element (with created scalar fields) 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 await books.create(args); } @@ -166,7 +171,7 @@ const createHandlers = { //pulled from query (match existing books) else if (match) { //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 const where = {}; @@ -174,7 +179,7 @@ const createHandlers = { //build the "where" object matchedNames.forEach(mn => { if (fields[mn].match !== true) { - where[mn] = { [Op.eq]: fields[mn].match }; + where[mn] = { [Op.eq]: fields[mn].match || fields[mn].set }; } }); }