Skip to content
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 .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ jobs:
- name: Test JavaScript wrappers and example runners
if: steps.select.outputs.typescript == 'true'
run: |
node --test scripts/materialize-sqlite-sources.test.mjs
node --test scripts/materialize-sqlite-sources.test.mjs scripts/companion-materialize.test.mjs
node --test scripts/mikro-orm-metro.test.mjs
bun --cwd packages/react-native-nitro-sqlite build
bun --cwd packages/integrations/react-native/typeorm build
Expand Down
105 changes: 98 additions & 7 deletions bun.lock

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"workspaces": [
"packages/*",
"packages/integrations/react-native/*",
"packages/integrations/nitro-ui/*",
"examples/react-native",
"examples/react-native/macos",
"examples/react-native/expo",
Expand Down
1 change: 1 addition & 0 deletions packages/integrations/nitro-ui/drizzle/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/src/*.ts
3 changes: 3 additions & 0 deletions packages/integrations/nitro-ui/drizzle/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
src/__tests__/
.tsbuild/
tsconfig.tsbuildinfo
20 changes: 20 additions & 0 deletions packages/integrations/nitro-ui/drizzle/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
MIT License

Copyright (c) 2024 Marc Rousavy & Christoph Pader
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
87 changes: 87 additions & 0 deletions packages/integrations/nitro-ui/drizzle/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# nitro-sqlite-drizzle

[Drizzle ORM](https://orm.drizzle.team) driver for `nitro-sqlite`.

## Installation

This 11.0.0 pre-release package is not published to npm. Build and install the matching
SQLite and Drizzle archives using the [companion installation guide](../../../../docs/content/docs/integrations/orms/index.mdx).
Install `drizzle-orm@0.45.4` and the app's React version alongside them. Each archive
includes the driver source. Use `nitro-sqlite` with `nitro-sqlite-drizzle` in NitroUI apps.


## Usage

```ts
import { sqliteTable, integer, text } from 'drizzle-orm/sqlite-core'
import { open } from 'nitro-sqlite'
import { drizzle } from 'nitro-sqlite-drizzle'

const users = sqliteTable('users', {
id: integer('id').primaryKey(),
name: text('name').notNull(),
})

const db = drizzle(open({ name: 'app.sqlite' }))

await db.insert(users).values({ id: 1, name: 'Ada' })
const rows = await db.select().from(users)
```

Every query is asynchronous. Inside `db.transaction()`, run queries through `tx`: a query awaited through `db` waits for the transaction and never completes.

See the [Drizzle ORM integration guide](../../../../docs/content/docs/integrations/orms/drizzle.mdx) for migrations, `useMigrations`, and transactions.


## Migrations

SQLite schema changes are SQL statements. Drizzle Kit bundles them with an ordered
journal, and the driver records each completed migration in `__drizzle_migrations`.
Pending migrations run in one transaction. A failure rolls them back together.

Generate a bundled `migrations.js` module with Drizzle Kit, then pass that module
to the driver. Configure your app's bundler to import its `.sql` files as strings.

```ts
import { migrate } from 'nitro-sqlite-drizzle'
import migrations from './drizzle/migrations'

await migrate(db, migrations)
```

In a React component, `useMigrations(db, migrations)` returns `{ success, error }`.
Wait for `success` before rendering components that query the migrated schema.
Changing the database or migration bundle resets the hook's state. Migrations
already queued still finish after a component unmounts.

```tsx
import { useMigrations } from 'nitro-sqlite-drizzle'

function DatabaseGate() {
const { success, error } = useMigrations(db, migrations)
if (error) throw error
if (!success) return null
return <AppContent />
}
```

Calls to `migrate` and `useMigrations` sharing a connection run in sequence.
Closing the connection remains the caller's responsibility. Close it only after
all queries, transactions, and migrations finish.

## Transactions and results

Use the callback's `tx` handle for every query inside a transaction, and await
nested transactions before using their parent again. Nested transactions use
SQLite savepoints. An escaped callback error rolls back its changes. Transaction
handles and their prepared queries reject after their callback ends.

Drizzle selected fields decode positional SQLite rows, so joins preserve columns
with repeated names. Queries without selected fields return objects keyed by
column name. `run()` returns the native raw result with `rowsAffected`, optional
`insertId`, `columnNames`, and positional `rows`. Binary views used as parameters
bind only their own bytes, rather than the entire backing buffer.

This driver supports Drizzle ORM 0.45.x. It uses asynchronous queries and does not
own or close the supplied SQLite connection. Its prepared-query wrappers do not
retain native statements and need no disposal.
64 changes: 64 additions & 0 deletions packages/integrations/nitro-ui/drizzle/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"name": "nitro-sqlite-drizzle",
"version": "11.0.0",
"description": "Drizzle ORM driver for nitro-sqlite.",
"source": "./src/index.ts",
"main": "./src/index.ts",
"types": "./src/index.ts",
"files": [
"src",
"!**/__tests__",
"README.md",
"scripts",
"LICENSE"
],
"scripts": {
"typecheck": "node scripts/materialize.mjs && tsc --noEmit",
"prepublishOnly": "node scripts/materialize.mjs && tsc --noEmit",
"materialize": "node scripts/materialize.mjs",
"prepack": "node scripts/materialize.mjs && tsc --noEmit",
"pack": "bun pm pack"
},
"keywords": [
"sqlite",
"drizzle",
"drizzle-orm",
"orm",
"ios",
"android",
"nitro-ui"
],
"repository": {
"type": "git",
"url": "git+https://github.com/margelo/nitro-sqlite.git"
},
"author": "Christoph Pader",
"license": "MIT",
"publishConfig": {
"registry": "https://registry.npmjs.org/",
"access": "public"
},
"peerDependencies": {
"drizzle-orm": ">=0.45.4 <1.0.0",
"react": ">=17.0.0",
"nitro-sqlite": ">=11.0.0-0"
},
"devDependencies": {
"drizzle-orm": "0.45.4",
"typescript": "^5.8.3"
},
"exports": {
".": {
"types": "./src/index.ts",
"default": "./src/index.ts"
},
"./driver": "./src/driver.ts",
"./session": "./src/session.ts",
"./migrator": "./src/migrator.ts",
"./package.json": "./package.json",
"./src/*.ts": "./src/*.ts",
"./src/*": "./src/*.ts"
},
"type": "module",
"module": "./src/index.ts"
}
Loading
Loading