drizzle-orm 0.29.1-fef5d1e → 0.29.2-bcd8e38
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/driver.cjs +56 -0
- package/expo-sqlite/driver.cjs.map +1 -0
- package/expo-sqlite/driver.d.cts +5 -0
- package/expo-sqlite/driver.d.ts +5 -0
- package/expo-sqlite/driver.js +35 -0
- package/expo-sqlite/driver.js.map +1 -0
- package/expo-sqlite/index.cjs +25 -0
- package/expo-sqlite/index.cjs.map +1 -0
- package/expo-sqlite/index.d.cts +2 -0
- package/expo-sqlite/index.d.ts +2 -0
- package/expo-sqlite/index.js +3 -0
- package/expo-sqlite/index.js.map +1 -0
- package/expo-sqlite/migrator.cjs +92 -0
- package/expo-sqlite/migrator.cjs.map +1 -0
- package/expo-sqlite/migrator.d.cts +29 -0
- package/expo-sqlite/migrator.d.ts +29 -0
- package/expo-sqlite/migrator.js +67 -0
- package/expo-sqlite/migrator.js.map +1 -0
- package/expo-sqlite/session.cjs +134 -0
- package/expo-sqlite/session.cjs.map +1 -0
- package/expo-sqlite/session.d.cts +46 -0
- package/expo-sqlite/session.d.ts +46 -0
- package/expo-sqlite/session.js +111 -0
- package/expo-sqlite/session.js.map +1 -0
- package/package.json +55 -1
- package/version.cjs +1 -1
- package/version.d.cts +1 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
|
@@ -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.SQLiteSyncDialect();
|
|
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.ExpoSQLiteSession(client, dialect, schema, { logger });
|
|
50
|
+
return new import_db.BaseSQLiteDatabase("sync", 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/expo-sqlite/driver.ts"],"sourcesContent":["import type { SQLiteDatabase, SQLiteRunResult } from 'expo-sqlite/next';\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 { SQLiteSyncDialect } from '~/sqlite-core/dialect.ts';\nimport type { DrizzleConfig } from '~/utils.ts';\nimport { ExpoSQLiteSession } from './session.ts';\n\nexport type ExpoSQLiteDatabase<\n TSchema extends Record<string, unknown> = Record<string, never>,\n> = BaseSQLiteDatabase<'sync', SQLiteRunResult, TSchema>;\n\nexport function drizzle<TSchema extends Record<string, unknown> = Record<string, never>>(\n client: SQLiteDatabase,\n config: DrizzleConfig<TSchema> = {},\n): ExpoSQLiteDatabase<TSchema> {\n const dialect = new SQLiteSyncDialect();\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 ExpoSQLiteSession(client, dialect, schema, { logger });\n return new BaseSQLiteDatabase('sync', dialect, session, schema) as ExpoSQLiteDatabase<TSchema>;\n}"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,oBAA8B;AAC9B,uBAKO;AACP,gBAAmC;AACnC,qBAAkC;AAElC,qBAAkC;AAM3B,SAAS,QACZ,QACA,SAAiC,CAAC,GACP;AAC3B,QAAM,UAAU,IAAI,iCAAkB;AACtC,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,iCAAkB,QAAQ,SAAS,QAAQ,EAAE,OAAO,CAAC;AACzE,SAAO,IAAI,6BAAmB,QAAQ,SAAS,SAAS,MAAM;AAClE;","names":[]}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { SQLiteDatabase, SQLiteRunResult } from 'expo-sqlite/next';
|
|
2
|
+
import { BaseSQLiteDatabase } from "../sqlite-core/db.cjs";
|
|
3
|
+
import type { DrizzleConfig } from "../utils.cjs";
|
|
4
|
+
export type ExpoSQLiteDatabase<TSchema extends Record<string, unknown> = Record<string, never>> = BaseSQLiteDatabase<'sync', SQLiteRunResult, TSchema>;
|
|
5
|
+
export declare function drizzle<TSchema extends Record<string, unknown> = Record<string, never>>(client: SQLiteDatabase, config?: DrizzleConfig<TSchema>): ExpoSQLiteDatabase<TSchema>;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { SQLiteDatabase, SQLiteRunResult } from 'expo-sqlite/next';
|
|
2
|
+
import { BaseSQLiteDatabase } from "../sqlite-core/db.js";
|
|
3
|
+
import type { DrizzleConfig } from "../utils.js";
|
|
4
|
+
export type ExpoSQLiteDatabase<TSchema extends Record<string, unknown> = Record<string, never>> = BaseSQLiteDatabase<'sync', SQLiteRunResult, TSchema>;
|
|
5
|
+
export declare function drizzle<TSchema extends Record<string, unknown> = Record<string, never>>(client: SQLiteDatabase, config?: DrizzleConfig<TSchema>): ExpoSQLiteDatabase<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 { SQLiteSyncDialect } from "../sqlite-core/dialect.js";
|
|
8
|
+
import { ExpoSQLiteSession } from "./session.js";
|
|
9
|
+
function drizzle(client, config = {}) {
|
|
10
|
+
const dialect = new SQLiteSyncDialect();
|
|
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 ExpoSQLiteSession(client, dialect, schema, { logger });
|
|
30
|
+
return new BaseSQLiteDatabase("sync", dialect, session, schema);
|
|
31
|
+
}
|
|
32
|
+
export {
|
|
33
|
+
drizzle
|
|
34
|
+
};
|
|
35
|
+
//# sourceMappingURL=driver.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/expo-sqlite/driver.ts"],"sourcesContent":["import type { SQLiteDatabase, SQLiteRunResult } from 'expo-sqlite/next';\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 { SQLiteSyncDialect } from '~/sqlite-core/dialect.ts';\nimport type { DrizzleConfig } from '~/utils.ts';\nimport { ExpoSQLiteSession } from './session.ts';\n\nexport type ExpoSQLiteDatabase<\n TSchema extends Record<string, unknown> = Record<string, never>,\n> = BaseSQLiteDatabase<'sync', SQLiteRunResult, TSchema>;\n\nexport function drizzle<TSchema extends Record<string, unknown> = Record<string, never>>(\n client: SQLiteDatabase,\n config: DrizzleConfig<TSchema> = {},\n): ExpoSQLiteDatabase<TSchema> {\n const dialect = new SQLiteSyncDialect();\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 ExpoSQLiteSession(client, dialect, schema, { logger });\n return new BaseSQLiteDatabase('sync', dialect, session, schema) as ExpoSQLiteDatabase<TSchema>;\n}"],"mappings":"AACA,SAAS,qBAAqB;AAC9B;AAAA,EACI;AAAA,EACA;AAAA,OAGG;AACP,SAAS,0BAA0B;AACnC,SAAS,yBAAyB;AAElC,SAAS,yBAAyB;AAM3B,SAAS,QACZ,QACA,SAAiC,CAAC,GACP;AAC3B,QAAM,UAAU,IAAI,kBAAkB;AACtC,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,kBAAkB,QAAQ,SAAS,QAAQ,EAAE,OAAO,CAAC;AACzE,SAAO,IAAI,mBAAmB,QAAQ,SAAS,SAAS,MAAM;AAClE;","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 expo_sqlite_exports = {};
|
|
17
|
+
module.exports = __toCommonJS(expo_sqlite_exports);
|
|
18
|
+
__reExport(expo_sqlite_exports, require("./driver.cjs"), module.exports);
|
|
19
|
+
__reExport(expo_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/expo-sqlite/index.ts"],"sourcesContent":["export * from './driver.ts';\nexport * from './session.ts';"],"mappings":";;;;;;;;;;;;;;;AAAA;AAAA;AAAA,gCAAc,wBAAd;AACA,gCAAc,yBADd;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/expo-sqlite/index.ts"],"sourcesContent":["export * from './driver.ts';\nexport * from './session.ts';"],"mappings":"AAAA,cAAc;AACd,cAAc;","names":[]}
|
|
@@ -0,0 +1,92 @@
|
|
|
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
|
+
try {
|
|
78
|
+
migrate(db, migrations).then(() => {
|
|
79
|
+
dispatch({ type: "migrated", payload: true });
|
|
80
|
+
});
|
|
81
|
+
} catch (error) {
|
|
82
|
+
dispatch({ type: "error", payload: error });
|
|
83
|
+
}
|
|
84
|
+
}, []);
|
|
85
|
+
return state;
|
|
86
|
+
};
|
|
87
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
88
|
+
0 && (module.exports = {
|
|
89
|
+
migrate,
|
|
90
|
+
useMigrations
|
|
91
|
+
});
|
|
92
|
+
//# sourceMappingURL=migrator.cjs.map
|
|
@@ -0,0 +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 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: ExpoSQLiteDatabase<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: 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\ttry {\n\t\t\tmigrate(db, migrations as any).then(() => {\n\t\t\t\tdispatch({ type: 'migrated', payload: true })\n\t\t\t})\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;AA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type { ExpoSQLiteDatabase } from "./driver.cjs";
|
|
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: ExpoSQLiteDatabase<TSchema>, config: MigrationConfig): Promise<void>;
|
|
14
|
+
interface State {
|
|
15
|
+
success: boolean;
|
|
16
|
+
error?: Error;
|
|
17
|
+
}
|
|
18
|
+
export declare const useMigrations: (db: ExpoSQLiteDatabase<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 { ExpoSQLiteDatabase } 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: ExpoSQLiteDatabase<TSchema>, config: MigrationConfig): Promise<void>;
|
|
14
|
+
interface State {
|
|
15
|
+
success: boolean;
|
|
16
|
+
error?: Error;
|
|
17
|
+
}
|
|
18
|
+
export declare const useMigrations: (db: ExpoSQLiteDatabase<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,67 @@
|
|
|
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
|
+
try {
|
|
54
|
+
migrate(db, migrations).then(() => {
|
|
55
|
+
dispatch({ type: "migrated", payload: true });
|
|
56
|
+
});
|
|
57
|
+
} catch (error) {
|
|
58
|
+
dispatch({ type: "error", payload: error });
|
|
59
|
+
}
|
|
60
|
+
}, []);
|
|
61
|
+
return state;
|
|
62
|
+
};
|
|
63
|
+
export {
|
|
64
|
+
migrate,
|
|
65
|
+
useMigrations
|
|
66
|
+
};
|
|
67
|
+
//# sourceMappingURL=migrator.js.map
|
|
@@ -0,0 +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 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: ExpoSQLiteDatabase<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: 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\ttry {\n\t\t\tmigrate(db, migrations as any).then(() => {\n\t\t\t\tdispatch({ type: 'migrated', payload: true })\n\t\t\t})\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,aAA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AAS,SAAS,MAAe,CAAC;AAAA,IACpD;AAAA,EACD,GAAG,CAAC,CAAC;AAEL,SAAO;AACR;","names":["state"]}
|
|
@@ -0,0 +1,134 @@
|
|
|
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
|
+
ExpoSQLitePreparedQuery: () => ExpoSQLitePreparedQuery,
|
|
22
|
+
ExpoSQLiteSession: () => ExpoSQLiteSession,
|
|
23
|
+
ExpoSQLiteTransaction: () => ExpoSQLiteTransaction
|
|
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 ExpoSQLiteSession 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] = "ExpoSQLiteSession";
|
|
40
|
+
logger;
|
|
41
|
+
prepareQuery(query, fields, executeMethod, customResultMapper) {
|
|
42
|
+
const stmt = this.client.prepareSync(query.sql);
|
|
43
|
+
return new ExpoSQLitePreparedQuery(stmt, query, this.logger, fields, executeMethod, customResultMapper);
|
|
44
|
+
}
|
|
45
|
+
transaction(transaction, config = {}) {
|
|
46
|
+
const tx = new ExpoSQLiteTransaction("sync", this.dialect, this, this.schema);
|
|
47
|
+
this.run(import_sql.sql.raw(`begin${config?.behavior ? " " + config.behavior : ""}`));
|
|
48
|
+
try {
|
|
49
|
+
const result = transaction(tx);
|
|
50
|
+
this.run(import_sql.sql`commit`);
|
|
51
|
+
return result;
|
|
52
|
+
} catch (err) {
|
|
53
|
+
this.run(import_sql.sql`rollback`);
|
|
54
|
+
throw err;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
class ExpoSQLiteTransaction extends import_sqlite_core.SQLiteTransaction {
|
|
59
|
+
static [import_entity.entityKind] = "ExpoSQLiteTransaction";
|
|
60
|
+
transaction(transaction) {
|
|
61
|
+
const savepointName = `sp${this.nestedIndex}`;
|
|
62
|
+
const tx = new ExpoSQLiteTransaction("sync", this.dialect, this.session, this.schema, this.nestedIndex + 1);
|
|
63
|
+
this.session.run(import_sql.sql.raw(`savepoint ${savepointName}`));
|
|
64
|
+
try {
|
|
65
|
+
const result = transaction(tx);
|
|
66
|
+
this.session.run(import_sql.sql.raw(`release savepoint ${savepointName}`));
|
|
67
|
+
return result;
|
|
68
|
+
} catch (err) {
|
|
69
|
+
this.session.run(import_sql.sql.raw(`rollback to savepoint ${savepointName}`));
|
|
70
|
+
throw err;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
class ExpoSQLitePreparedQuery extends import_session.SQLitePreparedQuery {
|
|
75
|
+
constructor(stmt, query, logger, fields, executeMethod, customResultMapper) {
|
|
76
|
+
super("sync", executeMethod, query);
|
|
77
|
+
this.stmt = stmt;
|
|
78
|
+
this.logger = logger;
|
|
79
|
+
this.fields = fields;
|
|
80
|
+
this.customResultMapper = customResultMapper;
|
|
81
|
+
}
|
|
82
|
+
static [import_entity.entityKind] = "ExpoSQLitePreparedQuery";
|
|
83
|
+
run(placeholderValues) {
|
|
84
|
+
const params = (0, import_sql.fillPlaceholders)(this.query.params, placeholderValues ?? {});
|
|
85
|
+
this.logger.logQuery(this.query.sql, params);
|
|
86
|
+
const { changes, lastInsertRowId } = this.stmt.executeSync(params);
|
|
87
|
+
return {
|
|
88
|
+
changes,
|
|
89
|
+
lastInsertRowId
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
all(placeholderValues) {
|
|
93
|
+
const { fields, joinsNotNullableMap, query, logger, stmt, customResultMapper } = this;
|
|
94
|
+
if (!fields && !customResultMapper) {
|
|
95
|
+
const params = (0, import_sql.fillPlaceholders)(query.params, placeholderValues ?? {});
|
|
96
|
+
logger.logQuery(query.sql, params);
|
|
97
|
+
return stmt.executeSync(params).getAllSync();
|
|
98
|
+
}
|
|
99
|
+
const rows = this.values(placeholderValues);
|
|
100
|
+
if (customResultMapper) {
|
|
101
|
+
return customResultMapper(rows);
|
|
102
|
+
}
|
|
103
|
+
return rows.map((row) => (0, import_utils.mapResultRow)(fields, row, joinsNotNullableMap));
|
|
104
|
+
}
|
|
105
|
+
get(placeholderValues) {
|
|
106
|
+
const params = (0, import_sql.fillPlaceholders)(this.query.params, placeholderValues ?? {});
|
|
107
|
+
this.logger.logQuery(this.query.sql, params);
|
|
108
|
+
const { fields, stmt, joinsNotNullableMap, customResultMapper } = this;
|
|
109
|
+
if (!fields && !customResultMapper) {
|
|
110
|
+
return stmt.executeSync(params).getFirstSync();
|
|
111
|
+
}
|
|
112
|
+
const rows = this.values(placeholderValues);
|
|
113
|
+
const row = rows[0];
|
|
114
|
+
if (!row) {
|
|
115
|
+
return void 0;
|
|
116
|
+
}
|
|
117
|
+
if (customResultMapper) {
|
|
118
|
+
return customResultMapper(rows);
|
|
119
|
+
}
|
|
120
|
+
return (0, import_utils.mapResultRow)(fields, row, joinsNotNullableMap);
|
|
121
|
+
}
|
|
122
|
+
values(placeholderValues) {
|
|
123
|
+
const params = (0, import_sql.fillPlaceholders)(this.query.params, placeholderValues ?? {});
|
|
124
|
+
this.logger.logQuery(this.query.sql, params);
|
|
125
|
+
return this.stmt.executeForRawResultSync(params).getAllSync();
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
129
|
+
0 && (module.exports = {
|
|
130
|
+
ExpoSQLitePreparedQuery,
|
|
131
|
+
ExpoSQLiteSession,
|
|
132
|
+
ExpoSQLiteTransaction
|
|
133
|
+
});
|
|
134
|
+
//# sourceMappingURL=session.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/expo-sqlite/session.ts"],"sourcesContent":["import type { SQLiteDatabase, SQLiteRunResult, SQLiteStatement } from 'expo-sqlite/next';\nimport { entityKind } from '~/entity.ts';\nimport type { Logger } from '~/logger.ts';\nimport { NoopLogger } from '~/logger.ts';\nimport type { RelationalSchemaConfig, TablesRelationalConfig } from '~/relations.ts';\nimport { fillPlaceholders, type Query, sql } from '~/sql/sql.ts';\nimport type { SQLiteSyncDialect } from '~/sqlite-core/dialect.ts';\nimport { SQLiteTransaction } from '~/sqlite-core/index.ts';\nimport type { SelectedFieldsOrdered } from '~/sqlite-core/query-builders/select.types.ts';\nimport {\n type PreparedQueryConfig as PreparedQueryConfigBase,\n SQLitePreparedQuery,\n type SQLiteExecuteMethod,\n SQLiteSession,\n type SQLiteTransactionConfig,\n} from '~/sqlite-core/session.ts';\nimport { mapResultRow } from '~/utils.ts';\n\nexport interface ExpoSQLiteSessionOptions {\n logger?: Logger;\n}\n\ntype PreparedQueryConfig = Omit<PreparedQueryConfigBase, 'statement' | 'run'>;\n\nexport class ExpoSQLiteSession<\n TFullSchema extends Record<string, unknown>,\n TSchema extends TablesRelationalConfig,\n> extends SQLiteSession<'sync', SQLiteRunResult, TFullSchema, TSchema> {\n static readonly [entityKind]: string = 'ExpoSQLiteSession';\n\n private logger: Logger;\n\n constructor(\n private client: SQLiteDatabase,\n dialect: SQLiteSyncDialect,\n private schema: RelationalSchemaConfig<TSchema> | undefined,\n options: ExpoSQLiteSessionOptions = {},\n\n ) {\n super(dialect);\n this.logger = options.logger ?? new NoopLogger();\n }\n\n prepareQuery<T extends Omit<PreparedQueryConfig, 'run'>>(\n query: Query,\n fields: SelectedFieldsOrdered | undefined,\n executeMethod: SQLiteExecuteMethod,\n customResultMapper?: (rows: unknown[][]) => unknown,\n ): ExpoSQLitePreparedQuery<T> {\n const stmt = this.client.prepareSync(query.sql);\n return new ExpoSQLitePreparedQuery(stmt, query, this.logger, fields, executeMethod, customResultMapper);\n }\n\n override transaction<T>(\n transaction: (tx: ExpoSQLiteTransaction<TFullSchema, TSchema>) => T,\n config: SQLiteTransactionConfig = {},\n ): T {\n const tx = new ExpoSQLiteTransaction('sync', this.dialect, this, this.schema);\n this.run(sql.raw(`begin${config?.behavior ? ' ' + config.behavior : ''}`));\n try {\n const result = transaction(tx);\n this.run(sql`commit`);\n return result;\n } catch (err) {\n this.run(sql`rollback`);\n throw err;\n }\n }\n}\n\nexport class ExpoSQLiteTransaction<\n TFullSchema extends Record<string, unknown>,\n TSchema extends TablesRelationalConfig,\n> extends SQLiteTransaction<'sync', SQLiteRunResult, TFullSchema, TSchema> {\n static readonly [entityKind]: string = 'ExpoSQLiteTransaction';\n\n override transaction<T>(transaction: (tx: ExpoSQLiteTransaction<TFullSchema, TSchema>) => T): T {\n const savepointName = `sp${this.nestedIndex}`;\n const tx = new ExpoSQLiteTransaction('sync', this.dialect, this.session, this.schema, this.nestedIndex + 1);\n this.session.run(sql.raw(`savepoint ${savepointName}`));\n try {\n const result = transaction(tx);\n this.session.run(sql.raw(`release savepoint ${savepointName}`));\n return result;\n } catch (err) {\n this.session.run(sql.raw(`rollback to savepoint ${savepointName}`));\n throw err;\n }\n }\n}\n\nexport class ExpoSQLitePreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig> extends SQLitePreparedQuery<\n { type: 'sync'; run: SQLiteRunResult; all: T['all']; get: T['get']; values: T['values']; execute: T['execute'] }\n> {\n static readonly [entityKind]: string = 'ExpoSQLitePreparedQuery';\n\n constructor(\n\t\tprivate stmt: SQLiteStatement,\n query: Query,\n private logger: Logger,\n private fields: SelectedFieldsOrdered | undefined,\n executeMethod: SQLiteExecuteMethod,\n private customResultMapper?: (rows: unknown[][]) => unknown,\n ) {\n super('sync', executeMethod, query);\n }\n\n run(placeholderValues?: Record<string, unknown>): SQLiteRunResult {\n const params = fillPlaceholders(this.query.params, placeholderValues ?? {});\n this.logger.logQuery(this.query.sql, params);\n const { changes, lastInsertRowId } = this.stmt.executeSync(params as any[]);\n return {\n changes,\n lastInsertRowId,\n };\n }\n\n all(placeholderValues?: Record<string, unknown>): T['all'] {\n const { fields, joinsNotNullableMap, query, logger, stmt, customResultMapper } = this;\n if (!fields && !customResultMapper) {\n const params = fillPlaceholders(query.params, placeholderValues ?? {});\n logger.logQuery(query.sql, params);\n return stmt.executeSync(params as any[]).getAllSync();\n }\n\n const rows = this.values(placeholderValues) as unknown[][];\n if (customResultMapper) {\n return customResultMapper(rows) as T['all'];\n }\n return rows.map((row) => mapResultRow(fields!, row, joinsNotNullableMap));\n }\n\n get(placeholderValues?: Record<string, unknown>): T['get'] {\n const params = fillPlaceholders(this.query.params, placeholderValues ?? {});\n this.logger.logQuery(this.query.sql, params);\n\n const { fields, stmt, joinsNotNullableMap, customResultMapper } = this;\n if (!fields && !customResultMapper) {\n return stmt.executeSync(params as any[]).getFirstSync();\n }\n\n const rows = this.values(placeholderValues) as unknown[][];\n const row = rows[0];\n\n if (!row) {\n return undefined;\n }\n\n if (customResultMapper) {\n return customResultMapper(rows) as T['get'];\n }\n\n return mapResultRow(fields!, row, joinsNotNullableMap);\n }\n\n values(placeholderValues?: Record<string, unknown>): T['values'] {\n const params = fillPlaceholders(this.query.params, placeholderValues ?? {});\n this.logger.logQuery(this.query.sql, params);\n return this.stmt.executeForRawResultSync(params as any[]).getAllSync();\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,oBAA2B;AAE3B,oBAA2B;AAE3B,iBAAkD;AAElD,yBAAkC;AAElC,qBAMO;AACP,mBAA6B;AAQtB,MAAM,0BAGH,6BAA6D;AAAA,EAKnE,YACY,QACR,SACQ,QACR,UAAoC,CAAC,GAEvC;AACE,UAAM,OAAO;AANL;AAEA;AAKR,SAAK,SAAS,QAAQ,UAAU,IAAI,yBAAW;AAAA,EACnD;AAAA,EAbA,QAAiB,wBAAU,IAAY;AAAA,EAE/B;AAAA,EAaR,aACI,OACA,QACA,eACA,oBAC0B;AAC1B,UAAM,OAAO,KAAK,OAAO,YAAY,MAAM,GAAG;AAC9C,WAAO,IAAI,wBAAwB,MAAM,OAAO,KAAK,QAAQ,QAAQ,eAAe,kBAAkB;AAAA,EAC1G;AAAA,EAES,YACL,aACA,SAAkC,CAAC,GAClC;AACD,UAAM,KAAK,IAAI,sBAAsB,QAAQ,KAAK,SAAS,MAAM,KAAK,MAAM;AAC5E,SAAK,IAAI,eAAI,IAAI,QAAQ,QAAQ,WAAW,MAAM,OAAO,WAAW,EAAE,EAAE,CAAC;AACzE,QAAI;AACA,YAAM,SAAS,YAAY,EAAE;AAC7B,WAAK,IAAI,sBAAW;AACpB,aAAO;AAAA,IACX,SAAS,KAAK;AACV,WAAK,IAAI,wBAAa;AACtB,YAAM;AAAA,IACV;AAAA,EACJ;AACJ;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,EAAE,WAAW;AAAA,IACxD;AAEA,UAAM,OAAO,KAAK,OAAO,iBAAiB;AAC1C,QAAI,oBAAoB;AACpB,aAAO,mBAAmB,IAAI;AAAA,IAClC;AACA,WAAO,KAAK,IAAI,CAAC,YAAQ,2BAAa,QAAS,KAAK,mBAAmB,CAAC;AAAA,EAC5E;AAAA,EAEA,IAAI,mBAAuD;AACvD,UAAM,aAAS,6BAAiB,KAAK,MAAM,QAAQ,qBAAqB,CAAC,CAAC;AAC1E,SAAK,OAAO,SAAS,KAAK,MAAM,KAAK,MAAM;AAE3C,UAAM,EAAE,QAAQ,MAAM,qBAAqB,mBAAmB,IAAI;AAClE,QAAI,CAAC,UAAU,CAAC,oBAAoB;AAChC,aAAO,KAAK,YAAY,MAAe,EAAE,aAAa;AAAA,IAC1D;AAEA,UAAM,OAAO,KAAK,OAAO,iBAAiB;AAC1C,UAAM,MAAM,KAAK,CAAC;AAElB,QAAI,CAAC,KAAK;AACN,aAAO;AAAA,IACX;AAEA,QAAI,oBAAoB;AACpB,aAAO,mBAAmB,IAAI;AAAA,IAClC;AAEA,eAAO,2BAAa,QAAS,KAAK,mBAAmB;AAAA,EACzD;AAAA,EAEA,OAAO,mBAA0D;AAC7D,UAAM,aAAS,6BAAiB,KAAK,MAAM,QAAQ,qBAAqB,CAAC,CAAC;AAC1E,SAAK,OAAO,SAAS,KAAK,MAAM,KAAK,MAAM;AAC3C,WAAO,KAAK,KAAK,wBAAwB,MAAe,EAAE,WAAW;AAAA,EACzE;AACJ;","names":[]}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { SQLiteDatabase, SQLiteRunResult, SQLiteStatement } from 'expo-sqlite/next';
|
|
2
|
+
import { entityKind } from "../entity.cjs";
|
|
3
|
+
import type { Logger } from "../logger.cjs";
|
|
4
|
+
import type { RelationalSchemaConfig, TablesRelationalConfig } from "../relations.cjs";
|
|
5
|
+
import { type Query } from "../sql/sql.cjs";
|
|
6
|
+
import type { SQLiteSyncDialect } from "../sqlite-core/dialect.cjs";
|
|
7
|
+
import { SQLiteTransaction } from "../sqlite-core/index.cjs";
|
|
8
|
+
import type { SelectedFieldsOrdered } from "../sqlite-core/query-builders/select.types.cjs";
|
|
9
|
+
import { type PreparedQueryConfig as PreparedQueryConfigBase, SQLitePreparedQuery, type SQLiteExecuteMethod, SQLiteSession, type SQLiteTransactionConfig } from "../sqlite-core/session.cjs";
|
|
10
|
+
export interface ExpoSQLiteSessionOptions {
|
|
11
|
+
logger?: Logger;
|
|
12
|
+
}
|
|
13
|
+
type PreparedQueryConfig = Omit<PreparedQueryConfigBase, 'statement' | 'run'>;
|
|
14
|
+
export declare class ExpoSQLiteSession<TFullSchema extends Record<string, unknown>, TSchema extends TablesRelationalConfig> extends SQLiteSession<'sync', SQLiteRunResult, TFullSchema, TSchema> {
|
|
15
|
+
private client;
|
|
16
|
+
private schema;
|
|
17
|
+
static readonly [entityKind]: string;
|
|
18
|
+
private logger;
|
|
19
|
+
constructor(client: SQLiteDatabase, dialect: SQLiteSyncDialect, schema: RelationalSchemaConfig<TSchema> | undefined, options?: ExpoSQLiteSessionOptions);
|
|
20
|
+
prepareQuery<T extends Omit<PreparedQueryConfig, 'run'>>(query: Query, fields: SelectedFieldsOrdered | undefined, executeMethod: SQLiteExecuteMethod, customResultMapper?: (rows: unknown[][]) => unknown): ExpoSQLitePreparedQuery<T>;
|
|
21
|
+
transaction<T>(transaction: (tx: ExpoSQLiteTransaction<TFullSchema, TSchema>) => T, config?: SQLiteTransactionConfig): T;
|
|
22
|
+
}
|
|
23
|
+
export declare class ExpoSQLiteTransaction<TFullSchema extends Record<string, unknown>, TSchema extends TablesRelationalConfig> extends SQLiteTransaction<'sync', SQLiteRunResult, TFullSchema, TSchema> {
|
|
24
|
+
static readonly [entityKind]: string;
|
|
25
|
+
transaction<T>(transaction: (tx: ExpoSQLiteTransaction<TFullSchema, TSchema>) => T): T;
|
|
26
|
+
}
|
|
27
|
+
export declare class ExpoSQLitePreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig> extends SQLitePreparedQuery<{
|
|
28
|
+
type: 'sync';
|
|
29
|
+
run: SQLiteRunResult;
|
|
30
|
+
all: T['all'];
|
|
31
|
+
get: T['get'];
|
|
32
|
+
values: T['values'];
|
|
33
|
+
execute: T['execute'];
|
|
34
|
+
}> {
|
|
35
|
+
private stmt;
|
|
36
|
+
private logger;
|
|
37
|
+
private fields;
|
|
38
|
+
private customResultMapper?;
|
|
39
|
+
static readonly [entityKind]: string;
|
|
40
|
+
constructor(stmt: SQLiteStatement, query: Query, logger: Logger, fields: SelectedFieldsOrdered | undefined, executeMethod: SQLiteExecuteMethod, customResultMapper?: ((rows: unknown[][]) => unknown) | undefined);
|
|
41
|
+
run(placeholderValues?: Record<string, unknown>): SQLiteRunResult;
|
|
42
|
+
all(placeholderValues?: Record<string, unknown>): T['all'];
|
|
43
|
+
get(placeholderValues?: Record<string, unknown>): T['get'];
|
|
44
|
+
values(placeholderValues?: Record<string, unknown>): T['values'];
|
|
45
|
+
}
|
|
46
|
+
export {};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { SQLiteDatabase, SQLiteRunResult, SQLiteStatement } from 'expo-sqlite/next';
|
|
2
|
+
import { entityKind } from "../entity.js";
|
|
3
|
+
import type { Logger } from "../logger.js";
|
|
4
|
+
import type { RelationalSchemaConfig, TablesRelationalConfig } from "../relations.js";
|
|
5
|
+
import { type Query } from "../sql/sql.js";
|
|
6
|
+
import type { SQLiteSyncDialect } from "../sqlite-core/dialect.js";
|
|
7
|
+
import { SQLiteTransaction } from "../sqlite-core/index.js";
|
|
8
|
+
import type { SelectedFieldsOrdered } from "../sqlite-core/query-builders/select.types.js";
|
|
9
|
+
import { type PreparedQueryConfig as PreparedQueryConfigBase, SQLitePreparedQuery, type SQLiteExecuteMethod, SQLiteSession, type SQLiteTransactionConfig } from "../sqlite-core/session.js";
|
|
10
|
+
export interface ExpoSQLiteSessionOptions {
|
|
11
|
+
logger?: Logger;
|
|
12
|
+
}
|
|
13
|
+
type PreparedQueryConfig = Omit<PreparedQueryConfigBase, 'statement' | 'run'>;
|
|
14
|
+
export declare class ExpoSQLiteSession<TFullSchema extends Record<string, unknown>, TSchema extends TablesRelationalConfig> extends SQLiteSession<'sync', SQLiteRunResult, TFullSchema, TSchema> {
|
|
15
|
+
private client;
|
|
16
|
+
private schema;
|
|
17
|
+
static readonly [entityKind]: string;
|
|
18
|
+
private logger;
|
|
19
|
+
constructor(client: SQLiteDatabase, dialect: SQLiteSyncDialect, schema: RelationalSchemaConfig<TSchema> | undefined, options?: ExpoSQLiteSessionOptions);
|
|
20
|
+
prepareQuery<T extends Omit<PreparedQueryConfig, 'run'>>(query: Query, fields: SelectedFieldsOrdered | undefined, executeMethod: SQLiteExecuteMethod, customResultMapper?: (rows: unknown[][]) => unknown): ExpoSQLitePreparedQuery<T>;
|
|
21
|
+
transaction<T>(transaction: (tx: ExpoSQLiteTransaction<TFullSchema, TSchema>) => T, config?: SQLiteTransactionConfig): T;
|
|
22
|
+
}
|
|
23
|
+
export declare class ExpoSQLiteTransaction<TFullSchema extends Record<string, unknown>, TSchema extends TablesRelationalConfig> extends SQLiteTransaction<'sync', SQLiteRunResult, TFullSchema, TSchema> {
|
|
24
|
+
static readonly [entityKind]: string;
|
|
25
|
+
transaction<T>(transaction: (tx: ExpoSQLiteTransaction<TFullSchema, TSchema>) => T): T;
|
|
26
|
+
}
|
|
27
|
+
export declare class ExpoSQLitePreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig> extends SQLitePreparedQuery<{
|
|
28
|
+
type: 'sync';
|
|
29
|
+
run: SQLiteRunResult;
|
|
30
|
+
all: T['all'];
|
|
31
|
+
get: T['get'];
|
|
32
|
+
values: T['values'];
|
|
33
|
+
execute: T['execute'];
|
|
34
|
+
}> {
|
|
35
|
+
private stmt;
|
|
36
|
+
private logger;
|
|
37
|
+
private fields;
|
|
38
|
+
private customResultMapper?;
|
|
39
|
+
static readonly [entityKind]: string;
|
|
40
|
+
constructor(stmt: SQLiteStatement, query: Query, logger: Logger, fields: SelectedFieldsOrdered | undefined, executeMethod: SQLiteExecuteMethod, customResultMapper?: ((rows: unknown[][]) => unknown) | undefined);
|
|
41
|
+
run(placeholderValues?: Record<string, unknown>): SQLiteRunResult;
|
|
42
|
+
all(placeholderValues?: Record<string, unknown>): T['all'];
|
|
43
|
+
get(placeholderValues?: Record<string, unknown>): T['get'];
|
|
44
|
+
values(placeholderValues?: Record<string, unknown>): T['values'];
|
|
45
|
+
}
|
|
46
|
+
export {};
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { entityKind } from "../entity.js";
|
|
2
|
+
import { NoopLogger } from "../logger.js";
|
|
3
|
+
import { fillPlaceholders, sql } from "../sql/sql.js";
|
|
4
|
+
import { SQLiteTransaction } from "../sqlite-core/index.js";
|
|
5
|
+
import {
|
|
6
|
+
SQLitePreparedQuery,
|
|
7
|
+
SQLiteSession
|
|
8
|
+
} from "../sqlite-core/session.js";
|
|
9
|
+
import { mapResultRow } from "../utils.js";
|
|
10
|
+
class ExpoSQLiteSession extends SQLiteSession {
|
|
11
|
+
constructor(client, dialect, schema, options = {}) {
|
|
12
|
+
super(dialect);
|
|
13
|
+
this.client = client;
|
|
14
|
+
this.schema = schema;
|
|
15
|
+
this.logger = options.logger ?? new NoopLogger();
|
|
16
|
+
}
|
|
17
|
+
static [entityKind] = "ExpoSQLiteSession";
|
|
18
|
+
logger;
|
|
19
|
+
prepareQuery(query, fields, executeMethod, customResultMapper) {
|
|
20
|
+
const stmt = this.client.prepareSync(query.sql);
|
|
21
|
+
return new ExpoSQLitePreparedQuery(stmt, query, this.logger, fields, executeMethod, customResultMapper);
|
|
22
|
+
}
|
|
23
|
+
transaction(transaction, config = {}) {
|
|
24
|
+
const tx = new ExpoSQLiteTransaction("sync", this.dialect, this, this.schema);
|
|
25
|
+
this.run(sql.raw(`begin${config?.behavior ? " " + config.behavior : ""}`));
|
|
26
|
+
try {
|
|
27
|
+
const result = transaction(tx);
|
|
28
|
+
this.run(sql`commit`);
|
|
29
|
+
return result;
|
|
30
|
+
} catch (err) {
|
|
31
|
+
this.run(sql`rollback`);
|
|
32
|
+
throw err;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
class ExpoSQLiteTransaction extends SQLiteTransaction {
|
|
37
|
+
static [entityKind] = "ExpoSQLiteTransaction";
|
|
38
|
+
transaction(transaction) {
|
|
39
|
+
const savepointName = `sp${this.nestedIndex}`;
|
|
40
|
+
const tx = new ExpoSQLiteTransaction("sync", this.dialect, this.session, this.schema, this.nestedIndex + 1);
|
|
41
|
+
this.session.run(sql.raw(`savepoint ${savepointName}`));
|
|
42
|
+
try {
|
|
43
|
+
const result = transaction(tx);
|
|
44
|
+
this.session.run(sql.raw(`release savepoint ${savepointName}`));
|
|
45
|
+
return result;
|
|
46
|
+
} catch (err) {
|
|
47
|
+
this.session.run(sql.raw(`rollback to savepoint ${savepointName}`));
|
|
48
|
+
throw err;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
class ExpoSQLitePreparedQuery extends SQLitePreparedQuery {
|
|
53
|
+
constructor(stmt, query, logger, fields, executeMethod, customResultMapper) {
|
|
54
|
+
super("sync", executeMethod, query);
|
|
55
|
+
this.stmt = stmt;
|
|
56
|
+
this.logger = logger;
|
|
57
|
+
this.fields = fields;
|
|
58
|
+
this.customResultMapper = customResultMapper;
|
|
59
|
+
}
|
|
60
|
+
static [entityKind] = "ExpoSQLitePreparedQuery";
|
|
61
|
+
run(placeholderValues) {
|
|
62
|
+
const params = fillPlaceholders(this.query.params, placeholderValues ?? {});
|
|
63
|
+
this.logger.logQuery(this.query.sql, params);
|
|
64
|
+
const { changes, lastInsertRowId } = this.stmt.executeSync(params);
|
|
65
|
+
return {
|
|
66
|
+
changes,
|
|
67
|
+
lastInsertRowId
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
all(placeholderValues) {
|
|
71
|
+
const { fields, joinsNotNullableMap, query, logger, stmt, customResultMapper } = this;
|
|
72
|
+
if (!fields && !customResultMapper) {
|
|
73
|
+
const params = fillPlaceholders(query.params, placeholderValues ?? {});
|
|
74
|
+
logger.logQuery(query.sql, params);
|
|
75
|
+
return stmt.executeSync(params).getAllSync();
|
|
76
|
+
}
|
|
77
|
+
const rows = this.values(placeholderValues);
|
|
78
|
+
if (customResultMapper) {
|
|
79
|
+
return customResultMapper(rows);
|
|
80
|
+
}
|
|
81
|
+
return rows.map((row) => mapResultRow(fields, row, joinsNotNullableMap));
|
|
82
|
+
}
|
|
83
|
+
get(placeholderValues) {
|
|
84
|
+
const params = fillPlaceholders(this.query.params, placeholderValues ?? {});
|
|
85
|
+
this.logger.logQuery(this.query.sql, params);
|
|
86
|
+
const { fields, stmt, joinsNotNullableMap, customResultMapper } = this;
|
|
87
|
+
if (!fields && !customResultMapper) {
|
|
88
|
+
return stmt.executeSync(params).getFirstSync();
|
|
89
|
+
}
|
|
90
|
+
const rows = this.values(placeholderValues);
|
|
91
|
+
const row = rows[0];
|
|
92
|
+
if (!row) {
|
|
93
|
+
return void 0;
|
|
94
|
+
}
|
|
95
|
+
if (customResultMapper) {
|
|
96
|
+
return customResultMapper(rows);
|
|
97
|
+
}
|
|
98
|
+
return mapResultRow(fields, row, joinsNotNullableMap);
|
|
99
|
+
}
|
|
100
|
+
values(placeholderValues) {
|
|
101
|
+
const params = fillPlaceholders(this.query.params, placeholderValues ?? {});
|
|
102
|
+
this.logger.logQuery(this.query.sql, params);
|
|
103
|
+
return this.stmt.executeForRawResultSync(params).getAllSync();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
export {
|
|
107
|
+
ExpoSQLitePreparedQuery,
|
|
108
|
+
ExpoSQLiteSession,
|
|
109
|
+
ExpoSQLiteTransaction
|
|
110
|
+
};
|
|
111
|
+
//# sourceMappingURL=session.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/expo-sqlite/session.ts"],"sourcesContent":["import type { SQLiteDatabase, SQLiteRunResult, SQLiteStatement } from 'expo-sqlite/next';\nimport { entityKind } from '~/entity.ts';\nimport type { Logger } from '~/logger.ts';\nimport { NoopLogger } from '~/logger.ts';\nimport type { RelationalSchemaConfig, TablesRelationalConfig } from '~/relations.ts';\nimport { fillPlaceholders, type Query, sql } from '~/sql/sql.ts';\nimport type { SQLiteSyncDialect } from '~/sqlite-core/dialect.ts';\nimport { SQLiteTransaction } from '~/sqlite-core/index.ts';\nimport type { SelectedFieldsOrdered } from '~/sqlite-core/query-builders/select.types.ts';\nimport {\n type PreparedQueryConfig as PreparedQueryConfigBase,\n SQLitePreparedQuery,\n type SQLiteExecuteMethod,\n SQLiteSession,\n type SQLiteTransactionConfig,\n} from '~/sqlite-core/session.ts';\nimport { mapResultRow } from '~/utils.ts';\n\nexport interface ExpoSQLiteSessionOptions {\n logger?: Logger;\n}\n\ntype PreparedQueryConfig = Omit<PreparedQueryConfigBase, 'statement' | 'run'>;\n\nexport class ExpoSQLiteSession<\n TFullSchema extends Record<string, unknown>,\n TSchema extends TablesRelationalConfig,\n> extends SQLiteSession<'sync', SQLiteRunResult, TFullSchema, TSchema> {\n static readonly [entityKind]: string = 'ExpoSQLiteSession';\n\n private logger: Logger;\n\n constructor(\n private client: SQLiteDatabase,\n dialect: SQLiteSyncDialect,\n private schema: RelationalSchemaConfig<TSchema> | undefined,\n options: ExpoSQLiteSessionOptions = {},\n\n ) {\n super(dialect);\n this.logger = options.logger ?? new NoopLogger();\n }\n\n prepareQuery<T extends Omit<PreparedQueryConfig, 'run'>>(\n query: Query,\n fields: SelectedFieldsOrdered | undefined,\n executeMethod: SQLiteExecuteMethod,\n customResultMapper?: (rows: unknown[][]) => unknown,\n ): ExpoSQLitePreparedQuery<T> {\n const stmt = this.client.prepareSync(query.sql);\n return new ExpoSQLitePreparedQuery(stmt, query, this.logger, fields, executeMethod, customResultMapper);\n }\n\n override transaction<T>(\n transaction: (tx: ExpoSQLiteTransaction<TFullSchema, TSchema>) => T,\n config: SQLiteTransactionConfig = {},\n ): T {\n const tx = new ExpoSQLiteTransaction('sync', this.dialect, this, this.schema);\n this.run(sql.raw(`begin${config?.behavior ? ' ' + config.behavior : ''}`));\n try {\n const result = transaction(tx);\n this.run(sql`commit`);\n return result;\n } catch (err) {\n this.run(sql`rollback`);\n throw err;\n }\n }\n}\n\nexport class ExpoSQLiteTransaction<\n TFullSchema extends Record<string, unknown>,\n TSchema extends TablesRelationalConfig,\n> extends SQLiteTransaction<'sync', SQLiteRunResult, TFullSchema, TSchema> {\n static readonly [entityKind]: string = 'ExpoSQLiteTransaction';\n\n override transaction<T>(transaction: (tx: ExpoSQLiteTransaction<TFullSchema, TSchema>) => T): T {\n const savepointName = `sp${this.nestedIndex}`;\n const tx = new ExpoSQLiteTransaction('sync', this.dialect, this.session, this.schema, this.nestedIndex + 1);\n this.session.run(sql.raw(`savepoint ${savepointName}`));\n try {\n const result = transaction(tx);\n this.session.run(sql.raw(`release savepoint ${savepointName}`));\n return result;\n } catch (err) {\n this.session.run(sql.raw(`rollback to savepoint ${savepointName}`));\n throw err;\n }\n }\n}\n\nexport class ExpoSQLitePreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig> extends SQLitePreparedQuery<\n { type: 'sync'; run: SQLiteRunResult; all: T['all']; get: T['get']; values: T['values']; execute: T['execute'] }\n> {\n static readonly [entityKind]: string = 'ExpoSQLitePreparedQuery';\n\n constructor(\n\t\tprivate stmt: SQLiteStatement,\n query: Query,\n private logger: Logger,\n private fields: SelectedFieldsOrdered | undefined,\n executeMethod: SQLiteExecuteMethod,\n private customResultMapper?: (rows: unknown[][]) => unknown,\n ) {\n super('sync', executeMethod, query);\n }\n\n run(placeholderValues?: Record<string, unknown>): SQLiteRunResult {\n const params = fillPlaceholders(this.query.params, placeholderValues ?? {});\n this.logger.logQuery(this.query.sql, params);\n const { changes, lastInsertRowId } = this.stmt.executeSync(params as any[]);\n return {\n changes,\n lastInsertRowId,\n };\n }\n\n all(placeholderValues?: Record<string, unknown>): T['all'] {\n const { fields, joinsNotNullableMap, query, logger, stmt, customResultMapper } = this;\n if (!fields && !customResultMapper) {\n const params = fillPlaceholders(query.params, placeholderValues ?? {});\n logger.logQuery(query.sql, params);\n return stmt.executeSync(params as any[]).getAllSync();\n }\n\n const rows = this.values(placeholderValues) as unknown[][];\n if (customResultMapper) {\n return customResultMapper(rows) as T['all'];\n }\n return rows.map((row) => mapResultRow(fields!, row, joinsNotNullableMap));\n }\n\n get(placeholderValues?: Record<string, unknown>): T['get'] {\n const params = fillPlaceholders(this.query.params, placeholderValues ?? {});\n this.logger.logQuery(this.query.sql, params);\n\n const { fields, stmt, joinsNotNullableMap, customResultMapper } = this;\n if (!fields && !customResultMapper) {\n return stmt.executeSync(params as any[]).getFirstSync();\n }\n\n const rows = this.values(placeholderValues) as unknown[][];\n const row = rows[0];\n\n if (!row) {\n return undefined;\n }\n\n if (customResultMapper) {\n return customResultMapper(rows) as T['get'];\n }\n\n return mapResultRow(fields!, row, joinsNotNullableMap);\n }\n\n values(placeholderValues?: Record<string, unknown>): T['values'] {\n const params = fillPlaceholders(this.query.params, placeholderValues ?? {});\n this.logger.logQuery(this.query.sql, params);\n return this.stmt.executeForRawResultSync(params as any[]).getAllSync();\n }\n}\n"],"mappings":"AACA,SAAS,kBAAkB;AAE3B,SAAS,kBAAkB;AAE3B,SAAS,kBAA8B,WAAW;AAElD,SAAS,yBAAyB;AAElC;AAAA,EAEI;AAAA,EAEA;AAAA,OAEG;AACP,SAAS,oBAAoB;AAQtB,MAAM,0BAGH,cAA6D;AAAA,EAKnE,YACY,QACR,SACQ,QACR,UAAoC,CAAC,GAEvC;AACE,UAAM,OAAO;AANL;AAEA;AAKR,SAAK,SAAS,QAAQ,UAAU,IAAI,WAAW;AAAA,EACnD;AAAA,EAbA,QAAiB,UAAU,IAAY;AAAA,EAE/B;AAAA,EAaR,aACI,OACA,QACA,eACA,oBAC0B;AAC1B,UAAM,OAAO,KAAK,OAAO,Y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BAAiB,KAAK,MAAM,QAAQ,qBAAqB,CAAC,CAAC;AAC1E,SAAK,OAAO,SAAS,KAAK,MAAM,KAAK,MAAM;AAC3C,WAAO,KAAK,KAAK,wBAAwB,MAAe,EAAE,WAAW;AAAA,EACzE;AACJ;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "drizzle-orm",
|
|
3
|
-
"version": "0.29.
|
|
3
|
+
"version": "0.29.2-bcd8e38",
|
|
4
4
|
"description": "Drizzle ORM package for SQL databases",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"scripts": {
|
|
@@ -51,15 +51,18 @@
|
|
|
51
51
|
"@planetscale/database": ">=1",
|
|
52
52
|
"@types/better-sqlite3": "*",
|
|
53
53
|
"@types/pg": "*",
|
|
54
|
+
"@types/react": ">=18",
|
|
54
55
|
"@types/sql.js": "*",
|
|
55
56
|
"@vercel/postgres": "*",
|
|
56
57
|
"better-sqlite3": ">=7",
|
|
57
58
|
"bun-types": "*",
|
|
59
|
+
"expo-sqlite": ">=13.2.0",
|
|
58
60
|
"knex": "*",
|
|
59
61
|
"kysely": "*",
|
|
60
62
|
"mysql2": ">=2",
|
|
61
63
|
"pg": ">=8",
|
|
62
64
|
"postgres": ">=3",
|
|
65
|
+
"react": ">=18",
|
|
63
66
|
"sql.js": ">=1",
|
|
64
67
|
"sqlite3": ">=5"
|
|
65
68
|
},
|
|
@@ -133,16 +136,19 @@
|
|
|
133
136
|
"@types/better-sqlite3": "^7.6.4",
|
|
134
137
|
"@types/node": "^20.2.5",
|
|
135
138
|
"@types/pg": "^8.10.1",
|
|
139
|
+
"@types/react": "^18.2.45",
|
|
136
140
|
"@types/sql.js": "^1.4.4",
|
|
137
141
|
"@vercel/postgres": "^0.3.0",
|
|
138
142
|
"better-sqlite3": "^8.4.0",
|
|
139
143
|
"bun-types": "^0.6.6",
|
|
140
144
|
"cpy": "^10.1.0",
|
|
145
|
+
"expo-sqlite": "^13.2.0",
|
|
141
146
|
"knex": "^2.4.2",
|
|
142
147
|
"kysely": "^0.25.0",
|
|
143
148
|
"mysql2": "^3.3.3",
|
|
144
149
|
"pg": "^8.11.0",
|
|
145
150
|
"postgres": "^3.3.5",
|
|
151
|
+
"react": "^18.2.0",
|
|
146
152
|
"sql.js": "^1.8.0",
|
|
147
153
|
"sqlite3": "^5.1.2",
|
|
148
154
|
"tslib": "^2.5.2",
|
|
@@ -585,6 +591,54 @@
|
|
|
585
591
|
"types": "./d1/session.d.ts",
|
|
586
592
|
"default": "./d1/session.js"
|
|
587
593
|
},
|
|
594
|
+
"./expo-sqlite/driver": {
|
|
595
|
+
"import": {
|
|
596
|
+
"types": "./expo-sqlite/driver.d.ts",
|
|
597
|
+
"default": "./expo-sqlite/driver.js"
|
|
598
|
+
},
|
|
599
|
+
"require": {
|
|
600
|
+
"types": "./expo-sqlite/driver.d.cts",
|
|
601
|
+
"default": "./expo-sqlite/driver.cjs"
|
|
602
|
+
},
|
|
603
|
+
"types": "./expo-sqlite/driver.d.ts",
|
|
604
|
+
"default": "./expo-sqlite/driver.js"
|
|
605
|
+
},
|
|
606
|
+
"./expo-sqlite": {
|
|
607
|
+
"import": {
|
|
608
|
+
"types": "./expo-sqlite/index.d.ts",
|
|
609
|
+
"default": "./expo-sqlite/index.js"
|
|
610
|
+
},
|
|
611
|
+
"require": {
|
|
612
|
+
"types": "./expo-sqlite/index.d.cts",
|
|
613
|
+
"default": "./expo-sqlite/index.cjs"
|
|
614
|
+
},
|
|
615
|
+
"types": "./expo-sqlite/index.d.ts",
|
|
616
|
+
"default": "./expo-sqlite/index.js"
|
|
617
|
+
},
|
|
618
|
+
"./expo-sqlite/migrator": {
|
|
619
|
+
"import": {
|
|
620
|
+
"types": "./expo-sqlite/migrator.d.ts",
|
|
621
|
+
"default": "./expo-sqlite/migrator.js"
|
|
622
|
+
},
|
|
623
|
+
"require": {
|
|
624
|
+
"types": "./expo-sqlite/migrator.d.cts",
|
|
625
|
+
"default": "./expo-sqlite/migrator.cjs"
|
|
626
|
+
},
|
|
627
|
+
"types": "./expo-sqlite/migrator.d.ts",
|
|
628
|
+
"default": "./expo-sqlite/migrator.js"
|
|
629
|
+
},
|
|
630
|
+
"./expo-sqlite/session": {
|
|
631
|
+
"import": {
|
|
632
|
+
"types": "./expo-sqlite/session.d.ts",
|
|
633
|
+
"default": "./expo-sqlite/session.js"
|
|
634
|
+
},
|
|
635
|
+
"require": {
|
|
636
|
+
"types": "./expo-sqlite/session.d.cts",
|
|
637
|
+
"default": "./expo-sqlite/session.cjs"
|
|
638
|
+
},
|
|
639
|
+
"types": "./expo-sqlite/session.d.ts",
|
|
640
|
+
"default": "./expo-sqlite/session.js"
|
|
641
|
+
},
|
|
588
642
|
"./knex": {
|
|
589
643
|
"import": {
|
|
590
644
|
"types": "./knex/index.d.ts",
|
package/version.cjs
CHANGED
package/version.d.cts
CHANGED
package/version.d.ts
CHANGED
package/version.js
CHANGED