From 972866a7047c09ced23fe1d3729af9c443ca4326 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Tue, 6 Apr 2021 09:06:52 +1000 Subject: [PATCH] Added unique keyword --- README.md | 9 +++++---- package.json | 2 +- source/build-type-graph.js | 27 +++++++++++++++++++++++---- source/keywords.json | 2 +- test/database/models/authors.js | 3 ++- test/database/models/books.js | 5 +++-- test/handlers/schema.js | 4 ++-- 7 files changed, 37 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 5d99c57..d18d9e8 100644 --- a/README.md +++ b/README.md @@ -103,12 +103,13 @@ wave('Author { name books { title } }') ## The Schema Language -The schema language is a layout of how queries should be made, as well as what can be made with them. There are two built-in keywords for the schema language: +The schema language is a layout of how queries should be made, as well as what can be made with them. There are several built-in keywords for the schema language: * type * scalar +* unique -`type` is used for defining new compound types. `scalar` is for defining new scalar types, such as `Date`. +`type` is used for defining new compound types. `scalar` is for defining new scalar types, such as `Date`. `unique` is a modifier on a field, indicating that it is unique in the database. The built-in types for the schema language are: @@ -123,12 +124,12 @@ These can be combined into compound types as so: scalar Date type Book { - String title + unique String title Date published } type Author { - String name + unique String name Book books } ``` diff --git a/package.json b/package.json index 7cc55cc..0757128 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sineql", - "version": "0.3.0", + "version": "0.3.1", "description": "A simple to use graphQL clone", "main": "source/index.js", "scripts": { diff --git a/source/build-type-graph.js b/source/build-type-graph.js index c6061e8..7921f8c 100644 --- a/source/build-type-graph.js +++ b/source/build-type-graph.js @@ -38,7 +38,8 @@ const buildTypeGraph = (schema, options) => { graph[tokens[pos++]] = { typeName: tokens[pos - 1], scalar: true }; if (options.debug) { - console.log(`Defined ${tokens[pos - 1]}:\n`, graph[tokens[pos - 1]]); + console.log(`Defined ${tokens[pos - 1]}:`); + console.log(graph[tokens[pos - 1]], '\n'); } break; @@ -49,7 +50,8 @@ const buildTypeGraph = (schema, options) => { } if (options.debug) { - console.log('\nType Graph:\n', graph, '\n'); + console.log('\nType Graph:'); + console.log(graph, '\n'); } return graph; @@ -62,11 +64,24 @@ const parseCompoundType = (tokens, pos, scalars, options) => { throw 'Expected \'{\' in compound type definition'; } + //schema modifiers + let modifiers = {}; + //graph component to be returned const compound = { typeName: tokens[pos - 1] }; //for each line of the compound type while (tokens[pos++] && tokens[pos] !== '}') { + //check for modifiers + while(['unique'].includes(tokens[pos])) { + if (Object.keys(modifiers).includes(tokens[pos])) { + throw `Unexpected duplicate modifier in schema (${tokens[pos]})`; + } + + modifiers[tokens[pos++]] = true; + } + + //scan the type & name let type = tokens[pos++]; const name = tokens[pos]; @@ -91,12 +106,16 @@ const parseCompoundType = (tokens, pos, scalars, options) => { //finally, push to the compound definition compound[name] = { - typeName: type + typeName: type, + ...modifiers }; + + modifiers = {}; //reset } if (options.debug) { - console.log(`Defined ${options.debugName}:\n`, compound); + console.log(`Defined ${options.debugName}:`); + console.log(compound, '\n'); } return compound; diff --git a/source/keywords.json b/source/keywords.json index 9a59b40..76ed8ee 100644 --- a/source/keywords.json +++ b/source/keywords.json @@ -1 +1 @@ -["type", "scalar", "create", "update", "delete", "set", "match", "typeName"] \ No newline at end of file +["type", "scalar", "create", "update", "delete", "set", "match", "unique", "typeName"] \ No newline at end of file diff --git a/test/database/models/authors.js b/test/database/models/authors.js index 1d4364a..00f91fd 100644 --- a/test/database/models/authors.js +++ b/test/database/models/authors.js @@ -11,6 +11,7 @@ module.exports = sequelize.define('authors', { }, name: { - type: Sequelize.TEXT + type: Sequelize.STRING, + unique: true } }); diff --git a/test/database/models/books.js b/test/database/models/books.js index 8de1a14..80008f9 100644 --- a/test/database/models/books.js +++ b/test/database/models/books.js @@ -11,11 +11,12 @@ module.exports = sequelize.define('books', { }, title: { - type: Sequelize.TEXT + type: Sequelize.STRING, + unique: true }, published: { - type: Sequelize.TEXT + type: Sequelize.STRING }, rating: { diff --git a/test/handlers/schema.js b/test/handlers/schema.js index 36c74c3..a26947a 100644 --- a/test/handlers/schema.js +++ b/test/handlers/schema.js @@ -3,13 +3,13 @@ module.exports = ` scalar Date type Book { - String title + unique String title Date published Float rating } type Author { - String name + unique String name Book books } `; \ No newline at end of file