Skip to content

Commit

Permalink
refactor assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Nov 17, 2022
1 parent 4c4ad33 commit 240c50a
Show file tree
Hide file tree
Showing 15 changed files with 43 additions and 33 deletions.
10 changes: 10 additions & 0 deletions lib/assert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const {assertOptions} = require(`assert-options`);

// this to allow override options-related errors globally (for pg-promise)
global.pgPromiseAssert = assertOptions;

module.exports = {
assert() {
return global.pgPromiseAssert.apply(null, [...arguments]);
}
};
10 changes: 5 additions & 5 deletions lib/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

const {Events} = require(`./events`);
const {assertOptions} = require(`assert-options`);
const {assert} = require(`./assert`);
const {resultQuery, multiResultQuery, streamQuery} = require(`./special-query`);
const {ConnectionContext} = require(`./context`);
const {DatabasePool} = require(`./database-pool`);
Expand Down Expand Up @@ -1289,7 +1289,7 @@ function Database(cn, dc, config) {
*/
obj.task = function () {
const args = npm.pubUtils.taskArgs(arguments);
assertOptions(args.options, [`tag`]);
assert(args.options, [`tag`]);
return taskProcessor.call(this, args, false);
};

Expand Down Expand Up @@ -1347,7 +1347,7 @@ function Database(cn, dc, config) {
*/
obj.taskIf = function () {
const args = npm.pubUtils.taskArgs(arguments);
assertOptions(args.options, [`tag`, `cnd`]);
assert(args.options, [`tag`, `cnd`]);
try {
let cnd = args.options.cnd;
if (`cnd` in args.options) {
Expand Down Expand Up @@ -1456,7 +1456,7 @@ function Database(cn, dc, config) {
*/
obj.tx = function () {
const args = npm.pubUtils.taskArgs(arguments);
assertOptions(args.options, [`tag`, `mode`]);
assert(args.options, [`tag`, `mode`]);
return taskProcessor.call(this, args, true);
};

Expand Down Expand Up @@ -1529,7 +1529,7 @@ function Database(cn, dc, config) {
*/
obj.txIf = function () {
const args = npm.pubUtils.taskArgs(arguments);
assertOptions(args.options, [`tag`, `mode`, `cnd`, `reusable`]);
assert(args.options, [`tag`, `mode`, `cnd`, `reusable`]);
try {
let cnd;
if (`cnd` in args.options) {
Expand Down
6 changes: 3 additions & 3 deletions lib/formatting.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Removal or modification of this copyright notice is prohibited.
*/

const {assertOptions} = require(`assert-options`);
const {assert} = require(`./assert`);

const npm = {
pgUtils: require(`pg/lib/utils`),
Expand Down Expand Up @@ -683,7 +683,7 @@ const $as = {
* @returns {string}
*/
array(arr, options) {
options = assertOptions(options, [`capSQL`]);
options = assert(options, [`capSQL`]);
arr = resolveFunc(arr);
if (isNull(arr)) {
return `null`;
Expand Down Expand Up @@ -850,7 +850,7 @@ const $as = {
* The function will throw an error, if any occurs during formatting.
*/
format(query, values, options) {
options = assertOptions(options, [`capSQL`, `partial`, `def`]);
options = assert(options, [`capSQL`, `partial`, `def`]);
const ctf = getCTF(query);
if (ctf) {
query = ctf.toPostgres.call(query, query);
Expand Down
6 changes: 3 additions & 3 deletions lib/helpers/column-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

const {InnerState} = require(`../inner-state`);
const {assertOptions} = require(`assert-options`);
const {assert} = require(`../assert`);
const {TableName} = require(`./table-name`);
const {Column} = require(`./column`);

Expand Down Expand Up @@ -172,7 +172,7 @@ class ColumnSet extends InnerState {
throw new TypeError(`Invalid parameter 'columns' specified.`);
}

opt = assertOptions(opt, [`table`, `inherit`]);
opt = assert(opt, [`table`, `inherit`]);

if (!npm.utils.isNull(opt.table)) {
this.table = (opt.table instanceof TableName) ? opt.table : new TableName(opt.table);
Expand Down Expand Up @@ -400,7 +400,7 @@ ColumnSet.prototype.assign = function (options) {
*
*/
ColumnSet.prototype.assignColumns = function (options) {
options = assertOptions(options, [`from`, `to`, `skip`]);
options = assert(options, [`from`, `to`, `skip`]);
const skip = (typeof options.skip === `string` && [options.skip]) || ((Array.isArray(options.skip) || typeof options.skip === `function`) && options.skip);
const from = (typeof options.from === `string` && options.from && (npm.formatting.as.alias(options.from) + `.`)) || ``;
const to = (typeof options.to === `string` && options.to && (npm.formatting.as.alias(options.to) + `.`)) || ``;
Expand Down
4 changes: 2 additions & 2 deletions lib/helpers/column.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

const {InnerState} = require(`../inner-state`);
const {assertOptions} = require(`assert-options`);
const {assert} = require(`../assert`);

const npm = {
os: require(`os`),
Expand Down Expand Up @@ -142,7 +142,7 @@ class Column extends InnerState {
this.cnd = info.cnd;
}
} else {
col = assertOptions(col, [`name`, `prop`, `mod`, `cast`, `cnd`, `def`, `init`, `skip`]);
col = assert(col, [`name`, `prop`, `mod`, `cast`, `cnd`, `def`, `init`, `skip`]);
if (`name` in col) {
if (!npm.utils.isText(col.name)) {
throw new TypeError(`Invalid 'name' value: ${npm.utils.toJson(col.name)}. A non-empty string was expected.`);
Expand Down
4 changes: 2 additions & 2 deletions lib/helpers/methods/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Removal or modification of this copyright notice is prohibited.
*/

const {assertOptions} = require(`assert-options`);
const {assert} = require(`../../assert`);
const {TableName} = require(`../table-name`);
const {ColumnSet} = require(`../column-set`);

Expand Down Expand Up @@ -168,7 +168,7 @@ function update(data, columns, table, options, capSQL) {
columns = new ColumnSet(columns || data);
}

options = assertOptions(options, [`tableAlias`, `valueAlias`, `emptyUpdate`]);
options = assert(options, [`tableAlias`, `valueAlias`, `emptyUpdate`]);

const format = npm.formatting.as.format,
useEmptyUpdate = `emptyUpdate` in options,
Expand Down
4 changes: 2 additions & 2 deletions lib/helpers/table-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Removal or modification of this copyright notice is prohibited.
*/

const {assertOptions} = require(`assert-options`);
const {assert} = require(`../assert`);

const npm = {
utils: require(`../utils`),
Expand Down Expand Up @@ -65,7 +65,7 @@ class TableName {
if (typeof table === `string`) {
this.table = table;
} else {
const config = assertOptions(table, [`table`, `schema`]);
const config = assert(table, [`table`, `schema`]);
this.table = config.table;
if (npm.utils.isText(config.schema)) {
this.schema = config.schema;
Expand Down
4 changes: 2 additions & 2 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const {PreparedStatement, ParameterizedQuery} = require(`./types`);
const {QueryFile} = require(`./query-file`);
const {queryResult} = require(`./query-result`);
const {parsePromise} = require(`./promise-parser`);
const {assertOptions} = require(`assert-options`);
const {assert} = require(`./assert`);

const npm = {
path: require(`path`),
Expand Down Expand Up @@ -191,7 +191,7 @@ let originalClientConnect;
*/
function $main(options) {

options = assertOptions(options, [`pgFormatting`, `pgNative`, `promiseLib`, `noLocking`, `capSQL`, `noWarnings`,
options = assert(options, [`pgFormatting`, `pgNative`, `promiseLib`, `noLocking`, `capSQL`, `noWarnings`,
`connect`, `disconnect`, `query`, `receive`, `task`, `transact`, `error`, `extend`, `schema`]);

let pg = npm.pg;
Expand Down
4 changes: 2 additions & 2 deletions lib/promise-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Removal or modification of this copyright notice is prohibited.
*/

const {assertOptions} = require(`assert-options`);
const {assert} = require(`./assert`);

/**
* @class PromiseAdapter
Expand Down Expand Up @@ -58,7 +58,7 @@ class PromiseAdapter {
throw new TypeError(`Adapter requires an api configuration object.`);
}

api = assertOptions(api, [`create`, `resolve`, `reject`, `all`]);
api = assert(api, [`create`, `resolve`, `reject`, `all`]);

this.create = api.create;
this.resolve = api.resolve;
Expand Down
4 changes: 2 additions & 2 deletions lib/query-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

const {InnerState} = require(`./inner-state`);
const {QueryFileError} = require(`./errors`);
const {assertOptions} = require(`assert-options`);
const {assert} = require(`./assert`);
const {ColorConsole} = require(`./utils/color`);

const npm = {
Expand Down Expand Up @@ -106,7 +106,7 @@ class QueryFile extends InnerState {

let filePath = file;

options = assertOptions(options, {
options = assert(options, {
debug: npm.utils.isDev(),
minify: (options && options.compress && options.minify === undefined) ? true : undefined,
compress: undefined,
Expand Down
4 changes: 2 additions & 2 deletions lib/tx-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

const {InnerState} = require(`./inner-state`);
const {addInspection} = require(`./utils`);
const {assertOptions} = require(`assert-options`);
const {assert} = require(`./assert`);

/**
* @enum {number}
Expand Down Expand Up @@ -103,7 +103,7 @@ const isolationLevel = {
class TransactionMode extends InnerState {

constructor(options) {
options = assertOptions(options, [`tiLevel`, `deferrable`, `readOnly`]);
options = assert(options, [`tiLevel`, `deferrable`, `readOnly`]);
const {readOnly, deferrable} = options;
let {tiLevel} = options;
let level, accessMode, deferrableMode, begin = `begin`;
Expand Down
4 changes: 2 additions & 2 deletions lib/types/parameterized-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
const {ServerFormatting} = require(`./server-formatting`);
const {ParameterizedQueryError} = require(`../errors`);
const {QueryFile} = require(`../query-file`);
const {assertOptions} = require(`assert-options`);
const {assert} = require(`../assert`);

const npm = {
EOL: require(`os`).EOL,
Expand Down Expand Up @@ -91,7 +91,7 @@ class ParameterizedQuery extends ServerFormatting {
text: options
};
} else {
options = assertOptions(options, [`text`, `values`, `binary`, `rowMode`]);
options = assert(options, [`text`, `values`, `binary`, `rowMode`]);
}
super(options);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/types/prepared-statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
const {ServerFormatting} = require(`./server-formatting`);
const {PreparedStatementError} = require(`../errors`);
const {QueryFile} = require(`../query-file`);
const {assertOptions} = require(`assert-options`);
const {assert} = require(`../assert`);

const npm = {
EOL: require(`os`).EOL,
Expand Down Expand Up @@ -89,7 +89,7 @@ const npm = {
*/
class PreparedStatement extends ServerFormatting {
constructor(options) {
options = assertOptions(options, [`name`, `text`, `values`, `binary`, `rowMode`, `rows`]);
options = assert(options, [`name`, `text`, `values`, `binary`, `rowMode`, `rows`]);
super(options);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/utils/public.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Removal or modification of this copyright notice is prohibited.
*/

const {assertOptions} = require(`assert-options`);
const {assert} = require(`../assert`);

const npm = {
fs: require(`fs`),
Expand Down Expand Up @@ -187,7 +187,7 @@ function enumSql(dir, options, cb) {
if (!npm.utils.isText(dir)) {
throw new TypeError(`Parameter 'dir' must be a non-empty text string.`);
}
options = assertOptions(options, [`recursive`, `ignoreErrors`]);
options = assert(options, [`recursive`, `ignoreErrors`]);
cb = (typeof cb === `function`) ? cb : null;
return _enumSql(dir, options, cb, ``);
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pg-promise",
"version": "10.14.1",
"version": "10.14.2",
"description": "PostgreSQL interface for Node.js",
"main": "lib/index.js",
"typings": "typescript/pg-promise.d.ts",
Expand Down Expand Up @@ -40,7 +40,7 @@
"node": ">=12.0"
},
"dependencies": {
"assert-options": "0.7.0",
"assert-options": "0.8.0",
"pg": "8.8.0",
"pg-minify": "1.6.2",
"spex": "3.2.0"
Expand Down

0 comments on commit 240c50a

Please sign in to comment.