Giter Club home page Giter Club logo

json-sql-builder2's People

Contributors

belgac avatar dependabot[bot] avatar g8up avatar mayur-dit avatar planetarydev avatar umer-mehmood avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

json-sql-builder2's Issues

Select all, with further columns?

Is there a method for doing a select all with columns? Ideally allowing for further columns. I've looked through the documentation but I don't seem to be able to find anything referencing this,

For example:

SELECT
	Table1.*,
	Table2.Column3
FROM
	Table1
	LEFT JOIN Table2 ON Table1.Column1 = Table2.Column2

Hoping to be able to do:

SELECT
	Table1.*,
	(
		SELECT
			COUNT(*)
		FROM
			Table1
		WHERE
			< some conditions >
	) `count`
FROM
	Table1
WHERE
	< some conditions >

no such file or directory, scandir '/sql/' issue var sql = new SQLBuilder('SQLServer');

I'm getting this below error on deploying aws lambda node 8.10.0

2019-05-09T04:23:22.996Z 6010d98c-2788-4c96-b972-49361876948c Error: ENOENT: no such file or directory, scandir '/sql/'
at Object.fs.readdirSync (fs.js:904:18)
at Object.walk (/var/task/src/index.js:144899:16)
at SQLBuilder._loadModules (/var/task/src/index.js:144709:23)
at new SQLBuilder (/var/task/src/index.js:144298:8)
at handler (/var/task/src/index.js:76189:15)

Compatibility with MongoDB

What is the state of this project regarding supporting MongoDB features? This is by far the most comprehensive project I have seen offering a somewhat compatible syntax, and I'd be willing to contribute in adding missing helpers and operators.

Was this why this project have been created, or was the idea to support yet another subset of JSON queries? And if so, why can't it be made to be an adapter layer between MongoDB queries and SQL queries?

I understand the limitations of both specifications, and I wouldn't expect everything to be supported, but having these features would allow projects to integrate SQL with NoSQL more easily, and it seems like this project has already the foundations for this.

SyntaxError: Unexpected token ...

  • node v8.2.1

got an error:

/root/code/node-crx-mid/node_modules/json-sql-builder2/sql/helpers/functions/string/left/left.js:248
                                                        people_id: { $column: { $type: 'INT', ...NOT_NULL, ...PRIMARY_KEY } },
                                                                                              ^^^

SyntaxError: Unexpected token ...
    at createScript (vm.js:74:10)
    at Object.runInThisContext (vm.js:116:10)
    at Module._compile (module.js:533:28)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Module.require (module.js:513:17)
    at require (internal/module.js:11:18)
    at SQLBuilder._register (/root/code/node-crx-mid/node_modules/json-sql-builder2/lib/builder.js:474:19)

Because the object spread operatior is not supported in node v8.2.1:

const People = new Table('people', {
people_id: { $column: { $type: 'INT', ...NOT_NULL, ...PRIMARY_KEY } },
first_name: { $column: { $type: 'VARCHAR', $size: 50, $notNull: true } },
last_name: { $column: { $type: 'VARCHAR', $size: 50, $notNull: true } },
age: { $column: { $type: 'INT', $notNull: true, $default: 0 } }
}, {
createIfNotExists: true
});

Invalid index on SQL Server params

If find this library excellent for my purpose, but it really emits wrong indices for SQL Server parameters.

I see there is a pull request for this already, and it needs to be fixed for this library to be usable.

not able to use trim(), upper(), lower(), etc

Hi,

I am trying to use trim, upper, lower functions, but am unable to.
getting errors as:

The Helper or Operator with the name '$trim' is not defined
The Helper or Operator with the name '$upper' is not defined
The Helper or Operator with the name '$lower' is not defined

if json-sql-bulder2 supports these, please share examples for the same

Feature request: add function LAST_INSERT_ID()

The MySQL function LAST_INSERT_ID() should be supported to facilitate inserts.

sql.$select({ last_id: { $lastInsertedId: true } });

should produce

SELECT LAST_INSERT_ID() AS last_id

Alter Table

I am writing a database library for many basic functions for many database types. I found your library and it coincides a lot with my own methodology concerning query generation. I am leaving an Query.raw() function available for more advanced queries, but one thing that seems to be missing from your library is $alterTable as opposed to $createTable.

I am going to check out the forks of your library to see if someone else has added such functionality but I was hoping you might be spurred to create an update to this and npm. I am currently writing my own query language and translators to work with many database types from Mongo to MsSql, Couch, Cockroach, and even Oracle. I was going to use Knex.js, but its methodology is not the same.

In any case, Thank You for this cool library. Ill have a credits page somewhere with all the packages used in the projects.

isValidIdent method question

Hello,

I am sending this json

{
    "$select": {
        "$columns": ["day", "firstname", "lastname"],
        "$from": "a.b.1"
    }
}

As a result I got:

{
  "sql": "SELECT day, firstname, lastname FROM a.b.\"1\"",
  "values": []
}

What I was expecting was:

