Skip to content

Commit

Permalink
fixed an issue with methods extend and merge not inheriting the t…
Browse files Browse the repository at this point in the history
…able.
  • Loading branch information
vitaly-t committed May 17, 2016
1 parent cae792e commit 8672b5b
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 9 deletions.
81 changes: 78 additions & 3 deletions lib/helpers/columnSet.js
Expand Up @@ -321,14 +321,49 @@ function ColumnSet(columns, options) {
* {@link helpers.Column Column},
* {@link helpers.ColumnSet.merge merge}
*
* @example
*
* var pgp = require('pg-promise')();
*
* var cs = new pgp.helpers.ColumnSet(['one', 'two'], {table: 'my-table'});
* console.log(cs);
* //=>
* // ColumnSet {
* // table: "my-table"
* // columns: [
* // Column {
* // name: "one"
* // }
* // Column {
* // name: "two"
* // }
* // ]
* // }
* var csExtended = cs.extend(['three']);
* console.log(csExtended);
* //=>
* // ColumnSet {
* // table: "my-table"
* // columns: [
* // Column {
* // name: "one"
* // }
* // Column {
* // name: "two"
* // }
* // Column {
* // name: "three"
* // }
* // ]
* // }
*/
this.extend = function (columns) {
var cs = columns;
if (!(cs instanceof ColumnSet)) {
cs = new ColumnSet(columns);
}
// Any any duplicate column will throw Error = 'Duplicate column name "name".',
return new ColumnSet(this.columns.concat(cs.columns));
// Any duplicate column will throw Error = 'Duplicate column name "name".',
return new ColumnSet(this.columns.concat(cs.columns), {table: this.table});
};

/**
Expand All @@ -347,6 +382,46 @@ function ColumnSet(columns, options) {
* {@link helpers.Column Column},
* {@link helpers.ColumnSet.extend extend}
*
* @example
*
* var pgp = require('pg-promise')();
*
* var cs = new pgp.helpers.ColumnSet(['?one', 'two:json'], {table: 'my-table'});
* //console.log(cs);
* //=>
* // ColumnSet {
* // table: "my-table"
* // columns: [
* // Column {
* // name: "one"
* // cnd: true
* // }
* // Column {
* // name: "two"
* // mod: ":json"
* // }
* // ]
* // }
* var csMerged = cs.merge(['two', 'three^']);
* console.log(csMerged);
* //=>
* // ColumnSet {
* // table: "my-table"
* // columns: [
* // Column {
* // name: "one"
* // cnd: true
* // }
* // Column {
* // name: "two"
* // }
* // Column {
* // name: "three"
* // mod: "^"
* // }
* // ]
* // }
*
*/
this.merge = function (columns) {
var cs = columns;
Expand All @@ -365,7 +440,7 @@ function ColumnSet(columns, options) {
cols.push(c);
}
});
return new ColumnSet(cols);
return new ColumnSet(cols, {table: this.table});
};

/**
Expand Down
4 changes: 2 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "pg-promise",
"version": "4.2.0",
"version": "4.2.1",
"description": "PostgreSQL via promises",
"main": "lib/index.js",
"scripts": {
Expand Down Expand Up @@ -40,7 +40,7 @@
},
"dependencies": {
"pg": "^4.5.5",
"spex": "^0.4.3",
"spex": "^0.4.4",
"pg-minify": "^0.3.2"
},
"devDependencies": {
Expand Down
12 changes: 8 additions & 4 deletions test/helpSpec.js
Expand Up @@ -615,11 +615,13 @@ describe("ColumnSet", function () {

describe("method 'extend'", function () {
it("must extend columns", function () {
var first = new helpers.ColumnSet(['one', 'two']);
var first = new helpers.ColumnSet(['one', 'two'], {table: 'my-table'});
var second = new helpers.ColumnSet(['three', 'four']);
var result = new helpers.ColumnSet(['one', 'two', 'three', 'four']);
var result = new helpers.ColumnSet(['one', 'two', 'three', 'four'], {table: 'my-table'});
var dest1 = first.extend(second);
var dest2 = first.extend(['three', 'four']);
expect(dest1.table).toBe(first.table);
expect(dest2.table).toBe(first.table);
expect(dest1.toString()).toBe(result.toString());
expect(dest2.toString()).toBe(result.toString());
});
Expand All @@ -633,11 +635,13 @@ describe("ColumnSet", function () {

describe("method 'merge'", function () {
it("must merge all columns", function () {
var first = new helpers.ColumnSet(['one', 'two']);
var first = new helpers.ColumnSet(['one', 'two'], {table: 'my-table'});
var second = new helpers.ColumnSet(['two', 'three']);
var result = new helpers.ColumnSet(['one', 'two', 'three']);
var result = new helpers.ColumnSet(['one', 'two', 'three'], {table: 'my-table'});
var dest1 = first.merge(second);
var dest2 = first.merge(['two', 'three']);
expect(dest1.table).toBe(first.table);
expect(dest2.table).toBe(first.table);
expect(dest1.toString()).toBe(result.toString());
expect(dest2.toString()).toBe(result.toString());
});
Expand Down

0 comments on commit 8672b5b

Please sign in to comment.