Skip to content

Commit f8c848d

Browse files
authored
Merge pull request #312 from joshua-journey-apps/execute-raw-sync
Add execute sync raw endpoint
2 parents 7979113 + 56f57de commit f8c848d

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

cpp/DBHostObject.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,23 @@ void DBHostObject::create_jsi_functions() {
361361

362362
return create_js_rows(rt, status);
363363
});
364+
365+
function_map["executeRawSync"] = HOSTFN("executeRawSync") {
366+
const std::string query = args[0].asString(rt).utf8(rt);
367+
std::vector<JSVariant> params = count == 2 && args[1].isObject()
368+
? to_variant_vec(rt, args[1])
369+
: std::vector<JSVariant>();
370+
371+
std::vector<std::vector<JSVariant>> results;
372+
373+
#ifdef OP_SQLITE_USE_LIBSQL
374+
auto status = opsqlite_libsql_execute_raw(db, query, &params, &results);
375+
#else
376+
auto status = opsqlite_execute_raw(db, query, &params, &results);
377+
#endif
378+
379+
return create_raw_result(rt, status, &results);
380+
});
364381

365382
function_map["execute"] = HOSTFN("execute") {
366383
const std::string query = args[0].asString(rt).utf8(rt);

example/src/tests/queries.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,23 @@ export function queriesTests() {
721721
expect(res).to.eql([[id, name, age, networth]]);
722722
});
723723

724+
it('Execute raw sync should return just an array of objects', async () => {
725+
const id = chance.integer();
726+
const name = chance.name();
727+
const age = chance.integer();
728+
const networth = chance.floating();
729+
730+
await db.execute(
731+
'INSERT INTO User (id, name, age, networth) VALUES(?, ?, ?, ?)',
732+
[id, name, age, networth],
733+
);
734+
735+
const res = db.executeRawSync(
736+
'SELECT id, name, age, networth FROM User',
737+
);
738+
expect(res).to.eql([[id, name, age, networth]]);
739+
});
740+
724741
it('Create fts5 virtual table', async () => {
725742
await db.execute(
726743
'CREATE VIRTUAL TABLE fts5_table USING fts5(name, content);',

src/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ type InternalDB = {
134134
prepareStatement: (query: string) => PreparedStatement;
135135
loadExtension: (path: string, entryPoint?: string) => void;
136136
executeRaw: (query: string, params?: Scalar[]) => Promise<any[]>;
137+
executeRawSync: (query: string, params?: Scalar[]) => any[];
137138
getDbPath: (location?: string) => string;
138139
reactiveExecute: (params: {
139140
query: string;
@@ -262,6 +263,11 @@ export type DB = {
262263
* It will be faster since a lot of repeated work is skipped and only the values you care about are returned
263264
*/
264265
executeRaw: (query: string, params?: Scalar[]) => Promise<any[]>;
266+
/**
267+
* Same as `executeRaw` but it will block the JS thread and therefore your UI and should be used with caution
268+
* It will return an array of arrays with just the values and not the keys
269+
*/
270+
executeRawSync: (query: string, params?: Scalar[]) => any[];
265271
/**
266272
* Get's the absolute path to the db file. Useful for debugging on local builds and for attaching the DB from users devices
267273
*/
@@ -478,6 +484,10 @@ function enhanceDB(db: InternalDB, options: DBParams): DB {
478484

479485
return db.executeRaw(query, sanitizedParams as Scalar[]);
480486
},
487+
executeRawSync: (query: string, params?: Scalar[]) => {
488+
const sanitizedParams = sanitizeArrayBuffersInArray(params);
489+
return db.executeRawSync(query, sanitizedParams as Scalar[]);
490+
},
481491
executeSync: (query: string, params?: Scalar[]): QueryResult => {
482492
const sanitizedParams = sanitizeArrayBuffersInArray(params);
483493

0 commit comments

Comments
 (0)