From 4a68132c307f4edb6e5e486456e67e2eacf4e07f Mon Sep 17 00:00:00 2001 From: David ALLIX Date: Thu, 17 Oct 2024 22:55:41 +0200 Subject: [PATCH] Fix $prefix with optional resultSchema --- biome.json | 2 +- package.json | 94 ++++++++++++++++++++-------------------- pnpm-lock.yaml | 102 ++++++++++++++++++++++---------------------- src/index.ts | 2 +- tests/index.test.ts | 36 ++++++++++++++++ 5 files changed, 137 insertions(+), 99 deletions(-) diff --git a/biome.json b/biome.json index aa3c6ef..998286e 100644 --- a/biome.json +++ b/biome.json @@ -1,5 +1,5 @@ { - "$schema": "https://biomejs.dev/schemas/1.9.3/schema.json", + "$schema": "https://biomejs.dev/schemas/1.9.4/schema.json", "formatter": { "useEditorconfig": true, "formatWithErrors": true, diff --git a/package.json b/package.json index 0a752e4..81f1041 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,9 @@ { - "name": "@a2lix/schemql", - "version": "0.1.0", - "description": "A lightweight TypeScript library that enhances your SQL workflow by combining raw SQL with targeted type safety and schema validation", - "license": "MIT", - "keywords": [ + "name": "@a2lix/schemql", + "version": "0.1.1", + "description": "A lightweight TypeScript library that enhances your SQL workflow by combining raw SQL with targeted type safety and schema validation", + "license": "MIT", + "keywords": [ "sql", "typescript", "database", @@ -19,45 +19,47 @@ "sql-first", "query-builder" ], - "author": { - "name": "David ALLIX", - "url": "https://a2lix.fr" - }, - "homepage": "https://github.com/a2lix/schemql", - "repository": { - "type": "git", - "url": "git+https://github.com/a2lix/schemql.git" - }, - "main": "./dist/index.cjs", - "module": "./dist/index.mjs", - "types": "./dist/index.d.ts", - "exports": { - ".": { - "import": { - "types": "./dist/index.d.mts", - "default": "./dist/index.mjs" - }, - "require": { - "types": "./dist/index.d.cts", - "default": "./dist/index.cjs" - } - } - }, - "files": ["dist"], - "scripts": { - "biome": "biome check --write ./src ./tests", - "test": "npx tsx --test **/tests/*.ts", - "build": "pkgroll --clean-dist --minify --src src/" - }, - "dependencies": { - "zod": "^3.23.8" - }, - "devDependencies": { - "@biomejs/biome": "^1.9.3", - "@types/node": "^22.7.5", - "pkgroll": "^2.5.0", - "ts-node": "^10.9.2", - "tsx": "^4.19.1", - "typescript": "^5.6.3" - } + "author": { + "name": "David ALLIX", + "url": "https://a2lix.fr" + }, + "homepage": "https://github.com/a2lix/schemql", + "repository": { + "type": "git", + "url": "git+https://github.com/a2lix/schemql.git" + }, + "main": "./dist/index.cjs", + "module": "./dist/index.mjs", + "types": "./dist/index.d.ts", + "exports": { + ".": { + "import": { + "types": "./dist/index.d.mts", + "default": "./dist/index.mjs" + }, + "require": { + "types": "./dist/index.d.cts", + "default": "./dist/index.cjs" + } + } + }, + "files": [ + "dist" + ], + "scripts": { + "biome": "biome check --write ./src ./tests", + "test": "tsx --test ./tests/**/*.ts", + "build": "pkgroll --clean-dist --minify --src src/" + }, + "dependencies": { + "zod": "^3.23.8" + }, + "devDependencies": { + "@biomejs/biome": "^1.9.4", + "@types/node": "^22.7.6", + "pkgroll": "^2.5.0", + "ts-node": "^10.9.2", + "tsx": "^4.19.1", + "typescript": "^5.6.3" + } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 84037ef..c4a7a0b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -13,17 +13,17 @@ importers: version: 3.23.8 devDependencies: '@biomejs/biome': - specifier: ^1.9.3 - version: 1.9.3 + specifier: ^1.9.4 + version: 1.9.4 '@types/node': - specifier: ^22.7.5 - version: 22.7.5 + specifier: ^22.7.6 + version: 22.7.6 pkgroll: specifier: ^2.5.0 version: 2.5.0(typescript@5.6.3) ts-node: specifier: ^10.9.2 - version: 10.9.2(@types/node@22.7.5)(typescript@5.6.3) + version: 10.9.2(@types/node@22.7.6)(typescript@5.6.3) tsx: specifier: ^4.19.1 version: 4.19.1 @@ -33,55 +33,55 @@ importers: packages: - '@biomejs/biome@1.9.3': - resolution: {integrity: sha512-POjAPz0APAmX33WOQFGQrwLvlu7WLV4CFJMlB12b6ZSg+2q6fYu9kZwLCOA+x83zXfcPd1RpuWOKJW0GbBwLIQ==} + '@biomejs/biome@1.9.4': + resolution: {integrity: sha512-1rkd7G70+o9KkTn5KLmDYXihGoTaIGO9PIIN2ZB7UJxFrWw04CZHPYiMRjYsaDvVV7hP1dYNRLxSANLaBFGpog==} engines: {node: '>=14.21.3'} hasBin: true - '@biomejs/cli-darwin-arm64@1.9.3': - resolution: {integrity: sha512-QZzD2XrjJDUyIZK+aR2i5DDxCJfdwiYbUKu9GzkCUJpL78uSelAHAPy7m0GuPMVtF/Uo+OKv97W3P9nuWZangQ==} + '@biomejs/cli-darwin-arm64@1.9.4': + resolution: {integrity: sha512-bFBsPWrNvkdKrNCYeAp+xo2HecOGPAy9WyNyB/jKnnedgzl4W4Hb9ZMzYNbf8dMCGmUdSavlYHiR01QaYR58cw==} engines: {node: '>=14.21.3'} cpu: [arm64] os: [darwin] - '@biomejs/cli-darwin-x64@1.9.3': - resolution: {integrity: sha512-vSCoIBJE0BN3SWDFuAY/tRavpUtNoqiceJ5PrU3xDfsLcm/U6N93JSM0M9OAiC/X7mPPfejtr6Yc9vSgWlEgVw==} + '@biomejs/cli-darwin-x64@1.9.4': + resolution: {integrity: sha512-ngYBh/+bEedqkSevPVhLP4QfVPCpb+4BBe2p7Xs32dBgs7rh9nY2AIYUL6BgLw1JVXV8GlpKmb/hNiuIxfPfZg==} engines: {node: '>=14.21.3'} cpu: [x64] os: [darwin] - '@biomejs/cli-linux-arm64-musl@1.9.3': - resolution: {integrity: sha512-VBzyhaqqqwP3bAkkBrhVq50i3Uj9+RWuj+pYmXrMDgjS5+SKYGE56BwNw4l8hR3SmYbLSbEo15GcV043CDSk+Q==} + '@biomejs/cli-linux-arm64-musl@1.9.4': + resolution: {integrity: sha512-v665Ct9WCRjGa8+kTr0CzApU0+XXtRgwmzIf1SeKSGAv+2scAlW6JR5PMFo6FzqqZ64Po79cKODKf3/AAmECqA==} engines: {node: '>=14.21.3'} cpu: [arm64] os: [linux] - '@biomejs/cli-linux-arm64@1.9.3': - resolution: {integrity: sha512-vJkAimD2+sVviNTbaWOGqEBy31cW0ZB52KtpVIbkuma7PlfII3tsLhFa+cwbRAcRBkobBBhqZ06hXoZAN8NODQ==} + '@biomejs/cli-linux-arm64@1.9.4': + resolution: {integrity: sha512-fJIW0+LYujdjUgJJuwesP4EjIBl/N/TcOX3IvIHJQNsAqvV2CHIogsmA94BPG6jZATS4Hi+xv4SkBBQSt1N4/g==} engines: {node: '>=14.21.3'} cpu: [arm64] os: [linux] - '@biomejs/cli-linux-x64-musl@1.9.3': - resolution: {integrity: sha512-TJmnOG2+NOGM72mlczEsNki9UT+XAsMFAOo8J0me/N47EJ/vkLXxf481evfHLlxMejTY6IN8SdRSiPVLv6AHlA==} + '@biomejs/cli-linux-x64-musl@1.9.4': + resolution: {integrity: sha512-gEhi/jSBhZ2m6wjV530Yy8+fNqG8PAinM3oV7CyO+6c3CEh16Eizm21uHVsyVBEB6RIM8JHIl6AGYCv6Q6Q9Tg==} engines: {node: '>=14.21.3'} cpu: [x64] os: [linux] - '@biomejs/cli-linux-x64@1.9.3': - resolution: {integrity: sha512-x220V4c+romd26Mu1ptU+EudMXVS4xmzKxPVb9mgnfYlN4Yx9vD5NZraSx/onJnd3Gh/y8iPUdU5CDZJKg9COA==} + '@biomejs/cli-linux-x64@1.9.4': + resolution: {integrity: sha512-lRCJv/Vi3Vlwmbd6K+oQ0KhLHMAysN8lXoCI7XeHlxaajk06u7G+UsFSO01NAs5iYuWKmVZjmiOzJ0OJmGsMwg==} engines: {node: '>=14.21.3'} cpu: [x64] os: [linux] - '@biomejs/cli-win32-arm64@1.9.3': - resolution: {integrity: sha512-lg/yZis2HdQGsycUvHWSzo9kOvnGgvtrYRgoCEwPBwwAL8/6crOp3+f47tPwI/LI1dZrhSji7PNsGKGHbwyAhw==} + '@biomejs/cli-win32-arm64@1.9.4': + resolution: {integrity: sha512-tlbhLk+WXZmgwoIKwHIHEBZUwxml7bRJgk0X2sPyNR3S93cdRq6XulAZRQJ17FYGGzWne0fgrXBKpl7l4M87Hg==} engines: {node: '>=14.21.3'} cpu: [arm64] os: [win32] - '@biomejs/cli-win32-x64@1.9.3': - resolution: {integrity: sha512-cQMy2zanBkVLpmmxXdK6YePzmZx0s5Z7KEnwmrW54rcXK3myCNbQa09SwGZ8i/8sLw0H9F3X7K4rxVNGU8/D4Q==} + '@biomejs/cli-win32-x64@1.9.4': + resolution: {integrity: sha512-8Y5wMhVIPaWe6jw2H+KlEm4wP/f7EW3810ZLmDlrEEy5KvBsb9ECEfu/kMWD484ijfQ8+nIi0giMgu9g1UAuuA==} engines: {node: '>=14.21.3'} cpu: [x64] os: [win32] @@ -410,8 +410,8 @@ packages: '@types/estree@1.0.6': resolution: {integrity: sha512-AYnb1nQyY49te+VRAVgmzfcgjYS91mY5P0TKUDCLEM+gNnA+3T6rWITXRLYCpahpqSQbN5cE+gHpnPyXjHWxcw==} - '@types/node@22.7.5': - resolution: {integrity: sha512-jML7s2NAzMWc//QSJ1a3prpk78cOPchGvXJsC3C6R6PSMoooztvRVQEz89gmBTBY1SPMaqo5teB4uNHPdetShQ==} + '@types/node@22.7.6': + resolution: {integrity: sha512-/d7Rnj0/ExXDMcioS78/kf1lMzYk4BZV8MZGTBKzTGZ6/406ukkbYlIsZmMPhcR5KlkunDHQLrtAVmSq7r+mSw==} '@types/resolve@1.20.2': resolution: {integrity: sha512-60BCwRFOZCQhDncwQdxxeOEEkbc5dIMccYLwbxsS4TUNeVECQ/pBJ0j09mrHOl/JJvpRPGwO9SvE4nR2Nb/a4Q==} @@ -420,8 +420,8 @@ packages: resolution: {integrity: sha512-ueEepnujpqee2o5aIYnvHU6C0A42MNdsIDeqy5BydrkuC5R1ZuUFnm27EeFJGoEHJQgn3uleRvmTXaJgfXbt4g==} engines: {node: '>=0.4.0'} - acorn@8.12.1: - resolution: {integrity: sha512-tcpGyI9zbizT9JbV6oYE477V6mTlXvvi0T0G3SNIYE2apm/G5huBa1+K89VGeovbg+jycCrfhl3ADxErOuO6Jg==} + acorn@8.13.0: + resolution: {integrity: sha512-8zSiw54Oxrdym50NlZ9sUusyO1Z1ZchgRLWRaK6c86XJFClyCgFKetdowBg5bKxyp/u+CDBJG4Mpp0m3HLZl9w==} engines: {node: '>=0.4.0'} hasBin: true @@ -676,39 +676,39 @@ packages: snapshots: - '@biomejs/biome@1.9.3': + '@biomejs/biome@1.9.4': optionalDependencies: - '@biomejs/cli-darwin-arm64': 1.9.3 - '@biomejs/cli-darwin-x64': 1.9.3 - '@biomejs/cli-linux-arm64': 1.9.3 - '@biomejs/cli-linux-arm64-musl': 1.9.3 - '@biomejs/cli-linux-x64': 1.9.3 - '@biomejs/cli-linux-x64-musl': 1.9.3 - '@biomejs/cli-win32-arm64': 1.9.3 - '@biomejs/cli-win32-x64': 1.9.3 - - '@biomejs/cli-darwin-arm64@1.9.3': + '@biomejs/cli-darwin-arm64': 1.9.4 + '@biomejs/cli-darwin-x64': 1.9.4 + '@biomejs/cli-linux-arm64': 1.9.4 + '@biomejs/cli-linux-arm64-musl': 1.9.4 + '@biomejs/cli-linux-x64': 1.9.4 + '@biomejs/cli-linux-x64-musl': 1.9.4 + '@biomejs/cli-win32-arm64': 1.9.4 + '@biomejs/cli-win32-x64': 1.9.4 + + '@biomejs/cli-darwin-arm64@1.9.4': optional: true - '@biomejs/cli-darwin-x64@1.9.3': + '@biomejs/cli-darwin-x64@1.9.4': optional: true - '@biomejs/cli-linux-arm64-musl@1.9.3': + '@biomejs/cli-linux-arm64-musl@1.9.4': optional: true - '@biomejs/cli-linux-arm64@1.9.3': + '@biomejs/cli-linux-arm64@1.9.4': optional: true - '@biomejs/cli-linux-x64-musl@1.9.3': + '@biomejs/cli-linux-x64-musl@1.9.4': optional: true - '@biomejs/cli-linux-x64@1.9.3': + '@biomejs/cli-linux-x64@1.9.4': optional: true - '@biomejs/cli-win32-arm64@1.9.3': + '@biomejs/cli-win32-arm64@1.9.4': optional: true - '@biomejs/cli-win32-x64@1.9.3': + '@biomejs/cli-win32-x64@1.9.4': optional: true '@cspotcode/source-map-support@0.8.1': @@ -920,7 +920,7 @@ snapshots: '@types/estree@1.0.6': {} - '@types/node@22.7.5': + '@types/node@22.7.6': dependencies: undici-types: 6.19.8 @@ -928,9 +928,9 @@ snapshots: acorn-walk@8.3.4: dependencies: - acorn: 8.12.1 + acorn: 8.13.0 - acorn@8.12.1: {} + acorn@8.13.0: {} ansi-regex@5.0.1: {} @@ -1154,15 +1154,15 @@ snapshots: supports-preserve-symlinks-flag@1.0.0: {} - ts-node@10.9.2(@types/node@22.7.5)(typescript@5.6.3): + ts-node@10.9.2(@types/node@22.7.6)(typescript@5.6.3): dependencies: '@cspotcode/source-map-support': 0.8.1 '@tsconfig/node10': 1.0.11 '@tsconfig/node12': 1.0.11 '@tsconfig/node14': 1.0.3 '@tsconfig/node16': 1.0.4 - '@types/node': 22.7.5 - acorn: 8.12.1 + '@types/node': 22.7.6 + acorn: 8.13.0 acorn-walk: 8.3.4 arg: 4.1.3 create-require: 1.1.1 diff --git a/src/index.ts b/src/index.ts index d8a6a9e..85747c1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -36,7 +36,7 @@ type SqlTemplateValue = | `@${TableNames}.*` | `@${ValidTableColumnCombinations}` | `@${ValidJsonPathCombinations}` - | `$${keyof ArrayElement & string}` + | `$${keyof ArrayElement> & string}` | `:${keyof TParams & string}` | `ยง${string}` diff --git a/tests/index.test.ts b/tests/index.test.ts index 984259f..a6c6269 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -242,6 +242,42 @@ describe('SchemQl - sql literal', () => { length_id: 6, }) }) + + it('should return the expected result, undefined case', async () => { + const result = await schemQlUnconfigured.first({ + queryFn: (sql, params) => { + assert.strictEqual( + sql, + normalizeString(` + SELECT + *, + LENGTH(id) AS length_id + FROM users + WHERE + id = :id + `) + ) + assert.deepEqual(params, { id: 'uuid-1' }) + return undefined + }, + resultSchema: zUserDb.and(z.object({ length_id: z.number() })).optional(), + params: { + id: 'uuid-1', + }, + paramsSchema: zUserDb.pick({ id: true }), + })((s) => + normalizeString(s.sql` + SELECT + *, + LENGTH(${'@users.id-'}) AS ${'$length_id'} + FROM ${'@users'} + WHERE + ${'@users.id-'} = ${':id'} + `) + ) + + assert.deepEqual(result, undefined) + }) }) describe('SchemQl - sql literal advanced', () => {