Skip to content

Commit 63baf66

Browse files
committed
Merge pull request brianc#200 from madebyherzblut/add-table-clone
Add method table.clone([config])
2 parents 1f25ed6 + 372e29a commit 63baf66

2 files changed

Lines changed: 50 additions & 5 deletions

File tree

lib/table.js

Lines changed: 15 additions & 4 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') {
@@ -135,7 +146,7 @@ Table.prototype.star = function(options) {
135146

136147
Table.prototype.literal = function(literal) {
137148
return new LiteralNode(literal);
138-
}
149+
};
139150

140151
Table.prototype.count = function(alias) {
141152
var name = this.alias || this._name,
@@ -166,9 +177,9 @@ Table.prototype.subQuery = function(alias) {
166177
var query = new Query(this);
167178
query.type = 'SUBQUERY';
168179
query.alias = alias;
169-
query.join = function(other) {
170-
return new JoinNode('INNER', this.toNode(), other.toNode(), other);
171-
}
180+
query.join = function(other) {
181+
return new JoinNode('INNER', this.toNode(), other.toNode(), other);
182+
};
172183
return query;
173184
};
174185

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)