Skip to content

Commit 6b7c8b3

Browse files
committed
Add drizzle migration docs
1 parent b6cf989 commit 6b7c8b3

1 file changed

Lines changed: 50 additions & 1 deletion

File tree

README.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,16 @@ import { sqlocalDriver } from '@nanostores/sql/sqlocal'
6868
export const db = openDb(sqlocalDriver('app.sqlite'))
6969
```
7070

71-
TODO: Vite headers
71+
Add a worker workaround to Vite (see [docs](https://github.com/DallasHoff/sqlocal/blob/main/README.md#cross-origin-isolation)):
72+
73+
```ts
74+
import { defineConfig } from 'vite'
75+
import sqlocal from 'sqlocal/vite'
76+
77+
export default defineConfig({
78+
plugins: [sqlocal()]
79+
})
80+
```
7281

7382
### Expo
7483

@@ -202,3 +211,43 @@ const $users = db.store<User>(
202211
.where(like(usersTable.name, `%${name}%`))
203212
)
204213
```
214+
215+
### Migrations with Drizzle
216+
217+
Install [Drizzle Kit](https://orm.drizzle.team/docs/kit-overview):
218+
219+
```bash
220+
npm add --save-dev drizzle-kit
221+
```
222+
223+
Generate SQL migration files from your schema:
224+
225+
```bash
226+
npx drizzle-kit generate
227+
```
228+
229+
This creates SQL files in `./drizzle` (e.g. `0000_create_users.sql`,
230+
`0001_add_posts.sql`). Import them as raw strings and apply
231+
with `migrateIfNeeded`:
232+
233+
```ts
234+
import { migrateIfNeeded } from '@nanostores/sql'
235+
236+
import migration0000 from './drizzle/0000_create_users.sql?raw'
237+
import migration0001 from './drizzle/0001_add_posts.sql?raw'
238+
239+
const migrations = [migration0000, migration0001]
240+
241+
const $migrationStatus = migrateIfNeeded(
242+
db,
243+
migrations.length,
244+
async prevVersion => {
245+
for (let i = Math.max(0, prevVersion); i < migrations.length; i++) {
246+
await db.driver.exec(migrations[i], [])
247+
}
248+
}
249+
)
250+
```
251+
252+
When you update your Drizzle schema, run `npx drizzle-kit generate` again,
253+
import the new file, and append it to `migrations`.

0 commit comments

Comments
 (0)