Coder Social home page Coder Social logo

json-sql's People

Contributors

anton-makarov avatar artzhookov avatar dmitryefimenko avatar fleg avatar noahprince22 avatar okv avatar roobingood avatar serp-dev avatar veob 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  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  avatar  avatar  avatar  avatar  avatar  avatar

json-sql's Issues

Unable to apply a function in conditions.

I need to make a query that looks like this:

select "stamp", DATE_TRUNC('week', stamp::timestamp) as "week", "location", sum("impression") as "impression", sum("click") as "click" from "testTable" where ("stamp" >= $1 and "stamp" < $2) and "DATE_TRUNC('week', stamp::timestamp)" in ($3) group by "stamp", "week", "location" order by "stamp";

I'm using js output of json like this:

  table: 'testTable',
  fields: 
   [ { name: 'stamp', display: 'Time' },
     { alias: 'week',
       expression: 'DATE_TRUNC(\'week\', stamp::timestamp)',
       display: 'Week',
       urlmap: 'week' },
     { name: 'location', display: 'Location' },
     { func: [Object], alias: 'impression', display: 'Impression' },
     { func: [Object], alias: 'click', display: 'Click' } ],
  condition: 
   { stamp: 
      { '$gte': '2017-01-01T00:00:00Z',
        '$lt': '2018-01-01T00:00:00Z' },
     'DATE_TRUNC(\'week\', stamp::timestamp)': { '$in': [Array] } },
  group: [ 'stamp', 'week', 'location' ],
  sort: [ 'stamp' ] }```


The problem is that this query won't run as `"DATE_TRUNC('week', stamp::timestamp)"` in condition is quoted. 

I've read unit tests and documentation and I've not seen a way to deal with this. 

thanks.

How to do a bulk insert?

How to execute a bulk insert? MySQL query like bellow.

INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);

How to Provide the Options

How can we pass options in below code:

var jsonSql = require('json-sql')(options);

I want to change the dailect to 'mssql' and separatedvalues to false

Please suggest

Advance group by

Hello! I have a hard sql query. This is part of sql
group by (cast(height / $delegates as integer) + (case when height % $delegates > 0 then 1 else 0 end)) having height <= $height

I made something like that:
group: {
expression: "(cast(height / $delegates as integer) + (case when height % $delegates > 0 then 1 else 0 end))",
having: {
height: {$lte: height}
}
}

Can you add this feature, I can´t make this.

Sqlite error with object insertion

Way too lazy to write a pull request, but I had to change line 50 in builder.js

this._values[this.options.valuesPrefix + placeholder] = value;

If I leave it as is, I get "SQLITE_RANGE: column index out of range"

The name of the properties in

"condition" to "where"

Great little library, works well for me.

Would it be possible to rename the "condition" template to "where"? Almost all other keywords follow standard SQL naming, but "where" does not and is very puzzling. What was the reasoning behind that design choice?

Clarification/Question

Hi, I'm trying to use your module to convert json objects into sql queries that I can output to a file,

So following your insert example, I eventually come up with

insert into test (check1, check2, check3) values ($p1, $p2, $p3);
{ p1: 'check1', p2: 'check2', p3: 'check3' }

Do you know how I could make that readable to SQL as part of a .sql file? where the values are part of the string?

Also do you have any plans to support foreign keys?

Otherwise thank you so much for this module. It is a life-saver.

Can we use have expression in Where condition

Can we use have expression in Where condition

e.g
select s.totalAmount where (s.TaxAmount + s.CessAmount) > 0

We are not able to provide the (s.TaxAmount + s.CessAmount) > 0 in the where condition

Using ROW_NUMBER()

I cannot find a way to generate a query like this:

SELECT ROW_NUMBER() OVER (ORDER BY id DESC), * FROM posts;

I am using PostgreSQL. Is it possible to use the ROW_NUMBER() value?

[mariadb/mysql] does not support named placeholders like :col1 - use ? instead

[mariadb/mysql] does not support named placeholders like :col1 - use ? instead

const jsonSql = require("json-sql")({
  namedValues: false,
  valuesPrefix: "$$",
});
jsonSql.setDialect("mysql");
    const sql = jsonSql.build({
      type: "update",
      table: "table1",
      condition: {
        guid: id,
      },
      modifier: updateFields,
    });
/// CONVERT placeholders
const query1 = sql.query.replace(/\$\$\d+/g, "?");

Maybe this can be built into the mysql dialect?

Where should I define the schema name to execute query on specific schema for pg?

I would like to execute the query on specific schema in postgreSQL. so where can I define the schema name?
tried bellow code but not working

jsonSql.build({
         type: 'select',
          table: 'user',
          schema: 'schemaName',
          fields: fields,
          condition: condition
})

working by this way table: 'schemaName.tableName'

jsonSql.build({
         type: 'select',
          table: 'schemaName.tableName',
          fields: fields,
          condition: condition
})

Is this valid way or have other way ?

MSSQL Connection

I'm new to NPM so please bear with me.. Can this be used for my MS SQL DB? If yes, how do connect my application using your package? I do not see in the documents on how to connect it.

Thanks.

on conflict update

Hello,

This library is great, but there is one crucial PostgresSQL missing feature ; on conflict update.

How complicated would it be to add it ?

Wrong value type "object"

My object event:
"payload": {
"cpf": "12345678990",
"id": ""
}

My build:
var sql = jsonSql.build({
type: 'insert',
table: 'perfis_'+event.operation,
values: event.payload //object
}).query;

Stacktrace:
Error: Wrong value type "object"
at Builder.pushValue (/var/task/node_modules/json-sql/lib/builder.js:55:9)
at /var/task/node_modules/json-sql/lib/dialects/base/blocks.js:188:26
at Dialect.buildBlock (/var/task/node_modules/json-sql/lib/dialects/base/index.js:323:9)
at /var/task/node_modules/json-sql/lib/dialects/base/blocks.js:357:21
at Function.
.map..collect (/var/task/node_modules/underscore/underscore.js:164:24)
at .(anonymous function) as map
at /var/task/node_modules/json-sql/lib/dialects/base/blocks.js:356:22
at Function.
.map.
.collect (/var/task/node_modules/underscore/underscore.js:164:24)
at _.(anonymous function) as map
at /var/task/node_modules/json-sql/lib/dialects/base/blocks.js:355:27

Incorrect syntax near 'returning'.

I just can't figure out how returning should work.

Builder config:
{ dialect: 'mssql', separatedValues: false }

Query:

const query = {
        table: 'Messages',
        type: 'insert',
        returning: ['ID'],
        values: {
          Name: 'Blah',
          Text: 'Some text'
        }
      }

Yeilds:
insert into "Messages" ("Name", "Text") values ('Blah', 'Some text') returning "ID";

Error:
RequestError: Incorrect syntax near 'returning'.

Obviously this will fail, as is not valid MSSQL syntax.

Am just trying to output the inserted row's primary key. It doesn't seem like valid

Wrong parameter naming in builder.js

Builder.prototype._getPlaceholder = function () {
	var placeholder = '';
	if (this.options.namedValues) placeholder += 'p';
	if (this.options.indexedValues) placeholder += this._placeholderId++;
	return placeholder;
};

The code at the beginning creates the naming of parameters in prepared statements. Unfortunately it creates prameters with the pattern <valuesPrefix>p<numberOfParameter>. PostgreSQL is not accepting this naming pattern but only<valuesPrefix><numberOfParameter> without the p. You might want to fix that.

handling Date value

I have a simple select statement with a condition having a column equaling to a Date instance. Please see the result. Does not seem like it's an expected behavior.

var date = new Date();
var result = jsonSql.build({
  table: 'users',
  condition: {
    insertdate: date
  }
});

// result.query: select * from "users" where "insertdate";
// result.values: []

Of course, running this results in an error.

I tried to poke around in the code, but could not quite see where to handle this. Any suggestions/help are appreciated.

Insert into multiple tables?

Hello, I can't figure out the recommended way using this module to insert into multiple tables at once.

Eg. I have a chat msg from a user, I want to insert into the users table data about the user, and the msg into the msgs table at the same time.

Eg.

START TRANSACTION;
INSERT INTO table1 VALUES ('1','2','3');
INSERT INTO table2 VALUES ('bob','smith');
COMMIT;

Thanks!

Roo

How to use the modulo operator with json-sql?

Is it possible to use the MODULO operator (% or MOD()) with json-sql?

I need something like this (find all even block heights):

select *
from blocks
where height % 2 = 0

I searched the code and the tests I couldn't find it.

Thanks in advance!
Kind regards
a1300

On executing the JSON to SQL getting the double quotes against the identifiers for My SQL

I have provided the options for MySQL database

var jsonSql = new (require('json-sql').Builder)(
{separatedValues: false},
{dailect: 'mysql'},
{wrappedIdentifiers: false
});

The complete code is:

//Declaring the paramters which are passed as arguments
var BuId;

//Getting the arguments from the command prompt.

//Removing the first two arguments from the list. First argument is the command "node" //and the second argument is the path of the script file.
var args = process.argv.slice(2);

//Assigning the arguments which are passed from command prompt to the variables
args.forEach(function (val, index, array) {
//console.log(index + ': ' + val);
if (index == 0)
{
BuId = val;
}
});

//To create new instance of json-sql builder. The script generated for which database can be specified here.
var jsonSql = new (require('json-sql').Builder)({separatedValues: false},{dailect: 'mysql'},{wrappedIdentifiers: false});

//Building the query
var sql = jsonSql.build({
type: 'select', //Defining the Query type - Whether Select, Insert etc...
table: 'businessunit', //Specifying the Table
alias: 'bu', //Specifying the alias name for the Table
//Specifying the fields for the tables and the alias name for each field.
fields: [{name: 'Name', table: 'bu', alias: 'BuName'},
{name: 'Company', table: 'bu', alias: 'Company'},
{name: 'UserName', table: 'usr', alias: 'UserName'},
{name: 'Email', table: 'usr', alias: 'Email'},
{name: 'PhoneNo', table: 'usr', alias: 'PhoneNo'},

        ],

//joining multiple tables by specifying the type of join and the alias name of the joining table
join: {
'userinfo': {type: 'inner', on: {'bu.BusinessUnitId': 'usr.BusinessUnitId'}, alias: 'usr'},
},
//Specifying the where conditions
condition: {

        //retrieving values based on the condition prc.PricingDate >= fromdate and prc.PricingDate <= todate
        'usr.BusinessUnitId': {$eq: BuId},  
        
  	}

});

sql.query

console.log(sql.query);
/console.log(sql.values);/

But on executing the JSON to SQL getting the double quotes against the identifiers in the result SQL as given below...
select "bu"."Name" as "BuName", "bu"."Company" as "Company", "usr"."UserName" as "UserName", "usr"."Email" as "Email", "usr"."PhoneNo" as "PhoneNo" from "businessunit" as "bu" inner join "userinfo" as "usr" on "bu"."BusinessUnitId" = "usr"."BusinessUnitId" where "usr"."BusinessUnitId" = '1';

Since there are double quotes for the alias and Field name, the query is not getting executed in MySQL....

Please help....

Use values in fields

First of all, thanks for making this!

I'm trying to do something like select 'Done' as status or select null as account is there a way to do this without the values getting double quoted?

mysql: Invalid query generated when there is a . in the table name

I am having issues generating valid mysql query when there is a . used in the table name.

        let jsonSql = require('json-sql')({
            dialect: 'mysql'
        });
        let sql = jsonSql.build({
            type: 'select',
            table: this.table,
            condition: {
                KundenNR: kundenNr
            }
        });
        console.log(sql.query);

Output:
select * from `dbo`.`clients` where `KundenNR` = $p1;
Expected Output:
select * from `dbo.clients` where `KundenNR` = $p1;

Subquery with join not working (Postgres)

I am using Postgres. Here is my table structure.

CREATE TABLE "public"."projects" (
	"id" int4 NOT NULL DEFAULT nextval('projects_id_seq'::regclass),
	"name" text NOT NULL COLLATE "default",
	"owner_id" int4
)
CREATE TABLE "public"."users" (
	"id" int4 NOT NULL DEFAULT nextval('users_id_seq'::regclass),
	"email" text NOT NULL COLLATE "default",
	"first_name" text COLLATE "default",
	"last_name" text COLLATE "default"
)
ALTER TABLE "public"."projects" ADD PRIMARY KEY ("id") NOT DEFERRABLE INITIALLY IMMEDIATE;
ALTER TABLE "public"."projects" ADD CONSTRAINT "projects_name_key" UNIQUE ("name") NOT DEFERRABLE INITIALLY IMMEDIATE;
ALTER TABLE "public"."users" ADD PRIMARY KEY ("id") NOT DEFERRABLE INITIALLY IMMEDIATE;
ALTER TABLE "public"."users" ADD CONSTRAINT "users_email_key" UNIQUE ("email") NOT DEFERRABLE INITIALLY IMMEDIATE;
ALTER TABLE "public"."projects" ADD CONSTRAINT "projects_owner_id_fkey" FOREIGN KEY ("owner_id") REFERENCES "public"."users" ("id") ON UPDATE NO ACTION ON DELETE SET NULL NOT DEFERRABLE INITIALLY IMMEDIATE;

Here is how I am calling jsonSql.build:

jsonSql.build({
    type: 'select',
    table: 'users',
    distinct: undefined,
    group: '',
    sort: {},
    limit: 25,
    offset: 0,
    fields: ['users.id',
        'users.email',
        'users.first_name',
        'users.last_name'
    ],
    condition: {},
    join: {
        projects: {
            type: 'inner',
            on: {
                'users.id': 'projects.owner_id'
            },
            select: {
                table: 'projects',
                fields: ['projects.id', 'projects.name'],
                join: {},
                sort: {},
                group: '',
                distinct: undefined,
                limit: undefined,
                offset: undefined,
                condition: {}
            },
            alias: 'projects'
        }
    }
});

That produces following query:
select "users"."id", "users"."email", "users"."first_name", "users"."last_name" from "users" inner join (select "projects"."id", "projects"."name" from "projects") as "projects" on "users"."id" = "projects"."owner_id" limit 25 offset 0;

When executed, it throws following error:
error: column projects.owner_id does not exist

escape symbols

Great library!
I am having a question:
How to use escape symbols when single quotes appear in the value?

Phpmyadmin uses backticks (`) instead of "

Is it possible to use json-sql for phpmyadmin, since that service uses backticks and my sql contsructed querys consist of double quotes. Besides: How can I retreive the query with the values instead of $p1 $p2 and so on?

Case conditions

Hey, I couldn't figure a way to do this - is there a way to have case conditions (case when x then ... etc) in a select query?

Thanks

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.