Socket
Socket
Sign inDemoInstall

knex

Package Overview
Dependencies
3
Maintainers
1
Versions
252
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.4.1 to 0.4.2

clients/base/sqlite3/grammar.js

2

clients/base/grammar.js

@@ -26,3 +26,3 @@ // Grammar

exports.BaseGrammar = {
exports.baseGrammar = {

@@ -29,0 +29,0 @@ // Compiles the current query builder.

@@ -17,3 +17,3 @@ // SchemaGrammar

var BaseGrammar = require('./grammar').BaseGrammar;
var baseGrammar = require('./grammar').baseGrammar;
var SchemaBuilder = require('../../lib/schemabuilder').SchemaBuilder;

@@ -24,3 +24,3 @@

exports.BaseSchemaGrammar = {
exports.baseSchemaGrammar = {

@@ -144,3 +144,3 @@ // The toSql on the "schema" is different than that on the "builder",

if (table instanceof SchemaBuilder) table = table.table;
return BaseGrammar.wrapTable.call(this, table);
return baseGrammar.wrapTable.call(this, table);
},

@@ -151,3 +151,3 @@

if (value && value.name) value = value.name;
return BaseGrammar.wrap.call(this, value);
return baseGrammar.wrap.call(this, value);
},

@@ -154,0 +154,0 @@

@@ -81,6 +81,6 @@ // Pool

// Tear down the pool, only necessary if you need it.
destroy: function() {
destroy: function(callback) {
var poolInstance = this.poolInstance;
poolInstance.drain(function() {
poolInstance.destroyAllNow();
poolInstance.destroyAllNow(callback);
});

@@ -87,0 +87,0 @@ delete this.poolInstance;

@@ -20,2 +20,3 @@ // ServerBase

if (config.debug) this.isDebugging = true;
this.attachGrammars();
this.connectionSettings = config.connection;

@@ -22,0 +23,0 @@ this.initPool(config.pool);

@@ -16,6 +16,7 @@ // MySQL

var ServerBase = require('./base').ServerBase;
var baseGrammar = require('../base/grammar').BaseGrammar;
var baseSchemaGrammar = require('../base/schemagrammar').BaseSchemaGrammar;
var Helpers = require('../../lib/helpers').Helpers;
var grammar = require('./mysql/grammar').grammar;
var schemaGrammar = require('./mysql/schemagrammar').schemaGrammar;
// Constructor for the MySQLClient.

@@ -26,2 +27,8 @@ exports.Client = ServerBase.extend({

// Attach the appropriate grammar definitions onto the current client.
attachGrammars: function() {
this.grammar = grammar;
this.schemaGrammar = schemaGrammar;
},
// Runs the query on the specified connection, providing the bindings

@@ -66,235 +73,2 @@ // and any other necessary prep work.

});
// Extends the standard sql grammar.
var grammar = exports.grammar = _.defaults({
// The keyword identifier wrapper format.
wrapValue: function(value) {
return (value !== '*' ? Helpers.format('`%s`', value) : "*");
},
// Parses the response, according to the way mySQL works...
handleResponse: function(builder, response) {
response = response[0];
if (builder.type === 'select') response = Helpers.skim(response);
if (builder.type === 'insert') response = [response.insertId];
if (builder.type === 'delete' || builder.type === 'update') response = response.affectedRows;
return response;
}
}, baseGrammar);
// Grammar for the schema builder.
var schemaGrammar = exports.schemaGrammar = _.defaults({
// The possible column modifiers.
modifiers: ['Unsigned', 'Nullable', 'Default', 'Increment', 'After', 'Comment'],
// Handle response for the schema.
handleResponse: function(builder, resp) {
if (builder.type === 'tableExists') return resp.length > 0;
if (builder.type === 'columnExists') return resp.length > 0;
return resp;
},
// Compile a create table command.
compileCreateTable: function(blueprint, command) {
var sql = baseSchemaGrammar.compileCreateTable.call(this, blueprint, command);
var conn = blueprint.client.connectionSettings;
if (conn.charset) sql += ' default character set ' + conn.charset;
if (conn.collation) sql += ' collate ' + conn.collation;
if (blueprint.flags.engine) {
sql += ' engine = ' + blueprint.flags.engine;
}
// Checks if the table is commented
var isCommented = this.getCommandByName(blueprint, 'comment');
// TODO: Handle max comment length.
var maxTableCommentLength = 60;
if (isCommented) {
sql += " comment = '" + isCommented.comment + "'";
}
return sql;
},
// Compile the query to determine if a table exists.
compileTableExists: function(blueprint) {
blueprint.bindings.unshift(blueprint.client.connectionSettings.database);
return 'select * from information_schema.tables where table_schema = ? and table_name = ?';
},
// Compile a query to determine if a column exists.
compileColumnExists: function(blueprint) {
return 'show columns from ' + this.wrapTable(blueprint) + ' like ?';
},
// Compile an add command.
compileAdd: function(blueprint) {
var columns = this.prefixArray('add', this.getColumns(blueprint));
return 'alter table ' + this.wrapTable(blueprint) + ' ' + columns.join(', ');
},
// Compile a primary key command.
compilePrimary: function(blueprint, command) {
return this.compileKey(blueprint, command, 'primary key');
},
// Compile a unique key command.
compileUnique: function(blueprint, command) {
return this.compileKey(blueprint, command, 'unique');
},
// Compile a plain index key command.
compileIndex: function(blueprint, command) {
return this.compileKey(blueprint, command, 'index');
},
// Compile an index creation command.
compileKey: function(blueprint, command, type) {
var columns = this.columnize(command.columns);
var table = this.wrapTable(blueprint);
return 'alter table ' + table + " add " + type + " " + command.index + "(" + columns + ")";
},
// Compile a drop column command.
compileDropColumn: function(blueprint, command) {
var columns = this.prefixArray('drop', this.wrapArray(command.columns));
return 'alter table ' + this.wrapTable(blueprint) + ' ' + columns.join(', ');
},
// Compile a drop primary key command.
compileDropPrimary: function(blueprint) {
return 'alter table ' + this.wrapTable(blueprint) + ' drop primary key';
},
// Compile a drop unique key command.
compileDropUnique: function(blueprint, command) {
return this.compileDropIndex(blueprint, command);
},
// Compile a drop index command.
compileDropIndex: function(blueprint, command) {
return 'alter table ' + this.wrapTable(blueprint) + ' drop index ' + command.index;
},
// Compile a drop foreign key command.
compileDropForeign: function(blueprint, command) {
return 'alter table ' + this.wrapTable(blueprint) + " drop foreign key " + command.index;
},
// Compile a rename table command.
compileRenameTable: function(blueprint, command) {
return 'rename table ' + this.wrapTable(blueprint) + ' to ' + this.wrapTable(command.to);
},
// Compile a rename column command.
compileRenameColumn: function(blueprint, command) {
return 'alter table ' + this.wrapTable(blueprint) + ' change ' +
this.wrapTable(command.from) + ' ' + this.wrapTable(command.to) + ' __datatype__';
},
// Compiles the comment on the table.
compileComment: function(blueprint, command) {
// Handled on create table...
},
// Create the column definition for a text type.
typeText: function(column) {
switch (column.length) {
case 'medium':
case 'mediumtext':
return 'mediumtext';
case 'long':
case 'longtext':
return 'longtext';
default:
return 'text';
}
},
// Create the column type definition for a bigint type.
typeBigInteger: function() {
return 'bigint';
},
// Create the column definition for a integer type.
typeInteger: function(column) {
return 'int(' + column.length + ')';
},
// Create the column definition for a float type.
typeFloat: function(column) {
return 'float(' + column.total + ',' + column.places + ')';
},
// Create the column definition for a decimal type.
typeDecimal: function(column) {
return 'decimal(' + column.precision + ', ' + column.scale + ')';
},
// Create the column definition for a boolean type.
typeBoolean: function() {
return 'tinyint(1)';
},
// Create the column definition for a enum type.
typeEnum: function(column) {
return "enum('" + column.allowed.join("', '") + "')";
},
// Create the column definition for a date-time type.
typeDateTime: function() {
return 'datetime';
},
// Create the column definition for a timestamp type.
typeTimestamp: function() {
return 'timestamp';
},
// Create the column definition for a bit type.
typeBit: function(column) {
return column.length !== false ? 'bit(' + column.length + ')' : 'bit';
},
// Get the SQL for an unsigned column modifier.
modifyUnsigned: function(blueprint, column) {
if (column.isUnsigned) return ' unsigned';
},
// Get the SQL for a default column modifier.
modifyDefault: function(blueprint, column) {
// TODO - no default on blob/text
if (column.defaultValue != void 0 && column.type != 'blob' && column.type.indexOf('text') === -1) {
return " default '" + this.getDefaultValue(column.defaultValue) + "'";
}
},
// Get the SQL for an auto-increment column modifier.
modifyIncrement: function(blueprint, column) {
if (column.autoIncrement && (column.type == 'integer' || column.type == 'bigInteger')) {
return ' not null auto_increment primary key';
}
},
// Get the SQL for an "after" column modifier.
modifyAfter: function(blueprint, column) {
if (column.isAfter) {
return ' after ' + this.wrap(column.isAfter);
}
},
// Get the SQL for a comment column modifier.
modifyComment: function(blueprint, column) {
// TODO: Look into limiting this length.
var maxColumnCommentLength = 255;
if (column.isCommented && _.isString(column.isCommented)) {
return " comment '" + column.isCommented + "'";
}
}
}, baseSchemaGrammar, grammar);
});

@@ -16,6 +16,7 @@ // PostgreSQL

var ServerBase = require('./base').ServerBase;
var baseGrammar = require('../base/grammar').BaseGrammar;
var baseSchemaGrammar = require('../base/schemagrammar').BaseSchemaGrammar;
var Helpers = require('../../lib/helpers').Helpers;
var grammar = require('./postgres/grammar').grammar;
var schemaGrammar = require('./postgres/schemagrammar').schemaGrammar;
// Constructor for the PostgreSQL Client

@@ -26,2 +27,8 @@ exports.Client = ServerBase.extend({

// Attach the appropriate grammar definitions onto the current client.
attachGrammars: function() {
this.grammar = grammar;
this.schemaGrammar = schemaGrammar;
},
// Runs the query on the specified connection, providing the bindings

@@ -66,234 +73,1 @@ // and any other necessary prep work.

});
// Extends the standard sql grammar.
var grammar = exports.grammar = _.defaults({
// The keyword identifier wrapper format.
wrapValue: function(value) {
return (value !== '*' ? Helpers.format('"%s"', value) : "*");
},
// Compiles a truncate query.
compileTruncate: function(qb) {
return 'truncate ' + this.wrapTable(qb.table) + ' restart identity';
},
// Compiles an `insert` query, allowing for multiple
// inserts using a single query statement.
compileInsert: function(qb) {
var values = qb.values;
var columns = _.pluck(values[0], 0);
var paramBlocks = [];
// If there are any "where" clauses, we need to omit
// any bindings that may have been associated with them.
if (qb.wheres.length > 0) this.clearWhereBindings(qb);
var sql = 'insert into ' + this.wrapTable(qb.table) + ' ';
if (columns.length === 0) {
sql += 'default values';
} else {
for (var i = 0, l = values.length; i < l; ++i) {
paramBlocks.push("(" + this.parameterize(_.pluck(values[i], 1)) + ")");
}
sql += "(" + this.columnize(columns) + ") values " + paramBlocks.join(', ');
}
if (qb.flags.returning) {
sql += ' returning "' + qb.flags.returning + '"';
}
return sql;
},
// Handles the response
handleResponse: function(builder, response) {
if (response.command === 'SELECT') return response.rows;
if (response.command === 'INSERT') {
return _.map(response.rows, function(row) {
return row[builder.flags.returning];
});
}
if (response.command === 'UPDATE' || response.command === 'DELETE') {
return response.rowCount;
}
return '';
}
}, baseGrammar);
// Grammar for the schema builder.
exports.schemaGrammar = _.defaults({
// The possible column modifiers.
modifiers: ['Increment', 'Nullable', 'Default'],
handleResponse: function(builder, resp) {
resp = resp[0];
if (builder.type === 'tableExists' || builder.type === 'columnExists') {
return resp.rows.length > 0;
}
return resp;
},
// Compile the query to determine if a table exists.
compileTableExists: function() {
return 'select * from information_schema.tables where table_name = ?';
},
// Compile the query to determine if a column exists in a table.
compileColumnExists: function() {
return 'select * from information_schema.columns where table_name = ? and column_name = ?';
},
// Compile a create table command.
compileAdd: function(blueprint) {
var table = this.wrapTable(blueprint);
var columns = this.prefixArray('add column', this.getColumns(blueprint));
return 'alter table ' + table + ' ' + columns.join(', ');
},
// Compile a primary key command.
compilePrimary: function(blueprint, command) {
var columns = this.columnize(command.columns);
return 'alter table ' + this.wrapTable(blueprint) + " add primary key (" + columns + ")";
},
// Compile a unique key command.
compileUnique: function(blueprint, command) {
var table = this.wrapTable(blueprint);
var columns = this.columnize(command.columns);
return 'alter table ' + table + ' add constraint ' + command.index + ' unique (' + columns + ')';
},
// Compile a plain index key command.
compileIndex: function(blueprint, command) {
var columns = this.columnize(command.columns);
return "create index " + command.index + " on " + this.wrapTable(blueprint) + ' (' + columns + ')';
},
// Compile a drop column command.
compileDropColumn: function(blueprint, command) {
var columns = this.prefixArray('drop column', this.wrapArray(command.columns));
var table = this.wrapTable(blueprint);
return 'alter table ' + table + ' ' + columns.join(', ');
},
// Compile a drop primary key command.
compileDropPrimary: function(blueprint) {
var table = blueprint.getTable();
return 'alter table ' + this.wrapTable(blueprint) + " drop constraint " + table + "_pkey";
},
// Compile a drop unique key command.
compileDropUnique: function(blueprint, command) {
var table = this.wrapTable(blueprint);
return "alter table " + table + " drop constraint " + command.index;
},
// Compile a drop foreign key command.
compileDropForeign: function(blueprint, command) {
var table = this.wrapTable(blueprint);
return "alter table " + table + " drop constraint " + command.index;
},
// Compile a rename table command.
compileRenameTable: function(blueprint, command) {
return 'alter table ' + this.wrapTable(blueprint) + ' rename to ' + this.wrapTable(command.to);
},
// Compile a rename column command.
compileRenameColumn: function(blueprint, command) {
return 'alter table ' + this.wrapTable(blueprint) + ' rename '+ this.wrapTable(command.from) + ' to ' + this.wrapTable(command.to);
},
// Compile a comment command.
compileComment: function(blueprint, command) {
var sql = '';
if (command.comment) {
sql += 'comment on table ' + this.wrapTable(blueprint) + ' is ' + "'" + command.comment + "'";
}
return sql;
},
// Compile any additional postgres specific items.
compileAdditional: function(blueprint, command) {
return _.compact(_.map(blueprint.columns, function(column) {
if (column.isCommented && _.isString(column.isCommented)) {
return 'comment on column ' + this.wrapTable(blueprint) + '.' + this.wrap(column.name) + " is '" + column.isCommented + "'";
}
}, this));
},
// Create the column definition for a integer type.
typeInteger: function(column) {
return column.autoIncrement ? 'serial' : 'integer';
},
// Create the column definition for a tiny integer type.
typeTinyInteger: function() {
return 'smallint';
},
// Create the column definition for a float type.
typeFloat: function() {
return 'real';
},
// Create the column definition for a decimal type.
typeDecimal: function(column) {
return "decimal(" + column.total + ", " + column.places + ")";
},
// Create the column definition for a boolean type.
typeBoolean: function() {
return 'boolean';
},
// Create the column definition for an enum type.
// Using method 2 here: http://stackoverflow.com/questions/10923213/postgres-enum-data-type-or-check-constraint
typeEnum: function(column) {
return 'text check(' + this.wrap(column.name) + " in('" + column.allowed.join("', '") + "'))";
},
// Create the column definition for a date-time type.
typeDateTime: function() {
return 'timestamp';
},
// Create the column definition for a timestamp type.
typeTimestamp: function() {
return 'timestamp';
},
// Create the column definition for a bit type.
typeBit: function(column) {
return column.length !== false ? 'bit(' + column.length + ')' : 'bit';
},
// Create the column definition for a binary type.
typeBinary: function() {
return 'bytea';
},
// Create the column definition for a uuid type.
typeUuid: function() {
return 'uuid';
},
// Create the column definition for a json type,
// checking whether the json type is supported - falling
// back to "text" if it's not.
typeJson: function(column, blueprint) {
if (parseFloat(blueprint.client.version) >= 9.2) return 'json';
return 'text';
},
// Get the SQL for an auto-increment column modifier.
modifyIncrement: function(blueprint, column) {
if (column.autoIncrement && (column.type == 'integer' || column.type == 'bigInteger')) {
return ' primary key not null';
}
}
}, baseSchemaGrammar, grammar);

@@ -15,3 +15,2 @@ // SQLite3

// All other local project modules needed in this scope.
var SQLite3Base = require('../base/sqlite3');
var ServerBase = require('./base').ServerBase;

@@ -23,2 +22,5 @@ var Builder = require('../../lib/builder').Builder;

var grammar = require('./sqlite3/grammar').grammar;
var schemaGrammar = require('./sqlite3/schemagrammar').schemaGrammar;
// Constructor for the SQLite3Client.

@@ -29,2 +31,8 @@ var SQLite3Client = exports.Client = ServerBase.extend({

// Attach the appropriate grammar definitions onto the current client.
attachGrammars: function() {
this.grammar = grammar;
this.schemaGrammar = schemaGrammar;
},
// Runs the query on the specified connection, providing the bindings

@@ -147,33 +155,1 @@ // and any other necessary prep work.

});
exports.grammar = _.defaults({
handleResponse: function(builder, resp) {
var ctx = resp[1]; resp = resp[0];
if (builder.type === 'select') {
resp = Helpers.skim(resp);
} else if (builder.type === 'insert') {
resp = [ctx.lastID];
} else if (builder.type === 'delete' || builder.type === 'update') {
resp = ctx.changes;
}
return resp;
}
}, SQLite3Base.grammar);
exports.schemaGrammar = _.defaults({
handleResponse: function(builder, resp) {
// This is an array, so we'll assume that the relevant info is on the first statement...
resp = resp[0];
var ctx = resp[1]; resp = resp[0];
if (builder.type === 'tableExists') {
return resp.length > 0;
} else if (builder.type === 'columnExists') {
return _.findWhere(resp, {name: builder.bindings[1]}) != null;
}
return resp;
}
}, SQLite3Base.schemaGrammar);

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

// Knex.js 0.4.0
// Knex.js 0.4.2
// --------------

@@ -60,4 +60,4 @@

knex.grammar = Dialect.grammar;
knex.schemaGrammar = Dialect.schemaGrammar;
knex.grammar = client.grammar;
knex.schemaGrammar = client.schemaGrammar;

@@ -95,3 +95,3 @@ // Main namespaces for key library components.

// Keep in sync with package.json
knex.VERSION = '0.4.1';
knex.VERSION = '0.4.2';

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

@@ -9,2 +9,4 @@ // JoinClause

// The "JoinClause" is an object holding any necessary info about a join,
// including the type, and any associated tables & columns being joined.
var JoinClause = function(type, table) {

@@ -18,2 +20,3 @@ this.joinType = type;

// Adds an "on" clause to the current join object.
on: function(first, operator, second) {

@@ -24,2 +27,3 @@ this.clauses.push({first: first, operator: operator, second: second, bool: 'and'});

// Adds an "and on" clause to the current join object.
andOn: function() {

@@ -29,2 +33,3 @@ return this.on.apply(this, arguments);

// Adds an "or on" clause to the current join object.
orOn: function(first, operator, second) {

@@ -35,2 +40,3 @@ this.clauses.push({first: first, operator: operator, second: second, bool: 'or'});

// Explicitly set the type of join, useful within a function when creating a grouped join.
type: function(type) {

@@ -37,0 +43,0 @@ this.joinType = type;

@@ -9,2 +9,7 @@ // Schema Interface

// The SchemaInterface are the publically accessible methods
// when creating or modifying an existing schema, Each of
// these methods are mixed into the `knex.schema` object,
// and pass-through to creating a `SchemaBuilder` instance,
// which is used as the context of the `this` value below.
var SchemaInterface = {

@@ -11,0 +16,0 @@

@@ -12,2 +12,6 @@ // Transaction

// Creates a new wrapper object for constructing a transaction.
// Called by the `knex.transaction`, which sets the correct client
// and handles the `container` object, passing along the correct
// `connection` to keep all of the transactions on the correct connection.
var Transaction = function(instance) {

@@ -14,0 +18,0 @@ this.client = instance.client;

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

@@ -31,3 +31,3 @@ "main": "knex.js",

"test": "mocha -R spec test/index.js",
"doc": "groc -o docs --verbose lib/*.js lib/**/*.js clients/*.js clients/**/*.js knex.js"
"doc": "groc -o docs --verbose lib/*.js lib/**/*.js clients/*.js clients/**/*.js clients/**/**/*.js knex.js"
},

@@ -34,0 +34,0 @@ "repository": {

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

},
grammar: require('../../clients/server/mysql').grammar
grammar: require('../../clients/server/mysql/grammar').grammar
});

@@ -17,0 +17,0 @@ });

SocketSocket SOC 2 Logo

Product

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

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc