-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from bravi-software/support-knex-0.8
Support knex 0.8
- Loading branch information
Showing
8 changed files
with
152 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,9 @@ test: | |
- .:/usr/src/app | ||
links: | ||
- mysql:mysql | ||
environment: | ||
# DEBUG: knextancy:tenant | ||
|
||
|
||
ci: | ||
build: . | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
var debug = require('debug'); | ||
|
||
|
||
module.exports = function (namespace) { | ||
return debug('knextancy:' + (namespace || '*')); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
var knex = require('knex'); | ||
var _ = require('underscore'); | ||
var override = require('./override').override; | ||
var before = require('./override').before; | ||
var after = require('./override').after; | ||
var debug = require('./debug')('client-multi-tenant'); | ||
|
||
|
||
exports.buildConfig = function (config, tenantId) { | ||
var multitenantConfig = _.clone(config || {}); | ||
|
||
multitenantConfig.tenantId = tenantId; | ||
|
||
var migrationsMultitenantConfig = _.clone(multitenantConfig.migrations || {}); | ||
multitenantConfig.migrations = _.extend(migrationsMultitenantConfig, { | ||
// custom migration with the table name prefix | ||
tableName: tenantId + '_' + (multitenantConfig.migrations.tableName || 'knex_migrations') | ||
}); | ||
|
||
return multitenantConfig; | ||
}; | ||
|
||
|
||
exports.install = function () { | ||
Object.defineProperty(knex.Client.prototype, 'tenantId', { | ||
get: function() { | ||
return this.config.tenantId; | ||
} | ||
}); | ||
|
||
override(knex.Client.prototype.QueryBuilder.prototype, 'toSQL', after(function(sql) { | ||
debug('knex.Client.prototype.QueryBuilder.prototype.toSQL', arguments); | ||
sql.sql = applyTenant(sql.sql, this.client.tenantId); | ||
return sql; | ||
})); | ||
|
||
override(knex.Client.prototype.Raw.prototype, 'set', before(function(sql, bindings) { | ||
debug('knex.Client.prototype.Raw.prototype.set', arguments); | ||
sql = applyTenant(sql, this.client.tenantId); | ||
return [sql, bindings]; | ||
})); | ||
|
||
override(knex.Client.prototype.SchemaBuilder.prototype, 'toSQL', after(function(sql) { | ||
debug('knex.Client.prototype.SchemaBuilder.prototype.toSQL', arguments); | ||
var client = this.client; | ||
|
||
sql.forEach(function (sql) { | ||
sql.sql = applyTenant(sql.sql, client.tenantId); | ||
}); | ||
|
||
return sql; | ||
})); | ||
|
||
override(knex.Client.prototype.Runner.prototype, 'query', after(function(promise, originalArgs) { | ||
debug('knex.Client.prototype.Runner.prototype.query', arguments); | ||
var options = originalArgs[0].options; | ||
|
||
var client = this.client; | ||
return promise.then(function(result) { | ||
if (!options || !options.nestTables) return result; | ||
|
||
return result.map(function (row) { | ||
var processedRow = {}; | ||
|
||
Object.keys(row).forEach(function (tableJoinName) { | ||
processedRow[unapplyTenant(tableJoinName, client.tenantId)] = row[tableJoinName]; | ||
}); | ||
|
||
return processedRow; | ||
}); | ||
}); | ||
})); | ||
}; | ||
|
||
|
||
function unapplyTenant (sql, tenant) { | ||
var regexp = new RegExp('^('+tenant+'+_)'); | ||
return sql.replace(regexp, '$_'); | ||
} | ||
|
||
|
||
function applyTenant (sql, tenant) { | ||
return sql.replace(/\$_/g, tenant + '_'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* Based on http://me.dt.in.th/page/JavaScript-override | ||
*/ | ||
module.exports.override = function (object, methodName, callback) { | ||
object[methodName] = callback(object[methodName]); | ||
}; | ||
|
||
|
||
module.exports.before = function (extraBehavior) { | ||
return function(original) { | ||
return function() { | ||
return original.apply(this, extraBehavior.apply(this, arguments)); | ||
}; | ||
}; | ||
}; | ||
|
||
|
||
module.exports.after = function (extraBehavior) { | ||
return function(original) { | ||
return function() { | ||
return extraBehavior.call(this, original.apply(this, arguments), arguments); | ||
}; | ||
}; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,5 +64,3 @@ describe("tenant queries", function() { | |
}); | ||
}); | ||
}); | ||
|
||
|