Socket
Socket
Sign inDemoInstall

knex

Package Overview
Dependencies
Maintainers
1
Versions
252
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

knex - npm Package Compare versions

Comparing version 0.4.9 to 0.4.10

19

clients/base/grammar.js

@@ -55,3 +55,3 @@ // Grammar

compileSelect: function(qb) {
var sql = {};
var sql = [];
if (_.isEmpty(qb.columns)) qb.columns = ['*'];

@@ -62,5 +62,10 @@ for (var i = 0, l = components.length; i < l; i++) {

if (result != null) {
sql[component] = this['compile' + Helpers.capitalize(component)](qb, result);
sql.push(this['compile' + Helpers.capitalize(component)](qb, result));
}
}
// If there is a transaction, and we have either `forUpdate` or `forShare` specified,
// call the appropriate additions to the select statement.
if (qb.transaction && qb.flags.selectMode) {
sql.push(this['compile' + qb.flags.selectMode](qb));
}
return _.compact(sql).join(' ');

@@ -287,2 +292,12 @@ },

// Adds a `for update` clause to the query, relevant with transactions.
compileForUpdate: function() {
return 'for update';
},
// Adds a `for share` clause to the query, relevant with transactions.
compileForShare: function() {
return 'for share';
},
// Puts the appropriate wrapper around a value depending on the database

@@ -289,0 +304,0 @@ // engine, unless it's a knex.raw value, in which case it's left alone.

@@ -81,4 +81,8 @@ // SQLite3 Grammar

return sql;
}
},
// For share and for update are not available in sqlite3.
compileForUpdate: function() {},
compileForShare: function() {}
}, baseGrammar);

@@ -85,0 +89,0 @@

31

clients/pool.js

@@ -27,2 +27,5 @@ // Pool

this.client = client;
if (!config || !client) {
throw new Error('The config and client are required to use the pool module.');
}
this.init();

@@ -33,7 +36,5 @@ };

