drizzle-team / drizzle-team/drizzle-orm
[FEATURE]: Running Migrations in the Browser for Local-First Apps
- Dominant language
- TypeScript
- Stars
- 35.8k
- Forks
- 1.6k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 4
Description
### Describe what you want
I have been experimenting with using Drizzle to interact with an SQLite database running in the browser with WebAssembly for local-first apps. For querying in the browser, Drizzle works really well, but it is not currently possible to run migrations generated with Drizzle Kit in the browser since the `migrate` function requires Node.js packages like `node:crypto` and `node:fs`.
I would like to add a method of executing Drizzle Kit migrations in the browser to Drizzle ORM. I have been prototyping such a system in [this repo](https://github.com/DallasHoff/drizzle-browser-migrate). Let me know what you think. The process currently works like this:
1. Generate the migrations with Drizzle Kit as normal.
`drizzle-kit generate:sqlite --schema ./src/schema.ts --out ./src/migrations`
2. Create an index.ts file that combines the journal file and migrations into a single file for the frontend bundle.
```ts
import { MigrationJournal } from '../migrator/types';
import _journal from './meta/_journal.json';
import Migration0000 from './0000_broad_cardiac.sql?raw';
import Migration0001 from './0001_silly_sumo.sql?raw';
export const journal: MigrationJournal = _journal;
export const migrations: Record = {
'0000_broad_cardiac': Migration0000,
'0001_silly_sumo': Migration0001,
};
```
3. Pass those to a new version of the `migrate` function that does not use Node.js packages.
```ts
import { journal, migrations } from './migrations/';
await migrate(db, { journal, migrations });
```
The usage of the `node:crypto` package is replaced by the Web Crypto API, and the migrations are passed directly instead of read from the filesystem in order to eliminate `node:fs` and because fetching individual migration files over the network would not be ideal.
Ideally, Drizzle Kit could be configured to generate that index.ts file from step 2 or something similar that combines the journal and migrations automatically. Thoughts on this? **Edit: with Expo support out, Drizzle can now almost do this, but the generated file should have a .ts extension instead of .js and the imports it contains of .sql files need to end in `?raw` to support Vite.**
I'd also like feedback on where in the Drizzle repo this new migrator implementation should be placed and how it should be named.
Contributor guide
Assessment
This issue has not been assessed yet.