Skip to content

Commit

Permalink
Add method table.clone([config])
Browse files Browse the repository at this point in the history
  • Loading branch information
madebyherzblut committed Jan 7, 2015
1 parent 2ad2ba1 commit db2e1c6
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
11 changes: 11 additions & 0 deletions lib/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ Table.define = function(config) {
return table;
};

Table.prototype.clone = function(config) {
return Table.define(lodash.extend({
schema: this._schema,
name: this._name,
sql: this.sql,
columnWhiteList: !!this.columnWhiteList,
snakeToCamel: !!this.snakeToCamel,
columns: this.columns
}, config || {}));
};

Table.prototype.createColumn = function(col) {
if(!(col instanceof Column)) {
if(typeof col === 'string') {
Expand Down
36 changes: 35 additions & 1 deletion test/table-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,40 @@ test('set and get schema', function () {
assert.equal(table.getSchema(), 'barbarz');
});

suite('table.clone', function() {
test('check if it is a copy, not just a reference', function() {
var table = Table.define({ name: 'foo', columns: [] });
var copy = table.clone();
assert.notEqual(table, copy);
});

test('copy columns', function() {
var table = Table.define({ name: 'foo', columns: ['bar'] });
var copy = table.clone();
assert(copy.get('bar') instanceof Column);
});

test('overwrite config while copying', function() {
var table = Table.define({
name: 'foo',
schema: 'foobar',
columns: ['bar'],
snakeToCamel: true,
columnWhiteList: true
});

var copy = table.clone({
schema: 'test',
snakeToCamel: false,
columnWhiteList: false
});

assert.equal(copy.getSchema(), 'test');
assert.equal(copy.snakeToCamel, false);
assert.equal(copy.columnWhiteList, false);
});
});

test('dialects', function () {
var sql = new Sql.Sql('mysql');
var foo = sql.define({ name: 'foo', columns: [ 'id' ] }),
Expand All @@ -200,4 +234,4 @@ test('dialects', function () {
bar = sql.define({ name: 'bar', columns: [ 'id' ] });
actual = foo.join(bar).on(bar.id.equals(1)).toString();
assert.equal(actual, '"foo" INNER JOIN "bar" ON ("bar"."id" = 1)');
});
});

0 comments on commit db2e1c6

Please sign in to comment.