mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-23 23:19:40 +01:00
core: add support for skipping database initialization
This commit is contained in:
@@ -255,30 +255,10 @@ const DataMappers: Partial<Record<ItemType, (row: any) => void>> = {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export type SQLiteOptions = {
|
async function init(db: Kysely<RawDatabaseSchema>, options: SQLiteOptions) {
|
||||||
dialect: (name: string) => Dialect;
|
|
||||||
journalMode?: "WAL" | "MEMORY" | "OFF" | "PERSIST" | "TRUNCATE" | "DELETE";
|
|
||||||
synchronous?: "normal" | "extra" | "full" | "off";
|
|
||||||
lockingMode?: "normal" | "exclusive";
|
|
||||||
tempStore?: "memory" | "file" | "default";
|
|
||||||
cacheSize?: number;
|
|
||||||
pageSize?: number;
|
|
||||||
password?: string;
|
|
||||||
};
|
|
||||||
export async function createDatabase(name: string, options: SQLiteOptions) {
|
|
||||||
const db = new Kysely<RawDatabaseSchema>({
|
|
||||||
dialect: options.dialect(name),
|
|
||||||
plugins: [new SqliteBooleanPlugin()]
|
|
||||||
});
|
|
||||||
try {
|
try {
|
||||||
if (options.password)
|
if (options.password)
|
||||||
await sql`PRAGMA key = ${sql.ref(options.password)}`.execute(db);
|
await sql`PRAGMA key = ${sql.ref(options.password)}`.execute(db);
|
||||||
|
|
||||||
const migrator = new Migrator({
|
|
||||||
db,
|
|
||||||
provider: new NNMigrationProvider()
|
|
||||||
});
|
|
||||||
|
|
||||||
await sql`PRAGMA journal_mode = ${sql.raw(
|
await sql`PRAGMA journal_mode = ${sql.raw(
|
||||||
options.journalMode || "WAL"
|
options.journalMode || "WAL"
|
||||||
)}`.execute(db);
|
)}`.execute(db);
|
||||||
@@ -309,6 +289,10 @@ export async function createDatabase(name: string, options: SQLiteOptions) {
|
|||||||
db
|
db
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const migrator = new Migrator({
|
||||||
|
db,
|
||||||
|
provider: new NNMigrationProvider()
|
||||||
|
});
|
||||||
const { error, results } = await migrator.migrateToLatest();
|
const { error, results } = await migrator.migrateToLatest();
|
||||||
|
|
||||||
results?.forEach((it) => {
|
results?.forEach((it) => {
|
||||||
@@ -325,11 +309,35 @@ export async function createDatabase(name: string, options: SQLiteOptions) {
|
|||||||
|
|
||||||
return db;
|
return db;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
await db.destroy();
|
await db.destroy();
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type SQLiteOptions = {
|
||||||
|
dialect: (name: string, init?: () => Promise<void>) => Dialect;
|
||||||
|
journalMode?: "WAL" | "MEMORY" | "OFF" | "PERSIST" | "TRUNCATE" | "DELETE";
|
||||||
|
synchronous?: "normal" | "extra" | "full" | "off";
|
||||||
|
lockingMode?: "normal" | "exclusive";
|
||||||
|
tempStore?: "memory" | "file" | "default";
|
||||||
|
cacheSize?: number;
|
||||||
|
pageSize?: number;
|
||||||
|
password?: string;
|
||||||
|
|
||||||
|
skipInitialization?: boolean;
|
||||||
|
};
|
||||||
|
export async function createDatabase(name: string, options: SQLiteOptions) {
|
||||||
|
const db = new Kysely<RawDatabaseSchema>({
|
||||||
|
dialect: options.dialect(name, async () => {
|
||||||
|
await init(db, options);
|
||||||
|
}),
|
||||||
|
plugins: [new SqliteBooleanPlugin()]
|
||||||
|
});
|
||||||
|
if (!options.skipInitialization) return await init(db, options);
|
||||||
|
return db;
|
||||||
|
}
|
||||||
|
|
||||||
export async function changeDatabasePassword(
|
export async function changeDatabasePassword(
|
||||||
db: Kysely<DatabaseSchema>,
|
db: Kysely<DatabaseSchema>,
|
||||||
password?: string
|
password?: string
|
||||||
|
|||||||
Reference in New Issue
Block a user