Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix $prefix with optional resultSchema #1

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion biome.json
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
94 changes: 48 additions & 46 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
}
}
102 changes: 51 additions & 51 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type SqlTemplateValue<TResultSchema, TParams, DB> =
| `@${TableNames<DB>}.*`
| `@${ValidTableColumnCombinations<DB>}`
| `@${ValidJsonPathCombinations<DB>}`
| `$${keyof ArrayElement<TResultSchema> & string}`
| `$${keyof ArrayElement<Exclude<TResultSchema, undefined>> & string}`
| `:${keyof TParams & string}`
| `§${string}`

Expand Down
36 changes: 36 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down