Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't export instance #366

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,10 @@ Sql.prototype.constant = function(value) {


// back compat shim for the Sql class constructor
var create = function(dialect, config) {
Sql.create = Sql.prototype.create = function(dialect, config) {
return new Sql(dialect, {});
};

module.exports = new Sql(DEFAULT_DIALECT, {});
module.exports.create = create;
module.exports = Sql;
module.exports.Sql = Sql;
module.exports.Table = Table;
4 changes: 3 additions & 1 deletion lib/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ var determineDialect = function(query, dialect) {
Dialect = sql.dialect;
} else {
// dialect is not specified, use the default dialect
Dialect = require('../').dialect;
var Sql = require('../');
var defaultSql = new Sql();
Dialect = defaultSql.dialect;
}
return Dialect;
};
Expand Down
3 changes: 2 additions & 1 deletion lib/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ var Table = function(config) {
this.foreignKeys = [];
this.table = this;
if (!config.sql) {
config.sql = require('./index');
var Sql = require('./index');
config.sql = new Sql();
}
this.sql = config.sql;
};
Expand Down
3 changes: 2 additions & 1 deletion test/column-tests.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

var assert = require('assert');
var sql = require(__dirname + '/../lib');
var Sql = require(__dirname + '/../lib');
var sql = new Sql();

describe('column', function() {
var table = sql.define({
Expand Down
6 changes: 4 additions & 2 deletions test/dialects/alias-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

var Harness = require('./support');
var customer = Harness.defineCustomerTable();
var Sql = require('../../lib').setDialect('postgres');
var Sql = require('../../lib');
var sql = new Sql();
sql.setDialect('postgres');

Harness.test({
query: customer.select(customer.name.isNull().as('nameIsNull')),
Expand Down Expand Up @@ -80,7 +82,7 @@ Harness.test({
});

Harness.test({
query: customer.select(Sql.functions.ROUND(customer.age.as('ageBetween'), 2)),
query: customer.select(sql.functions.ROUND(customer.age.as('ageBetween'), 2)),
pg: {
text : 'SELECT ROUND("customer"."age", $1) FROM "customer"',
string: 'SELECT ROUND("customer"."age", 2) FROM "customer"'
Expand Down
15 changes: 8 additions & 7 deletions test/dialects/array-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
var Harness = require('./support');
var post = Harness.definePostTable();
var Sql = require('../../lib');
var sql = new Sql();

// Array columns
Harness.test({
query: post.update({
tags: post.tags.concat(Sql.array('nodejs'))
tags: post.tags.concat(sql.array('nodejs'))
}),
pg: {
text : 'UPDATE "post" SET "tags" = ("post"."tags" || ARRAY[$1])',
Expand All @@ -18,7 +19,7 @@ Harness.test({

Harness.test({
query: post.select(
post.tags.contains(Sql.array('nodejs', 'js'))
post.tags.contains(sql.array('nodejs', 'js'))
),
pg: {
text : 'SELECT ("post"."tags" @> ARRAY[$1, $2]) FROM "post"',
Expand All @@ -29,7 +30,7 @@ Harness.test({

Harness.test({
query: post.select(
post.tags.containedBy(Sql.array('nodejs', 'js'))
post.tags.containedBy(sql.array('nodejs', 'js'))
),
pg: {
text : 'SELECT ("post"."tags" <@ ARRAY[$1, $2]) FROM "post"',
Expand All @@ -40,7 +41,7 @@ Harness.test({

Harness.test({
query: post.select(
post.tags.overlap(Sql.array('nodejs', 'js'))
post.tags.overlap(sql.array('nodejs', 'js'))
),
pg: {
text : 'SELECT ("post"."tags" && ARRAY[$1, $2]) FROM "post"',
Expand Down Expand Up @@ -69,7 +70,7 @@ Harness.test({

// Array literals
Harness.test({
query: post.select(Sql.array(1,2,3)),
query: post.select(sql.array(1,2,3)),
pg: {
text : 'SELECT ARRAY[$1, $2, $3] FROM "post"',
string: 'SELECT ARRAY[1, 2, 3] FROM "post"'
Expand All @@ -78,7 +79,7 @@ Harness.test({
});

Harness.test({
query: post.select(Sql.array(1,2,3).slice(2,3)),
query: post.select(sql.array(1,2,3).slice(2,3)),
pg: {
text : 'SELECT (ARRAY[$1, $2, $3])[$4:$5] FROM "post"',
string: 'SELECT (ARRAY[1, 2, 3])[2:3] FROM "post"'
Expand All @@ -87,7 +88,7 @@ Harness.test({
});

Harness.test({
query: post.select(Sql.array(1,2,3).at(2)),
query: post.select(sql.array(1,2,3).at(2)),
pg: {
text : 'SELECT (ARRAY[$1, $2, $3])[$4] FROM "post"',
string: 'SELECT (ARRAY[1, 2, 3])[2] FROM "post"'
Expand Down
26 changes: 13 additions & 13 deletions test/dialects/date-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
var Harness = require('./support');
var customer = Harness.defineCustomerTable();
var Sql = require('../../lib');
var sql = new Sql();

Harness.test({
query: customer.select(Sql.functions.YEAR(customer.metadata)),
query: customer.select(sql.functions.YEAR(customer.metadata)),
pg: {
text : 'SELECT EXTRACT(YEAR FROM "customer"."metadata") FROM "customer"',
string: 'SELECT EXTRACT(YEAR FROM "customer"."metadata") FROM "customer"'
Expand All @@ -30,7 +31,7 @@ Harness.test({
});

Harness.test({
query: customer.select(Sql.functions.MONTH(customer.metadata)),
query: customer.select(sql.functions.MONTH(customer.metadata)),
pg: {
text : 'SELECT EXTRACT(MONTH FROM "customer"."metadata") FROM "customer"',
string: 'SELECT EXTRACT(MONTH FROM "customer"."metadata") FROM "customer"'
Expand Down Expand Up @@ -58,7 +59,7 @@ Harness.test({
});

Harness.test({
query: customer.select(Sql.functions.DAY(customer.metadata)),
query: customer.select(sql.functions.DAY(customer.metadata)),
pg: {
text : 'SELECT EXTRACT(DAY FROM "customer"."metadata") FROM "customer"',
string: 'SELECT EXTRACT(DAY FROM "customer"."metadata") FROM "customer"'
Expand All @@ -83,7 +84,7 @@ Harness.test({
});

Harness.test({
query: customer.select(Sql.functions.HOUR(customer.metadata)),
query: customer.select(sql.functions.HOUR(customer.metadata)),
pg: {
text : 'SELECT EXTRACT(HOUR FROM "customer"."metadata") FROM "customer"',
string: 'SELECT EXTRACT(HOUR FROM "customer"."metadata") FROM "customer"'
Expand Down Expand Up @@ -111,7 +112,7 @@ Harness.test({
});

Harness.test({
query: customer.select(Sql.functions.CURRENT_TIMESTAMP()),
query: customer.select(sql.functions.CURRENT_TIMESTAMP()),
pg: {
text : 'SELECT CURRENT_TIMESTAMP FROM "customer"',
string: 'SELECT CURRENT_TIMESTAMP FROM "customer"'
Expand All @@ -136,7 +137,7 @@ Harness.test({
});

Harness.test({
query: Sql.select(Sql.functions.CURRENT_TIMESTAMP().plus(Sql.interval({hours:1}))),
query: sql.select(sql.functions.CURRENT_TIMESTAMP().plus(sql.interval({hours:1}))),
pg: {
text : 'SELECT (CURRENT_TIMESTAMP + INTERVAL \'1 HOUR\')',
string: 'SELECT (CURRENT_TIMESTAMP + INTERVAL \'1 HOUR\')'
Expand All @@ -149,7 +150,7 @@ Harness.test({
});

Harness.test({
query: Sql.select(Sql.functions.CURRENT_TIMESTAMP().minus(Sql.interval({years:3}))),
query: sql.select(sql.functions.CURRENT_TIMESTAMP().minus(sql.interval({years:3}))),
pg: {
text : 'SELECT (CURRENT_TIMESTAMP - INTERVAL \'3 YEAR\')',
string: 'SELECT (CURRENT_TIMESTAMP - INTERVAL \'3 YEAR\')'
Expand All @@ -162,7 +163,7 @@ Harness.test({
});

Harness.test({
query: Sql.select(Sql.functions.CURRENT_TIMESTAMP().minus(Sql.interval({years:3, months:2}))),
query: sql.select(sql.functions.CURRENT_TIMESTAMP().minus(sql.interval({years:3, months:2}))),
pg: {
text : 'SELECT (CURRENT_TIMESTAMP - INTERVAL \'3 YEAR 2 MONTH\')',
string: 'SELECT (CURRENT_TIMESTAMP - INTERVAL \'3 YEAR 2 MONTH\')'
Expand All @@ -175,7 +176,7 @@ Harness.test({
});

Harness.test({
query: Sql.select(Sql.functions.CURRENT_TIMESTAMP().plus(Sql.interval({hours:1, minutes:20}))),
query: sql.select(sql.functions.CURRENT_TIMESTAMP().plus(sql.interval({hours:1, minutes:20}))),
pg: {
text : 'SELECT (CURRENT_TIMESTAMP + INTERVAL \'1 HOUR 20 MINUTE\')',
string: 'SELECT (CURRENT_TIMESTAMP + INTERVAL \'1 HOUR 20 MINUTE\')'
Expand All @@ -188,7 +189,7 @@ Harness.test({
});

Harness.test({
query: Sql.select(Sql.functions.CURRENT_TIMESTAMP().plus(Sql.interval({hours:'sql\'injection', minutes:20}))),
query: sql.select(sql.functions.CURRENT_TIMESTAMP().plus(sql.interval({hours:'sql\'injection', minutes:20}))),
pg: {
text : 'SELECT (CURRENT_TIMESTAMP + INTERVAL \'20 MINUTE\')',
string: 'SELECT (CURRENT_TIMESTAMP + INTERVAL \'20 MINUTE\')'
Expand All @@ -201,7 +202,7 @@ Harness.test({
});

Harness.test({
query: Sql.select(Sql.functions.CURRENT_TIMESTAMP().minus(Sql.interval({days: 1, hours:5, minutes: 'sql\'injection'}))),
query: sql.select(sql.functions.CURRENT_TIMESTAMP().minus(sql.interval({days: 1, hours:5, minutes: 'sql\'injection'}))),
pg: {
text : 'SELECT (CURRENT_TIMESTAMP - INTERVAL \'1 DAY 5 HOUR\')',
string: 'SELECT (CURRENT_TIMESTAMP - INTERVAL \'1 DAY 5 HOUR\')'
Expand All @@ -214,7 +215,7 @@ Harness.test({
});

Harness.test({
query: Sql.select(Sql.functions.CURRENT_TIMESTAMP().minus(Sql.interval({years: 2, months: 5}))),
query: sql.select(sql.functions.CURRENT_TIMESTAMP().minus(sql.interval({years: 2, months: 5}))),
pg: {
text : 'SELECT (CURRENT_TIMESTAMP - INTERVAL \'2 YEAR 5 MONTH\')',
string: 'SELECT (CURRENT_TIMESTAMP - INTERVAL \'2 YEAR 5 MONTH\')'
Expand All @@ -225,4 +226,3 @@ Harness.test({
},
params: []
});

2 changes: 0 additions & 2 deletions test/dialects/distinct-on-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

var Harness = require('./support');
var user = Harness.defineUserTable();
var Sql = require('../../lib').setDialect('postgres');

Harness.test({
query: user.select().distinctOn(user.id),
Expand All @@ -21,4 +20,3 @@ Harness.test({
},
params: []
});

9 changes: 5 additions & 4 deletions test/dialects/function-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

var Harness = require('./support');
var Sql = require('../../lib');
var sql = new Sql();


var post = Harness.definePostTable();

Harness.test({
query: post.select(Sql.functions.LENGTH(post.content)),
query: post.select(sql.functions.LENGTH(post.content)),
pg: {
text : 'SELECT LENGTH("post"."content") FROM "post"',
string: 'SELECT LENGTH("post"."content") FROM "post"'
Expand All @@ -31,7 +33,7 @@ Harness.test({
});

Harness.test({
query: post.select(Sql.functions.LEFT(post.content,4)),
query: post.select(sql.functions.LEFT(post.content,4)),
pg: {
text : 'SELECT LEFT("post"."content", $1) FROM "post"',
string: 'SELECT LEFT("post"."content", 4) FROM "post"'
Expand All @@ -56,7 +58,7 @@ Harness.test({
});

Harness.test({
query: post.select(Sql.functions.RIGHT(post.content,4)),
query: post.select(sql.functions.RIGHT(post.content,4)),
pg: {
text : 'SELECT RIGHT("post"."content", $1) FROM "post"',
string: 'SELECT RIGHT("post"."content", 4) FROM "post"'
Expand All @@ -79,4 +81,3 @@ Harness.test({
},
params: [4]
});

6 changes: 4 additions & 2 deletions test/dialects/hstore-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

var Harness = require('./support');
var customer = Harness.defineCustomerTable();
var Sql = require('../../lib').setDialect('postgres');
var Sql = require('../../lib');
var sql = new Sql();
sql.setDialect('postgres');

Harness.test({
query: customer.update({
metadata: customer.metadata.concat(Sql.functions.HSTORE('age', 20))
metadata: customer.metadata.concat(sql.functions.HSTORE('age', 20))
}),
pg: {
text : 'UPDATE "customer" SET "metadata" = ("customer"."metadata" || HSTORE($1, $2))',
Expand Down
1 change: 0 additions & 1 deletion test/dialects/ilike-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
var Harness = require('./support');
var post = Harness.definePostTable();
var user = Harness.defineUserTable();
var Sql = require('../../lib').setDialect('postgres');

Harness.test({
query: post.select(post.content, post.userId).where(post.content.ilike('A%')),
Expand Down
3 changes: 2 additions & 1 deletion test/dialects/join-to-tests.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
var sql = require(__dirname + '/../../lib');
var Sql = require(__dirname + '/../../lib');
var sql = new Sql();

var Harness = require('./support');

Expand Down
1 change: 0 additions & 1 deletion test/dialects/json-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

var Harness = require('./support');
var customer = Harness.defineCustomerTable();
var Sql = require('../../lib').setDialect('postgres');

Harness.test({
query: customer.select(customer.metadata.key('age')),
Expand Down
4 changes: 3 additions & 1 deletion test/dialects/matches-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
var Harness = require('./support');
var post = Harness.definePostTable();
var customerAlias = Harness.defineCustomerAliasTable();
var sql = require(__dirname + '/../../lib').setDialect('postgres');
var Sql = require(__dirname + '/../../lib');
var sql = new Sql();
sql.setDialect('postgres');

//Postgres needs the to_tsquery function to use with @@ operator
Harness.test({
Expand Down
5 changes: 3 additions & 2 deletions test/dialects/order-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

var Harness = require('./support');
var post = Harness.definePostTable();
var sql = require('../../lib');
var Sql = require('../../lib');
var sql = new Sql();

Harness.test({
query: post.select(post.content).order(post.content),
Expand Down Expand Up @@ -336,4 +337,4 @@ Harness.test({
string: 'SELECT "post"."content" FROM "post" ORDER BY "post"."content"'
},
params: []
});
});
1 change: 0 additions & 1 deletion test/dialects/regex-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

var Harness = require('./support');
var customer = Harness.defineCustomerTable();
var Sql = require('../../lib').setDialect('postgres');

Harness.test({
query: customer.select(customer.metadata.regex('age')),
Expand Down
Loading