Skip to content

Commit db2e1c6

Browse files
Add method table.clone([config])
1 parent 2ad2ba1 commit db2e1c6

2 files changed

Lines changed: 46 additions & 1 deletion

File tree

lib/table.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ Table.define = function(config) {
4747
return table;
4848
};
4949

50+
Table.prototype.clone = function(config) {
51+
return Table.define(lodash.extend({
52+
schema: this._schema,
53+
name: this._name,
54+
sql: this.sql,
55+
columnWhiteList: !!this.columnWhiteList,
56+
snakeToCamel: !!this.snakeToCamel,
57+
columns: this.columns
58+
}, config || {}));
59+
};
60+
5061
Table.prototype.createColumn = function(col) {
5162
if(!(col instanceof Column)) {
5263
if(typeof col === 'string') {

test/table-tests.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,40 @@ test('set and get schema', function () {
187187
assert.equal(table.getSchema(), 'barbarz');
188188
});
189189

190+
suite('table.clone', function() {
191+
test('check if it is a copy, not just a reference', function() {
192+
var table = Table.define({ name: 'foo', columns: [] });
193+
var copy = table.clone();
194+
assert.notEqual(table, copy);
195+
});
196+
197+
test('copy columns', function() {
198+
var table = Table.define({ name: 'foo', columns: ['bar'] });
199+
var copy = table.clone();
200+
assert(copy.get('bar') instanceof Column);
201+
});
202+
203+
test('overwrite config while copying', function() {
204+
var table = Table.define({
205+
name: 'foo',
206+
schema: 'foobar',
207+
columns: ['bar'],
208+
snakeToCamel: true,
209+
columnWhiteList: true
210+
});
211+
212+
var copy = table.clone({
213+
schema: 'test',
214+
snakeToCamel: false,
215+
columnWhiteList: false
216+
});
217+
218+
assert.equal(copy.getSchema(), 'test');
219+
assert.equal(copy.snakeToCamel, false);
220+
assert.equal(copy.columnWhiteList, false);
221+
});
222+
});
223+
190224
test('dialects', function () {
191225
var sql = new Sql.Sql('mysql');
192226
var foo = sql.define({ name: 'foo', columns: [ 'id' ] }),
@@ -200,4 +234,4 @@ test('dialects', function () {
200234
bar = sql.define({ name: 'bar', columns: [ 'id' ] });
201235
actual = foo.join(bar).on(bar.id.equals(1)).toString();
202236
assert.equal(actual, '"foo" INNER JOIN "bar" ON ("bar"."id" = 1)');
203-
});
237+
});

0 commit comments

Comments
 (0)