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

Improvements - Add adapters #5

Merged
merged 5 commits into from
Nov 22, 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
62 changes: 6 additions & 56 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const zUserDb = z.object({
id: z.string(),
email: z.string(),
metadata: z.preprocess(
parseJsonPreprocessor, // ! Zod handles JSON parsing for JSON columns
parseJsonPreprocessor, // ! Zod handles JSON parsing for this JSON columns 'metadata'
z.object({
role: z.enum(['user', 'admin']).default('user'),
})
Expand All @@ -71,68 +71,18 @@ export interface DB {
<details>
<summary>2. Initialize your instance of SchemQl with the DB interface typing</summary>
<br>
Example with better-sqlite3, but you can use your favorite library.

Here the 4 methods first/firstOrThrow/all/iterate are defined at the instance level, but you can define them at the query level if you prefer.
You can also ignore some methods if you don't need them.
Example with better-sqlite3 adapter.

```typescript
import { SchemQl } from '@a2lix/schemql'
import SQLite from 'better-sqlite3'
import { BetterSqlite3Adapter } from '@a2lix/schemql/adapters/better-sqlite3'
import type { DB } from '@/schema'

const db = new SQLite('sqlite.db')

const schemQl = new SchemQl<DB>({
queryFns: { // Optional at this level, but simplifies usage
first: (sql) => {
const stmt = db.prepare(sql)
return (params) => {
return stmt.get(params)
}
},
firstOrThrow: (sql) => {
const stmt = db.prepare(sql)
return (params) => {
const first = stmt.get(params)
if (first === undefined) {
throw new Error('No result found')
}
return first
}
},
all: (sql) => {
const stmt = db.prepare(sql)
return (params) => {
return params ? stmt.all(params) : stmt.all()
}
},
iterate: (sql) => {
const stmt = db.prepare(sql)
return (params) => {
return stmt.iterate(params)
}
},
},
adapter: new BetterSqlite3Adapter('sqlite.db'),
shouldStringifyObjectParams: true, // Optional. Automatically stringify objects (useful for JSON)
})
```

You can also keep initialization simple if your move some logic in you own custom class

```typescript
const sqliteDb = new SQLiteDb() // Custom class with better error handling

const schemQl = new SchemQl<DB>({
queryFns: {
first: sqliteDb.queryFirst.bind(db),
firstOrThrow: sqliteDb.queryFirstOrThrow.bind(db),
all: sqliteDb.queryAll.bind(db),
},
shouldStringifyObjectParams: true,
})
```

</details>

<details open>
Expand Down Expand Up @@ -275,8 +225,8 @@ const iterResults = await schemQl.iterate({
| `${'@table1'}` | `table1` | **Table Selection**: Prefix `@` eases table selection/validation |
| `${'@table1.col1'}` | `table1.col1` | **Column Selection**: Use `@` for table and column validation |
| `${'@table1.col1-'}` | `col1` | Final `-` excludes table name (useful if table is aliased) |
| `${'@table1.col1 ->jsonpath1'}` | `table1.col1->'jsonpath1'` | **JSON Field Selection**: Use `->` for JSON paths |
| `${'@table1.col1 ->>jsonpath1'}` | `table1.col1->>'jsonpath1'` | JSON field (raw) with `->>` syntax |
| `${'@table1.col1 ->jsonpath1'}` | `table1.col1 ->'jsonpath1'` | **JSON Field Selection**: Use `->` for JSON paths |
| `${'@table1.col1 ->>jsonpath1'}` | `table1.col1 ->>'jsonpath1'` | JSON field (raw) with `->>` syntax |
| `${'@table1.col1 $.jsonpath1'}` | `'$.jsonpath1'` | JSONPath with `$` prefix |
| `${'$resultCol1'}` | `resultCol1` | **Result Selection**: `$` prefix targets resultSchema fields |
| `${':param1'}` | `:param1` | **Parameter Selection**: `:` prefix eases parameter validation |
Expand Down
44 changes: 41 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@a2lix/schemql",
"version": "0.3.1",
"version": "0.4.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": [
Expand Down Expand Up @@ -44,6 +44,36 @@
"types": "./dist/index.d.cts",
"default": "./dist/index.cjs"
}
},
"./adapters/base-error": {
"import": {
"types": "./dist/adapters/baseAdapterError.d.mts",
"default": "./dist/adapters/baseAdapterError.mjs"
},
"require": {
"types": "./dist/adapters/baseAdapterError.d.cts",
"default": "./dist/adapters/baseAdapterError.cjs"
}
},
"./adapters/better-sqlite3": {
"import": {
"types": "./dist/adapters/betterSqlite3Adapter.d.mts",
"default": "./dist/adapters/betterSqlite3Adapter.mjs"
},
"require": {
"types": "./dist/adapters/betterSqlite3Adapter.d.cts",
"default": "./dist/adapters/betterSqlite3Adapter.cjs"
}
},
"./adapters/d1": {
"import": {
"types": "./dist/adapters/d1Adapter.d.mts",
"default": "./dist/adapters/d1Adapter.mjs"
},
"require": {
"types": "./dist/adapters/d1Adapter.d.cts",
"default": "./dist/adapters/d1Adapter.cjs"
}
}
},
"files": [
Expand All @@ -60,10 +90,18 @@
},
"devDependencies": {
"@biomejs/biome": "^1.9.4",
"@types/node": "^22.8.1",
"@types/node": "^22.9.1",
"pkgroll": "^2.5.1",
"ts-node": "^10.9.2",
"tsx": "^4.19.1",
"tsx": "^4.19.2",
"typescript": "^5.6.3"
},
"peerDependencies": {
"@cloudflare/workers-types": "^4.20241112.0",
"better-sqlite3": "^11.5.0"
},
"optionalDependencies": {
"@cloudflare/workers-types": "^4.20241112.0",
"better-sqlite3": "^11.5.0"
}
}
Loading