{
  "sql": "SELECT day, firstname, lastname FROM a.b.1",
  "values": []
}

The point in the code that changes what I expected is in the following validation.

isValidIdent(identifier){
return /^[a-z_][a-z0-9_$]*$/.test(identifier) === true && !this._reservedWords[identifier.toUpperCase()];
}

Is there any reason the identifier must not begin with numbers?

I could fix it, if there is no problem in changing this condition.

Best regards.

Support SQL Server XML modify()

I'm trying to do a:

UPDATE ball SET [cmdList].modify('insert <val>1030</val> into (/cmdList)[1]'), ogga.modify('insert <val>30</val> into (/ogga)[1]') WHERE [ballId] = @param3"

Or actually, I wanted the inserts to be sql arguments as well, but that does not seem to fit very well with SQL server rigid regime.

I ended up assigning the modify string to the json key in the $set, and setting the value (sql argument) to null. Then I had to do a nasty regex to ret rid of the argument part and some misplaced brackets:

const nastyModifyRegex = /\[modify\((.+?)\[1\]\]?\)\]\s?=\s?@param\d+/g

const correctNastyModifyFunction = (output: ISQLlyThings) => ({...output, sql: output.sql.replace(nastyModifyRegex, "modify($1[1])")})

It would be nice to have support for modify in the library. I tried to read up on operators, but it seems this needs to be built into the library, no plugin-system available.

order by function(value)

how can I achieve select name from foo order by upper(name);? I can only find select upper(name) as uname, name from foo order by uname; but it overloads select.

`new SQLBuilder('MySQL')` Fails on Windows

The following code is fails on Windows.

const SQLBuilder = require('json-sql-builder2');
const sql = new SQLBuilder('MySQL');

The details of error is:

Debugger listening on ws://127.0.0.1:36065/9492f253-abbe-409b-b76d-178e0321aa1b
Debugger attached.
path.js:28
    throw new TypeError('Path must be a string. Received ' + inspect(path));
    ^

TypeError: Path must be a string. Received undefined
    at assertPath (path.js:28:11)
    at Object.join (path.js:489:7)
    at SQLBuilder._register (c:\Users\hidori\GitHub\node-json-sql-builder2-test\node_modules\json-sql-builder2\lib\builder.js:485:33)
    at _.forEach (c:\Users\hidori\GitHub\node-json-sql-builder2-test\node_modules\json-sql-builder2\lib\builder.js:459:10)
    at arrayEach (c:\Users\hidori\GitHub\node-json-sql-builder2-test\node_modules\lodash\lodash.js:516:11)
    at Function.forEach (c:\Users\hidori\GitHub\node-json-sql-builder2-test\node_modules\lodash\lodash.js:9342:14)
    at SQLBuilder._loadModules (c:\Users\hidori\GitHub\node-json-sql-builder2-test\node_modules\json-sql-builder2\lib\builder.js:457:5)
    at new SQLBuilder (c:\Users\hidori\GitHub\node-json-sql-builder2-test\node_modules\json-sql-builder2\lib\builder.js:48:8)
    at Object.<anonymous> (c:\Users\hidori\GitHub\node-json-sql-builder2-test\index.js:2:13)
    at Module._compile (module.js:649:14)
Waiting for the debugger to disconnect...
TypeError: Path must be a string. Received undefined
path.js:28
    at assertPath (path.js:28:11)
    at Object.join (path.js:489:7)
    at SQLBuilder._register (c:\Users\hidori\GitHub\node-json-sql-builder2-test\node_modules\json-sql-builder2\lib\builder.js:485:33)
    at _.forEach (c:\Users\hidori\GitHub\node-json-sql-builder2-test\node_modules\json-sql-builder2\lib\builder.js:459:10)
    at arrayEach (c:\Users\hidori\GitHub\node-json-sql-builder2-test\node_modules\lodash\lodash.js:516:11)
    at Function.forEach (c:\Users\hidori\GitHub\node-json-sql-builder2-test\node_modules\lodash\lodash.js:9342:14)
    at SQLBuilder._loadModules (c:\Users\hidori\GitHub\node-json-sql-builder2-test\node_modules\json-sql-builder2\lib\builder.js:457:5)
    at new SQLBuilder (c:\Users\hidori\GitHub\node-json-sql-builder2-test\node_modules\json-sql-builder2\lib\builder.js:48:8)
    at Object.<anonymous> (c:\Users\hidori\GitHub\node-json-sql-builder2-test\index.js:2:13)
    at Module._compile (module.js:649:14)

So, builder.js contains following codes:

		this.currentModulePath = path.join('../sql/', file.split('/sql')[1]);
		op.sqlPath = './' + this.currentModulePath.split('/sql/')[1];

these are NOT GOOD for Windows.

On Windows, the file path is separated by \. not /.

complex sql handling : case statements, string functions, analytic functions

need to write/create sqls with all the possible complex constructs in a single/same sql.

Example as follows:

