Knex.VERSION = '0.1.7';
-
- var rethrower = function(err) {
- throw err;
- };diff --git a/.travis.yml b/.travis.yml index 778b2d4c..e33d0cb0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ # .travis.yml language: node_js node_js: - - 0.11 - 0.10 - 0.8 - 0.6 diff --git a/clients/base.js b/clients/base.js index 6e18b308..2a0b77be 100644 --- a/clients/base.js +++ b/clients/base.js @@ -81,11 +81,11 @@ exports.protoProps = { }); }, - finishTransaction: function(type, trans, dfd) { + finishTransaction: function(type, trans, dfd, msg) { var ctx = this; nodefn.call(trans.connection.query.bind(trans.connection), type + ';', []).then(function(resp) { - if (type === 'commit') dfd.resolve(resp); - if (type === 'rollback') dfd.reject(resp); + if (type === 'commit') dfd.resolve(msg || resp); + if (type === 'rollback') dfd.reject(msg || resp); }).ensure(function() { ctx.releaseConnection(trans.connection); trans.connection = null; diff --git a/clients/mysql.js b/clients/mysql.js index 6dada348..82abc1d7 100644 --- a/clients/mysql.js +++ b/clients/mysql.js @@ -84,7 +84,7 @@ MysqlClient.grammar = { MysqlClient.schemaGrammar = _.extend({}, base.schemaGrammar, MysqlClient.grammar, { // The possible column modifiers. - modifiers: ['Unsigned', 'Nullable', 'Default', 'Increment', 'After'], + modifiers: ['Unsigned', 'Nullable', 'Default', 'Increment', 'After', 'Comment'], // Compile a create table command. compileCreateTable: function(blueprint, command) { @@ -98,6 +98,11 @@ MysqlClient.schemaGrammar = _.extend({}, base.schemaGrammar, MysqlClient.grammar sql += ' engine = ' + blueprint.isEngine; } + if (blueprint.tableComment) { + var maxTableCommentLength = 60; + sql += " comment = '" + blueprint.tableComment + "'" + } + return sql; }, @@ -251,6 +256,14 @@ MysqlClient.schemaGrammar = _.extend({}, base.schemaGrammar, MysqlClient.grammar if (column.isAfter) { return ' after ' + this.wrap(column.isAfter); } + }, + + // Get the SQL for a comment column modifier. (MySQL) + modifyComment: function(blueprint, column) { + var maxColumnCommentLength = 255; + if (_.isString(column.comment)) { + return " comment '" + column.comment + "'"; + } } }); diff --git a/clients/postgres.js b/clients/postgres.js index 0083d68a..e7a6f865 100644 --- a/clients/postgres.js +++ b/clients/postgres.js @@ -169,6 +169,25 @@ PostgresClient.schemaGrammar = _.extend({}, base.schemaGrammar, PostgresClient.g return 'alter table ' + this.wrapTable(blueprint) + ' rename to ' + this.wrapTable(command.to); }, + // Compile a comment command. + compileComment: function(blueprint, command) { + var table = this.wrapTable(blueprint); + var comment; + if (command.comment == void 0) { + comment = 'NULL' + } else { + comment = "'" + command.comment + "'"; + } + var identifier; + if (command.isTable) { + identifier = 'table ' + table; + } else { + var column = this.wrap(command.columnName); + identifier = 'column ' + table + '.' + column; + } + return 'comment on ' + identifier + ' is ' + comment; + }, + // Create the column definition for a string type. typeString: function(column) { return "varchar(" + column.length + ")"; @@ -235,4 +254,4 @@ PostgresClient.schemaGrammar = _.extend({}, base.schemaGrammar, PostgresClient.g return ' primary key'; } } -}); \ No newline at end of file +}); diff --git a/clients/sqlite3.js b/clients/sqlite3.js index e335e972..7241a64f 100644 --- a/clients/sqlite3.js +++ b/clients/sqlite3.js @@ -87,11 +87,11 @@ _.extend(Sqlite3Client.prototype, base.protoProps, { }); }, - finishTransaction: function(type, trans, dfd) { + finishTransaction: function(type, trans, dfd, msg) { var ctx = this; nodefn.call(trans.connection.run.bind(trans.connection), type + ';', []).then(function(resp) { - if (type === 'commit') dfd.resolve(resp); - if (type === 'rollback') dfd.reject(resp); + if (type === 'commit') dfd.resolve(msg || resp); + if (type === 'rollback') dfd.reject(msg || resp); }).ensure(function() { ctx.releaseConnection(trans.connection); trans.connection = null; @@ -210,7 +210,7 @@ Sqlite3Client.schemaGrammar = _.extend({}, base.schemaGrammar, Sqlite3Client.gra // Get all of the commands with a given name. getCommandsByName: function(blueprint, name) { - return _.where(blueprint.commands, function(value) { return value.name == name; }); + return _.find(blueprint.commands, function(value) { return value.name == name; }) || []; }, // Get the primary key syntax for a table creation statement. diff --git a/docs/knex.html b/docs/knex.html index 386a371d..4a193062 100644 --- a/docs/knex.html +++ b/docs/knex.html @@ -91,11 +91,7 @@ http://knexjs.org -
Knex.VERSION = '0.1.7';
-
- var rethrower = function(err) {
- throw err;
- }; Knex.VERSION = '0.1.8'; var operators = ['=', '<', '>', '<=', '>=', '<>', 'like', 'not like', 'between', 'ilike'];
+ var operators = ['=', '<', '>', '<=', '>=', '<>', '!=', 'like', 'not like', 'between', 'ilike'];
_.extend(Builder.prototype, Common, {
@@ -2335,17 +2331,16 @@ transaction completes or fails, we know what to do.
- Call the container with the transaction
-commit & rollback objects
+ Ensure the transacting object methods are bound with the correct context.
- container({
- commit: function() {
- client.finishTransaction('commit', this, dfd);
+ var containerObj = {
+ commit: function(msg) {
+ client.finishTransaction('commit', this, dfd, msg);
},
- rollback: function() {
- client.finishTransaction('rollback', this, dfd);
+ rollback: function(msg) {
+ client.finishTransaction('rollback', this, dfd, msg);
},
@@ -2362,7 +2357,24 @@ commit & rollback objects
connection: connection
- });
+ };
+ _.bindAll(containerObj, 'commit', 'rollback');
+
+
+
+
+
+
+
+
+ ¶
+
+ Call the container with the transaction
+commit & rollback objects.
+
+
+
+ container(containerObj);
return dfd.promise;
});
@@ -2371,11 +2383,11 @@ commit & rollback objects
-
+
Knex.Schema
@@ -2384,31 +2396,16 @@ commit & rollback objects
-
-
-
-
- ¶
-
-
-
-
- var initSchema = function(Target, client) {
-
-
-
-
- Top level object for Schema related functions
-
+
- var Schema = Target.Schema = {};
+ var initSchema = function(Target, client) {
@@ -2419,6 +2416,21 @@ commit & rollback objects
+ Top level object for Schema related functions
+
+
+
+ var Schema = Target.Schema = {};
+
+
+
+
+
+
+
+
+ ¶
+
Attach main static methods, which passthrough to the
SchemaBuilder instance methods
@@ -2440,11 +2452,11 @@ SchemaBuilder instance methods
-
+
All of the Schame methods that should be called with a
SchemaBuilder context, to disallow calling more than one method at once.
@@ -2456,11 +2468,11 @@ SchemaBuilder instance methods
-
+
Modify a table on the schema.
@@ -2474,11 +2486,11 @@ SchemaBuilder instance methods
-
+
Create a new table on the schema.
@@ -2493,11 +2505,11 @@ SchemaBuilder instance methods
-
+
Drop a table from the schema.
@@ -2511,11 +2523,11 @@ SchemaBuilder instance methods
-
+
Drop a table from the schema if it exists.
@@ -2529,11 +2541,11 @@ SchemaBuilder instance methods
-
+
Rename a table on the schema.
@@ -2547,11 +2559,11 @@ SchemaBuilder instance methods
-
+
Determine if the given table exists.
@@ -2567,11 +2579,11 @@ SchemaBuilder instance methods
-
+
Knex.SchemaBuilder
@@ -2580,11 +2592,11 @@ SchemaBuilder instance methods
-
+
@@ -2603,11 +2615,11 @@ SchemaBuilder instance methods
-
+
A callback from the table building Knex.schemaBuilder calls.
@@ -2621,11 +2633,11 @@ SchemaBuilder instance methods
-
+
Get the raw sql statements for the blueprint.
@@ -2636,11 +2648,11 @@ SchemaBuilder instance methods
-
+
Add the commands that are implied by the blueprint.
@@ -2653,11 +2665,11 @@ SchemaBuilder instance methods
-
+
Add indicies
@@ -2675,11 +2687,11 @@ SchemaBuilder instance methods
-
+
If the index has been specified on the given column, but is simply
equal to "true" (boolean), no name has been specified for this
@@ -2694,11 +2706,11 @@ index, so we will simply call the index methods without one.
-
+
+
+
+
+
+
+
+
+
+ ¶
+
+ Add table comments. (Postgres)
+
+
+
+ if (this.tableComment) {
+ this._addCommand('comment', {
+ comment: this.tableComment,
+ isTable: true
+ });
+ }
+
+
+
+
+
+
+
+
+ ¶
+
+ Add column comments. (Postgres)
+
+
+
+ for (var i = 0, l = this.columns.length; i < l; i++) {
+ var column = this.columns[i];
+ if (_.has(column, 'comment')) {
+ this._addCommand('comment', {
+ comment: column.comment,
+ columnName: column.name,
+ isTable: false
+ });
+ }
}
var statements = [];
@@ -2718,11 +2774,11 @@ the index specification, and pass in the name to the method.
-
+
Each type of command has a corresponding compiler function on the schema
grammar which is used to build the necessary SQL statements to build
@@ -2745,11 +2801,11 @@ the blueprint element, so we'll just call that compilers function.
-
+
Determine if the blueprint has a create command.
@@ -2765,11 +2821,11 @@ the blueprint element, so we'll just call that compilers function.
-
+
Sets the engine to use when creating the table in MySql
@@ -2784,11 +2840,11 @@ the blueprint element, so we'll just call that compilers function.
-
+
Indicate that the given columns should be dropped.
@@ -2802,11 +2858,11 @@ the blueprint element, so we'll just call that compilers function.
-
+
Indicate that the given columns should be dropped.
@@ -2819,11 +2875,11 @@ the blueprint element, so we'll just call that compilers function.
-
+
Indicate that the given primary key should be dropped.
@@ -2836,11 +2892,11 @@ the blueprint element, so we'll just call that compilers function.
-
+
Indicate that the given unique key should be dropped.
@@ -2853,11 +2909,11 @@ the blueprint element, so we'll just call that compilers function.
-
+
Indicate that the given index should be dropped.
@@ -2870,11 +2926,11 @@ the blueprint element, so we'll just call that compilers function.
-
+
Indicate that the given foreign key should be dropped.
@@ -2887,11 +2943,11 @@ the blueprint element, so we'll just call that compilers function.
-
+
Specify the primary key(s) for the table.
@@ -2904,11 +2960,11 @@ the blueprint element, so we'll just call that compilers function.
-
+
Specify a unique index for the table.
@@ -2921,11 +2977,11 @@ the blueprint element, so we'll just call that compilers function.
-
+
Specify an index for the table.
@@ -2938,11 +2994,11 @@ the blueprint element, so we'll just call that compilers function.
-
+
Specify a foreign key for the table.
@@ -2950,16 +3006,20 @@ the blueprint element, so we'll just call that compilers function.
foreign: function(columns, name) {
return this._indexCommand('foreign', columns, name);
+ },
+
+ comment: function(comment) {
+ this.tableComment = comment || null;
},
-
+
Create a new auto-incrementing column on the table.
@@ -2972,11 +3032,11 @@ the blueprint element, so we'll just call that compilers function.
-
+
Create a new string column on the table.
@@ -2989,11 +3049,11 @@ the blueprint element, so we'll just call that compilers function.
-
+
Alias varchar to string
@@ -3006,11 +3066,11 @@ the blueprint element, so we'll just call that compilers function.
-
+
Create a new text column on the table.
@@ -3023,11 +3083,11 @@ the blueprint element, so we'll just call that compilers function.
-
+
Create a new integer column on the table.
@@ -3040,11 +3100,11 @@ the blueprint element, so we'll just call that compilers function.
-
+
Create a new tinyinteger column on the table.
@@ -3057,11 +3117,11 @@ the blueprint element, so we'll just call that compilers function.
-
+
Alias for tinyinteger column.
@@ -3074,11 +3134,11 @@ the blueprint element, so we'll just call that compilers function.
-
+
Create a new float column on the table.
@@ -3094,11 +3154,11 @@ the blueprint element, so we'll just call that compilers function.
-
+
Create a new decimal column on the table.
@@ -3114,11 +3174,11 @@ the blueprint element, so we'll just call that compilers function.
-
+
Alias to "bool"
@@ -3131,11 +3191,11 @@ the blueprint element, so we'll just call that compilers function.
-
+
Create a new boolean column on the table
@@ -3148,11 +3208,11 @@ the blueprint element, so we'll just call that compilers function.
-
+
Create a new date column on the table.
@@ -3165,11 +3225,11 @@ the blueprint element, so we'll just call that compilers function.
-
+
Create a new date-time column on the table.
@@ -3182,11 +3242,11 @@ the blueprint element, so we'll just call that compilers function.
-
+
Create a new time column on the table.
@@ -3199,11 +3259,11 @@ the blueprint element, so we'll just call that compilers function.
-
+
Create a new timestamp column on the table.
@@ -3216,11 +3276,11 @@ the blueprint element, so we'll just call that compilers function.
-
+
Add creation and update timestamps to the table.
@@ -3234,11 +3294,11 @@ the blueprint element, so we'll just call that compilers function.
-
+
Alias to enum.
@@ -3251,11 +3311,11 @@ the blueprint element, so we'll just call that compilers function.
-
+
Create a new enum column on the table.
@@ -3268,11 +3328,11 @@ the blueprint element, so we'll just call that compilers function.
-
+
Create a new bit column on the table.
@@ -3285,11 +3345,11 @@ the blueprint element, so we'll just call that compilers function.
-
+
Create a new binary column on the table.
@@ -3302,11 +3362,11 @@ the blueprint element, so we'll just call that compilers function.
-
+
@@ -3315,11 +3375,11 @@ the blueprint element, so we'll just call that compilers function.
-
+
Create a new drop index command on the blueprint.
If the index is an array of columns, the developer means
@@ -3339,11 +3399,11 @@ to drop an index merely by specifying the columns involved.
-
+
Add a new index command to the blueprint.
If no name was specified for this index, we will create one using a basic
@@ -3365,11 +3425,11 @@ index type, such as primary or index, which makes the index unique.
-
+
Add a new column to the blueprint.
@@ -3386,11 +3446,11 @@ index type, such as primary or index, which makes the index unique.
-
+
Add a new command to the blueprint.
@@ -3406,11 +3466,11 @@ index type, such as primary or index, which makes the index unique.
-
+
Chainable object used in creating SchemaBuilder commands.
@@ -3425,11 +3485,11 @@ index type, such as primary or index, which makes the index unique.
-
+
Sets the default value for a column.
For boolean columns, we'll permit 'false'
@@ -3449,11 +3509,11 @@ to be used as default values.
-
+
Sets an integer as unsigned, is a no-op
if the column type is not an integer.
@@ -3468,11 +3528,11 @@ if the column type is not an integer.
-
+
Allows the column to contain null values.
@@ -3486,11 +3546,11 @@ if the column type is not an integer.
-
+
Adds an index on the specified column.
@@ -3504,11 +3564,11 @@ if the column type is not an integer.
-
+
Sets this column as the primary key.
@@ -3522,11 +3582,11 @@ if the column type is not an integer.
-
+
Sets this column as unique.
@@ -3540,11 +3600,11 @@ if the column type is not an integer.
-
+
Sets the column to be inserted after another,
used in MySql alter tables.
@@ -3554,6 +3614,24 @@ used in MySql alter tables.
after: function(name) {
this.isAfter = name;
return this;
+ },
+
+
+
+
+
+
+
+
+ ¶
+
+ Adds a comment to this column.
+
+
+
+ comment: function(comment) {
+ this.comment = comment || null;
+ return this;
}
};
@@ -3563,11 +3641,11 @@ used in MySql alter tables.
-
+
Compile a foreign key command.
@@ -3580,11 +3658,11 @@ used in MySql alter tables.
-
+
We need to prepare several of the elements of the foreign key definition
before we can create the SQL, such as wrapping the tables and convert
@@ -3601,11 +3679,11 @@ an array of columns to comma-delimited strings for the SQL queries.
-
+
Once we have the basic foreign key creation statement constructed we can
build out the syntax for what should happen on an update or delete of
@@ -3622,11 +3700,11 @@ the affected columns, which will get something like "cascade", etc.
-
+
Each of the column types have their own compiler functions which are
responsible for turning the column definition into its SQL format
@@ -3647,11 +3725,11 @@ for the platform. Then column modifiers are compiled and added.
-
+
Add the column modifiers to the definition.
@@ -3671,11 +3749,11 @@ for the platform. Then column modifiers are compiled and added.
-
+
Get the SQL for the column data type.
@@ -3688,11 +3766,11 @@ for the platform. Then column modifiers are compiled and added.
-
+
Add a prefix to an array of values, utilized in the client libs.
@@ -3705,11 +3783,11 @@ for the platform. Then column modifiers are compiled and added.
-
+
Wrap a table in keyword identifiers.
@@ -3723,11 +3801,11 @@ for the platform. Then column modifiers are compiled and added.
-
+
Wrap a value in keyword identifiers.
@@ -3741,11 +3819,11 @@ for the platform. Then column modifiers are compiled and added.
-
+
Format a value so that it can be used in "default" clauses.
@@ -3763,11 +3841,11 @@ for the platform. Then column modifiers are compiled and added.
-
+
Knex.Raw
@@ -3776,11 +3854,11 @@ for the platform. Then column modifiers are compiled and added.
-
+
Helpful for injecting a snippet of raw SQL into a
Knex block... in most cases, we'll check if the value
@@ -3806,11 +3884,11 @@ is an instanceof Raw, and if it is, use the supplied value.
-
+
Returns the raw sql for the query.
@@ -3823,11 +3901,11 @@ is an instanceof Raw, and if it is, use the supplied value.
-
+
Returns the bindings for a raw query.
@@ -3841,11 +3919,11 @@ is an instanceof Raw, and if it is, use the supplied value.
-
+
Simple capitalization of a word.
@@ -3858,11 +3936,11 @@ is an instanceof Raw, and if it is, use the supplied value.
-
+
Sorts an object based on the names.
@@ -3877,11 +3955,11 @@ is an instanceof Raw, and if it is, use the supplied value.
-
+
Sets up a multi-query to be executed with serial promises.
@@ -3899,11 +3977,11 @@ is an instanceof Raw, and if it is, use the supplied value.
-
+
Knex.Initialize
@@ -3912,11 +3990,11 @@ is an instanceof Raw, and if it is, use the supplied value.
-
+
Takes a hash of options to initialize the database
connection. The client is required to choose which client
@@ -3932,11 +4010,11 @@ into client.initialize.
-
+
A name for the connection isn't required in
cases where there is only a single connection.
@@ -3951,11 +4029,11 @@ cases where there is only a single connection.
-
+
Don't try to initialize the same name twice... If necessary,
delete the instance from Knex.Instances.
@@ -3973,11 +4051,11 @@ delete the instance from Knex.Instances.
-
+
Checks if this is a default client. If it's not,
that means it's a custom lib, set the object to the client.
@@ -3994,11 +4072,11 @@ that means it's a custom lib, set the object to the client.
-
+
Creates a new instance of the db client, passing the name and options.
@@ -4009,11 +4087,11 @@ that means it's a custom lib, set the object to the client.
-
+
Setup the grammars specific to the client.
@@ -4025,11 +4103,11 @@ that means it's a custom lib, set the object to the client.
-
+
If this is named "default" then we're setting this on the Knex
@@ -4045,11 +4123,11 @@ that means it's a custom lib, set the object to the client.
-
+
Inherit static properties, without any that don't apply except
on the "root" Knex.
@@ -4061,11 +4139,11 @@ on the "root" Knex.
-
+
Initialize the schema builder methods.
@@ -4080,11 +4158,11 @@ on the "root" Knex.
-
+
Specifically set the client on the current target.
@@ -4096,11 +4174,11 @@ on the "root" Knex.
-
+
Setup the transacting function properly for this connection.
@@ -4113,11 +4191,11 @@ on the "root" Knex.
-
+
Executes a Raw query.
@@ -4132,11 +4210,11 @@ on the "root" Knex.
-
+
Add this instance to the global Knex instances, and return.
@@ -4150,11 +4228,11 @@ on the "root" Knex.
-
+
Default client paths, located in the ./clients directory.
@@ -4171,11 +4249,11 @@ on the "root" Knex.
-
+
Named instances of Knex, presumably with different database
connections, the main instance being named "main"...
@@ -4187,11 +4265,11 @@ connections, the main instance being named "main"...
-
+