Skip to content

Commit 5084f56

Browse files
authored
Merge pull request #236 from OP-Engineering/re-add-executeRawAsync
Re add executeRawAsync function
2 parents a0c6f27 + a040e8f commit 5084f56

File tree

3 files changed

+131
-1
lines changed

3 files changed

+131
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Some of the big supported features:
2424
- Load runtime extensions
2525
- JSONB support
2626

27+
It also contains a simple [Key-Value store](https://op-engineering.github.io/op-sqlite/docs/key_value_storage) you can use without adding one more dependency to your app.
28+
2729
# License
2830

2931
MIT License.

docs/docs/ORM_Libs.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
---
2+
sidebar_position: 10
3+
---
4+
5+
# ORMs & Libs
6+
7+
OP-SQLite is not an ORM. It doesn't keep track of entities or creates SQL queries for you. It's pretty much a raw bindings to the sqlite3 C api. That being said, ORMs are useful (they make the easy things easier while making the hard things impossible) and there are ORMs that use op-sqlite as their main driver
8+
9+
## DrizzleORM
10+
11+
Drizzle works with op-sqlite. Follow their documentation to set up a new project:
12+
13+
https://orm.drizzle.team/docs/connect-op-sqlite
14+
15+
## TypeORM
16+
17+
TypeORM is not directly supported as in the past is was broken and people started opening issues on the repo. Here is an example driver you can adjust and pass to the driver param when creating a new TypeORM instance:
18+
19+
```ts
20+
import { QueryResult, Transaction, open } from '@op-engineering/op-sqlite';
21+
22+
const enhanceQueryResult = (result: QueryResult): void => {
23+
result.rows.item = (idx: number) => result.rows[idx];
24+
};
25+
26+
export const typeORMDriver = {
27+
openDatabase: (
28+
options: {
29+
name: string;
30+
location?: string;
31+
encryptionKey: string;
32+
},
33+
ok: (db: any) => void,
34+
fail: (msg: string) => void
35+
): any => {
36+
try {
37+
if (!options.encryptionKey || options.encryptionKey.length === 0) {
38+
throw new Error('[op-sqlite]: Encryption key is required');
39+
}
40+
41+
const database = open({
42+
location: options.location,
43+
name: options.name,
44+
encryptionKey: options.encryptionKey,
45+
});
46+
47+
const connection = {
48+
executeSql: async (
49+
sql: string,
50+
params: any[] | undefined,
51+
ok: (res: QueryResult) => void,
52+
fail: (msg: string) => void
53+
) => {
54+
try {
55+
const response = await database.execute(sql, params);
56+
enhanceQueryResult(response);
57+
ok(response);
58+
} catch (e) {
59+
fail(`[op-sqlite]: Error executing SQL: ${e as string}`);
60+
}
61+
},
62+
transaction: (
63+
fn: (tx: Transaction) => Promise<void>
64+
): Promise<void> => {
65+
return database.transaction(fn);
66+
},
67+
close: (ok: any, fail: any) => {
68+
try {
69+
database.close();
70+
ok();
71+
} catch (e) {
72+
fail(`[op-sqlite]: Error closing db: ${e as string}`);
73+
}
74+
},
75+
attach: (
76+
dbNameToAttach: string,
77+
alias: string,
78+
location: string | undefined,
79+
callback: () => void
80+
) => {
81+
database.attach(options.name, dbNameToAttach, alias, location);
82+
83+
callback();
84+
},
85+
detach: (alias: string, callback: () => void) => {
86+
database.detach(options.name, alias);
87+
88+
callback();
89+
},
90+
};
91+
92+
ok(connection);
93+
94+
return connection;
95+
} catch (e) {
96+
fail(`[op-sqlite]: Error opening database: ${e as string}`);
97+
}
98+
},
99+
};
100+
```
101+
102+
## PowerSync
103+
104+
PowerSync uses op-sqlite internally to power their synchronization engine.

src/index.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,6 @@ function enhanceDB(db: InternalDB, options: DBParams): DB {
388388
commitHook: db.commitHook,
389389
rollbackHook: db.rollbackHook,
390390
loadExtension: db.loadExtension,
391-
executeRaw: db.executeRaw,
392391
getDbPath: db.getDbPath,
393392
reactiveExecute: db.reactiveExecute,
394393
sync: db.sync,
@@ -409,6 +408,31 @@ function enhanceDB(db: InternalDB, options: DBParams): DB {
409408
? await db.executeWithHostObjects(query, sanitizedParams as Scalar[])
410409
: await db.executeWithHostObjects(query);
411410
},
411+
executeRaw: async (query: string, params?: Scalar[]) => {
412+
const sanitizedParams = params?.map((p) => {
413+
if (ArrayBuffer.isView(p)) {
414+
return p.buffer;
415+
}
416+
417+
return p;
418+
});
419+
420+
return db.executeRaw(query, sanitizedParams as Scalar[]);
421+
},
422+
// Wrapper for executeRaw, drizzleORM uses this function
423+
// at some point I changed the API but they did not pin their dependency to a specific version
424+
// so re-inserting this so it starts working again
425+
executeRawAsync: async (query: string, params?: Scalar[]) => {
426+
const sanitizedParams = params?.map((p) => {
427+
if (ArrayBuffer.isView(p)) {
428+
return p.buffer;
429+
}
430+
431+
return p;
432+
});
433+
434+
return db.executeRaw(query, sanitizedParams as Scalar[]);
435+
},
412436
executeSync: (query: string, params?: Scalar[]): QueryResult => {
413437
const sanitizedParams = params?.map((p) => {
414438
if (ArrayBuffer.isView(p)) {

0 commit comments

Comments
 (0)