drizzle-orm 0.30.0 → 0.30.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/expo-sqlite/migrator.cjs +4 -6
- package/expo-sqlite/migrator.cjs.map +1 -1
- package/expo-sqlite/migrator.js +4 -6
- package/expo-sqlite/migrator.js.map +1 -1
- package/op-sqlite/driver.cjs +56 -0
- package/op-sqlite/driver.cjs.map +1 -0
- package/op-sqlite/driver.d.cts +5 -0
- package/op-sqlite/driver.d.ts +5 -0
- package/op-sqlite/driver.js +35 -0
- package/op-sqlite/driver.js.map +1 -0
- package/op-sqlite/index.cjs +25 -0
- package/op-sqlite/index.cjs.map +1 -0
- package/op-sqlite/index.d.cts +2 -0
- package/op-sqlite/index.d.ts +2 -0
- package/op-sqlite/index.js +3 -0
- package/op-sqlite/index.js.map +1 -0
- package/op-sqlite/migrator.cjs +90 -0
- package/op-sqlite/migrator.cjs.map +1 -0
- package/op-sqlite/migrator.d.cts +29 -0
- package/op-sqlite/migrator.d.ts +29 -0
- package/op-sqlite/migrator.js +65 -0
- package/op-sqlite/migrator.js.map +1 -0
- package/op-sqlite/session.cjs +130 -0
- package/op-sqlite/session.cjs.map +1 -0
- package/op-sqlite/session.d.cts +46 -0
- package/op-sqlite/session.d.ts +46 -0
- package/op-sqlite/session.js +107 -0
- package/op-sqlite/session.js.map +1 -0
- package/package.json +103 -50
- package/sqlite-core/dialect.cjs +1 -1
- package/sqlite-core/dialect.cjs.map +1 -1
- package/sqlite-core/dialect.d.cts +1 -1
- package/sqlite-core/dialect.d.ts +1 -1
- package/sqlite-core/dialect.js +1 -1
- package/sqlite-core/dialect.js.map +1 -1
- package/version.cjs +1 -1
- package/version.d.cts +1 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
package/expo-sqlite/migrator.cjs
CHANGED
|
@@ -74,13 +74,11 @@ const useMigrations = (db, migrations) => {
|
|
|
74
74
|
const [state, dispatch] = (0, import_react.useReducer)(fetchReducer, initialState);
|
|
75
75
|
(0, import_react.useEffect)(() => {
|
|
76
76
|
dispatch({ type: "migrating" });
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
});
|
|
81
|
-
} catch (error) {
|
|
77
|
+
migrate(db, migrations).then(() => {
|
|
78
|
+
dispatch({ type: "migrated", payload: true });
|
|
79
|
+
}).catch((error) => {
|
|
82
80
|
dispatch({ type: "error", payload: error });
|
|
83
|
-
}
|
|
81
|
+
});
|
|
84
82
|
}, []);
|
|
85
83
|
return state;
|
|
86
84
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/expo-sqlite/migrator.ts"],"sourcesContent":["import { useEffect, useReducer } from 'react';\nimport type { MigrationMeta } from '~/migrator.ts';\nimport type { ExpoSQLiteDatabase } from './driver.ts';\n\ninterface MigrationConfig {\n\tjournal: {\n\t\tentries: { idx: number; when: number; tag: string; breakpoints: boolean }[];\n\t};\n\tmigrations: Record<string, string>;\n}\n\nasync function readMigrationFiles({ journal, migrations }: MigrationConfig): Promise<MigrationMeta[]> {\n\tconst migrationQueries: MigrationMeta[] = [];\n\n\tfor await (const journalEntry of journal.entries) {\n\t\tconst query = migrations[`m${journalEntry.idx.toString().padStart(4, '0')}`];\n\n\t\tif (!query) {\n\t\t\tthrow new Error(`Missing migration: ${journalEntry.tag}`);\n\t\t}\n\n\t\ttry {\n\t\t\tconst result = query.split('--> statement-breakpoint').map((it) => {\n\t\t\t\treturn it;\n\t\t\t});\n\n\t\t\tmigrationQueries.push({\n\t\t\t\tsql: result,\n\t\t\t\tbps: journalEntry.breakpoints,\n\t\t\t\tfolderMillis: journalEntry.when,\n\t\t\t\thash: '',\n\t\t\t});\n\t\t} catch {\n\t\t\tthrow new Error(`Failed to parse migration: ${journalEntry.tag}`);\n\t\t}\n\t}\n\n\treturn migrationQueries;\n}\n\nexport async function migrate<TSchema extends Record<string, unknown>>(\n\tdb: ExpoSQLiteDatabase<TSchema>,\n\tconfig: MigrationConfig,\n) {\n\tconst migrations = await readMigrationFiles(config);\n\treturn db.dialect.migrate(migrations, db.session);\n}\n\ninterface State {\n\tsuccess: boolean;\n\terror?: Error;\n}\n\ntype Action =\n\t| { type: 'migrating' }\n\t| { type: 'migrated'; payload: true }\n\t| { type: 'error'; payload: Error };\n\nexport const useMigrations = (db: ExpoSQLiteDatabase<any>, migrations: {\n\tjournal: {\n\t\tentries: { idx: number; when: number; tag: string; breakpoints: boolean }[];\n\t};\n\tmigrations: Record<string, string>;\n}): State => {\n\tconst initialState: State = {\n\t\tsuccess: false,\n\t\terror: undefined,\n\t};\n\n\tconst fetchReducer = (state: State, action: Action): State => {\n\t\tswitch (action.type) {\n\t\t\tcase 'migrating': {\n\t\t\t\treturn { ...initialState };\n\t\t\t}\n\t\t\tcase 'migrated': {\n\t\t\t\treturn { ...initialState, success: action.payload };\n\t\t\t}\n\t\t\tcase 'error': {\n\t\t\t\treturn { ...initialState, error: action.payload };\n\t\t\t}\n\t\t\tdefault: {\n\t\t\t\treturn state;\n\t\t\t}\n\t\t}\n\t};\n\n\tconst [state, dispatch] = useReducer(fetchReducer, initialState);\n\n\tuseEffect(() => {\n\t\tdispatch({ type: 'migrating' })
|
|
1
|
+
{"version":3,"sources":["../../src/expo-sqlite/migrator.ts"],"sourcesContent":["import { useEffect, useReducer } from 'react';\nimport type { MigrationMeta } from '~/migrator.ts';\nimport type { ExpoSQLiteDatabase } from './driver.ts';\n\ninterface MigrationConfig {\n\tjournal: {\n\t\tentries: { idx: number; when: number; tag: string; breakpoints: boolean }[];\n\t};\n\tmigrations: Record<string, string>;\n}\n\nasync function readMigrationFiles({ journal, migrations }: MigrationConfig): Promise<MigrationMeta[]> {\n\tconst migrationQueries: MigrationMeta[] = [];\n\n\tfor await (const journalEntry of journal.entries) {\n\t\tconst query = migrations[`m${journalEntry.idx.toString().padStart(4, '0')}`];\n\n\t\tif (!query) {\n\t\t\tthrow new Error(`Missing migration: ${journalEntry.tag}`);\n\t\t}\n\n\t\ttry {\n\t\t\tconst result = query.split('--> statement-breakpoint').map((it) => {\n\t\t\t\treturn it;\n\t\t\t});\n\n\t\t\tmigrationQueries.push({\n\t\t\t\tsql: result,\n\t\t\t\tbps: journalEntry.breakpoints,\n\t\t\t\tfolderMillis: journalEntry.when,\n\t\t\t\thash: '',\n\t\t\t});\n\t\t} catch {\n\t\t\tthrow new Error(`Failed to parse migration: ${journalEntry.tag}`);\n\t\t}\n\t}\n\n\treturn migrationQueries;\n}\n\nexport async function migrate<TSchema extends Record<string, unknown>>(\n\tdb: ExpoSQLiteDatabase<TSchema>,\n\tconfig: MigrationConfig,\n) {\n\tconst migrations = await readMigrationFiles(config);\n\treturn db.dialect.migrate(migrations, db.session);\n}\n\ninterface State {\n\tsuccess: boolean;\n\terror?: Error;\n}\n\ntype Action =\n\t| { type: 'migrating' }\n\t| { type: 'migrated'; payload: true }\n\t| { type: 'error'; payload: Error };\n\nexport const useMigrations = (db: ExpoSQLiteDatabase<any>, migrations: {\n\tjournal: {\n\t\tentries: { idx: number; when: number; tag: string; breakpoints: boolean }[];\n\t};\n\tmigrations: Record<string, string>;\n}): State => {\n\tconst initialState: State = {\n\t\tsuccess: false,\n\t\terror: undefined,\n\t};\n\n\tconst fetchReducer = (state: State, action: Action): State => {\n\t\tswitch (action.type) {\n\t\t\tcase 'migrating': {\n\t\t\t\treturn { ...initialState };\n\t\t\t}\n\t\t\tcase 'migrated': {\n\t\t\t\treturn { ...initialState, success: action.payload };\n\t\t\t}\n\t\t\tcase 'error': {\n\t\t\t\treturn { ...initialState, error: action.payload };\n\t\t\t}\n\t\t\tdefault: {\n\t\t\t\treturn state;\n\t\t\t}\n\t\t}\n\t};\n\n\tconst [state, dispatch] = useReducer(fetchReducer, initialState);\n\n\tuseEffect(() => {\n\t\tdispatch({ type: 'migrating' })\n\t\tmigrate(db, migrations as any).then(() => {\n\t\t\tdispatch({ type: 'migrated', payload: true })\n\t\t}).catch((error) => {\n\t\t\tdispatch({ type: 'error', payload: error as Error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,OAAO,QAAQ;AAAA,MACjD;AAAA,MACA,SAAS;AACR,eAAOA;AAAA,MACR;AAAA,IACD;AAAA,EACD;AAEA,QAAM,CAAC,OAAO,QAAQ,QAAI,yBAAW,cAAc,YAAY;AAE/D,8BAAU,MAAM;AACf,aAAS,EAAE,MAAM,YAAY,CAAC;AAC9B,YAAQ,IAAI,UAAiB,EAAE,KAAK,MAAM;AACzC,eAAS,EAAE,MAAM,YAAY,SAAS,KAAK,CAAC;AAAA,IAC7C,CAAC,EAAE,MAAM,CAAC,UAAU;AACnB,eAAS,EAAE,MAAM,SAAS,SAAS,MAAe,CAAC;AAAA,IACpD,CAAC;AAAA,EACF,GAAG,CAAC,CAAC;AAEL,SAAO;AACR;","names":["state"]}
|
package/expo-sqlite/migrator.js
CHANGED
|
@@ -50,13 +50,11 @@ const useMigrations = (db, migrations) => {
|
|
|
50
50
|
const [state, dispatch] = useReducer(fetchReducer, initialState);
|
|
51
51
|
useEffect(() => {
|
|
52
52
|
dispatch({ type: "migrating" });
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
});
|
|
57
|
-
} catch (error) {
|
|
53
|
+
migrate(db, migrations).then(() => {
|
|
54
|
+
dispatch({ type: "migrated", payload: true });
|
|
55
|
+
}).catch((error) => {
|
|
58
56
|
dispatch({ type: "error", payload: error });
|
|
59
|
-
}
|
|
57
|
+
});
|
|
60
58
|
}, []);
|
|
61
59
|
return state;
|
|
62
60
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/expo-sqlite/migrator.ts"],"sourcesContent":["import { useEffect, useReducer } from 'react';\nimport type { MigrationMeta } from '~/migrator.ts';\nimport type { ExpoSQLiteDatabase } from './driver.ts';\n\ninterface MigrationConfig {\n\tjournal: {\n\t\tentries: { idx: number; when: number; tag: string; breakpoints: boolean }[];\n\t};\n\tmigrations: Record<string, string>;\n}\n\nasync function readMigrationFiles({ journal, migrations }: MigrationConfig): Promise<MigrationMeta[]> {\n\tconst migrationQueries: MigrationMeta[] = [];\n\n\tfor await (const journalEntry of journal.entries) {\n\t\tconst query = migrations[`m${journalEntry.idx.toString().padStart(4, '0')}`];\n\n\t\tif (!query) {\n\t\t\tthrow new Error(`Missing migration: ${journalEntry.tag}`);\n\t\t}\n\n\t\ttry {\n\t\t\tconst result = query.split('--> statement-breakpoint').map((it) => {\n\t\t\t\treturn it;\n\t\t\t});\n\n\t\t\tmigrationQueries.push({\n\t\t\t\tsql: result,\n\t\t\t\tbps: journalEntry.breakpoints,\n\t\t\t\tfolderMillis: journalEntry.when,\n\t\t\t\thash: '',\n\t\t\t});\n\t\t} catch {\n\t\t\tthrow new Error(`Failed to parse migration: ${journalEntry.tag}`);\n\t\t}\n\t}\n\n\treturn migrationQueries;\n}\n\nexport async function migrate<TSchema extends Record<string, unknown>>(\n\tdb: ExpoSQLiteDatabase<TSchema>,\n\tconfig: MigrationConfig,\n) {\n\tconst migrations = await readMigrationFiles(config);\n\treturn db.dialect.migrate(migrations, db.session);\n}\n\ninterface State {\n\tsuccess: boolean;\n\terror?: Error;\n}\n\ntype Action =\n\t| { type: 'migrating' }\n\t| { type: 'migrated'; payload: true }\n\t| { type: 'error'; payload: Error };\n\nexport const useMigrations = (db: ExpoSQLiteDatabase<any>, migrations: {\n\tjournal: {\n\t\tentries: { idx: number; when: number; tag: string; breakpoints: boolean }[];\n\t};\n\tmigrations: Record<string, string>;\n}): State => {\n\tconst initialState: State = {\n\t\tsuccess: false,\n\t\terror: undefined,\n\t};\n\n\tconst fetchReducer = (state: State, action: Action): State => {\n\t\tswitch (action.type) {\n\t\t\tcase 'migrating': {\n\t\t\t\treturn { ...initialState };\n\t\t\t}\n\t\t\tcase 'migrated': {\n\t\t\t\treturn { ...initialState, success: action.payload };\n\t\t\t}\n\t\t\tcase 'error': {\n\t\t\t\treturn { ...initialState, error: action.payload };\n\t\t\t}\n\t\t\tdefault: {\n\t\t\t\treturn state;\n\t\t\t}\n\t\t}\n\t};\n\n\tconst [state, dispatch] = useReducer(fetchReducer, initialState);\n\n\tuseEffect(() => {\n\t\tdispatch({ type: 'migrating' })
|
|
1
|
+
{"version":3,"sources":["../../src/expo-sqlite/migrator.ts"],"sourcesContent":["import { useEffect, useReducer } from 'react';\nimport type { MigrationMeta } from '~/migrator.ts';\nimport type { ExpoSQLiteDatabase } from './driver.ts';\n\ninterface MigrationConfig {\n\tjournal: {\n\t\tentries: { idx: number; when: number; tag: string; breakpoints: boolean }[];\n\t};\n\tmigrations: Record<string, string>;\n}\n\nasync function readMigrationFiles({ journal, migrations }: MigrationConfig): Promise<MigrationMeta[]> {\n\tconst migrationQueries: MigrationMeta[] = [];\n\n\tfor await (const journalEntry of journal.entries) {\n\t\tconst query = migrations[`m${journalEntry.idx.toString().padStart(4, '0')}`];\n\n\t\tif (!query) {\n\t\t\tthrow new Error(`Missing migration: ${journalEntry.tag}`);\n\t\t}\n\n\t\ttry {\n\t\t\tconst result = query.split('--> statement-breakpoint').map((it) => {\n\t\t\t\treturn it;\n\t\t\t});\n\n\t\t\tmigrationQueries.push({\n\t\t\t\tsql: result,\n\t\t\t\tbps: journalEntry.breakpoints,\n\t\t\t\tfolderMillis: journalEntry.when,\n\t\t\t\thash: '',\n\t\t\t});\n\t\t} catch {\n\t\t\tthrow new Error(`Failed to parse migration: ${journalEntry.tag}`);\n\t\t}\n\t}\n\n\treturn migrationQueries;\n}\n\nexport async function migrate<TSchema extends Record<string, unknown>>(\n\tdb: ExpoSQLiteDatabase<TSchema>,\n\tconfig: MigrationConfig,\n) {\n\tconst migrations = await readMigrationFiles(config);\n\treturn db.dialect.migrate(migrations, db.session);\n}\n\ninterface State {\n\tsuccess: boolean;\n\terror?: Error;\n}\n\ntype Action =\n\t| { type: 'migrating' }\n\t| { type: 'migrated'; payload: true }\n\t| { type: 'error'; payload: Error };\n\nexport const useMigrations = (db: ExpoSQLiteDatabase<any>, migrations: {\n\tjournal: {\n\t\tentries: { idx: number; when: number; tag: string; breakpoints: boolean }[];\n\t};\n\tmigrations: Record<string, string>;\n}): State => {\n\tconst initialState: State = {\n\t\tsuccess: false,\n\t\terror: undefined,\n\t};\n\n\tconst fetchReducer = (state: State, action: Action): State => {\n\t\tswitch (action.type) {\n\t\t\tcase 'migrating': {\n\t\t\t\treturn { ...initialState };\n\t\t\t}\n\t\t\tcase 'migrated': {\n\t\t\t\treturn { ...initialState, success: action.payload };\n\t\t\t}\n\t\t\tcase 'error': {\n\t\t\t\treturn { ...initialState, error: action.payload };\n\t\t\t}\n\t\t\tdefault: {\n\t\t\t\treturn state;\n\t\t\t}\n\t\t}\n\t};\n\n\tconst [state, dispatch] = useReducer(fetchReducer, initialState);\n\n\tuseEffect(() => {\n\t\tdispatch({ type: 'migrating' })\n\t\tmigrate(db, migrations as any).then(() => {\n\t\t\tdispatch({ type: 'migrated', payload: true })\n\t\t}).catch((error) => {\n\t\t\tdispatch({ type: 'error', payload: error as Error })\n\t\t});\n\t}, []);\n\n\treturn state;\n};\n"],"mappings":"AAAA,SAAS,WAAW,kBAAkB;AAWtC,eAAe,mBAAmB,EAAE,SAAS,WAAW,GAA8C;AACrG,QAAM,mBAAoC,CAAC;AAE3C,mBAAiB,gBAAgB,QAAQ,SAAS;AACjD,UAAM,QAAQ,WAAW,IAAI,aAAa,IAAI,SAAS,EAAE,SAAS,GAAG,GAAG,CAAC,EAAE;AAE3E,QAAI,CAAC,OAAO;AACX,YAAM,IAAI,MAAM,sBAAsB,aAAa,GAAG,EAAE;AAAA,IACzD;AAEA,QAAI;AACH,YAAM,SAAS,MAAM,MAAM,0BAA0B,EAAE,IAAI,CAAC,OAAO;AAClE,eAAO;AAAA,MACR,CAAC;AAED,uBAAiB,KAAK;AAAA,QACrB,KAAK;AAAA,QACL,KAAK,aAAa;AAAA,QAClB,cAAc,aAAa;AAAA,QAC3B,MAAM;AAAA,MACP,CAAC;AAAA,IACF,QAAQ;AACP,YAAM,IAAI,MAAM,8BAA8B,aAAa,GAAG,EAAE;AAAA,IACjE;AAAA,EACD;AAEA,SAAO;AACR;AAEA,eAAsB,QACrB,IACA,QACC;AACD,QAAM,aAAa,MAAM,mBAAmB,MAAM;AAClD,SAAO,GAAG,QAAQ,QAAQ,YAAY,GAAG,OAAO;AACjD;AAYO,MAAM,gBAAgB,CAAC,IAA6B,eAK9C;AACZ,QAAM,eAAsB;AAAA,IAC3B,SAAS;AAAA,IACT,OAAO;AAAA,EACR;AAEA,QAAM,eAAe,CAACA,QAAc,WAA0B;AAC7D,YAAQ,OAAO,MAAM;AAAA,MACpB,KAAK,aAAa;AACjB,eAAO,EAAE,GAAG,aAAa;AAAA,MAC1B;AAAA,MACA,KAAK,YAAY;AAChB,eAAO,EAAE,GAAG,cAAc,SAAS,OAAO,QAAQ;AAAA,MACnD;AAAA,MACA,KAAK,SAAS;AACb,eAAO,EAAE,GAAG,cAAc,OAAO,OAAO,QAAQ;AAAA,MACjD;AAAA,MACA,SAAS;AACR,eAAOA;AAAA,MACR;AAAA,IACD;AAAA,EACD;AAEA,QAAM,CAAC,OAAO,QAAQ,IAAI,WAAW,cAAc,YAAY;AAE/D,YAAU,MAAM;AACf,aAAS,EAAE,MAAM,YAAY,CAAC;AAC9B,YAAQ,IAAI,UAAiB,EAAE,KAAK,MAAM;AACzC,eAAS,EAAE,MAAM,YAAY,SAAS,KAAK,CAAC;AAAA,IAC7C,CAAC,EAAE,MAAM,CAAC,UAAU;AACnB,eAAS,EAAE,MAAM,SAAS,SAAS,MAAe,CAAC;AAAA,IACpD,CAAC;AAAA,EACF,GAAG,CAAC,CAAC;AAEL,SAAO;AACR;","names":["state"]}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var driver_exports = {};
|
|
20
|
+
__export(driver_exports, {
|
|
21
|
+
drizzle: () => drizzle
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(driver_exports);
|
|
24
|
+
var import_logger = require("../logger.cjs");
|
|
25
|
+
var import_relations = require("../relations.cjs");
|
|
26
|
+
var import_db = require("../sqlite-core/db.cjs");
|
|
27
|
+
var import_dialect = require("../sqlite-core/dialect.cjs");
|
|
28
|
+
var import_session = require("./session.cjs");
|
|
29
|
+
function drizzle(client, config = {}) {
|
|
30
|
+
const dialect = new import_dialect.SQLiteAsyncDialect();
|
|
31
|
+
let logger;
|
|
32
|
+
if (config.logger === true) {
|
|
33
|
+
logger = new import_logger.DefaultLogger();
|
|
34
|
+
} else if (config.logger !== false) {
|
|
35
|
+
logger = config.logger;
|
|
36
|
+
}
|
|
37
|
+
let schema;
|
|
38
|
+
if (config.schema) {
|
|
39
|
+
const tablesConfig = (0, import_relations.extractTablesRelationalConfig)(
|
|
40
|
+
config.schema,
|
|
41
|
+
import_relations.createTableRelationsHelpers
|
|
42
|
+
);
|
|
43
|
+
schema = {
|
|
44
|
+
fullSchema: config.schema,
|
|
45
|
+
schema: tablesConfig.tables,
|
|
46
|
+
tableNamesMap: tablesConfig.tableNamesMap
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
const session = new import_session.OPSQLiteSession(client, dialect, schema, { logger });
|
|
50
|
+
return new import_db.BaseSQLiteDatabase("async", dialect, session, schema);
|
|
51
|
+
}
|
|
52
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
53
|
+
0 && (module.exports = {
|
|
54
|
+
drizzle
|
|
55
|
+
});
|
|
56
|
+
//# sourceMappingURL=driver.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/op-sqlite/driver.ts"],"sourcesContent":["import type { OPSQLiteConnection, QueryResult } from '@op-engineering/op-sqlite';\nimport { DefaultLogger } from '~/logger.ts';\nimport {\n createTableRelationsHelpers,\n extractTablesRelationalConfig,\n type RelationalSchemaConfig,\n type TablesRelationalConfig,\n} from '~/relations.ts';\nimport { BaseSQLiteDatabase } from '~/sqlite-core/db.ts';\nimport { SQLiteAsyncDialect } from '~/sqlite-core/dialect.ts';\nimport type { DrizzleConfig } from '~/utils.ts';\nimport { OPSQLiteSession } from './session.ts';\n\nexport type OPSQLiteDatabase<\n TSchema extends Record<string, unknown> = Record<string, never>,\n> = BaseSQLiteDatabase<'async', QueryResult, TSchema>;\n\nexport function drizzle<TSchema extends Record<string, unknown> = Record<string, never>>(\n client: OPSQLiteConnection,\n config: DrizzleConfig<TSchema> = {},\n): OPSQLiteDatabase<TSchema> {\n const dialect = new SQLiteAsyncDialect();\n let logger;\n if (config.logger === true) {\n logger = new DefaultLogger();\n } else if (config.logger !== false) {\n logger = config.logger;\n }\n\n let schema: RelationalSchemaConfig<TablesRelationalConfig> | undefined;\n if (config.schema) {\n const tablesConfig = extractTablesRelationalConfig(\n config.schema,\n createTableRelationsHelpers,\n );\n schema = {\n fullSchema: config.schema,\n schema: tablesConfig.tables,\n tableNamesMap: tablesConfig.tableNamesMap,\n };\n }\n\n const session = new OPSQLiteSession(client, dialect, schema, { logger });\n return new BaseSQLiteDatabase('async', dialect, session, schema) as OPSQLiteDatabase<TSchema>;\n}"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,oBAA8B;AAC9B,uBAKO;AACP,gBAAmC;AACnC,qBAAmC;AAEnC,qBAAgC;AAMzB,SAAS,QACZ,QACA,SAAiC,CAAC,GACT;AACzB,QAAM,UAAU,IAAI,kCAAmB;AACvC,MAAI;AACJ,MAAI,OAAO,WAAW,MAAM;AACxB,aAAS,IAAI,4BAAc;AAAA,EAC/B,WAAW,OAAO,WAAW,OAAO;AAChC,aAAS,OAAO;AAAA,EACpB;AAEA,MAAI;AACJ,MAAI,OAAO,QAAQ;AACf,UAAM,mBAAe;AAAA,MACjB,OAAO;AAAA,MACP;AAAA,IACJ;AACA,aAAS;AAAA,MACL,YAAY,OAAO;AAAA,MACnB,QAAQ,aAAa;AAAA,MACrB,eAAe,aAAa;AAAA,IAChC;AAAA,EACJ;AAEA,QAAM,UAAU,IAAI,+BAAgB,QAAQ,SAAS,QAAQ,EAAE,OAAO,CAAC;AACvE,SAAO,IAAI,6BAAmB,SAAS,SAAS,SAAS,MAAM;AACnE;","names":[]}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { OPSQLiteConnection, QueryResult } from '@op-engineering/op-sqlite';
|
|
2
|
+
import { BaseSQLiteDatabase } from "../sqlite-core/db.cjs";
|
|
3
|
+
import type { DrizzleConfig } from "../utils.cjs";
|
|
4
|
+
export type OPSQLiteDatabase<TSchema extends Record<string, unknown> = Record<string, never>> = BaseSQLiteDatabase<'async', QueryResult, TSchema>;
|
|
5
|
+
export declare function drizzle<TSchema extends Record<string, unknown> = Record<string, never>>(client: OPSQLiteConnection, config?: DrizzleConfig<TSchema>): OPSQLiteDatabase<TSchema>;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { OPSQLiteConnection, QueryResult } from '@op-engineering/op-sqlite';
|
|
2
|
+
import { BaseSQLiteDatabase } from "../sqlite-core/db.js";
|
|
3
|
+
import type { DrizzleConfig } from "../utils.js";
|
|
4
|
+
export type OPSQLiteDatabase<TSchema extends Record<string, unknown> = Record<string, never>> = BaseSQLiteDatabase<'async', QueryResult, TSchema>;
|
|
5
|
+
export declare function drizzle<TSchema extends Record<string, unknown> = Record<string, never>>(client: OPSQLiteConnection, config?: DrizzleConfig<TSchema>): OPSQLiteDatabase<TSchema>;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { DefaultLogger } from "../logger.js";
|
|
2
|
+
import {
|
|
3
|
+
createTableRelationsHelpers,
|
|
4
|
+
extractTablesRelationalConfig
|
|
5
|
+
} from "../relations.js";
|
|
6
|
+
import { BaseSQLiteDatabase } from "../sqlite-core/db.js";
|
|
7
|
+
import { SQLiteAsyncDialect } from "../sqlite-core/dialect.js";
|
|
8
|
+
import { OPSQLiteSession } from "./session.js";
|
|
9
|
+
function drizzle(client, config = {}) {
|
|
10
|
+
const dialect = new SQLiteAsyncDialect();
|
|
11
|
+
let logger;
|
|
12
|
+
if (config.logger === true) {
|
|
13
|
+
logger = new DefaultLogger();
|
|
14
|
+
} else if (config.logger !== false) {
|
|
15
|
+
logger = config.logger;
|
|
16
|
+
}
|
|
17
|
+
let schema;
|
|
18
|
+
if (config.schema) {
|
|
19
|
+
const tablesConfig = extractTablesRelationalConfig(
|
|
20
|
+
config.schema,
|
|
21
|
+
createTableRelationsHelpers
|
|
22
|
+
);
|
|
23
|
+
schema = {
|
|
24
|
+
fullSchema: config.schema,
|
|
25
|
+
schema: tablesConfig.tables,
|
|
26
|
+
tableNamesMap: tablesConfig.tableNamesMap
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
const session = new OPSQLiteSession(client, dialect, schema, { logger });
|
|
30
|
+
return new BaseSQLiteDatabase("async", dialect, session, schema);
|
|
31
|
+
}
|
|
32
|
+
export {
|
|
33
|
+
drizzle
|
|
34
|
+
};
|
|
35
|
+
//# sourceMappingURL=driver.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/op-sqlite/driver.ts"],"sourcesContent":["import type { OPSQLiteConnection, QueryResult } from '@op-engineering/op-sqlite';\nimport { DefaultLogger } from '~/logger.ts';\nimport {\n createTableRelationsHelpers,\n extractTablesRelationalConfig,\n type RelationalSchemaConfig,\n type TablesRelationalConfig,\n} from '~/relations.ts';\nimport { BaseSQLiteDatabase } from '~/sqlite-core/db.ts';\nimport { SQLiteAsyncDialect } from '~/sqlite-core/dialect.ts';\nimport type { DrizzleConfig } from '~/utils.ts';\nimport { OPSQLiteSession } from './session.ts';\n\nexport type OPSQLiteDatabase<\n TSchema extends Record<string, unknown> = Record<string, never>,\n> = BaseSQLiteDatabase<'async', QueryResult, TSchema>;\n\nexport function drizzle<TSchema extends Record<string, unknown> = Record<string, never>>(\n client: OPSQLiteConnection,\n config: DrizzleConfig<TSchema> = {},\n): OPSQLiteDatabase<TSchema> {\n const dialect = new SQLiteAsyncDialect();\n let logger;\n if (config.logger === true) {\n logger = new DefaultLogger();\n } else if (config.logger !== false) {\n logger = config.logger;\n }\n\n let schema: RelationalSchemaConfig<TablesRelationalConfig> | undefined;\n if (config.schema) {\n const tablesConfig = extractTablesRelationalConfig(\n config.schema,\n createTableRelationsHelpers,\n );\n schema = {\n fullSchema: config.schema,\n schema: tablesConfig.tables,\n tableNamesMap: tablesConfig.tableNamesMap,\n };\n }\n\n const session = new OPSQLiteSession(client, dialect, schema, { logger });\n return new BaseSQLiteDatabase('async', dialect, session, schema) as OPSQLiteDatabase<TSchema>;\n}"],"mappings":"AACA,SAAS,qBAAqB;AAC9B;AAAA,EACI;AAAA,EACA;AAAA,OAGG;AACP,SAAS,0BAA0B;AACnC,SAAS,0BAA0B;AAEnC,SAAS,uBAAuB;AAMzB,SAAS,QACZ,QACA,SAAiC,CAAC,GACT;AACzB,QAAM,UAAU,IAAI,mBAAmB;AACvC,MAAI;AACJ,MAAI,OAAO,WAAW,MAAM;AACxB,aAAS,IAAI,cAAc;AAAA,EAC/B,WAAW,OAAO,WAAW,OAAO;AAChC,aAAS,OAAO;AAAA,EACpB;AAEA,MAAI;AACJ,MAAI,OAAO,QAAQ;AACf,UAAM,eAAe;AAAA,MACjB,OAAO;AAAA,MACP;AAAA,IACJ;AACA,aAAS;AAAA,MACL,YAAY,OAAO;AAAA,MACnB,QAAQ,aAAa;AAAA,MACrB,eAAe,aAAa;AAAA,IAChC;AAAA,EACJ;AAEA,QAAM,UAAU,IAAI,gBAAgB,QAAQ,SAAS,QAAQ,EAAE,OAAO,CAAC;AACvE,SAAO,IAAI,mBAAmB,SAAS,SAAS,SAAS,MAAM;AACnE;","names":[]}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
15
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
16
|
+
var op_sqlite_exports = {};
|
|
17
|
+
module.exports = __toCommonJS(op_sqlite_exports);
|
|
18
|
+
__reExport(op_sqlite_exports, require("./driver.cjs"), module.exports);
|
|
19
|
+
__reExport(op_sqlite_exports, require("./session.cjs"), module.exports);
|
|
20
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
21
|
+
0 && (module.exports = {
|
|
22
|
+
...require("./driver.cjs"),
|
|
23
|
+
...require("./session.cjs")
|
|
24
|
+
});
|
|
25
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/op-sqlite/index.ts"],"sourcesContent":["export * from './driver.ts';\nexport * from './session.ts';"],"mappings":";;;;;;;;;;;;;;;AAAA;AAAA;AAAA,8BAAc,wBAAd;AACA,8BAAc,yBADd;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/op-sqlite/index.ts"],"sourcesContent":["export * from './driver.ts';\nexport * from './session.ts';"],"mappings":"AAAA,cAAc;AACd,cAAc;","names":[]}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var migrator_exports = {};
|
|
20
|
+
__export(migrator_exports, {
|
|
21
|
+
migrate: () => migrate,
|
|
22
|
+
useMigrations: () => useMigrations
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(migrator_exports);
|
|
25
|
+
var import_react = require("react");
|
|
26
|
+
async function readMigrationFiles({ journal, migrations }) {
|
|
27
|
+
const migrationQueries = [];
|
|
28
|
+
for await (const journalEntry of journal.entries) {
|
|
29
|
+
const query = migrations[`m${journalEntry.idx.toString().padStart(4, "0")}`];
|
|
30
|
+
if (!query) {
|
|
31
|
+
throw new Error(`Missing migration: ${journalEntry.tag}`);
|
|
32
|
+
}
|
|
33
|
+
try {
|
|
34
|
+
const result = query.split("--> statement-breakpoint").map((it) => {
|
|
35
|
+
return it;
|
|
36
|
+
});
|
|
37
|
+
migrationQueries.push({
|
|
38
|
+
sql: result,
|
|
39
|
+
bps: journalEntry.breakpoints,
|
|
40
|
+
folderMillis: journalEntry.when,
|
|
41
|
+
hash: ""
|
|
42
|
+
});
|
|
43
|
+
} catch {
|
|
44
|
+
throw new Error(`Failed to parse migration: ${journalEntry.tag}`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return migrationQueries;
|
|
48
|
+
}
|
|
49
|
+
async function migrate(db, config) {
|
|
50
|
+
const migrations = await readMigrationFiles(config);
|
|
51
|
+
return db.dialect.migrate(migrations, db.session);
|
|
52
|
+
}
|
|
53
|
+
const useMigrations = (db, migrations) => {
|
|
54
|
+
const initialState = {
|
|
55
|
+
success: false,
|
|
56
|
+
error: void 0
|
|
57
|
+
};
|
|
58
|
+
const fetchReducer = (state2, action) => {
|
|
59
|
+
switch (action.type) {
|
|
60
|
+
case "migrating": {
|
|
61
|
+
return { ...initialState };
|
|
62
|
+
}
|
|
63
|
+
case "migrated": {
|
|
64
|
+
return { ...initialState, success: action.payload };
|
|
65
|
+
}
|
|
66
|
+
case "error": {
|
|
67
|
+
return { ...initialState, error: action.payload };
|
|
68
|
+
}
|
|
69
|
+
default: {
|
|
70
|
+
return state2;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
const [state, dispatch] = (0, import_react.useReducer)(fetchReducer, initialState);
|
|
75
|
+
(0, import_react.useEffect)(() => {
|
|
76
|
+
dispatch({ type: "migrating" });
|
|
77
|
+
migrate(db, migrations).then(() => {
|
|
78
|
+
dispatch({ type: "migrated", payload: true });
|
|
79
|
+
}).catch((error) => {
|
|
80
|
+
dispatch({ type: "error", payload: error });
|
|
81
|
+
});
|
|
82
|
+
}, []);
|
|
83
|
+
return state;
|
|
84
|
+
};
|
|
85
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
86
|
+
0 && (module.exports = {
|
|
87
|
+
migrate,
|
|
88
|
+
useMigrations
|
|
89
|
+
});
|
|
90
|
+
//# sourceMappingURL=migrator.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/op-sqlite/migrator.ts"],"sourcesContent":["import { useEffect, useReducer } from \"react\";\nimport type { MigrationMeta } from '~/migrator.ts';\nimport type { OPSQLiteDatabase } from './driver.ts';\n\ninterface MigrationConfig {\n journal: {\n entries: { idx: number; when: number; tag: string; breakpoints: boolean }[];\n };\n migrations: Record<string, string>;\n}\n\nasync function readMigrationFiles({ journal, migrations }: MigrationConfig): Promise<MigrationMeta[]> {\n const migrationQueries: MigrationMeta[] = [];\n\n for await (const journalEntry of journal.entries) {\n const query = migrations[`m${journalEntry.idx.toString().padStart(4, '0')}`];\n\n if (!query) {\n throw new Error(`Missing migration: ${journalEntry.tag}`);\n }\n\n try {\n const result = query.split('--> statement-breakpoint').map((it) => {\n return it;\n });\n\n migrationQueries.push({\n sql: result,\n bps: journalEntry.breakpoints,\n folderMillis: journalEntry.when,\n hash: '',\n });\n } catch {\n throw new Error(`Failed to parse migration: ${journalEntry.tag}`);\n }\n }\n\n return migrationQueries;\n}\n\nexport async function migrate<TSchema extends Record<string, unknown>>(\n db: OPSQLiteDatabase<TSchema>,\n config: MigrationConfig,\n) {\n const migrations = await readMigrationFiles(config);\n return db.dialect.migrate(migrations, db.session);\n}\n\ninterface State {\n\tsuccess: boolean;\n\terror?: Error;\n}\n\ntype Action =\n\t| { type: 'migrating' }\n\t| { type: 'migrated'; payload: true }\n\t| { type: 'error'; payload: Error }\n\nexport const useMigrations = (db: OPSQLiteDatabase<any>, migrations: {\n\tjournal: {\n\t\tentries: { idx: number; when: number; tag: string; breakpoints: boolean }[];\n\t};\n\tmigrations: Record<string, string>;\n}): State => {\n\tconst initialState: State = {\n\t\tsuccess: false,\n\t\terror: undefined,\n\t}\n\n\tconst fetchReducer = (state: State, action: Action): State => {\n\t\tswitch (action.type) {\n\t\t\tcase 'migrating': {\n\t\t\t\treturn { ...initialState }\n\t\t\t}\n\t\t\tcase 'migrated': {\n\t\t\t\treturn { ...initialState, success: action.payload }\n\t\t\t}\n\t\t\tcase 'error': {\n\t\t\t\treturn { ...initialState, error: action.payload }\n\t\t\t}\n\t\t\tdefault: {\n\t\t\t\treturn state\n\t\t\t}\n\t\t}\n\t}\n\n\tconst [state, dispatch] = useReducer(fetchReducer, initialState);\n\n\tuseEffect(() => {\n\t\tdispatch({ type: 'migrating' })\n\t\tmigrate(db, migrations).then(() => {\n\t\t\tdispatch({ type: 'migrated', payload: true })\n\t\t}).catch((error) => {\n\t\t\tdispatch({ type: 'error', payload: error as Error })\n\t\t});\n\t}, []);\n\n\treturn state;\n}"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAsC;AAWtC,eAAe,mBAAmB,EAAE,SAAS,WAAW,GAA8C;AAClG,QAAM,mBAAoC,CAAC;AAE3C,mBAAiB,gBAAgB,QAAQ,SAAS;AAC9C,UAAM,QAAQ,WAAW,IAAI,aAAa,IAAI,SAAS,EAAE,SAAS,GAAG,GAAG,CAAC,EAAE;AAE3E,QAAI,CAAC,OAAO;AACR,YAAM,IAAI,MAAM,sBAAsB,aAAa,GAAG,EAAE;AAAA,IAC5D;AAEA,QAAI;AACA,YAAM,SAAS,MAAM,MAAM,0BAA0B,EAAE,IAAI,CAAC,OAAO;AAC/D,eAAO;AAAA,MACX,CAAC;AAED,uBAAiB,KAAK;AAAA,QAClB,K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2
|
+
interface MigrationConfig {
|
|
3
|
+
journal: {
|
|
4
|
+
entries: {
|
|
5
|
+
idx: number;
|
|
6
|
+
when: number;
|
|
7
|
+
tag: string;
|
|
8
|
+
breakpoints: boolean;
|
|
9
|
+
}[];
|
|
10
|
+
};
|
|
11
|
+
migrations: Record<string, string>;
|
|
12
|
+
}
|
|
13
|
+
export declare function migrate<TSchema extends Record<string, unknown>>(db: OPSQLiteDatabase<TSchema>, config: MigrationConfig): Promise<void>;
|
|
14
|
+
interface State {
|
|
15
|
+
success: boolean;
|
|
16
|
+
error?: Error;
|
|
17
|
+
}
|
|
18
|
+
export declare const useMigrations: (db: OPSQLiteDatabase<any>, migrations: {
|
|
19
|
+
journal: {
|
|
20
|
+
entries: {
|
|
21
|
+
idx: number;
|
|
22
|
+
when: number;
|
|
23
|
+
tag: string;
|
|
24
|
+
breakpoints: boolean;
|
|
25
|
+
}[];
|
|
26
|
+
};
|
|
27
|
+
migrations: Record<string, string>;
|
|
28
|
+
}) => State;
|
|
29
|
+
export {};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { OPSQLiteDatabase } from "./driver.js";
|
|
2
|
+
interface MigrationConfig {
|
|
3
|
+
journal: {
|
|
4
|
+
entries: {
|
|
5
|
+
idx: number;
|
|
6
|
+
when: number;
|
|
7
|
+
tag: string;
|
|
8
|
+
breakpoints: boolean;
|
|
9
|
+
}[];
|
|
10
|
+
};
|
|
11
|
+
migrations: Record<string, string>;
|
|
12
|
+
}
|
|
13
|
+
export declare function migrate<TSchema extends Record<string, unknown>>(db: OPSQLiteDatabase<TSchema>, config: MigrationConfig): Promise<void>;
|
|
14
|
+
interface State {
|
|
15
|
+
success: boolean;
|
|
16
|
+
error?: Error;
|
|
17
|
+
}
|
|
18
|
+
export declare const useMigrations: (db: OPSQLiteDatabase<any>, migrations: {
|
|
19
|
+
journal: {
|
|
20
|
+
entries: {
|
|
21
|
+
idx: number;
|
|
22
|
+
when: number;
|
|
23
|
+
tag: string;
|
|
24
|
+
breakpoints: boolean;
|
|
25
|
+
}[];
|
|
26
|
+
};
|
|
27
|
+
migrations: Record<string, string>;
|
|
28
|
+
}) => State;
|
|
29
|
+
export {};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { useEffect, useReducer } from "react";
|
|
2
|
+
async function readMigrationFiles({ journal, migrations }) {
|
|
3
|
+
const migrationQueries = [];
|
|
4
|
+
for await (const journalEntry of journal.entries) {
|
|
5
|
+
const query = migrations[`m${journalEntry.idx.toString().padStart(4, "0")}`];
|
|
6
|
+
if (!query) {
|
|
7
|
+
throw new Error(`Missing migration: ${journalEntry.tag}`);
|
|
8
|
+
}
|
|
9
|
+
try {
|
|
10
|
+
const result = query.split("--> statement-breakpoint").map((it) => {
|
|
11
|
+
return it;
|
|
12
|
+
});
|
|
13
|
+
migrationQueries.push({
|
|
14
|
+
sql: result,
|
|
15
|
+
bps: journalEntry.breakpoints,
|
|
16
|
+
folderMillis: journalEntry.when,
|
|
17
|
+
hash: ""
|
|
18
|
+
});
|
|
19
|
+
} catch {
|
|
20
|
+
throw new Error(`Failed to parse migration: ${journalEntry.tag}`);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return migrationQueries;
|
|
24
|
+
}
|
|
25
|
+
async function migrate(db, config) {
|
|
26
|
+
const migrations = await readMigrationFiles(config);
|
|
27
|
+
return db.dialect.migrate(migrations, db.session);
|
|
28
|
+
}
|
|
29
|
+
const useMigrations = (db, migrations) => {
|
|
30
|
+
const initialState = {
|
|
31
|
+
success: false,
|
|
32
|
+
error: void 0
|
|
33
|
+
};
|
|
34
|
+
const fetchReducer = (state2, action) => {
|
|
35
|
+
switch (action.type) {
|
|
36
|
+
case "migrating": {
|
|
37
|
+
return { ...initialState };
|
|
38
|
+
}
|
|
39
|
+
case "migrated": {
|
|
40
|
+
return { ...initialState, success: action.payload };
|
|
41
|
+
}
|
|
42
|
+
case "error": {
|
|
43
|
+
return { ...initialState, error: action.payload };
|
|
44
|
+
}
|
|
45
|
+
default: {
|
|
46
|
+
return state2;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
const [state, dispatch] = useReducer(fetchReducer, initialState);
|
|
51
|
+
useEffect(() => {
|
|
52
|
+
dispatch({ type: "migrating" });
|
|
53
|
+
migrate(db, migrations).then(() => {
|
|
54
|
+
dispatch({ type: "migrated", payload: true });
|
|
55
|
+
}).catch((error) => {
|
|
56
|
+
dispatch({ type: "error", payload: error });
|
|
57
|
+
});
|
|
58
|
+
}, []);
|
|
59
|
+
return state;
|
|
60
|
+
};
|
|
61
|
+
export {
|
|
62
|
+
migrate,
|
|
63
|
+
useMigrations
|
|
64
|
+
};
|
|
65
|
+
//# sourceMappingURL=migrator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/op-sqlite/migrator.ts"],"sourcesContent":["import { useEffect, useReducer } from \"react\";\nimport type { MigrationMeta } from '~/migrator.ts';\nimport type { OPSQLiteDatabase } from './driver.ts';\n\ninterface MigrationConfig {\n journal: {\n entries: { idx: number; when: number; tag: string; breakpoints: boolean }[];\n };\n migrations: Record<string, string>;\n}\n\nasync function readMigrationFiles({ journal, migrations }: MigrationConfig): Promise<MigrationMeta[]> {\n const migrationQueries: MigrationMeta[] = [];\n\n for await (const journalEntry of journal.entries) {\n const query = migrations[`m${journalEntry.idx.toString().padStart(4, '0')}`];\n\n if (!query) {\n throw new Error(`Missing migration: ${journalEntry.tag}`);\n }\n\n try {\n const result = query.split('--> statement-breakpoint').map((it) => {\n return it;\n });\n\n migrationQueries.push({\n sql: result,\n bps: journalEntry.breakpoints,\n folderMillis: journalEntry.when,\n hash: '',\n });\n } catch {\n throw new Error(`Failed to parse migration: ${journalEntry.tag}`);\n }\n }\n\n return migrationQueries;\n}\n\nexport async function migrate<TSchema extends Record<string, unknown>>(\n db: OPSQLiteDatabase<TSchema>,\n config: MigrationConfig,\n) {\n const migrations = await readMigrationFiles(config);\n return db.dialect.migrate(migrations, db.session);\n}\n\ninterface State {\n\tsuccess: boolean;\n\terror?: Error;\n}\n\ntype Action =\n\t| { type: 'migrating' }\n\t| { type: 'migrated'; payload: true }\n\t| { type: 'error'; payload: Error }\n\nexport const useMigrations = (db: OPSQLiteDatabase<any>, migrations: {\n\tjournal: {\n\t\tentries: { idx: number; when: number; tag: string; breakpoints: boolean }[];\n\t};\n\tmigrations: Record<string, string>;\n}): State => {\n\tconst initialState: State = {\n\t\tsuccess: false,\n\t\terror: undefined,\n\t}\n\n\tconst fetchReducer = (state: State, action: Action): State => {\n\t\tswitch (action.type) {\n\t\t\tcase 'migrating': {\n\t\t\t\treturn { ...initialState }\n\t\t\t}\n\t\t\tcase 'migrated': {\n\t\t\t\treturn { ...initialState, success: action.payload }\n\t\t\t}\n\t\t\tcase 'error': {\n\t\t\t\treturn { ...initialState, error: action.payload }\n\t\t\t}\n\t\t\tdefault: {\n\t\t\t\treturn state\n\t\t\t}\n\t\t}\n\t}\n\n\tconst [state, dispatch] = useReducer(fetchReducer, initialState);\n\n\tuseEffect(() => {\n\t\tdispatch({ type: 'migrating' })\n\t\tmigrate(db, migrations).then(() => {\n\t\t\tdispatch({ type: 'migrated', payload: true })\n\t\t}).catch((error) => {\n\t\t\tdispatch({ type: 'error', payload: error as Error })\n\t\t});\n\t}, []);\n\n\treturn state;\n}"],"mappings":"AAAA,SAAS,WAAW,kBAAkB;AAWtC,eAAe,mBAAmB,EAAE,SAAS,WAAW,GAA8C;AAClG,QAAM,mBAAoC,CAAC;AAE3C,mBAAiB,gBAAgB,QAAQ,SAAS;AAC9C,UAAM,QAAQ,WAAW,IAAI,aAAa,IAAI,SAAS,EAAE,SAAS,GAAG,GAAG,CAAC,EAAE;AAE3E,QAAI,CAAC,OAAO;AACR,YAAM,IAAI,MAAM,sBAAsB,aAAa,GAAG,EAAE;AAAA,IAC5D;AAEA,QAAI;AACA,YAAM,SAAS,MAAM,MAAM,0BAA0B,EAAE,IAAI,CAAC,OAAO;AAC/D,eAA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
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var session_exports = {};
|
|
20
|
+
__export(session_exports, {
|
|
21
|
+
OPSQLitePreparedQuery: () => OPSQLitePreparedQuery,
|
|
22
|
+
OPSQLiteSession: () => OPSQLiteSession,
|
|
23
|
+
OPSQLiteTransaction: () => OPSQLiteTransaction
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(session_exports);
|
|
26
|
+
var import_entity = require("../entity.cjs");
|
|
27
|
+
var import_logger = require("../logger.cjs");
|
|
28
|
+
var import_sql = require("../sql/sql.cjs");
|
|
29
|
+
var import_sqlite_core = require("../sqlite-core/index.cjs");
|
|
30
|
+
var import_session = require("../sqlite-core/session.cjs");
|
|
31
|
+
var import_utils = require("../utils.cjs");
|
|
32
|
+
class OPSQLiteSession extends import_session.SQLiteSession {
|
|
33
|
+
constructor(client, dialect, schema, options = {}) {
|
|
34
|
+
super(dialect);
|
|
35
|
+
this.client = client;
|
|
36
|
+
this.schema = schema;
|
|
37
|
+
this.logger = options.logger ?? new import_logger.NoopLogger();
|
|
38
|
+
}
|
|
39
|
+
static [import_entity.entityKind] = "OPSQLiteSession";
|
|
40
|
+
logger;
|
|
41
|
+
prepareQuery(query, fields, executeMethod, customResultMapper) {
|
|
42
|
+
return new OPSQLitePreparedQuery(this.client, query, this.logger, fields, executeMethod, customResultMapper);
|
|
43
|
+
}
|
|
44
|
+
transaction(transaction, config = {}) {
|
|
45
|
+
const tx = new OPSQLiteTransaction("async", this.dialect, this, this.schema);
|
|
46
|
+
this.run(import_sql.sql.raw(`begin${config?.behavior ? " " + config.behavior : ""}`));
|
|
47
|
+
try {
|
|
48
|
+
const result = transaction(tx);
|
|
49
|
+
this.run(import_sql.sql`commit`);
|
|
50
|
+
return result;
|
|
51
|
+
} catch (err) {
|
|
52
|
+
this.run(import_sql.sql`rollback`);
|
|
53
|
+
throw err;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
class OPSQLiteTransaction extends import_sqlite_core.SQLiteTransaction {
|
|
58
|
+
static [import_entity.entityKind] = "OPSQLiteTransaction";
|
|
59
|
+
transaction(transaction) {
|
|
60
|
+
const savepointName = `sp${this.nestedIndex}`;
|
|
61
|
+
const tx = new OPSQLiteTransaction("async", this.dialect, this.session, this.schema, this.nestedIndex + 1);
|
|
62
|
+
this.session.run(import_sql.sql.raw(`savepoint ${savepointName}`));
|
|
63
|
+
try {
|
|
64
|
+
const result = transaction(tx);
|
|
65
|
+
this.session.run(import_sql.sql.raw(`release savepoint ${savepointName}`));
|
|
66
|
+
return result;
|
|
67
|
+
} catch (err) {
|
|
68
|
+
this.session.run(import_sql.sql.raw(`rollback to savepoint ${savepointName}`));
|
|
69
|
+
throw err;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
class OPSQLitePreparedQuery extends import_session.SQLitePreparedQuery {
|
|
74
|
+
constructor(client, query, logger, fields, executeMethod, customResultMapper) {
|
|
75
|
+
super("sync", executeMethod, query);
|
|
76
|
+
this.client = client;
|
|
77
|
+
this.logger = logger;
|
|
78
|
+
this.fields = fields;
|
|
79
|
+
this.customResultMapper = customResultMapper;
|
|
80
|
+
}
|
|
81
|
+
static [import_entity.entityKind] = "OPSQLitePreparedQuery";
|
|
82
|
+
run(placeholderValues) {
|
|
83
|
+
const params = (0, import_sql.fillPlaceholders)(this.query.params, placeholderValues ?? {});
|
|
84
|
+
this.logger.logQuery(this.query.sql, params);
|
|
85
|
+
return this.client.executeAsync(this.query.sql, params);
|
|
86
|
+
}
|
|
87
|
+
async all(placeholderValues) {
|
|
88
|
+
const { fields, joinsNotNullableMap, query, logger, customResultMapper, client } = this;
|
|
89
|
+
if (!fields && !customResultMapper) {
|
|
90
|
+
const params = (0, import_sql.fillPlaceholders)(query.params, placeholderValues ?? {});
|
|
91
|
+
logger.logQuery(query.sql, params);
|
|
92
|
+
return client.execute(query.sql, params).rows?._array || [];
|
|
93
|
+
}
|
|
94
|
+
const rows = await this.values(placeholderValues);
|
|
95
|
+
if (customResultMapper) {
|
|
96
|
+
return customResultMapper(rows);
|
|
97
|
+
}
|
|
98
|
+
return rows.map((row) => (0, import_utils.mapResultRow)(fields, row, joinsNotNullableMap));
|
|
99
|
+
}
|
|
100
|
+
async get(placeholderValues) {
|
|
101
|
+
const { fields, joinsNotNullableMap, customResultMapper, query, logger, client } = this;
|
|
102
|
+
const params = (0, import_sql.fillPlaceholders)(query.params, placeholderValues ?? {});
|
|
103
|
+
logger.logQuery(query.sql, params);
|
|
104
|
+
if (!fields && !customResultMapper) {
|
|
105
|
+
const rows2 = client.execute(query.sql, params).rows?._array || [];
|
|
106
|
+
return rows2[0];
|
|
107
|
+
}
|
|
108
|
+
const rows = await this.values(placeholderValues);
|
|
109
|
+
const row = rows[0];
|
|
110
|
+
if (!row) {
|
|
111
|
+
return void 0;
|
|
112
|
+
}
|
|
113
|
+
if (customResultMapper) {
|
|
114
|
+
return customResultMapper(rows);
|
|
115
|
+
}
|
|
116
|
+
return (0, import_utils.mapResultRow)(fields, row, joinsNotNullableMap);
|
|
117
|
+
}
|
|
118
|
+
values(placeholderValues) {
|
|
119
|
+
const params = (0, import_sql.fillPlaceholders)(this.query.params, placeholderValues ?? {});
|
|
120
|
+
this.logger.logQuery(this.query.sql, params);
|
|
121
|
+
return this.client.executeRawAsync(this.query.sql, params);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
125
|
+
0 && (module.exports = {
|
|
126
|
+
OPSQLitePreparedQuery,
|
|
127
|
+
OPSQLiteSession,
|
|
128
|
+
OPSQLiteTransaction
|
|
129
|
+
});
|
|
130
|
+
//# sourceMappingURL=session.cjs.map
|