Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,19 @@ if (!someTeam.players) {
}
```

### Database names and schemas (SQL adapters only)

To determine to which database and/or schema a model is referring, the model
can define `database` (MySQL, Postgres) and `schema` (Postgres) properties:

```javascript
var User = function () {
this.db = 'mydatabase';
this.schema = 'myschema';

...
```

- - -
Model JavaScript ORM copyright 2112 mde@fleegix.org.

Expand Down
56 changes: 44 additions & 12 deletions lib/adapters/sql/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,18 @@ utils.mixin(Adapter.prototype, new (function () {
// Package up the SQL-specific query data used in generating the
// statements and processing the results
this._getSqlMetadata = function (query) {
var tableName = this._tableizeModelName(query.model.modelName)
var tableName = this._tableizeModelNameFull(query.model.modelName)
, includes = this._getIncludes(query)
, selects = this._getSelectCols(query, tableName, includes)
, meta;

/*if(query.model.hasOwnProperty('schema')) {
tableName = query.model.schema+'.'+tableName;
}
if(query.model.hasOwnProperty('db')) {
tableName = query.model.db+'.'+tableName;
}*/

meta = {
mainTableName: tableName
, selects: selects
Expand All @@ -35,6 +43,20 @@ utils.mixin(Adapter.prototype, new (function () {
return meta;
};

this._tableizeModelNameFull = function (modelName) {
var tableName = this._tableizeModelName(modelName);

if (model.hasOwnProperty(modelName)) {
if(model[modelName].hasOwnProperty('schema')) {
tableName = model[modelName].schema+'.'+tableName;
}
if(model[modelName].hasOwnProperty('db')) {
tableName = model[modelName].db+'.'+tableName;
}
}
return tableName;
};

this._getIncludes = function (query) {
var self = this
, includes = query.opts.includes;
Expand Down Expand Up @@ -134,7 +156,7 @@ utils.mixin(Adapter.prototype, new (function () {
updates.push(update);
}
}
sql += 'UPDATE ' + this._tableizeModelName(modelName) + ' SET ';
sql += 'UPDATE ' + this._tableizeModelNameFull(modelName) + ' SET ';
sql += updates.join(', ') + ' ';
sql += 'WHERE ' + this.transformConditions(query.conditions);
sql += ';'
Expand All @@ -144,15 +166,24 @@ utils.mixin(Adapter.prototype, new (function () {

this._createDeleteStatementWithConditions = function (query) {
var sql = '';
sql += 'DELETE FROM ' + this._tableizeModelName(query.model.modelName) + ' ';
sql += 'DELETE FROM ' + this._tableizeModelNameFull(query.model.modelName) + ' ';
sql += 'WHERE ' + this.transformConditions(query.conditions);
sql += ';'
return sql;
};

/**
* Removes db and schema from table name, eg. mydatabse.myschema.tablename => tablename
**/
this.clearSchema = function (fullModelName)
{
var parts = fullModelName.split('.');
return parts[parts.length - 1];
};

this._createSelectStatement = function (modelName, ownerModelName) {
var name
, assumedName = utils.string.getInflection(modelName, 'constructor', 'singular')
, assumedName = utils.string.getInflection(this.clearSchema(modelName), 'constructor', 'singular')
, ownerName
, tableName
, assumedTableName
Expand All @@ -167,7 +198,7 @@ utils.mixin(Adapter.prototype, new (function () {
// Otherwise it's a named association, need to look up the
// actual model via it's owner's associations list
else {
ownerName = utils.string.getInflection(ownerModelName, 'constructor', 'singular');
ownerName = utils.string.getInflection(this.clearSchema(ownerModelName), 'constructor', 'singular');
name = model.getAssociation(ownerName, assumedName).model;
}

Expand Down Expand Up @@ -338,7 +369,7 @@ utils.mixin(Adapter.prototype, new (function () {
vals.push(prop);
}
}
sql += 'INSERT INTO ' + this._tableizeModelName(modelName) + ' ';
sql += 'INSERT INTO ' + this._tableizeModelNameFull(modelName) + ' ';
sql += '(' + cols.join(', ') + ')';
sql += ' VALUES ';
sql += '(' + vals.join(', ') + ')';
Expand Down Expand Up @@ -403,9 +434,9 @@ utils.mixin(EventedQueryProcessor.prototype, new (function () {
// First item in this list should be the owner table for
// any subsequent associations, e.g., ['users', 'profiles']
tables.forEach(function (t) {
self.models[t] = {
ctor: converter._modelizeTableName(t, mainTable)
, assnType: t == mainTable ? null : model.getAssociation(mainTable, t).type
self.models[Adapter.prototype.clearSchema(t)] = {
ctor: converter._modelizeTableName(Adapter.prototype.clearSchema(t), Adapter.prototype.clearSchema(mainTable))
, assnType: Adapter.prototype.clearSchema(t) == Adapter.prototype.clearSchema(mainTable) ? null : model.getAssociation(Adapter.prototype.clearSchema(mainTable), Adapter.prototype.clearSchema(t)).type
}
});

Expand All @@ -417,7 +448,7 @@ utils.mixin(EventedQueryProcessor.prototype, new (function () {
, colArr
, query = this.query
, meta = query._sqlMetadata
, mainTable = meta.mainTableName
, mainTable = Adapter.prototype.clearSchema(meta.mainTableName)
, tables = meta.selects
, table
, key
Expand Down Expand Up @@ -445,8 +476,9 @@ utils.mixin(EventedQueryProcessor.prototype, new (function () {
// when a new id shows up. (Owner object record is repeated for multiple
// associations.) For each subsquent record, instantiate the association
// and append it to an array in the named property for that association
tables.forEach(function (p) {
var params = obj[p]
tables.forEach(function (fullTableName) {
var p = Adapter.prototype.clearSchema(fullTableName)
, params = obj[p]
, modelItem = self.models[p]
, keyName;

Expand Down
2 changes: 1 addition & 1 deletion lib/adapters/sql/postgres.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ utils.mixin(Adapter.prototype, new (function () {
};

this.bySQL = function (query, model, callback) {
var name = this._tableizeModelName(model.modelName)
var name = this._tableizeModelNameFull(model.modelName)
, sql = 'SELECT ' + name + '.* FROM ' + name;
sql += ' ' + query;
this._itemsWithSQL.apply(sql, [name], name, query, callback);
Expand Down