From 1087b149c03ae3ba411213d6288494c58d4983da Mon Sep 17 00:00:00 2001 From: Vitaly Tomilov Date: Sat, 25 May 2024 09:24:04 +0100 Subject: [PATCH 1/2] adding _TN function --- lib/helpers/index.js | 6 ++++- lib/helpers/table-name.js | 31 +++++++++++++++++++++++- test/typescript/types.ts | 42 -------------------------------- typescript/pg-promise.d.ts | 49 +++++++++++++++++++++++++++++++------- 4 files changed, 75 insertions(+), 53 deletions(-) delete mode 100644 test/typescript/types.ts diff --git a/lib/helpers/index.js b/lib/helpers/index.js index 820f2309..1919a105 100644 --- a/lib/helpers/index.js +++ b/lib/helpers/index.js @@ -9,7 +9,7 @@ const {Column} = require('./column'); const {ColumnSet} = require('./column-set'); -const {TableName} = require('./table-name'); +const {TableName, _TN} = require('./table-name'); const method = require('./methods'); /** @@ -24,6 +24,9 @@ const method = require('./methods'); * @property {function} TableName * {@link helpers.TableName TableName} class constructor. * + * @property {function} _TN + * {@link helpers._TN _TN} Table-Name template tag function. + * * @property {function} ColumnSet * {@link helpers.ColumnSet ColumnSet} class constructor. * @@ -64,6 +67,7 @@ module.exports = config => { return method.sets(data, columns, capSQL()); }, TableName, + _TN, ColumnSet, Column }; diff --git a/lib/helpers/table-name.js b/lib/helpers/table-name.js index 3f5e440d..3b62e2c0 100644 --- a/lib/helpers/table-name.js +++ b/lib/helpers/table-name.js @@ -46,6 +46,7 @@ const npm = { * @returns {helpers.TableName} * * @see + * {@link helpers._TN _TN}, * {@link helpers.TableName#toPostgres toPostgres} * * @example @@ -125,4 +126,32 @@ npm.utils.addInspection(TableName, function () { return this.toString(); }); -module.exports = {TableName}; +/** + * + * @interface Table + * @description + * Structure for any table name/path. + * + * @property {string} [schema] - schema name, if specified + * @property {string} table - table name + */ + +/** + * @function helpers._TN + * @description + * Table-Name template tag function, to convert any `"schema.table"` string + * into `{schema, table}` object. + * + * @example + * const {ColumnSet, _TN} = pgp.helpers; + * + * const cs = new ColumnSet(['id', 'name'], {table: _TN`schema.table`}); + * + * @returns {Table} + */ +function _TN(data) { + const [schema, table] = data[0].split('.'); + return table === undefined ? {table: schema} : {schema, table}; +} + +module.exports = {TableName, _TN}; diff --git a/test/typescript/types.ts b/test/typescript/types.ts deleted file mode 100644 index 28accf10..00000000 --- a/test/typescript/types.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { - IQueryFileOptions, - IFormattingOptions, - IConnectionOptions, - IPreparedParsed, - IParameterizedParsed, - IPreparedStatement, - IParameterizedQuery, - QueryParam, - IColumnDescriptor, - IColumnConfig, - IColumnSetOptions, - ITable, - QueryColumns, - TableName, - Column, - ColumnSet, - queryResult, - PreparedStatement, - ParameterizedQuery, - QueryFile, - PromiseAdapter, - IDatabase, - IMain, - ITask, - IBaseProtocol, - IConnected, - ITaskContext, - IEventContext, - errors, - IInitOptions, - ILibConfig, - IFormatting, - txMode, - IUtils, - IHelpers, - IGenericPromise, - ILostContext, - IPromiseConfig, - ICTFObject -} - from '../../typescript/pg-promise'; diff --git a/typescript/pg-promise.d.ts b/typescript/pg-promise.d.ts index 75845efe..b9cbc7d1 100644 --- a/typescript/pg-promise.d.ts +++ b/typescript/pg-promise.d.ts @@ -130,8 +130,8 @@ declare namespace pgPromise { } interface ITable { - table: string schema?: string + table: string } interface IPromiseConfig { @@ -207,7 +207,11 @@ declare namespace pgPromise { assign(source?: { source?: object, prefix?: string }): string - assignColumns(options?: { from?: string, to?: string, skip?: string | string[] | ((c: Column) => boolean) }): string + assignColumns(options?: { + from?: string, + to?: string, + skip?: string | string[] | ((c: Column) => boolean) + }): string extend(columns: Column | ColumnSet | Array | Column>): ColumnSet @@ -382,7 +386,10 @@ declare namespace pgPromise { multi(query: QueryParam, values?: any): XPromise> // API: https://vitaly-t.github.io/pg-promise/Database.html#stream - stream(qs: NodeJS.ReadableStream, init: (stream: NodeJS.ReadableStream) => void): XPromise<{ processed: number, duration: number }> + stream(qs: NodeJS.ReadableStream, init: (stream: NodeJS.ReadableStream) => void): XPromise<{ + processed: number, + duration: number + }> // API: https://vitaly-t.github.io/pg-promise/Database.html#func func(funcName: string, values?: any, qrm?: queryResult): XPromise @@ -410,7 +417,10 @@ declare namespace pgPromise { taskIf(tag: string | number, cb: (t: ITask & Ext) => T | XPromise): XPromise - taskIf(options: { tag?: any, cnd?: boolean | ((t: ITask & Ext) => boolean) }, cb: (t: ITask & Ext) => T | XPromise): XPromise + taskIf(options: { + tag?: any, + cnd?: boolean | ((t: ITask & Ext) => boolean) + }, cb: (t: ITask & Ext) => T | XPromise): XPromise // Transactions; // API: https://vitaly-t.github.io/pg-promise/Database.html#tx @@ -418,7 +428,10 @@ declare namespace pgPromise { tx(tag: string | number, cb: (t: ITask & Ext) => T | XPromise): XPromise - tx(options: { tag?: any, mode?: _txMode.TransactionMode | null }, cb: (t: ITask & Ext) => T | XPromise): XPromise + tx(options: { + tag?: any, + mode?: _txMode.TransactionMode | null + }, cb: (t: ITask & Ext) => T | XPromise): XPromise // Conditional Transactions; // API: https://vitaly-t.github.io/pg-promise/Database.html#txIf @@ -426,7 +439,12 @@ declare namespace pgPromise { txIf(tag: string | number, cb: (t: ITask & Ext) => T | XPromise): XPromise - txIf(options: { tag?: any, mode?: _txMode.TransactionMode | null, reusable?: boolean | ((t: ITask & Ext) => boolean), cnd?: boolean | ((t: ITask & Ext) => boolean) }, cb: (t: ITask & Ext) => T | XPromise): XPromise + txIf(options: { + tag?: any, + mode?: _txMode.TransactionMode | null, + reusable?: boolean | ((t: ITask & Ext) => boolean), + cnd?: boolean | ((t: ITask & Ext) => boolean) + }, cb: (t: ITask & Ext) => T | XPromise): XPromise } // Database object in connected state; @@ -656,7 +674,10 @@ declare namespace pgPromise { camelizeVar(text: string): string - enumSql(dir: string, options?: { recursive?: boolean, ignoreErrors?: boolean }, cb?: (file: string, name: string, path: string) => any): any + enumSql(dir: string, options?: { + recursive?: boolean, + ignoreErrors?: boolean + }, cb?: (file: string, name: string, path: string) => any): any taskArgs(args: IArguments): ITaskArguments } @@ -665,11 +686,19 @@ declare namespace pgPromise { // API: https://vitaly-t.github.io/pg-promise/helpers.html interface IHelpers { - concat(queries: Array): string + concat(queries: Array): string insert(data: object | object[], columns?: QueryColumns | null, table?: string | ITable | TableName): string - update(data: object | object[], columns?: QueryColumns | null, table?: string | ITable | TableName, options?: { tableAlias?: string, valueAlias?: string, emptyUpdate?: any }): any + update(data: object | object[], columns?: QueryColumns | null, table?: string | ITable | TableName, options?: { + tableAlias?: string, + valueAlias?: string, + emptyUpdate?: any + }): any values(data: object | object[], columns?: QueryColumns | null): string @@ -678,6 +707,8 @@ declare namespace pgPromise { Column: typeof Column ColumnSet: typeof ColumnSet TableName: typeof TableName + + _TN: (data: TemplateStringsArray) => ITable } interface IGenericPromise { From 0df81a14b8a735b4f41ac41991db59c1d291ee82 Mon Sep 17 00:00:00 2001 From: Vitaly Tomilov Date: Sat, 25 May 2024 09:39:31 +0100 Subject: [PATCH 2/2] update package --- package.json | 12 +-- test/db.spec.js | 248 ++++++++++++++++++++++++------------------------ 2 files changed, 130 insertions(+), 130 deletions(-) diff --git a/package.json b/package.json index 5a673091..0094a4df 100644 --- a/package.json +++ b/package.json @@ -47,16 +47,16 @@ "spex": "3.3.0" }, "devDependencies": { - "@eslint/js": "9.1.1", - "@types/node": "20.12.7", + "@eslint/js": "9.3.0", + "@types/node": "20.12.12", "bluebird": "3.7.2", "coveralls": "3.1.1", - "cspell": "8.7.0", - "eslint": "9.1.0", - "globals": "15.0.0", + "cspell": "8.8.3", + "eslint": "9.3.0", + "globals": "15.3.0", "istanbul": "0.4.5", "jasmine-node": "3.0.0", - "jsdoc": "4.0.2", + "jsdoc": "4.0.3", "JSONStream": "1.3.5", "pg-query-stream": "4.5.5", "tslint": "6.1.3", diff --git a/test/db.spec.js b/test/db.spec.js index 369758ca..d7d4dea3 100644 --- a/test/db.spec.js +++ b/test/db.spec.js @@ -16,7 +16,7 @@ const options = { }; const dbHeader = header(options); const pgp = dbHeader.pgp; -const db = dbHeader.db; +const dbSpec = dbHeader.db; const dummy = () => { // dummy/empty function; @@ -59,7 +59,7 @@ describe('Connection', () => { describe('with default parameters', () => { let status = 'connecting', error, doneRes; beforeEach(done => { - db.connect() + dbSpec.connect() .then(obj => { status = 'success'; doneRes = obj.done(); // release connection; @@ -83,7 +83,7 @@ describe('Connection', () => { describe('for regular queries', () => { let result, sco; beforeEach(done => { - db.connect() + dbSpec.connect() .then(obj => { sco = obj; return sco.one('select count(*) from users'); @@ -115,7 +115,7 @@ describe('Connection', () => { describe('for raw queries', () => { let result, sco; beforeEach(done => { - db.connect() + dbSpec.connect() .then(obj => { sco = obj; return sco.result('select * from users'); @@ -224,7 +224,7 @@ describe('Connection', () => { describe('direct end() call', () => { let txt; beforeEach(done => { - db.connect() + dbSpec.connect() .then(obj => { const c = capture(); obj.client.end(true); @@ -292,7 +292,7 @@ describe('Connection', () => { describe('on repeated disconnection', () => { let error; beforeEach(done => { - db.connect() + dbSpec.connect() .then(obj => { obj.done(); // disconnecting; try { @@ -315,7 +315,7 @@ describe('Connection', () => { describe('when executing a disconnected query', () => { let error; beforeEach(done => { - db.connect() + dbSpec.connect() .then(obj => { obj.done(); // disconnecting; return obj.query(); // invalid disconnected query; @@ -366,7 +366,7 @@ describe('Connection', () => { error = reason; }), // Terminate connection after a short delay, before the query finishes promise.delay(1000) - .then(() => db.one('SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE pid = $1', pid))]) + .then(() => dbSpec.one('SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE pid = $1', pid))]) .finally(() => { obj.done(error); done(); @@ -406,7 +406,7 @@ describe('Direct Connection', () => { describe('successful connection', () => { let sco, doneRes; beforeEach(done => { - db.connect({direct: true}) + dbSpec.connect({direct: true}) .then(obj => { sco = obj; doneRes = sco.done(); @@ -422,7 +422,7 @@ describe('Direct Connection', () => { describe('direct end() call', () => { let txt; beforeEach(done => { - db.connect({direct: true}) + dbSpec.connect({direct: true}) .then(obj => { const c = capture(); obj.client.end(); @@ -523,7 +523,7 @@ describe('Method \'map\'', () => { describe('positive:', () => { let pValue, pIndex, pArr, pData; beforeEach(done => { - db.map('SELECT 1 as value', null, (value, index, arr) => { + dbSpec.map('SELECT 1 as value', null, (value, index, arr) => { pValue = value; pIndex = index; pArr = arr; @@ -547,7 +547,7 @@ describe('Method \'map\'', () => { describe('with invalid parameters', () => { let err; beforeEach(done => { - db.map('SELECT 1') + dbSpec.map('SELECT 1') .catch(error => { err = error; }) @@ -562,7 +562,7 @@ describe('Method \'map\'', () => { describe('with error thrown inside the callback', () => { let err; beforeEach(done => { - db.map('SELECT 1', null, () => { + dbSpec.map('SELECT 1', null, () => { throw new Error('Ops!'); }) .catch(error => { @@ -583,7 +583,7 @@ describe('Method \'each\'', () => { describe('positive:', () => { let pValue, pIndex, pArr, pData; beforeEach(done => { - db.each('SELECT 1 as value', null, (value, index, arr) => { + dbSpec.each('SELECT 1 as value', null, (value, index, arr) => { pValue = value; pIndex = index; pArr = arr; @@ -607,7 +607,7 @@ describe('Method \'each\'', () => { describe('with invalid parameters', () => { let err; beforeEach(done => { - db.each('SELECT 1') + dbSpec.each('SELECT 1') .catch(error => { err = error; }) @@ -622,7 +622,7 @@ describe('Method \'each\'', () => { describe('with error thrown inside the callback', () => { let err; beforeEach(done => { - db.each('SELECT 1', null, () => { + dbSpec.each('SELECT 1', null, () => { throw new Error('Ops!'); }) .catch(error => { @@ -642,7 +642,7 @@ describe('Method \'none\'', () => { it('must resolve with \'null\'', () => { let result, error, finished; - db.none('select * from users where id = $1', 12345678) + dbSpec.none('select * from users where id = $1', 12345678) .then(data => { result = data; }) @@ -663,7 +663,7 @@ describe('Method \'none\'', () => { it('must reject for a single query', () => { let result, error, finished; - db.none('select * from users') + dbSpec.none('select * from users') .then(data => { result = data; }) @@ -685,7 +685,7 @@ describe('Method \'none\'', () => { it('must reject for multi-queries', () => { let result, error, finished; - db.none('select 1; select * from users') + dbSpec.none('select 1; select * from users') .then(data => { result = data; }) @@ -713,7 +713,7 @@ describe('Method \'one\'', () => { it('must resolve with one object', () => { let result, error; - db.one('select 123 as value') + dbSpec.one('select 123 as value') .then(data => { result = data; }) @@ -734,7 +734,7 @@ describe('Method \'one\'', () => { describe('value transformation', () => { let result, context; beforeEach(done => { - db.one('select count(*) from users', null, function (value) { + dbSpec.one('select count(*) from users', null, function (value) { 'use strict'; // NOTE: Outside the strict mode, only objects can be passed in as this context context = this; @@ -756,7 +756,7 @@ describe('Method \'one\'', () => { it('must reject for a single query', () => { let result, error, finished; - db.one('select * from users where id = $1', 12345678) + dbSpec.one('select * from users where id = $1', 12345678) .then(data => { result = data; }) @@ -777,7 +777,7 @@ describe('Method \'one\'', () => { it('must reject for a multi-query', () => { let result, error, finished; - db.one('select 1; select * from users where id = $1', 12345678) + dbSpec.one('select 1; select * from users where id = $1', 12345678) .then(data => { result = data; }) @@ -801,7 +801,7 @@ describe('Method \'one\'', () => { describe('When multiple rows are found', () => { it('must reject for a single query', () => { let result, error, finished; - db.one('select * from users') + dbSpec.one('select * from users') .then(data => { result = data; }) @@ -821,7 +821,7 @@ describe('Method \'one\'', () => { }); it('must reject for a multi-query', () => { let result, error, finished; - db.one('select 1; select * from users') + dbSpec.one('select 1; select * from users') .then(data => { result = data; }) @@ -848,7 +848,7 @@ describe('Method \'oneOrNone\'', () => { it('must resolve with one object when found', () => { let result, error; - db.oneOrNone('select * from users where id = $1', 1) + dbSpec.oneOrNone('select * from users where id = $1', 1) .then(data => { result = data; }) @@ -868,7 +868,7 @@ describe('Method \'oneOrNone\'', () => { it('must resolve with null when no data found', () => { let result, error, finished; - db.oneOrNone('select * from users where id = $1', 12345678) + dbSpec.oneOrNone('select * from users where id = $1', 12345678) .then(data => { result = data; finished = true; @@ -888,7 +888,7 @@ describe('Method \'oneOrNone\'', () => { describe('value transformation', () => { let result, context; beforeEach(done => { - db.oneOrNone('select count(*) from users', null, function (value) { + dbSpec.oneOrNone('select count(*) from users', null, function (value) { 'use strict'; // NOTE: Outside strict mode, only objects can be passed in as this context context = this; @@ -908,7 +908,7 @@ describe('Method \'oneOrNone\'', () => { it('must reject when multiple rows are found', () => { let result, error, finished; - db.oneOrNone('select * from users') + dbSpec.oneOrNone('select * from users') .then(data => { result = data; finished = true; @@ -932,7 +932,7 @@ describe('Method \'many\'', () => { it('must resolve with array of objects', () => { let result, error; - db.many('select * from users') + dbSpec.many('select * from users') .then(data => { result = data; }, reason => { @@ -951,7 +951,7 @@ describe('Method \'many\'', () => { it('must reject when no data found', () => { let result, error, finished; - db.many('select * from users where id = $1', 12345678) + dbSpec.many('select * from users where id = $1', 12345678) .then(data => { result = data; finished = true; @@ -975,7 +975,7 @@ describe('Method \'manyOrNone\'', () => { it('must resolve with array of objects', () => { let result, error; - db.manyOrNone('select * from users') + dbSpec.manyOrNone('select * from users') .then(data => { result = data; }, reason => { @@ -994,7 +994,7 @@ describe('Method \'manyOrNone\'', () => { it('must resolve with an empty array when no data found', () => { let result, error, finished; - db.manyOrNone('select * from users where id = $1', 12345678) + dbSpec.manyOrNone('select * from users where id = $1', 12345678) .then(data => { result = data; finished = true; @@ -1019,7 +1019,7 @@ describe('Executing method query', () => { describe('with invalid query as parameter', () => { let result; beforeEach(done => { - promise.any([db.query(), db.query(''), db.query(' '), db.query({}), db.query(1), db.query(null)]) + promise.any([dbSpec.query(), dbSpec.query(''), dbSpec.query(' '), dbSpec.query({}), dbSpec.query(1), dbSpec.query(null)]) .catch(err => { result = err; }) @@ -1039,7 +1039,7 @@ describe('Executing method query', () => { describe('with invalid qrm as parameter', () => { let result; beforeEach(done => { - promise.any([db.query('something', undefined, ''), db.query('something', undefined, '2'), db.query('something', undefined, -1), db.query('something', undefined, 0), db.query('something', undefined, 100), db.query('something', undefined, NaN), db.query('something', undefined, 1 / 0), db.query('something', undefined, -1 / 0), db.query('something', undefined, 2.45)]) + promise.any([dbSpec.query('something', undefined, ''), dbSpec.query('something', undefined, '2'), dbSpec.query('something', undefined, -1), dbSpec.query('something', undefined, 0), dbSpec.query('something', undefined, 100), dbSpec.query('something', undefined, NaN), dbSpec.query('something', undefined, 1 / 0), dbSpec.query('something', undefined, -1 / 0), dbSpec.query('something', undefined, 2.45)]) .catch(err => { result = err; }) @@ -1070,7 +1070,7 @@ describe('Executing method query', () => { let result; beforeEach(done => { - promise.all([db.query(getQuery1, [], pgp.queryResult.one), db.query(getQuery2, 456, pgp.queryResult.one), db.query(getQuery3, 789, pgp.queryResult.one)]) + promise.all([dbSpec.query(getQuery1, [], pgp.queryResult.one), dbSpec.query(getQuery2, 456, pgp.queryResult.one), dbSpec.query(getQuery3, 789, pgp.queryResult.one)]) .then(data => { result = data; }) @@ -1094,7 +1094,7 @@ describe('Executing method query', () => { query = e.query; params = e.params; }; - db.query(throwError, 123) + dbSpec.query(throwError, 123) .catch(err => { error = err; }) @@ -1121,7 +1121,7 @@ describe('Executing method query', () => { query = e.query; params = e.params; }; - db.query(invalidFunc, 123) + dbSpec.query(invalidFunc, 123) .catch(err => { error = err; }) @@ -1151,7 +1151,7 @@ describe('Transactions', () => { let result, error, context, THIS, tag; beforeEach(done => { - db.tx('complex', function (t) { + dbSpec.tx('complex', function (t) { tag = t.ctx.tag; THIS = this; context = t; @@ -1184,7 +1184,7 @@ describe('Transactions', () => { let error, THIS, context, ctx; beforeEach(done => { options.capSQL = true; - db.tx(function (t) { + dbSpec.tx(function (t) { THIS = this; context = t; return this.batch([this.none('update users set login=$1 where id = $2', ['TestName', 1]), this.tx(function () { @@ -1212,7 +1212,7 @@ describe('Transactions', () => { describe('Detached Transaction', () => { let error; beforeEach(done => { - db.tx(t => { + dbSpec.tx(t => { return t; }) .then(obj => { @@ -1233,7 +1233,7 @@ describe('Transactions', () => { describe('bottom-level failure', () => { let result, nestError, THIS1, THIS2, context1, context2; beforeEach(done => { - db.tx(function (t1) { + dbSpec.tx(function (t1) { THIS1 = this; context1 = t1; return this.batch([this.none('update users set login=$1', 'External'), this.tx(function (t2) { @@ -1245,8 +1245,8 @@ describe('Transactions', () => { }) .then(dummy, reason => { nestError = reason.data[1].result.data[1].result; - return promise.all([db.one('select count(*) from users where login = $1', 'External'), // 0 is expected; - db.one('select count(*) from users where login = $1', 'Internal') // 0 is expected; + return promise.all([dbSpec.one('select count(*) from users where login = $1', 'External'), // 0 is expected; + dbSpec.one('select count(*) from users where login = $1', 'Internal') // 0 is expected; ]); }) .then(data => { @@ -1273,7 +1273,7 @@ describe('Transactions', () => { describe('top-level failure', () => { let result; beforeEach(done => { - db.tx(function () { + dbSpec.tx(function () { return this.batch([this.none(`update users set login=$1 where id = 1`, 'Test'), this.tx(function () { @@ -1286,10 +1286,10 @@ describe('Transactions', () => { }); }) .then(dummy, () => { - return promise.all([db.one(`select count(*) - from users - where login = $1`, 'Test'), // 0 is expected; - db.one(`select count(*) + return promise.all([dbSpec.one(`select count(*) + from users + where login = $1`, 'Test'), // 0 is expected; + dbSpec.one(`select count(*) from person where name = $1`, 'Test') // 0 is expected; ]); @@ -1311,7 +1311,7 @@ describe('Transactions', () => { describe('for a transaction', () => { let error; beforeEach(done => { - db.tx() + dbSpec.tx() .catch(reason => { error = reason; }) @@ -1325,7 +1325,7 @@ describe('Transactions', () => { describe('for a task', () => { let error; beforeEach(done => { - db.task() + dbSpec.task() .catch(reason => { error = reason; }) @@ -1344,7 +1344,7 @@ describe('Transactions', () => { const ctx = []; beforeEach(done => { options.capSQL = true; - db.tx(0, function () { + dbSpec.tx(0, function () { ctx.push(this.ctx); return this.tx(1, function () { ctx.push(this.ctx); @@ -1412,7 +1412,7 @@ describe('Transactions', () => { options.query = e => { query = e.query; }; - db.tx(() => { + dbSpec.tx(() => { throw { code: 'something' }; @@ -1434,7 +1434,7 @@ describe('Transactions', () => { describe('Closing after a protocol violation', () => { let error, value; beforeEach(done => { - db.task(task => task.tx(tx => tx.one('select \'\u0000\'')) + dbSpec.task(task => task.tx(tx => tx.one('select \'\u0000\'')) .then(() => { throw new Error('expected error'); }, () => { @@ -1457,7 +1457,7 @@ describe('Transactions', () => { options.query = e => { query = e.query; }; - db.tx(() => { + dbSpec.tx(() => { throw { code: 'ECONNRESET' }; @@ -1498,7 +1498,7 @@ describe('Transactions', () => { error = reason; }), // Terminate the connections during verify, which causes an 'error' event from the pool promise.delay(500).then(() => { - return db.query(`SELECT pg_terminate_backend(pid) + return dbSpec.query(`SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE pid <> pg_backend_pid();`); })]).then(() => { @@ -1529,7 +1529,7 @@ describe('Conditional Transaction', () => { describe('with default parameters', () => { let firstCtx, secondCtx; beforeEach(done => { - db.txIf(t => { + dbSpec.txIf(t => { firstCtx = t.ctx; return t.txIf(t2 => { secondCtx = t2.ctx; @@ -1547,7 +1547,7 @@ describe('Conditional Transaction', () => { describe('with condition simple override', () => { let firstCtx, secondCtx; beforeEach(done => { - db.txIf({cnd: false}, t => { + dbSpec.txIf({cnd: false}, t => { firstCtx = t.ctx; return t.txIf(t2 => { secondCtx = t2.ctx; @@ -1563,7 +1563,7 @@ describe('Conditional Transaction', () => { describe('with successful condition-function override', () => { let firstCtx, secondCtx; beforeEach(done => { - db.txIf({cnd: () => false}, t => { + dbSpec.txIf({cnd: () => false}, t => { firstCtx = t.ctx; return t.txIf({cnd: a => !a.ctx.inTransaction}, t2 => { secondCtx = t2.ctx; @@ -1584,7 +1584,7 @@ describe('Conditional Transaction', () => { let error; beforeEach(done => { - db.txIf({cnd: errorCondition}, () => { + dbSpec.txIf({cnd: errorCondition}, () => { }) .catch(err => { error = err; @@ -1601,7 +1601,7 @@ describe('Reusable Transaction', () => { describe('as value with default condition', () => { let ctx1, ctx2; beforeEach(done => { - db.tx(t1 => { + dbSpec.tx(t1 => { ctx1 = t1.ctx; return t1.txIf({reusable: true}, t2 => { ctx2 = t2.ctx; @@ -1616,7 +1616,7 @@ describe('Reusable Transaction', () => { describe('as value with true condition', () => { let ctx1, ctx2; beforeEach(done => { - db.tx('first', t1 => { + dbSpec.tx('first', t1 => { ctx1 = t1.ctx; return t1.txIf({tag: 'second', cnd: true, reusable: false}, t2 => { ctx2 = t2.ctx; @@ -1638,7 +1638,7 @@ describe('Reusable Transaction', () => { let ctx1, ctx2; beforeEach(done => { - db.tx(t1 => { + dbSpec.tx(t1 => { ctx1 = t1.ctx; return t1.txIf({reusable: getReusable}, t2 => { ctx2 = t2.ctx; @@ -1657,7 +1657,7 @@ describe('Reusable Transaction', () => { let error; beforeEach(done => { - db.tx(t => { + dbSpec.tx(t => { return t.txIf({reusable: getReusable}, () => { }); }) @@ -1676,7 +1676,7 @@ describe('Conditional Task', () => { describe('with default parameters', () => { let firstCtx, secondCtx; beforeEach(done => { - db.taskIf(t => { + dbSpec.taskIf(t => { firstCtx = t.ctx; return t.taskIf(t2 => { secondCtx = t2.ctx; @@ -1691,7 +1691,7 @@ describe('Conditional Task', () => { describe('with successful condition-function override', () => { let firstCtx, secondCtx; beforeEach(done => { - db.taskIf({cnd: true}, t1 => { + dbSpec.taskIf({cnd: true}, t1 => { firstCtx = t1.ctx; return t1.taskIf({cnd: () => false}, t2 => { secondCtx = t2.ctx; @@ -1711,7 +1711,7 @@ describe('Conditional Task', () => { let error; beforeEach(done => { - db.taskIf({cnd: errorCondition}, () => { + dbSpec.taskIf({cnd: errorCondition}, () => { }) .catch(err => { error = err; @@ -1729,9 +1729,9 @@ describe('Return data from a query must match the request type', () => { describe('when no data returned', () => { let error; beforeEach(done => { - db.none(`select * - from person - where name = $1`, 'John') + dbSpec.none(`select * + from person + where name = $1`, 'John') .catch(err => { error = err; done(); @@ -1745,9 +1745,9 @@ describe('Return data from a query must match the request type', () => { it('method \'one\' must throw an error when there was no data returned', () => { let result, error; - db.one(`select * - from person - where name = $1`, 'Unknown') + dbSpec.one(`select * + from person + where name = $1`, 'Unknown') .then(data => { result = data; }, reason => { @@ -1766,8 +1766,8 @@ describe('Return data from a query must match the request type', () => { it('method \'one\' must throw an error when more than one row was returned', () => { let result, error; - db.one(`select * - from person`) + dbSpec.one(`select * + from person`) .then(data => { result = data; }, reason => { @@ -1786,9 +1786,9 @@ describe('Return data from a query must match the request type', () => { it('method \'oneOrNone\' must resolve into null when no data returned', () => { let result, error; - db.oneOrNone(`select * - from person - where name = $1`, 'Unknown') + dbSpec.oneOrNone(`select * + from person + where name = $1`, 'Unknown') .then(data => { result = data; }, reason => { @@ -1806,9 +1806,9 @@ describe('Return data from a query must match the request type', () => { it('method \'any\' must return an empty array when no records found', () => { let result, error; - db.any(`select * - from person - where name = 'Unknown'`) + dbSpec.any(`select * + from person + where name = 'Unknown'`) .then(data => { result = data; }, reason => { @@ -1830,8 +1830,8 @@ describe('Return data from a query must match the request type', () => { describe('Queries must not allow invalid QRM (Query Request Mask) combinations', () => { it('method \'query\' must throw an error when mask is one+many', () => { let result, error; - db.query(`select * - from person`, undefined, pgp.queryResult.one | pgp.queryResult.many) + dbSpec.query(`select * + from person`, undefined, pgp.queryResult.one | pgp.queryResult.many) .then(data => { result = data; }, reason => { @@ -1849,8 +1849,8 @@ describe('Queries must not allow invalid QRM (Query Request Mask) combinations', }); it('method \'query\' must throw an error when QRM is > 6', () => { let result, error; - db.query(`select * - from person`, undefined, 7) + dbSpec.query(`select * + from person`, undefined, 7) .then(data => { result = data; }, reason => { @@ -1868,8 +1868,8 @@ describe('Queries must not allow invalid QRM (Query Request Mask) combinations', }); it('method \'query\' must throw an error when QRM is < 1', () => { let result, error; - db.query(`select * - from person`, undefined, 0) + dbSpec.query(`select * + from person`, undefined, 0) .then(data => { result = data; }) @@ -1889,8 +1889,8 @@ describe('Queries must not allow invalid QRM (Query Request Mask) combinations', it('method \'query\' must throw an error when QRM is of the wrong type', () => { let result, error; - db.query(`select * - from person`, undefined, 'wrong qrm') + dbSpec.query(`select * + from person`, undefined, 'wrong qrm') .then(data => { result = data; }) @@ -1915,7 +1915,7 @@ describe('Method \'result\'', () => { describe('for a single query', () => { let result; beforeEach(done => { - db.result('select 1 as one') + dbSpec.result('select 1 as one') .then(data => { result = data; done(); @@ -1931,7 +1931,7 @@ describe('Method \'result\'', () => { describe('for a multi-query', () => { let result; beforeEach(done => { - db.result('select 1 as one;select 2 as two') + dbSpec.result('select 1 as one;select 2 as two') .then(data => { result = data; done(); @@ -1951,7 +1951,7 @@ describe('Method \'multiResult\'', () => { describe('for a single query', () => { let result; beforeEach(done => { - db.multiResult('select 1 as one') + dbSpec.multiResult('select 1 as one') .then(data => { result = data; done(); @@ -1967,7 +1967,7 @@ describe('Method \'multiResult\'', () => { describe('for a multi-query', () => { let result; beforeEach(done => { - db.multiResult(`select 1 as one; + dbSpec.multiResult(`select 1 as one; select 2 as two; select * from users @@ -1993,7 +1993,7 @@ describe('Method \'multi\'', () => { describe('for a single query', () => { let result; beforeEach(done => { - db.multi('select 1 as one') + dbSpec.multi('select 1 as one') .then(data => { result = data; done(); @@ -2009,7 +2009,7 @@ describe('Method \'multi\'', () => { describe('for a multi-query', () => { let result; beforeEach(done => { - db.multi(`select 1 as one; + dbSpec.multi(`select 1 as one; select 2 as two; select * from users @@ -2036,7 +2036,7 @@ describe('Querying an entity', () => { let result; beforeEach(done => { options.capSQL = true; - db.func('get_users') + dbSpec.func('get_users') .then(data => { result = data; }) @@ -2054,7 +2054,7 @@ describe('Querying an entity', () => { describe('single-row function', () => { let result; beforeEach(done => { - db.func('findUser', 1, pgp.queryResult.one) + dbSpec.func('findUser', 1, pgp.queryResult.one) .then(data => { result = data; }) @@ -2069,17 +2069,17 @@ describe('Querying an entity', () => { describe('with invalid parameters', () => { let result; beforeEach(done => { - promise.any([db.func(), // undefined function name; - db.func(''), // empty-string function name; - db.func(' '), // white-space string for function name; - db.func(1), // invalid-type function name; - db.func(null), // null function name; + promise.any([dbSpec.func(), // undefined function name; + dbSpec.func(''), // empty-string function name; + dbSpec.func(' '), // white-space string for function name; + dbSpec.func(1), // invalid-type function name; + dbSpec.func(null), // null function name; // query function overrides: - db.query({ + dbSpec.query({ entity: null, type: 'func' - }), db.query({ + }), dbSpec.query({ entity: '', type: 'func' - }), db.query({ + }), dbSpec.query({ entity: ' ', type: 'func' })]) .catch(reason => { @@ -2102,7 +2102,7 @@ describe('Querying an entity', () => { options.error = function (err, e) { errCtx = e; }; - db.proc('camelCase', [() => { + dbSpec.proc('camelCase', [() => { throw new Error('bad proc params'); }]) .catch(reason => { @@ -2127,7 +2127,7 @@ describe('Querying an entity', () => { options.error = function (err, e) { errCtx = e; }; - db.func('camelCase', [() => { + dbSpec.func('camelCase', [() => { throw new Error('bad func params'); }]) .catch(reason => { @@ -2153,7 +2153,7 @@ describe('Querying an entity', () => { errCtx = e; }; options.capSQL = true; - db.proc('camelCase', () => { + dbSpec.proc('camelCase', () => { throw new Error('bad proc name'); }) .catch(reason => { @@ -2176,15 +2176,15 @@ describe('Querying an entity', () => { describe('stored procedures', () => { describe('normal call', () => { it('must resolve with null', async () => { - const res = await db.proc('empty_proc'); + const res = await dbSpec.proc('empty_proc'); expect(res).toBeNull(); }); it('must support output values', async () => { - const res = await db.proc('output_proc', [null, 'world']); + const res = await dbSpec.proc('output_proc', [null, 'world']); expect(res).toEqual({output1: true, output2: 'world-hello!'}); }); it('must support transformation', async () => { - const res = await db.proc('output_proc', [null, 'world'], a => a.output2); + const res = await dbSpec.proc('output_proc', [null, 'world'], a => a.output2); expect(res).toBe('world-hello!'); }); }); @@ -2192,7 +2192,7 @@ describe('Querying an entity', () => { describe('with invalid name', () => { let err; beforeEach(done => { - db.proc() + dbSpec.proc() .catch(e => { err = e.message; done(); @@ -2211,7 +2211,7 @@ describe('Task', () => { describe('with detached connection', () => { let error, tsk; beforeEach(done => { - db.task(async function () { + dbSpec.task(async function () { tsk = this; }) .then(() => { @@ -2236,7 +2236,7 @@ describe('Task', () => { describe('inside a transaction', () => { let context; beforeEach(done => { - db.tx(tx => { + dbSpec.tx(tx => { return tx.task(t => { context = t; }); @@ -2251,7 +2251,7 @@ describe('Task', () => { describe('with a callback that returns nothing', () => { let result; beforeEach(done => { - db.task(dummy) + dbSpec.task(dummy) .then(data => { result = data; }) @@ -2264,7 +2264,7 @@ describe('Task', () => { describe('with a callback that returns a value', () => { it('must resolve with the value', async () => { - const result = await db.task(() => 123); + const result = await dbSpec.task(() => 123); expect(result).toBe(123); }); }); @@ -2272,7 +2272,7 @@ describe('Task', () => { describe('with the callback throwing an error', () => { let result; beforeEach(done => { - db.task(() => { + dbSpec.task(() => { throw new Error('test'); }) .catch(reason => { @@ -2289,7 +2289,7 @@ describe('Task', () => { describe('with a simple promise result', () => { let result, context, THIS; beforeEach(done => { - db.task(async function (t) { + dbSpec.task(async function (t) { THIS = this; context = t; return 'Ok'; @@ -2323,7 +2323,7 @@ describe('Task', () => { return 'success'; } - db.task('testTag', myTask) + dbSpec.task('testTag', myTask) .then(data => { result = data; }) @@ -2347,7 +2347,7 @@ describe('negative query formatting', () => { describe('with invalid property name', () => { let error; beforeEach(done => { - db.one('select ${invalid}', {}) + dbSpec.one('select ${invalid}', {}) .catch(e => { error = e; }) @@ -2362,7 +2362,7 @@ describe('negative query formatting', () => { describe('with invalid variable index', () => { let error; beforeEach(done => { - db.one('select $1', []) + dbSpec.one('select $1', []) .catch(e => { error = e; }) @@ -2378,7 +2378,7 @@ describe('negative query formatting', () => { let error; const err = new Error('ops!'); beforeEach(done => { - db.one('select $1', [() => { + dbSpec.one('select $1', [() => { throw err; }]) .catch(e => { @@ -2396,7 +2396,7 @@ describe('negative query formatting', () => { let error; const err = 'ops!'; beforeEach(done => { - db.one('select $1', [() => { + dbSpec.one('select $1', [() => { throw err; }]) .catch(e => { @@ -2415,7 +2415,7 @@ describe('negative query formatting', () => { describe('Multi-result queries', () => { let result; beforeEach(done => { - db.one('select 1 as one;select 2 as two') + dbSpec.one('select 1 as one;select 2 as two') .then(data => { result = data; })