-
-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
794 additions
and
473 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { readMigrationFiles } from 'drizzle-orm/migrator'; | ||
import { writeFileSync } from 'node:fs'; | ||
import { join } from 'node:path'; | ||
|
||
const dbBase = join(__dirname, '../../src/database'); | ||
const migrationsFolder = join(dbBase, './migrations'); | ||
const migrations = readMigrationFiles({ migrationsFolder: migrationsFolder }); | ||
|
||
writeFileSync( | ||
join(dbBase, './client/migrations.json'), | ||
JSON.stringify(migrations, null, 2), // null, 2 adds indentation for better readability | ||
); | ||
|
||
console.log('🏁 client migrations.json compiled!'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { IdbFs, PGlite } from '@electric-sql/pglite'; | ||
import { vector } from '@electric-sql/pglite/vector'; | ||
import { drizzle } from 'drizzle-orm/pglite'; | ||
|
||
import * as schema from '../schemas'; | ||
|
||
const client = new PGlite({ | ||
extensions: { vector }, | ||
fs: new IdbFs('lobechat'), | ||
relaxedDurability: true, | ||
}); | ||
|
||
export const clientDB = drizzle({ client, schema }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { clientDB } from './db'; | ||
import migrations from './migrations.json'; | ||
|
||
export const migrate = async () => { | ||
//prevent multiple schema migrations to be run | ||
let isLocalDBSchemaSynced = false; | ||
|
||
if (!isLocalDBSchemaSynced) { | ||
const start = Date.now(); | ||
try { | ||
// refs: https://github.com/drizzle-team/drizzle-orm/discussions/2532 | ||
// @ts-ignore | ||
await clientDB.dialect.migrate(migrations, clientDB.session, {}); | ||
isLocalDBSchemaSynced = true; | ||
|
||
console.info(`✅ Local database ready in ${Date.now() - start}ms`); | ||
} catch (cause) { | ||
console.error('❌ Local database schema migration failed', cause); | ||
throw cause; | ||
} | ||
} | ||
|
||
return clientDB; | ||
}; |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.