multi-instance really working

This commit is contained in:
Tim Griesser 2013-05-08 16:52:44 -04:00
parent 0b418bd283
commit 4da1986190
2 changed files with 27 additions and 21 deletions

24
knex.js
View File

@ -16,7 +16,10 @@
// `Knex` is the root namespace and a chainable function: `Knex('tableName')` // `Knex` is the root namespace and a chainable function: `Knex('tableName')`
var Knex = function(table) { var Knex = function(table) {
return new Knex.Builder(table); if (!Knex.Instances['main']) {
throw new Error('The Knex instance has not been initialized yet.');
}
return Knex.Instances['main'](table);
}; };
// Keep in sync with package.json // Keep in sync with package.json
@ -1429,7 +1432,10 @@
// `Knex` block... in most cases, we'll check if the value // `Knex` block... in most cases, we'll check if the value
// is an instanceof Raw, and if it is, use the supplied value. // is an instanceof Raw, and if it is, use the supplied value.
Knex.Raw = function(value) { Knex.Raw = function(value) {
return new Raw(value); if (!Knex.Instances['main']) {
throw new Error('The Knex instance has not been initialized yet.');
}
return Knex.Instances['main'].Raw(value);
}; };
var Raw = function(value) { var Raw = function(value) {
@ -1507,7 +1513,7 @@
// cases where there is only a single connection. // cases where there is only a single connection.
if (_.isObject(name)) { if (_.isObject(name)) {
options = name; options = name;
name = 'default'; name = 'main';
} }
// Don't try to initialize the same `name` twice... If necessary, // Don't try to initialize the same `name` twice... If necessary,
@ -1539,11 +1545,8 @@
client.schemaGrammar = _.extend({}, client.grammar, Knex.SchemaGrammar, client.schemaGrammar); client.schemaGrammar = _.extend({}, client.grammar, Knex.SchemaGrammar, client.schemaGrammar);
// If this is named "default" then we're setting this on the Knex // If this is named "default" then we're setting this on the Knex
if (name === 'default') {
Target = Knex;
} else {
Target = function(table) { Target = function(table) {
var builder = new Target.Builder(table); var builder = new Knex.Builder(table);
builder.client = client; builder.client = client;
builder.grammar = client.grammar; builder.grammar = client.grammar;
return builder; return builder;
@ -1552,10 +1555,13 @@
// Inherit static properties, without any that don't apply except // Inherit static properties, without any that don't apply except
// on the "root" `Knex`. // on the "root" `Knex`.
_.extend(Target, _.omit(Knex, 'Initialize', 'Instances', 'VERSION')); _.extend(Target, _.omit(Knex, 'Initialize', 'Instances', 'VERSION'));
}
// Initialize the schema builder methods. // Initialize the schema builder methods.
if (name === 'main') {
initSchema(Knex, client);
} else {
initSchema(Target, client); initSchema(Target, client);
}
// Specifically set the client on the current target. // Specifically set the client on the current target.
Target.client = client; Target.client = client;
@ -1583,7 +1589,7 @@
}; };
// Named instances of Knex, presumably with different database // Named instances of Knex, presumably with different database
// connections, the main instance being named "default"... // connections, the main instance being named "main"...
Knex.Instances = {}; Knex.Instances = {};
// Export the Knex module // Export the Knex module

View File

@ -6,7 +6,7 @@ var conn = require(process.env.KNEX_TEST || './shared/config');
// The output goes here. // The output goes here.
exports.output = {}; exports.output = {};
var MySql = Knex.Initialize('mysql', { Knex.Initialize({
client: 'mysql', client: 'mysql',
connection: conn.mysql connection: conn.mysql
}); });
@ -37,13 +37,13 @@ describe('Knex', function() {
var allDone; var allDone;
Q.all([ Q.all([
require('./string')(MySql, 'mysql'), require('./string')(Knex, 'mysql'),
require('./string')(Postgres, 'postgres'), require('./string')(Postgres, 'postgres'),
require('./string')(Sqlite3, 'sqlite3') require('./string')(Sqlite3, 'sqlite3')
]).then(function() { ]).then(function() {
Knex.runQuery = runQuery; Knex.runQuery = runQuery;
return Q.all([ return Q.all([
require('./regular')(MySql, 'mysql'), require('./regular')(Knex, 'mysql'),
require('./regular')(Postgres, 'postgres'), require('./regular')(Postgres, 'postgres'),
require('./regular')(Sqlite3, 'sqlite3') require('./regular')(Sqlite3, 'sqlite3')
]); ]);