// Some basic defaults for the pool... generally you don't really want to keep
// mutable objects on the prototype, but in this case we're not supposed to be
// messing around with them, so it should be alright.
// Some basic defaults for the pool...
defaults: function() {
var poolInstance = this;
var pool = this;
return {

@@ -43,7 +44,7 @@ min: 2,

create: function(callback) {
var promise = poolInstance.client.getRawConnection()
var promise = pool.client.getRawConnection()
.tap(function(connection) {
connection.__cid = _.uniqueId('__cid');
if (poolInstance.config.afterCreate) {
return nodefn.call(poolInstance.config.afterCreate, connection);
if (pool.config.afterCreate) {
return nodefn.call(pool.config.afterCreate, connection);
}

@@ -54,4 +55,4 @@ });

destroy: function(connection) {
if (poolInstance.config.beforeDestroy) {
return poolInstance.config.beforeDestroy(connection, function() {
if (pool.config.beforeDestroy) {
return pool.config.beforeDestroy(connection, function() {
connection.end();

@@ -87,6 +88,10 @@ });

var poolInstance = this.poolInstance;
poolInstance.drain(function() {
poolInstance.destroyAllNow(callback);
});
delete this.poolInstance;
if (poolInstance) {
poolInstance.drain(function() {
poolInstance.destroyAllNow(callback);
});
delete this.poolInstance;
} else {
callback();
}
return this;

@@ -93,0 +98,0 @@ }

@@ -22,4 +22,9 @@ // MySQL Grammar

return response;
},
// Adds a `for share` clause to the query, relevant with transactions.
compileForShare: function() {
return 'lock in share mode';
}
}, baseGrammar);

@@ -36,4 +36,4 @@ // MySQL SchemaGrammar

if (conn.charset) sql += ' default character set ' + conn.charset;
if (conn.collation) sql += ' collate ' + conn.collation;
if (builder.flags.charset || conn.charset) sql += ' default character set ' + (builder.flags.charset || conn.charset);
if (builder.flags.collation || conn.collation) sql += ' collate ' + (builder.flags.collation || conn.collation);
if (builder.flags.engine) {

@@ -40,0 +40,0 @@ sql += ' engine = ' + builder.flags.engine;

@@ -1,2 +0,2 @@

// Knex.js 0.4.9
// Knex.js 0.4.10
// --------------

@@ -94,3 +94,3 @@

// Keep in sync with package.json
knex.VERSION = '0.4.9';
knex.VERSION = '0.4.10';

@@ -97,0 +97,0 @@ // Runs a new transaction, taking a container and returning a promise

@@ -48,2 +48,9 @@ // Builder

// Alias to from, for "insert" statements
// e.g. builder.insert({a: value}).into('tableName')
into: function(tableName) {
this.table = tableName;
return this;
},
// Adds a column or columns to the list of "columns"

@@ -72,3 +79,3 @@ // being selected on the query.

'joins', 'wheres', 'orders', 'columns', 'bindings',
'grammar', 'transaction', 'unions', 'flags'
'grammar', 'transaction', 'unions', 'flags', 'type'
];

@@ -75,0 +82,0 @@ for (var i = 0, l = items.length; i < l; i++) {

@@ -124,4 +124,14 @@ // Common

if (this.transaction) throw new Error('A transaction has already been set for the current query chain');
var flags = this.flags;
this.transaction = t;
this.usingConnection = t.connection;
// Add "forUpdate" and "forShare" here, since these are only relevant
// within the context of a transaction.
this.forUpdate = function() {
flags.selectMode = 'ForUpdate';
};
this.forShare = function() {
flags.selectMode = 'ForShare';
};
}

@@ -128,0 +138,0 @@ return this;

@@ -59,2 +59,16 @@ // Schema Builder

// Sets the character set for the table in MySql
charset: function(charset) {
if (!this.creating()) throw new Error('The `engine` modifier may only be used while creating a table.');
this.flags.charset = charset;
return this;
},
// Sets the collation for the table in MySql
collate: function(collation) {
if (!this.creating()) throw new Error('The `engine` modifier may only be used while creating a table.');
this.flags.collation = collation;
return this;
},
// Adds a comment to the current table being created.

@@ -61,0 +75,0 @@ comment: function(comment) {

{
"name": "knex",
"version": "0.4.9",
"version": "0.4.10",
"description": "A query builder for Postgres, MySQL and SQLite3, designed to be flexible, portable, and fun to use.",

@@ -5,0 +5,0 @@ "main": "knex.js",

@@ -30,2 +30,3 @@ // Helps with error handling on errors swallowed by promises.

require('./unit/transaction');
require('./unit/clients/pool');
require('./unit/clients/base');

@@ -32,0 +33,0 @@ require('./unit/clients/base/grammar');

@@ -15,3 +15,5 @@ var when = require('when');

knex.schema.dropTableIfExists('accounts'),
knex.schema.dropTableIfExists('test_default_table')
knex.schema.dropTableIfExists('test_default_table'),
knex.schema.dropTableIfExists('composite_key_test'),
knex.schema.dropTableIfExists('charset_collate_test')
]);

@@ -71,2 +73,22 @@ });

it('allows for composite keys', function() {
return knex.schema.createTable('composite_key_test', function(table) {
table.integer('column_a');
table.integer('column_b');
table.unique(['column_a', 'column_b']);
}).logMe('sql');
});
it('is possible to set the table collation with table.charset and table.collate', function() {
return knex.schema.createTable('charset_collate_test', function(table) {
table.charset('latin1');
table.collate('latin1_general_ci');
table.engine('InnoDB');
table.increments();
table.integer('account_id');
table.text('details');
table.tinyint('status');
}).logMe('sql');
});
});

@@ -73,0 +95,0 @@

@@ -85,3 +85,31 @@ module.exports = {

}
},
'allows for composite keys': {
mysql: {
bindings: [],
sql: ['create table `composite_key_test` (`column_a` int(11), `column_b` int(11)) default character set utf8','alter table `composite_key_test` add unique composite_key_test_column_a_column_b_unique(`column_a`, `column_b`)']
},
postgresql: {
bindings: [],
sql: ['create table "composite_key_test" ("column_a" integer, "column_b" integer)','alter table "composite_key_test" add constraint composite_key_test_column_a_column_b_unique unique ("column_a", "column_b")']
},
sqlite3: {
bindings: [],
sql: ['create table "composite_key_test" ("column_a" integer, "column_b" integer)','create unique index composite_key_test_column_a_column_b_unique on "composite_key_test" ("column_a", "column_b")']
}
},
'is possible to set the table collation with table.charset and table.collate': {
mysql: {
bindings: [],
sql: ['create table `charset_collate_test` (`id` int(11) unsigned not null auto_increment primary key, `account_id` int(11), `details` text, `status` tinyint) default character set latin1 collate latin1_general_ci engine = InnoDB']
},
postgresql: {
bindings: [],
sql: ['create table "charset_collate_test" ("id" serial primary key not null, "account_id" integer, "details" text, "status" smallint)']
},
sqlite3: {
bindings: [],
sql: ['create table "charset_collate_test" ("id" integer primary key autoincrement not null, "account_id" integer, "details" text, "status" tinyint)']
}
}
};

@@ -119,2 +119,7 @@ var _ = require('underscore');

it('should keep the correct type when cloning the instance', function() {
var cloned = builder.insert({a: 'value'}).into('tableName').clone();
expect(cloned.type).to.equal('insert');
});
});

@@ -219,2 +224,50 @@

describe('transacting', function() {
it('accepts an object once - otherwise throws an error', function() {
var trx = {};
builder.transacting(trx);
expect(builder.transaction).to.eql(trx);
try {
builder.transacting(trx);
} catch (e) {
expect(e.message).to.equal('A transaction has already been set for the current query chain');
}
});
it('attaches two methods, forUpdate and forShare', function() {
expect(builder.forUpdate).to.not.exist;
expect(builder.forShare).to.not.exist;
builder.transacting({});
expect(builder.forUpdate).to.be.a('function');
expect(builder.forShare).to.be.a('function');
});
it('adds a forUpdate or forShare clause to the query, but not both', function() {
var qb = builder.select('*').from('users').transacting({}).where('id', '>', 10);
qb.forShare();
expect(qb.toString()).to.equal('select * from `users` where `id` > 10 lock in share mode');
qb.forUpdate();
expect(qb.toString()).to.equal('select * from `users` where `id` > 10 for update');
});
});
describe('whereRaw', function() {

@@ -221,0 +274,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc