mirror of
https://github.com/Ratstail91/sineQL.git
synced 2025-11-29 02:34:28 +11:00
Stripped out the bad code
This commit is contained in:
@@ -1,35 +1,13 @@
|
|||||||
const database = require('./database.js');
|
const database = require('./database.js');
|
||||||
|
|
||||||
console.log(database);
|
|
||||||
|
|
||||||
//the handler routines
|
//the handler routines
|
||||||
const handler = {
|
const handler = {
|
||||||
Book: scalars => {
|
Book: scalars => {
|
||||||
//takes an array of scalar types as objects: { typeName: 'String', name: 'title' }
|
//TODO
|
||||||
//must return an array of objects containing the results
|
|
||||||
|
|
||||||
return database.books.map(b => {
|
|
||||||
const ret = {};
|
|
||||||
|
|
||||||
if (scalars.some(s => s.name == 'title')) {
|
|
||||||
ret.title = b.title;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (scalars.some(s => s.name == 'author')) {
|
|
||||||
ret.author = b.author;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (scalars.some(s => s.name == 'published')) {
|
|
||||||
ret.published = b.published;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
|
|
||||||
Author: scalars => {
|
Author: scalars => {
|
||||||
//takes an array of scalar types as objects: { typeName: 'String', name: 'name' }
|
//TODO
|
||||||
//must return an array of objects containing the results
|
|
||||||
},
|
},
|
||||||
|
|
||||||
create: (sets, matches) => {
|
create: (sets, matches) => {
|
||||||
|
|||||||
@@ -22,9 +22,9 @@ const main = (schema, handler) => {
|
|||||||
|
|
||||||
//no leading keyword - regular query
|
//no leading keyword - regular query
|
||||||
default:
|
default:
|
||||||
const [result] = parseQuery(handler, tokens, pos, typeGraph[tokens[pos - 1]], typeGraph);
|
//TODO: parse the query
|
||||||
|
|
||||||
return [200, result];
|
return [200, ''];
|
||||||
|
|
||||||
//TODO
|
//TODO
|
||||||
break;
|
break;
|
||||||
@@ -41,10 +41,10 @@ const main = (schema, handler) => {
|
|||||||
const buildTypeGraph = schema => {
|
const buildTypeGraph = schema => {
|
||||||
//the default graph
|
//the default graph
|
||||||
let graph = {
|
let graph = {
|
||||||
String: { scalar: true, nullable: true },
|
String: { scalar: true },
|
||||||
Integer: { scalar: true, nullable: true },
|
Integer: { scalar: true },
|
||||||
Float: { scalar: true, nullable: true },
|
Float: { scalar: true },
|
||||||
Boolean: { scalar: true, nullable: true },
|
Boolean: { scalar: true },
|
||||||
};
|
};
|
||||||
|
|
||||||
//parse the schema
|
//parse the schema
|
||||||
@@ -63,6 +63,10 @@ const buildTypeGraph = schema => {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case 'scalar':
|
case 'scalar':
|
||||||
|
if (['type', 'scalar'].includes(graph[tokens[pos]])) {
|
||||||
|
throw 'Unexpected keyword ' + graph[tokens[pos]];
|
||||||
|
}
|
||||||
|
|
||||||
graph[tokens[pos++]] = { scalar: true };
|
graph[tokens[pos++]] = { scalar: true };
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -106,6 +110,11 @@ const parseCompoundType = (tokens, pos) => {
|
|||||||
type = type.slice(0, type.length - 2);
|
type = type.slice(0, type.length - 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//can't use keywords
|
||||||
|
if (['type', 'scalar'].includes(type) || ['type', 'scalar'].includes(name)) {
|
||||||
|
throw 'Unexpected keyword found as type field or type name';
|
||||||
|
}
|
||||||
|
|
||||||
//no mangled types or names
|
//no mangled types or names
|
||||||
checkAlphaNumeric(type);
|
checkAlphaNumeric(type);
|
||||||
checkAlphaNumeric(name);
|
checkAlphaNumeric(name);
|
||||||
@@ -121,51 +130,6 @@ const parseCompoundType = (tokens, pos) => {
|
|||||||
return compound;
|
return compound;
|
||||||
};
|
};
|
||||||
|
|
||||||
const parseQuery = (handler, tokens, pos, typeGraph, superTypeGraph) => {
|
|
||||||
//cache this for later
|
|
||||||
const startPos = pos;
|
|
||||||
|
|
||||||
//the opening brace
|
|
||||||
if (tokens[pos++] != '{') {
|
|
||||||
throw 'Expected \'{\' in query, found ' + tokens[pos - 1];
|
|
||||||
}
|
|
||||||
|
|
||||||
//the fields to pass to the handler
|
|
||||||
const queryFields = [];
|
|
||||||
|
|
||||||
//while not at the end
|
|
||||||
while (tokens[pos] != '}') {
|
|
||||||
//not the end of the query
|
|
||||||
if (!tokens[pos]) {
|
|
||||||
throw 'Expected field in query, got end';
|
|
||||||
}
|
|
||||||
|
|
||||||
//prevent using keywords
|
|
||||||
if (['create', 'update', 'delete', 'set', 'match'].includes(tokens[pos])) {
|
|
||||||
throw 'Unexpected keyword ' + tokens[pos];
|
|
||||||
}
|
|
||||||
|
|
||||||
//type is a scalar, and can be queried
|
|
||||||
if (superTypeGraph[typeGraph[tokens[pos]].typeName].scalar) {
|
|
||||||
//push the scalar object to the queryFields
|
|
||||||
queryFields.push({ typeName: typeGraph[tokens[pos]].typeName, name: tokens[pos] });
|
|
||||||
|
|
||||||
pos++;
|
|
||||||
} else {
|
|
||||||
//parse the subquery using the correct sub-typegraph
|
|
||||||
const [result, increment] = parseQuery(handler, tokens, pos + 1, superTypeGraph[typeGraph[tokens[pos]].typeName], superTypeGraph);
|
|
||||||
|
|
||||||
queryFields.push({ ...typeGraph[tokens[pos]], name: tokens[pos], subquery: handler[typeGraph[tokens[pos]].typeName](result) });
|
|
||||||
|
|
||||||
pos += increment + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(' handler', queryFields);
|
|
||||||
|
|
||||||
return [queryFields, pos - startPos];
|
|
||||||
};
|
|
||||||
|
|
||||||
//utils
|
//utils
|
||||||
const checkAlphaNumeric = (str) => {
|
const checkAlphaNumeric = (str) => {
|
||||||
if (!/^[a-z0-9]+$/i.test(str)) {
|
if (!/^[a-z0-9]+$/i.test(str)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user