Skip to content

Commit

Permalink
Restructure drivers (#14)
Browse files Browse the repository at this point in the history
* Add deno drivers

* Restructure drivers

* Rename drivers to be a bit more descriptive. We won't always have a package name anyway.

* Oops, seems this only works from tsc 5.6 onwards

* Remove @db/sqlite driver for now, see denodrivers/sqlite3#138

* Revert readme changes, we'll document this later

* Bundle driver tests

* Cleanup

* Check in lockfile
  • Loading branch information
benmerckx authored Sep 11, 2024
1 parent 6f0e8cc commit 8e307fe
Show file tree
Hide file tree
Showing 16 changed files with 135 additions and 112 deletions.
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@jsr:registry=https://npm.jsr.io
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,14 @@ const PostTags = table({

Currently supported drivers:

| Driver | import |
| ---------------- | ------------------------------ |
| `better-sqlite3` | `'rado/driver/better-sqlite3'` |
| `bun-sqlite` | `'rado/driver/bun-sqlite'` |
| `mysql2` | `'rado/driver/mysql2'` |
| `pg` | `'rado/driver/pg'` |
| `pglite` | `'rado/driver/pglite'` |
| `sql.js` | `'rado/driver/sql.js'` |
| Driver | import |
| ---------------------- | ------------------------------ |
| `better-sqlite3` | `'rado/driver/better-sqlite3'` |
| `bun:sqlite` | `'rado/driver/bun-sqlite'` |
| `mysql2` | `'rado/driver/mysql2'` |
| `pg` | `'rado/driver/pg'` |
| `@electric-sql/pglite` | `'rado/driver/pglite'` |
| `sql.js` | `'rado/driver/sql.js'` |

Pass an instance of the database to the `connect` function to get started:

Expand Down
Binary file modified bun.lockb
Binary file not shown.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"cycles": "madge --circular src/index.ts src/sqlite.ts src/mysql.ts src/postgres.ts",
"test:bun": "bun test",
"test:node": "node --test-force-exit --test-concurrency=1 --import tsx --test \"**/*.test.ts\"",
"test:deno": "deno test --no-check --allow-read"
"test:deno": "deno test --no-check -A --unstable-ffi"
},
"sideEffects": false,
"exports": {
Expand All @@ -35,7 +35,7 @@
"@types/glob": "^8.1.0",
"@types/pg": "^8.11.8",
"@types/sql.js": "^1.4.9",
"better-sqlite3": "^11.2.1",
"better-sqlite3": "^11.3.0",
"esbuild": "^0.23.1",
"glob": "^11.0.0",
"madge": "^8.0.0",
Expand All @@ -45,7 +45,7 @@
"sql.js": "^1.11.0",
"sqlite3": "^5.1.7",
"tsx": "^4.19.0",
"typescript": "^5.5.4",
"typescript": "^5.6.2",
"@alinea/suite": "^0.4.0"
}
}
8 changes: 6 additions & 2 deletions src/core/expr/Include.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ export class Include<Result, Meta extends QueryMeta = QueryMeta>
#mapFromDriverValue = (value: any, specs: DriverSpecs): any => {
const {select, first} = getData(this)
const parsed = specs.parsesJson ? value : JSON.parse(value)
if (first)
return parsed ? select!.mapRow({values: parsed, index: 0, specs}) : null
if (first) {
const result = parsed
? select!.mapRow({values: parsed, index: 0, specs})
: null
return result
}
if (!parsed) return []
const rows: Array<Array<unknown>> = parsed
const ctx: MapRowContext = {
Expand Down
6 changes: 6 additions & 0 deletions src/driver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export {connect as 'better-sqlite3'} from './driver/better-sqlite3.ts'
export {connect as 'bun:sqlite'} from './driver/bun-sqlite.ts'
export {connect as 'mysql2'} from './driver/mysql2.ts'
export {connect as 'pg'} from './driver/pg.ts'
export {connect as '@electric-sql/pglite'} from './driver/pglite.ts'
export {connect as 'sql.js'} from './driver/sql.js.ts'
2 changes: 1 addition & 1 deletion src/driver/pglite.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {PGlite, Transaction} from '@electric-sql/pglite'
import {AsyncDatabase, type TransactionOptions} from '../core/Database.ts'
import type {AsyncDriver, AsyncStatement, BatchQuery} from '../core/Driver.ts'
import {AsyncDatabase, type TransactionOptions} from '../index.ts'
import {postgresDialect} from '../postgres/dialect.ts'
import {postgresDiff} from '../postgres/diff.ts'
import {setTransaction} from '../postgres/transactions.ts'
Expand Down
35 changes: 0 additions & 35 deletions test/TestDriver.ts

This file was deleted.

2 changes: 2 additions & 0 deletions test/TestRuntime.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const isBun = 'Bun' in globalThis
export const isDeno = 'Deno' in globalThis
export const isNode = !isBun && 'process' in globalThis
// @ts-ignore
export const isCi = !isDeno && 'CI' in process.env
108 changes: 108 additions & 0 deletions test/driver.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import * as driver from '@/driver.ts'
import {type DefineTest, type Describe, suite} from '@alinea/suite'
import {isBun, isCi, isNode} from './TestRuntime.ts'
import {testBasic} from './integration/TestBasic.ts'
import {testCTE} from './integration/TestCTE.ts'
import {testColumns} from './integration/TestColumns.ts'
import {testConstraints} from './integration/TestConstraints.ts'
import {testInclude} from './integration/TestInclude.ts'
import {testJoins} from './integration/TestJoins.ts'
import {testJson} from './integration/TestJson.ts'
import {testMigration} from './integration/TestMigration.ts'
import {testPreparedQuery} from './integration/TestPreparedQuery.ts'
import {testSubquery} from './integration/TestSubquery.ts'
import {testTransactions} from './integration/TestTransactions.ts'

const init = {
'better-sqlite3': {
condition: isNode,
supportsDiff: true,
async client() {
const {default: Database} = await import('better-sqlite3')
return new Database(':memory:')
}
},
'bun:sqlite': {
condition: isBun,
supportsDiff: true,
async client() {
const {Database} = await import('bun:sqlite')
return new Database(':memory:')
}
},
mysql2: {
condition: isCi,
supportsDiff: false,
async client() {
const {default: mysql2} = await import('mysql2')
const client = mysql2.createConnection(
'mysql://root:[email protected]:3306/mysql'
)
return client
}
},
'@electric-sql/pglite': {
condition: true,
supportsDiff: true,
async client() {
const {PGlite} = await import('@electric-sql/pglite')
return new PGlite()
}
},
pg: {
condition: isCi,
supportsDiff: true,
async client() {
const {default: pg} = await import('pg')
const client = new pg.Client({
connectionString: 'postgres://postgres:[email protected]:5432/postgres'
})
await client.connect()
return client
}
},
'sql.js': {
condition: true,
supportsDiff: true,
async client() {
const {default: init} = await import('sql.js')
const {Database} = await init()
return new Database()
}
}
}

async function createTests() {
const clients = await Promise.all(
Object.entries(init)
.filter(([name, meta]) => meta.condition)
.map(
async ([name, meta]) =>
[name, await meta.client()] as [keyof typeof init, any]
)
)
return (test: DefineTest) => {
for (const [name, client] of clients) {
const {supportsDiff} = init[name]
const db = driver[name](client)
const prefixed: Describe = (description, fn) =>
test(`${name}: ${description}`, fn)
const withName = Object.assign(prefixed, test)

testBasic(db, withName)
testColumns(db, withName)
testSubquery(db, withName)
testPreparedQuery(db, withName)
testJoins(db, withName)
testJson(db, withName)
testTransactions(db, withName)
testConstraints(db, withName)
testCTE(db, withName)
testInclude(db, withName)

if (supportsDiff) testMigration(db, withName)
}
}
}

suite(import.meta, await createTests())
9 changes: 0 additions & 9 deletions test/driver/better-sqlite3.test.ts

This file was deleted.

10 changes: 0 additions & 10 deletions test/driver/bun-sqlite.test.ts

This file was deleted.

16 changes: 0 additions & 16 deletions test/driver/mysql2.test.ts

This file was deleted.

13 changes: 0 additions & 13 deletions test/driver/pg.test.ts

This file was deleted.

7 changes: 0 additions & 7 deletions test/driver/pglite.test.ts

This file was deleted.

8 changes: 0 additions & 8 deletions test/driver/sql.js.test.ts

This file was deleted.

0 comments on commit 8e307fe

Please sign in to comment.