From feccab768e67ed98e51c0576938b9edb30f679f4 Mon Sep 17 00:00:00 2001 From: Mirek Rusin Date: Thu, 22 Aug 2024 13:30:02 +0200 Subject: [PATCH] chore: updating repo (#79) --- .eslintrc | 15 --------------- .gitignore | 7 ++++--- .npmignore | 5 ++--- Makefile | 10 ++++++---- eslint.config.mjs | 1 + package.json | 28 ++++++++++++---------------- src/assign-object.test.ts | 1 + src/assign.test.ts | 1 + src/defined.ts | 2 ++ src/demargin.test.ts | 1 + src/demargin.ts | 12 ++++++------ src/identifier.test.ts | 1 + src/identifier.ts | 2 +- src/in.test.ts | 7 ++++--- src/in.ts | 2 +- src/inline-table-of-column.ts | 2 +- src/inline-table-of-objects.test.ts | 1 + src/insert-not-matched.ts | 2 +- src/insert-object.test.ts | 1 + src/insert-object.ts | 2 +- src/insert-objects.test.ts | 9 +++++---- src/insert-objects.ts | 2 +- src/interpolate.test.ts | 1 + src/interpolate.ts | 8 ++++---- src/interpolate1.ts | 2 +- src/is-null.test.ts | 1 + src/json-modify.test.ts | 1 + src/json-path.test.ts | 1 + src/json-path.ts | 2 +- src/keys-of-objects.ts | 2 +- src/keywords.ts | 2 +- src/lines.test.ts | 7 ++++--- src/margin-of-first-line.test.ts | 1 + src/margin-of-last-line.test.ts | 1 + src/margin.test.ts | 1 + src/merge-1n.ts | 2 +- src/merge1n.test.ts | 1 + src/modify-jsons.test.ts | 1 + src/modify-jsons.ts | 2 ++ src/not-in.ts | 2 +- src/order-by.test.ts | 1 + src/order-by.ts | 2 +- src/random-identifier.ts | 6 ++---- src/select.ts | 4 ++-- src/sql.test.ts | 7 ++++--- src/star.ts | 2 +- src/table-hint-limited.ts | 2 +- src/test/{sql.ts => client.ts} | 18 +++++++----------- src/undefined-propagation.test.ts | 5 +++-- src/update-object.test.ts | 1 + src/update-objects.ts | 2 +- src/update.test.ts | 1 + src/upsert-objects.ts | 2 +- src/where.test.ts | 1 + src/where.ts | 10 +++++----- tsconfig.cjs.json | 12 +++++++++--- tsconfig.json | 28 +++++++++------------------- tsconfig.mjs.json | 12 ++++++++++++ 58 files changed, 141 insertions(+), 127 deletions(-) delete mode 100644 .eslintrc create mode 100644 eslint.config.mjs rename src/test/{sql.ts => client.ts} (90%) create mode 100644 tsconfig.mjs.json diff --git a/.eslintrc b/.eslintrc deleted file mode 100644 index 16931f1..0000000 --- a/.eslintrc +++ /dev/null @@ -1,15 +0,0 @@ -{ - "root": true, - "parser": "@typescript-eslint/parser", - "plugins": [ - "@typescript-eslint" - ], - "extends": [ - "plugin:@typescript-eslint/recommended", - "@appliedblockchain" - ], - "rules": { - "no-unused-vars": "off", - "@typescript-eslint/no-non-null-assertion": "off" - } -} diff --git a/.gitignore b/.gitignore index 7f4a63d..ebca015 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ -cjs -mjs -node_modules .vscode/configurationCache.log .vscode/dryrun.log .vscode/targets.log +cjs +coverage +mjs +node_modules diff --git a/.npmignore b/.npmignore index 85c3b6f..8bcd23e 100644 --- a/.npmignore +++ b/.npmignore @@ -1,9 +1,8 @@ .github .vscode -*.sh *.test.* jest.* Makefile -src test -tsconfig.json +tsconfig.* +wip diff --git a/Makefile b/Makefile index 05182cf..b32f6ba 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,7 @@ test: - pnpm exec jest + pnpm tsc + pnpm eslint src + pnpm jest --coverage clean: rm -Rf cjs mjs test/*.js @@ -11,7 +13,7 @@ build-cjs: build-mjs: rm -Rf mjs - pnpm exec tsc -d --sourceMap --outDir mjs + pnpm exec tsc -p tsconfig.mjs.json build: build-cjs build-mjs @@ -20,11 +22,11 @@ rebuild: clean build update: pnpm up --latest -preversion: rebuild test +preversion: test rebuild postversion: git push git push --tags - pnpm publish + pnpm publish --access public .PHONY: test diff --git a/eslint.config.mjs b/eslint.config.mjs new file mode 100644 index 0000000..fedac59 --- /dev/null +++ b/eslint.config.mjs @@ -0,0 +1 @@ +export { default } from '@prelude/eslint-config' diff --git a/package.json b/package.json index 2c9c1b7..9e82905 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ }, "scripts": { "test": "make test", + "build": "make build", "preversion": "make preversion", "postversion": "make postversion" }, @@ -33,21 +34,16 @@ "author": "Mirek Rusin ", "license": "MIT", "devDependencies": { - "@appliedblockchain/eslint-config": "3.1.1", - "@babel/core": "7.22.1", - "@swc/core": "1.3.62", - "@swc/jest": "0.2.26", - "@tsconfig/node18": "2.0.1", - "@types/debug": "4.1.8", - "@types/jest": "29.5.2", - "@types/node": "20.2.5", - "@types/tedious": "4.0.9", - "@typescript-eslint/eslint-plugin": "5.59.8", - "@typescript-eslint/parser": "5.59.8", - "debug": "4.3.4", - "eslint": "8.42.0", - "jest": "29.5.0", - "tedious": "16.1.0", - "typescript": "5.1.3" + "@jest/globals": "^29.7.0", + "@prelude/eslint-config": "0.0.1", + "@swc/core": "1.7.14", + "@swc/jest": "0.2.36", + "@tsconfig/node20": "^20.1.4", + "@types/node": "22.5.0", + "@types/tedious": "4.0.14", + "eslint": "9.9.0", + "jest": "29.7.0", + "tedious": "19.0.0", + "typescript": "5.5.4" } } diff --git a/src/assign-object.test.ts b/src/assign-object.test.ts index 20a8ab3..af82290 100644 --- a/src/assign-object.test.ts +++ b/src/assign-object.test.ts @@ -1,3 +1,4 @@ +import { test, expect } from '@jest/globals' import * as Tsql from './index.js' test('assigns auto values', () => { diff --git a/src/assign.test.ts b/src/assign.test.ts index ca79ffd..40cbaac 100644 --- a/src/assign.test.ts +++ b/src/assign.test.ts @@ -1,3 +1,4 @@ +import { test, expect } from '@jest/globals' import * as Tsql from './index.js' test('assigns auto values', () => { diff --git a/src/defined.ts b/src/defined.ts index 44fef24..f458074 100644 --- a/src/defined.ts +++ b/src/defined.ts @@ -4,6 +4,8 @@ const defined = const object_ = Object.assign({}, object) for (const key in object) { if (typeof object_[key] === 'undefined') { + + // eslint-disable-next-line @typescript-eslint/no-dynamic-delete delete object_[key] } } diff --git a/src/demargin.test.ts b/src/demargin.test.ts index cdeaed1..064907a 100644 --- a/src/demargin.test.ts +++ b/src/demargin.test.ts @@ -1,3 +1,4 @@ +import { test, expect } from '@jest/globals' import * as Tsql from './index.js' test('simple', () => { diff --git a/src/demargin.ts b/src/demargin.ts index 3a48a74..d205a5b 100644 --- a/src/demargin.ts +++ b/src/demargin.ts @@ -7,17 +7,17 @@ export const demargin = throw new TypeError(`Expected value to be string, got ${value}.`) } const lines = value.split('\n') - if (lines.length && lines[0].split('').every(_ => _ === ' ')) { + if ((lines.length > 0) && lines[0].split('').every(_ => _ === ' ')) { lines.shift() } - if (lines.length && lines[lines.length - 1].split('').every(_ => _ === ' ')) { + if ((lines.length > 0) && lines[lines.length - 1].split('').every(_ => _ === ' ')) { lines.pop() } let margin = Infinity - for (let i = 0; i < lines.length; i++) { - for (let j = 0; j < lines[i].length; j++) { - if (lines[i][j] !== ' ') { - margin = Math.min(margin, j) + for (const line of lines) { + for (let i = 0; i < line.length; i++) { + if (line[i] !== ' ') { + margin = Math.min(margin, i) break } } diff --git a/src/identifier.test.ts b/src/identifier.test.ts index d0c69da..028e612 100644 --- a/src/identifier.test.ts +++ b/src/identifier.test.ts @@ -1,3 +1,4 @@ +import { test, expect } from '@jest/globals' import * as Tsql from './index.js' test('basic', () => { diff --git a/src/identifier.ts b/src/identifier.ts index b248a3b..1961675 100644 --- a/src/identifier.ts +++ b/src/identifier.ts @@ -11,7 +11,7 @@ export type Identifier = export function isPlain(value: string) { return ( !keywords[value.toLowerCase()] && - !!String(value).match(/^[a-z_@#][a-z0-9_]*$/i) + !!String(value).match(/^[a-z_@#]\w*$/i) ) } diff --git a/src/in.test.ts b/src/in.test.ts index a44742a..65e806a 100644 --- a/src/in.test.ts +++ b/src/in.test.ts @@ -1,9 +1,10 @@ +import { test, expect } from '@jest/globals' import * as Tsql from './index.js' test('in', () => { - expect(Tsql.in('foo', [ 1 ])!.toString()).toEqual('foo in (1)') - expect(Tsql.in('foo', [])!.toString()).toEqual('0=1') - expect(Tsql.in('foo', null)!.toString()).toEqual('0=1') + expect(String(Tsql.in('foo', [ 1 ]))).toEqual('foo in (1)') + expect(String(Tsql.in('foo', []))).toEqual('0=1') + expect(String(Tsql.in('foo', null))).toEqual('0=1') expect(typeof Tsql.in('foo', undefined)).toBe('undefined') expect( Tsql.and( diff --git a/src/in.ts b/src/in.ts index 270b1af..ae9950f 100644 --- a/src/in.ts +++ b/src/in.ts @@ -17,7 +17,7 @@ export const in_ = (l: S | string, r: undefined | null | unknown[]): undefined | S => typeof r === 'undefined' ? undefined : - r && !isNull(r) && r.length ? + r && !isNull(r) && (r.length > 0) ? tsql`${fallback(l, id)} in ${row(r)}` : logicalFalse diff --git a/src/inline-table-of-column.ts b/src/inline-table-of-column.ts index 05996cc..b7ae514 100644 --- a/src/inline-table-of-column.ts +++ b/src/inline-table-of-column.ts @@ -12,7 +12,7 @@ import tsql from './template.js' */ export const inlineTableOfColumn = (table: Sid | string, column: Sid | string, values: unknown[]): S => { - if (!values.length) { + if (values.length === 0) { throw new Error(`Can't generate literal table ${table} using column ${column} with an empty array of values.`) } const table_ = id(table) diff --git a/src/inline-table-of-objects.test.ts b/src/inline-table-of-objects.test.ts index 4085389..ec763b3 100644 --- a/src/inline-table-of-objects.test.ts +++ b/src/inline-table-of-objects.test.ts @@ -1,3 +1,4 @@ +import { test, expect } from '@jest/globals' import * as Tsql from './index.js' test('literalTableOfObjects', () => { diff --git a/src/insert-not-matched.ts b/src/insert-not-matched.ts index 523bdae..5afbb5a 100644 --- a/src/insert-not-matched.ts +++ b/src/insert-not-matched.ts @@ -42,7 +42,7 @@ export const insertNotMatched = throw new TypeError(`Expected array of values, got ${objects}.`) } - if (!objects.length) { + if (objects.length === 0) { return tsql`select 0;` } diff --git a/src/insert-object.test.ts b/src/insert-object.test.ts index 06c9442..c389877 100644 --- a/src/insert-object.test.ts +++ b/src/insert-object.test.ts @@ -1,3 +1,4 @@ +import { test, expect } from '@jest/globals' import * as Tsql from './index.js' test('insert-object', () => { diff --git a/src/insert-object.ts b/src/insert-object.ts index f4be11c..1f3a0a8 100644 --- a/src/insert-object.ts +++ b/src/insert-object.ts @@ -17,7 +17,7 @@ export const insertObject = (table: Sid | string, object: Record, output?: S): S => { const object_ = defined(object) const keys = Object.keys(object_) - if (!keys.length) { + if (keys.length === 0) { throw new TypeError(`Expected object with keys, got ${object_}.`) } const table_ = id(table) diff --git a/src/insert-objects.test.ts b/src/insert-objects.test.ts index 2ed7af5..2217b02 100644 --- a/src/insert-objects.test.ts +++ b/src/insert-objects.test.ts @@ -1,9 +1,10 @@ -import Sql from './test/sql.js' +import { beforeAll, afterAll, afterEach, test, expect } from '@jest/globals' +import Client from './test/client.js' -let sql: Sql +let sql: Client beforeAll(async () => { - sql = await Sql.random() + sql = await Client.random() }, 30 * 1000) afterAll(async () => { @@ -44,7 +45,7 @@ test.each([ 1000, 1001, 2000 -])('insert %i records', async (length) => { +])('insert %i records', async length => { const users = Array.from({ length }, (_, i) => ({ name: `user-${i}` })) await sql.insertObjects('Users', users) await expect(sql.count('Users')).resolves.toBe(length) diff --git a/src/insert-objects.ts b/src/insert-objects.ts index a396af7..43e4786 100644 --- a/src/insert-objects.ts +++ b/src/insert-objects.ts @@ -26,7 +26,7 @@ export const insertObjects = throw new TypeError(`Expected array of values, got ${objects}.`) } - if (!objects.length) { + if (objects.length === 0) { return tsql`select 0;` } diff --git a/src/interpolate.test.ts b/src/interpolate.test.ts index 67d8b78..e22516d 100644 --- a/src/interpolate.test.ts +++ b/src/interpolate.test.ts @@ -1,3 +1,4 @@ +import { test, expect } from '@jest/globals' import * as Tsql from './index.js' test('simple', () => { diff --git a/src/interpolate.ts b/src/interpolate.ts index 0c7dae6..4ccd7e7 100644 --- a/src/interpolate.ts +++ b/src/interpolate.ts @@ -6,7 +6,7 @@ type Margin = { open: boolean } -function updateMargin(margin: Margin, value: string) { +function updateMargin(mutableMargin: Margin, value: string) { // Locate start of last line. const start = value.lastIndexOf('\n') + 1 @@ -27,8 +27,8 @@ function updateMargin(margin: Margin, value: string) { const blanks = end === value.length // Update margin. - margin.column = multiline ? column : margin.column + (margin.open ? column : 0) - margin.open = (margin.open || multiline) && blanks + mutableMargin.column = multiline ? column : mutableMargin.column + (mutableMargin.open ? column : 0) + mutableMargin.open = (mutableMargin.open || multiline) && blanks } /** @@ -40,7 +40,7 @@ export const interpolate = if (!Array.isArray(xs) || !Array.isArray(ys)) { throw new TypeError(`Expected xs and ys to be an array, got ${xs} and ${ys}.`) } - if (!xs.length && !ys.length) { + if ((xs.length === 0) && (ys.length === 0)) { return [] } if (xs.length - 1 !== ys.length) { diff --git a/src/interpolate1.ts b/src/interpolate1.ts index a3e0018..ea5de92 100644 --- a/src/interpolate1.ts +++ b/src/interpolate1.ts @@ -4,7 +4,7 @@ export const interpolate1 = if (!Array.isArray(xs)) { throw new TypeError(`Expected xs to be an array, got ${xs}.`) } - if (!xs.length) { + if (xs.length === 0) { return [] } const rs: (T | I)[] = [ xs[0] ] diff --git a/src/is-null.test.ts b/src/is-null.test.ts index 66681a1..1fdcf25 100644 --- a/src/is-null.test.ts +++ b/src/is-null.test.ts @@ -1,3 +1,4 @@ +import { test, expect } from '@jest/globals' import * as Tsql from './index.js' test('is-null', () => { diff --git a/src/json-modify.test.ts b/src/json-modify.test.ts index 5e0072d..ef2d989 100644 --- a/src/json-modify.test.ts +++ b/src/json-modify.test.ts @@ -1,3 +1,4 @@ +import { test, expect } from '@jest/globals' import * as Tsql from './index.js' test('jsonModify', () => { diff --git a/src/json-path.test.ts b/src/json-path.test.ts index 82ef0b9..3ab199c 100644 --- a/src/json-path.test.ts +++ b/src/json-path.test.ts @@ -1,3 +1,4 @@ +import { test, expect } from '@jest/globals' import * as Tsql from './index.js' test('jsonPath', () => { diff --git a/src/json-path.ts b/src/json-path.ts index 7186781..4d19f1e 100644 --- a/src/json-path.ts +++ b/src/json-path.ts @@ -14,7 +14,7 @@ export const jsonPath = if (mode) { parts.push(mode) } - if (parts.length) { + if (parts.length > 0) { return `${parts.join(' ')} $.${path}` } return `$.${path}` diff --git a/src/keys-of-objects.ts b/src/keys-of-objects.ts index 8f78047..3a8ecca 100644 --- a/src/keys-of-objects.ts +++ b/src/keys-of-objects.ts @@ -13,7 +13,7 @@ export const keysOfObjects = if (!Array.isArray(objects)) { throw new TypeError(`Expected array, got ${objects}.`) } - if (!objects.length) { + if (objects.length === 0) { throw new TypeError('Expected non empty array.') } const keys = Object.keys(objects[0]).sort() diff --git a/src/keywords.ts b/src/keywords.ts index d6a1018..97a150c 100644 --- a/src/keywords.ts +++ b/src/keywords.ts @@ -973,7 +973,7 @@ const keywords_ = [ ] /** An array of MSSQL keywords. */ -const keywords: { [keyword: string]: undefined | true } = +const keywords: Record = keywords_.reduce((r, _) => ({ ...r, [_]: true }), {}) export default keywords diff --git a/src/lines.test.ts b/src/lines.test.ts index 433d453..2d5bff9 100644 --- a/src/lines.test.ts +++ b/src/lines.test.ts @@ -1,8 +1,9 @@ +import { test, expect } from '@jest/globals' import * as Tsql from './index.js' test('simple', () => { expect(() => Tsql.lines([])).toThrow('Expected at least one line.') - expect(() => Tsql.lines([undefined, undefined])).toThrow('Expected at least one line.') - expect(Tsql.lines(['foo', 'bar'])?.toString()).toEqual(`N'foo'\nN'bar'`) - expect(Tsql.lines(['foo', undefined, 'bar'])?.toString()).toEqual(`N'foo'\nN'bar'`) + expect(() => Tsql.lines([ undefined, undefined ])).toThrow('Expected at least one line.') + expect(Tsql.lines([ 'foo', 'bar' ])?.toString()).toEqual('N\'foo\'\nN\'bar\'') + expect(Tsql.lines([ 'foo', undefined, 'bar' ])?.toString()).toEqual('N\'foo\'\nN\'bar\'') }) diff --git a/src/margin-of-first-line.test.ts b/src/margin-of-first-line.test.ts index 66ebfec..72821ca 100644 --- a/src/margin-of-first-line.test.ts +++ b/src/margin-of-first-line.test.ts @@ -1,3 +1,4 @@ +import { test, expect } from '@jest/globals' import marginOfFirstLine from './margin-of-first-line.js' test('marginOfFirstLine', () => { diff --git a/src/margin-of-last-line.test.ts b/src/margin-of-last-line.test.ts index c10e906..59f408c 100644 --- a/src/margin-of-last-line.test.ts +++ b/src/margin-of-last-line.test.ts @@ -1,3 +1,4 @@ +import { test, expect } from '@jest/globals' import marginOfLastLine from './margin-of-last-line.js' test('marginOfLastLine', () => { diff --git a/src/margin.test.ts b/src/margin.test.ts index 9f6ad28..c0e7106 100644 --- a/src/margin.test.ts +++ b/src/margin.test.ts @@ -1,3 +1,4 @@ +import { test, expect } from '@jest/globals' import * as Tsql from './index.js' test('margin', () => { diff --git a/src/merge-1n.ts b/src/merge-1n.ts index f4b461f..2587a83 100644 --- a/src/merge-1n.ts +++ b/src/merge-1n.ts @@ -24,7 +24,7 @@ export const merge1n = if (!Array.isArray(values)) { throw new TypeError(`Expected array of values, got ${values}.`) } - if (!values.length) { + if (values.length === 0) { return tsql`delete from ${table_} where ${lcolumn_} = ${lid};` } return tsql` diff --git a/src/merge1n.test.ts b/src/merge1n.test.ts index 7ffc7e1..ed678ba 100644 --- a/src/merge1n.test.ts +++ b/src/merge1n.test.ts @@ -1,3 +1,4 @@ +import { test, expect } from '@jest/globals' import * as Tsql from './index.js' test('merge1n', () => { diff --git a/src/modify-jsons.test.ts b/src/modify-jsons.test.ts index 181080b..f2d5a95 100644 --- a/src/modify-jsons.test.ts +++ b/src/modify-jsons.test.ts @@ -1,3 +1,4 @@ +import { test, expect } from '@jest/globals' import * as Tsql from './index.js' test('modify-jsons', () => { diff --git a/src/modify-jsons.ts b/src/modify-jsons.ts index c966103..f4613e2 100644 --- a/src/modify-jsons.ts +++ b/src/modify-jsons.ts @@ -24,6 +24,8 @@ export const modify = (column: string, jsonKeys: string[]): S => assign( targetPrefixed(column), + + // eslint-disable-next-line sonarjs/no-nested-template-literals jsonKeys.reduce((_, jsonKey) => tsql`json_modify(${_}, ${`$.${jsonKey}`}, json_query(${sourcePrefixed(column)}, ${`$.${jsonKey}`}))`, targetPrefixed(column) as S) ) as S diff --git a/src/not-in.ts b/src/not-in.ts index 99b4beb..29c2b4a 100644 --- a/src/not-in.ts +++ b/src/not-in.ts @@ -9,7 +9,7 @@ export const notIn = (l: S | string, r: undefined | null | unknown[]): undefined | S => typeof r === 'undefined' ? undefined : - r && !isNull(r) && r.length ? + r && !isNull(r) && (r.length > 0) ? tsql`${fallback(l, id)} not in ${row(r)}` : tsql`${fallback(l, id)} is not null` diff --git a/src/order-by.test.ts b/src/order-by.test.ts index efff24c..783388c 100644 --- a/src/order-by.test.ts +++ b/src/order-by.test.ts @@ -1,3 +1,4 @@ +import { test, expect } from '@jest/globals' import * as Tsql from './index.js' test('order-by', () => { diff --git a/src/order-by.ts b/src/order-by.ts index b936727..9706a40 100644 --- a/src/order-by.ts +++ b/src/order-by.ts @@ -4,6 +4,6 @@ import raw from './raw.js' const orderBy = (terms: [identifier: undefined | Identifier, direction: 'asc' | 'desc'][]) => - list(terms.map(_ => _ && _[0] ? raw(`${id(_[0])} ${_[1]}`) : undefined)) + list(terms.map(_ => _?.[0] != null ? raw(`${id(_[0])} ${_[1]}`) : undefined)) export default orderBy diff --git a/src/random-identifier.ts b/src/random-identifier.ts index 9607161..067f469 100644 --- a/src/random-identifier.ts +++ b/src/random-identifier.ts @@ -5,10 +5,8 @@ const alphabet = 'abcdefghijklmnopqrstuvwxyABCDEFGHIJKLMNOPQRSTUVWXYZ' const randomLetter = (): string => { - while (true) { - const index = Math.min(alphabet.length - 1, Math.floor(Math.random() * alphabet.length)) - return alphabet[index] - } + const index = Math.min(alphabet.length - 1, Math.floor(Math.random() * alphabet.length)) + return alphabet[index] } const randomIdentifier = diff --git a/src/select.ts b/src/select.ts index f035c08..b77e885 100644 --- a/src/select.ts +++ b/src/select.ts @@ -16,11 +16,11 @@ export const select = if (typeof what_ === 'undefined') { throw new Error('Empty projection.') } - const from_ = from ? id(from) : undefined + const from_ = from != null ? id(from) : undefined return line( tsql`select ${what_}`, tsql`from ${from_}`, - where ? tsql`where ${where}` : undefined + where != null ? tsql`where ${where}` : undefined ) } diff --git a/src/sql.test.ts b/src/sql.test.ts index bf52ddb..2857237 100644 --- a/src/sql.test.ts +++ b/src/sql.test.ts @@ -1,10 +1,11 @@ +import { beforeEach, describe, test, expect, afterAll, beforeAll } from '@jest/globals' import * as Tsql from './index.js' -import Sql from './test/sql.js' +import Client from './test/client.js' -let sql: Sql +let sql: Client beforeAll(async () => { - sql = await Sql.random() + sql = await Client.random() }, 30 * 1000) afterAll(async () => { diff --git a/src/star.ts b/src/star.ts index b2abe82..b5912c8 100644 --- a/src/star.ts +++ b/src/star.ts @@ -5,7 +5,7 @@ import tsql from './template.js' export const star = (table?: Sid | string): S => - table ? + table != null ? tsql`${id(table)}.*` : tsql`*` diff --git a/src/table-hint-limited.ts b/src/table-hint-limited.ts index a7c1d5a..eb3d227 100644 --- a/src/table-hint-limited.ts +++ b/src/table-hint-limited.ts @@ -24,7 +24,7 @@ export type TableHintLimited = export type t = TableHintLimited -export const values: Set = new Set([ +export const values = new Set([ 'keepidentity', 'keepdefaults', 'holdlock', diff --git a/src/test/sql.ts b/src/test/client.ts similarity index 90% rename from src/test/sql.ts rename to src/test/client.ts index 6b8a31d..2922fbc 100644 --- a/src/test/sql.ts +++ b/src/test/client.ts @@ -1,11 +1,8 @@ import { Connection, Request } from 'tedious' import * as Tsql from '../index.js' -import debugOf from 'debug' import randomIdentifier from '../random-identifier.js' -const debug = debugOf('sql') - -export default class Sql { +export default class Client { connection: Connection @@ -32,13 +29,13 @@ export default class Sql { } static async createDatabase(database: string) { - const sql = await new Sql({ database: 'master' }).connect() + const sql = await new Client({ database: 'master' }).connect() await sql.rows`create database ${Tsql.id(database)}` sql.close() } static async dropDatabase(database: string) { - const sql = await new Sql({ database: 'master' }).connect() + const sql = await new Client({ database: 'master' }).connect() await sql.rows`drop database ${Tsql.id(database)}` sql.close() } @@ -46,13 +43,13 @@ export default class Sql { static async random() { const database = randomIdentifier('test_', 16).toString() await this.createDatabase(database) - return new Sql({ database }).connect() + return new Client({ database }).connect() } async connect(): Promise { return new Promise((resolve, reject) => { - this.connection.connect(err => { - err ? reject(err) : resolve(this) + this.connection.connect((err: unknown) => { + err != null ? reject(err) : resolve(this) }) }) } @@ -68,9 +65,8 @@ export default class Sql { return } const sqlString = sql.toString() - debug('query', sqlString) const request = new Request(sqlString, (err, _rowCount, rows) => { - if (err) { + if (err != null) { reject(new Error(err.message + `; ${sqlString}`)) } for (const row of rows) { diff --git a/src/undefined-propagation.test.ts b/src/undefined-propagation.test.ts index 21defb2..2bae77f 100644 --- a/src/undefined-propagation.test.ts +++ b/src/undefined-propagation.test.ts @@ -1,3 +1,4 @@ +import { test, expect } from '@jest/globals' import * as Tsql from './index.js' test('or', () => { @@ -58,8 +59,8 @@ test('assign object with undefined propagation', () => { test('assign', () => { expect(Tsql.assign('a', undefined)).toBe(undefined) - expect(Tsql.assign('a', null)!.toString()).toBe('a = null') - expect(Tsql.assign('a', 1)!.toString()).toBe('a = 1') + expect(String(Tsql.assign('a', null))).toBe('a = null') + expect(String(Tsql.assign('a', 1))).toBe('a = 1') }) test('list', () => { diff --git a/src/update-object.test.ts b/src/update-object.test.ts index 2beb1e8..d603821 100644 --- a/src/update-object.test.ts +++ b/src/update-object.test.ts @@ -1,3 +1,4 @@ +import { test, expect } from '@jest/globals' import * as Tsql from './index.js' test('update', () => { diff --git a/src/update-objects.ts b/src/update-objects.ts index 5ca254d..d25dfd1 100644 --- a/src/update-objects.ts +++ b/src/update-objects.ts @@ -36,7 +36,7 @@ export const updateObjects = throw new TypeError(`Expected array of values, got ${objects}.`) } - if (!objects.length) { + if (objects.length === 0) { return tsql`select 0;` } diff --git a/src/update.test.ts b/src/update.test.ts index 4c9211f..75fbf55 100644 --- a/src/update.test.ts +++ b/src/update.test.ts @@ -1,3 +1,4 @@ +import { test, expect } from '@jest/globals' import * as Tsql from './index.js' test('update', () => { diff --git a/src/upsert-objects.ts b/src/upsert-objects.ts index 01a01fe..8f1a9ef 100644 --- a/src/upsert-objects.ts +++ b/src/upsert-objects.ts @@ -44,7 +44,7 @@ export const upsertObjects = throw new TypeError(`Expected array of values, got ${objects}.`) } - if (!objects.length) { + if (objects.length === 0) { return tsql`select 0;` } diff --git a/src/where.test.ts b/src/where.test.ts index 30c8ff1..451758c 100644 --- a/src/where.test.ts +++ b/src/where.test.ts @@ -1,3 +1,4 @@ +import { test, expect } from '@jest/globals' import * as Tsql from './index.js' const t = diff --git a/src/where.ts b/src/where.ts index 4e9ec97..cf77f3f 100644 --- a/src/where.ts +++ b/src/where.ts @@ -126,14 +126,14 @@ export function visit(value: unknown): S { const key = single(value) if (key) { const value_ = (value as Record)[key] - if (key && (key in logical) && Array.isArray(value_)) { + if ((key in logical) && Array.isArray(value_)) { if (typeof logical[key as keyof Logical] !== 'function') { console.log('ERROR', key, Object.keys(logical), logical[key as keyof Logical], logical) } return logical[key as keyof Logical](...value_.map(visit)) } - if (key && (key in unary)) { - return unary[key as keyof Unary](visit(value_))! + if ((key in unary)) { + return unary[key as keyof Unary](visit(value_)) as S } } if (isObject(value)) { @@ -163,7 +163,7 @@ export function where(value: Where): S { */ export function and(...xs: (undefined | Where)[]): S { const xs_ = xs.filter(isDefined) - if (!xs_.length) { + if (xs_.length === 0) { return logicalTrue } return template`(${raw(interpolate1(unique(xs_.map(where)), raw(' and ')).join(''))})` @@ -178,7 +178,7 @@ export function and(...xs: (undefined | Where)[]): S { */ export function or(...xs: (undefined | Where)[]): S { const xs_ = xs.filter(isDefined) - if (!xs_.length) { + if (xs_.length === 0) { return logicalFalse } return template`(${raw(interpolate1(unique(xs_.map(where)), raw(' or ')).join(''))})` diff --git a/tsconfig.cjs.json b/tsconfig.cjs.json index 5bdb3b4..9c3c6f8 100644 --- a/tsconfig.cjs.json +++ b/tsconfig.cjs.json @@ -2,7 +2,13 @@ "extends": "./tsconfig.json", "compilerOptions": { "module": "CommonJS", - "verbatimModuleSyntax": false, - "outDir": "cjs" - } + "moduleResolution": "Node", + "outDir": "cjs", + "noEmit": false, + "declaration": true, + "sourceMap": true + }, + "include": [ + "src" + ] } diff --git a/tsconfig.json b/tsconfig.json index c378230..88d1b92 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,26 +1,16 @@ { - "extends": "@tsconfig/node18/tsconfig.json", + "extends": "@tsconfig/node20/tsconfig.json", "compilerOptions": { - "target": "ES2022", - "lib": [ - "ES2022" - ], + "lib": ["ESNext"], "module": "NodeNext", - "moduleResolution": "nodenext", - "resolveJsonModule": true, - "verbatimModuleSyntax": true, - "strict": true, - "noFallthroughCasesInSwitch": true, - "noUnusedLocals": true, - "noUnusedParameters": true, - "noImplicitReturns": true, - "esModuleInterop": true, - "noImplicitAny": true + "target": "ESNext", + "moduleResolution": "NodeNext", + "noImplicitAny": false, + "noEmit": true }, "include": [ - "src" - ], - "exclude": [ - "node_modules" + "eslint.config.mjs", + "src", + "wip" ] } diff --git a/tsconfig.mjs.json b/tsconfig.mjs.json new file mode 100644 index 0000000..95eebb7 --- /dev/null +++ b/tsconfig.mjs.json @@ -0,0 +1,12 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "mjs", + "noEmit": false, + "declaration": true, + "sourceMap": true + }, + "include": [ + "src" + ] +}