API Reference
parseSQL(sqlStatement, options)
Parses the given SQL statement to a JSON output object containing Mongo query components. It automatically determines if the output should be an aggregate pipeline or query depending on if a straight query is possible. The output JSON object properties can be used to construct a MongoDB query.
sqlStatement
<string>
- The SQL statement to parseoptions
<object>
- Options for the parserdatabase
<string>
- The database type. Can bemysql
orpostgresql
. Defaults tomysql
.- Returns:
<object>
type
<string>
- The type of query. Can bequery
oraggregate
.collection
<string>
- The collection to query for aquery
type.query
<object>
- The query object to use for aquery
type.projection
<object>
- The projection object to use for aquery
type.limit
<number>
- The limit to use for aquery
type.pipeline
<array>
- The aggregate array to use for anaggregate
type.collections
<array>
- The collections to use for anaggregate
type.
Example parseSQL
usage for a query
Node.js:
const SQLMongoParser=require('@synatic/noql');
SQLMongoParser.parseSQL(
"select id from `films` where `id` > 10 limit 10",
{ database: 'postgresql' /* or 'mysql' */ } );
Output:
Example parseSQL
usage for an aggregate
Node.js:
Output:Using the output with MongoClient
To use the output object, construct a query with MongoClient
from the MongoDB NodeJS Driver
Example usage with MongoClient
Node.js:
const SQLParser = require('@synatic/noql');
const {MongoClient} = require('mongodb');
(async () => {
try {
client = new MongoClient('mongodb://127.0.0.1:27017');
await client.connect();
const db = client.db('noql-test');
const parsedSQL = SQLParser.parseSQL(
"select id from `films` limit 10",
{ database: 'postgresql' /* or 'mysql' */ });
if (parsedSQL.type === 'query') {
console.log(
await db
.collection(parsedSQL.collection)
.find(parsedSQL.query || {}, parsedSQL.projection || {})
.limit(parsedSQL.limit || 50)
.toArray()
);
} else if (parsedSQL.type === 'aggregate') {
console.log(
await db
.collection(parsedSQL.collections[0])
.aggregate(parsedSQL.pipeline)
.toArray()
);
}
} catch (exp) {
console.error(exp);
}
})();
parseSQLtoAST(sqlStatement, options)
Parses a SQL statement to an AST (abstract syntax tree)
sqlStatement
<string>
- The SQL statement to parseoptions
<object>
- Options for the parserdatabase
<string>
- The database type. Can bemysql
orpostgresql
. Defaults tomysql
.- Returns:
<object>
tableList
<string[]>
- A list of tables used in the querycolumnList
<string[]>
- A list of columns used in the queryast
<object>
- The AST (abstract syntax tree) of the query
Example parseSQLtoAST
usage
Node.js
const SQLMongoParser = require('@synatic/noql');
SQLMongoParser.parseSQLtoAST(
"select id from `films`",
{ database: 'postgresql' /* or 'mysql' */ });
Output:
{
"tableList": [
"select::null::films"
],
"columnList": [
"select::null::id"
],
"ast": {
"with": null,
"type": "select",
"options": null,
"distinct": null,
"columns": [
{
"expr": {
"type": "column_ref",
"table": null,
"column": "id"
},
"as": null
}
],
"from": [
{
"db": null,
"table": "films",
"as": null
}
],
"where": null,
"groupby": null,
"having": null,
"orderby": null,
"limit": null,
"for_update": null
}
}
makeMongoQuery(sqlStatement, options)
Generates Mongo query components from a SQL statement if possible. Throws an exception if not possible.
Use canQuery
to test if a query can be created, or if an aggregate must be made instead.
If an aggregate must be made, use makeMongoAggregate
instead. If you want NoQL to automatically choose, use parseSQL
instead.
sqlStatement
<string>
- The SQL statement to parseoptions
<object>
- Options for the parserdatabase
<string>
- The database type. Can bemysql
orpostgresql
. Defaults tomysql
.- Returns:
<object>
type
<string>
- The type of query. It will bequery
.collection
<string>
- The collection to query for aquery
type.query
<object>
- The query object to use for aquery
type.projection
<object>
- The projection object to use for aquery
type.limit
<number>
- The limit to use for aquery
type.
Example makeMongoQuery
usage
Node.js
const SQLMongoParser=require('@synatic/noql');
SQLMongoParser.makeMongoQuery(
"select id from `films` where id > 10 limit 10",
{ database: 'postgresql' /* or 'mysql' */ });
Output:
makeMongoAggregate(sqlStatement, options)
Generates a mongo aggregate pipeline components from a SQL statement.
If your SQL statement can be represented by a straight query, use makeMongoQuery
instead. If you want NoQL to automatically choose, use parseSQL
instead.
sqlStatement
<string>
- The SQL statement to parseoptions
<object>
- Options for the parserdatabase
<string>
- The database type. Can bemysql
orpostgresql
. Defaults tomysql
.- Returns:
<object>
type
<string>
- The type of query. It will beaggregate
.pipeline
<array>
- The aggregate pipeline array to use for anaggregate
type.collections
<array>
- The collections to use for anaggregate
type.
Example makeMongoAggregate
usage
Node.js
Output:canQuery(sqlStatement, options)
Checks if a SQL statement can be represented by a straight Mongo query, or if an aggregate query must be used.