From 057b09bc9d87676dc010cabc23acb7f360fe869c Mon Sep 17 00:00:00 2001 From: Vitaly Tomilov Date: Sat, 25 May 2024 10:36:26 +0100 Subject: [PATCH] improving _TN function (#933) --- lib/helpers/index.js | 2 +- lib/helpers/table-name.js | 12 ++++++++---- package.json | 2 +- typescript/pg-promise.d.ts | 2 +- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/helpers/index.js b/lib/helpers/index.js index 1919a105..38edd2c4 100644 --- a/lib/helpers/index.js +++ b/lib/helpers/index.js @@ -25,7 +25,7 @@ const method = require('./methods'); * {@link helpers.TableName TableName} class constructor. * * @property {function} _TN - * {@link helpers._TN _TN} Table-Name template tag function. + * {@link helpers._TN _TN} Table-Name conversion function. * * @property {function} ColumnSet * {@link helpers.ColumnSet ColumnSet} class constructor. diff --git a/lib/helpers/table-name.js b/lib/helpers/table-name.js index 3b62e2c0..dbda3502 100644 --- a/lib/helpers/table-name.js +++ b/lib/helpers/table-name.js @@ -139,18 +139,22 @@ npm.utils.addInspection(TableName, function () { /** * @function helpers._TN * @description - * Table-Name template tag function, to convert any `"schema.table"` string + * Table-Name helper 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`}); + * // Using as a regular function: + * const cs1 = new ColumnSet(['id', 'name'], {table: _TN('schema.table')}); + * + * // Using as a template-tag function: + * const cs2 = new ColumnSet(['id', 'name'], {table: _TN`schema.table`}); * * @returns {Table} */ -function _TN(data) { - const [schema, table] = data[0].split('.'); +function _TN(s) { + const [schema, table] = (s.raw ? s[0] : s).split('.'); return table === undefined ? {table: schema} : {schema, table}; } diff --git a/package.json b/package.json index 5df46ef6..a7d8ad0a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pg-promise", - "version": "11.7.0", + "version": "11.7.1", "description": "PostgreSQL interface for Node.js", "main": "lib/index.js", "typings": "typescript/pg-promise.d.ts", diff --git a/typescript/pg-promise.d.ts b/typescript/pg-promise.d.ts index b9cbc7d1..98d51899 100644 --- a/typescript/pg-promise.d.ts +++ b/typescript/pg-promise.d.ts @@ -708,7 +708,7 @@ declare namespace pgPromise { ColumnSet: typeof ColumnSet TableName: typeof TableName - _TN: (data: TemplateStringsArray) => ITable + _TN: (data: TemplateStringsArray | string) => ITable } interface IGenericPromise {