select distinct
cast(NULL as string) as c1
,case when t1.c2 is null then lower(trim(t2.c1)) else lower(trim(t1.c2)) end as c2
,regexp_replace(t2.c2,"^0+","") as c3
,t2.c4 as c4
,cast(NULL as string) as c4
,from_unixtime(unix_timestamp(t4.c6,'dd/MMM/yyyy'),'yyyy-MM-dd') as c5


from
db.t21 t2

left outer join db.t1 on

lower(TRIM(t1.c1)) = lower('XYZS') AND
lower(TRIM(t2.c1)) = lower(TRIM(t1.c1))

left outer join
(select ---------------------)

max(case when c1 = 'value' then c1 end) as c1 from db.t1

do we have relevant scenarios covered in json-sql-builder2?
does it support all these?
Awiating response!
Thanks!

help: psql function call

Thanks for the wonderful library.
What is the best way to define the call to the psql [postgresql] function using json-sql-builder2?

for example, we would like to call

select * from analytics.compute_salary($1, $2, $3)

where as $1,$2,$3 might be values/params in json-sql-builder2.

Any help would be helpful here.

$ine broken for $createView

This JSON:

{
    "$select": {
        "$column1": true,
        "$from": "tbl1"
    },
    "$view": "view1",
    "$ine": true
}

Produces the following SQL:

CREATE VIEW  IF NOT EXISTSview1 AS SELECT column1 FROM tbl1

MySQL | possible to search JSON values

Quick question, do you support a mysql query like this:
select * from event where data->"$.foo" = 'bar'


The data column is of type JSON, I tried a few different approaches through the json builder, but no success.

'$from' rejects multiple source

Hi, I'm new to json-sql-builder2 :-)

I'm trying to write the flowing query by JSON

select
	*
from
	label, label_article_rel
where
	label.label_id = label_article_rel.label_id

I wrote the following JSON

const SqlBuilder = require('json-sql-builder2');
const builder= new SqlBuilder('MySQL');
const query = builder.$select({
    $from: ['label', 'label_article_rel'],
    $where: {
        'label.label_id': 'label_article_rel.label_id',
    }});

It's fails. the error is:

Using Helper '$from' must be type of 'Object, String', but got Type 'Array' with value '["label","label_article_rel"]'

Division arithmetic helper does not exists

Hello, we have a use case, in which we have to divide a column with an integer value. To do that I am not able to find any arithmetic helper. There exists two for addition and multiplication, but we also have to apply division operation on a column.

Example: Select * from column1 * 1, column2 / 2 from Table

Kindly let me know if there is any way to achieve this. Thanks

custom functions?

What's the easiest way to run select to_timestamp(foo.datetime) from foo?Is there way to pass a raw "to_timestamp(foo.datetime)" expression? Same question about now()

$concat broken for SQLite

SQLite does not support the CONCAT() function, so $concat does not work.
Instead, the SQL implementation for CONCAT should seperate strings with pipes.

So MySQL:

SELECT
    CONCAT("one", "two", column1)
FROM
    ...

Should be:

SELECT
    "one" || "two" || column1
FROM
    ...

Support for Virtual Column in Select Statement for Postgres

We need to add a virtual column in select statement while composing the sql query.

Use Case:
We are trying to build a query using postgresql dialect. The query then will be executed on AWS Athena Service. And in case of virtual columns athena requires string values in single qoutes i.e. 'value' as "Column_Name". So the issue is json-sql-builder2 does not add single qoutes for the value of virtual column. Let me know if there is any way. Thanks

return full query in object

How can I output the raw text query without having to rely on separate software to reconstruct it from sql and values (that is arguments) array?

Documentation

Hi. The documentation doesn't contains information about how I put the user, password, database, etc. Can you help me?

syntax error in readme.md

const SQLBuilder = require('json-sql-builder2');
// create a new instance of the SQLBuilder and load the language extension for mysql
var sql = new SQLBuilder('MySQL');

// lets start some query fun
var totalSalary = sql.$select({
    job_title: true,
    total_salary: { $sum: 'salary' },
    $from: 'people',
    $where: {
        job_title: { $in: ['Sales Manager', 'Account Manager'] },
        age: { $gte: 18 },
        country_code: 'US',
    },
    $groupBy: 'job_title',
});

comma missing in total_salary line

sql select returning query which is not runnable in mssql.

const SQLBuilder = require('json-sql-builder2');

const sql = new SQLBuilder('SQLServer');
sql.$select({ job_title: true, total_salary: { $sum: 'salary' }, $from: 'people', $where: { job_title: { $in: ['Sales Manager', 'Account Manager'] }, age: { $gte: 18 }, country_code: 'US', }, $groupBy: 'job_title', })
Converted to

{"sql":"SELECT job_title, SUM(salary) AS total_salary FROM people WHERE job_title IN (@param1, @param2) AND age >= @param3 AND country_code = @param4 GROUP BY job_title","values":{"param1":"Sales Manager","param2":"Account Manager","param3":18,"param4":"US"}}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.