Skip to content

Commit

Permalink
Merge pull request #123 from OP-Engineering/expose-sync
Browse files Browse the repository at this point in the history
Expose sync call for libsql
  • Loading branch information
ospfranco authored Jul 15, 2024
2 parents ce883d1 + bc4d69b commit 645bdb4
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 13 deletions.
17 changes: 16 additions & 1 deletion cpp/DBHostObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,16 @@ void DBHostObject::create_jsi_functions() {
return promise;
});

#ifdef OP_SQLITE_USE_LIBSQL
auto sync = HOSTFN("sync", 0) {
BridgeResult result = opsqlite_libsql_sync(db_name);
if (result.type == SQLiteError) {
throw std::runtime_error(result.message);
}
return {};
});
#endif

#ifndef OP_SQLITE_USE_LIBSQL
auto load_file = HOSTFN("loadFile", 1) {
if (sizeof(args) < 1) {
Expand Down Expand Up @@ -797,7 +807,9 @@ void DBHostObject::create_jsi_functions() {
function_map["executeBatchAsync"] = std::move(execute_batch_async);
function_map["prepareStatement"] = std::move(prepare_statement);
function_map["getDbPath"] = std::move(get_db_path);
#ifndef OP_SQLITE_USE_LIBSQL
#ifdef OP_SQLITE_USE_LIBSQL
function_map["sync"] = std::move(sync);
#else OP_SQLITE_USE_LIBSQL
function_map["loadFile"] = std::move(load_file);
function_map["updateHook"] = std::move(update_hook);
function_map["commitHook"] = std::move(commit_hook);
Expand Down Expand Up @@ -850,6 +862,9 @@ jsi::Value DBHostObject::get(jsi::Runtime &rt,
if (name == "getDbPath") {
return jsi::Value(rt, function_map["getDbPath"]);
}
if (name == "sync") {
return jsi::Value(rt, function_map["sync"]);
}
#ifdef OP_SQLITE_USE_LIBSQL
if (name == "loadFile") {
return HOSTFN("loadFile", 0) {
Expand Down
15 changes: 15 additions & 0 deletions cpp/libsql/bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,21 @@ BridgeResult opsqlite_libsql_detach(std::string const &mainDBName,
};
}

BridgeResult opsqlite_libsql_sync(std::string const &name) {
check_db_open(name);

auto db = db_map[name].db;
const char *err = NULL;

int status = libsql_sync(db, &err);

if (status != 0) {
return {.type = SQLiteError, .message = err};
}

return {.type = SQLiteOk};
}

BridgeResult opsqlite_libsql_remove(std::string const &name,
std::string const &path) {
if (db_map.count(name) == 1) {
Expand Down
2 changes: 2 additions & 0 deletions cpp/libsql/bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ BridgeResult opsqlite_libsql_attach(std::string const &mainDBName,
BridgeResult opsqlite_libsql_detach(std::string const &mainDBName,
std::string const &alias);

BridgeResult opsqlite_libsql_sync(std::string const &name);

BridgeResult opsqlite_libsql_execute(
std::string const &name, std::string const &query,
const std::vector<JSVariant> *params, std::vector<DumbHostObject> *results,
Expand Down
4 changes: 2 additions & 2 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ PODS:
- hermes-engine (0.74.0):
- hermes-engine/Pre-built (= 0.74.0)
- hermes-engine/Pre-built (0.74.0)
- op-sqlite (6.1.3):
- op-sqlite (6.1.4):
- React
- React-callinvoker
- React-Core
Expand Down Expand Up @@ -1358,7 +1358,7 @@ SPEC CHECKSUMS:
fmt: 4c2741a687cc09f0634a2e2c72a838b99f1ff120
glog: c5d68082e772fa1c511173d6b30a9de2c05a69a2
hermes-engine: 6eae7edb2f563ee41d7c1f91f4f2e57c26d8a5c3
op-sqlite: 3b399d7e847783963ba53b0fe979e667225b76de
op-sqlite: 6681bc41de4af899fd7014c936b039ac330c007e
RCT-Folly: 045d6ecaa59d826c5736dfba0b2f4083ff8d79df
RCTDeprecation: 3ca8b6c36bfb302e1895b72cfe7db0de0c92cd47
RCTRequired: 9fc183af555fd0c89a366c34c1ae70b7e03b1dc5
Expand Down
5 changes: 3 additions & 2 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"android": "react-native run-android",
"ios": "react-native run-ios --scheme='debug' --simulator='iPhone 15 Pro'",
"start": "react-native start",
"pods": "cd ios && rm -rf Pods && rm -rf Podfile.lock && bundle exec pod install",
"pods": "cd ios && bundle exec pod install",
"pods:nuke": "cd ios && rm -rf Pods && rm -rf Podfile.lock && bundle exec pod install",
"build:android": "cd android && ./gradlew assembleDebug --no-daemon --console=plain -PreactNativeArchitectures=arm64-v8a",
"build:ios": "cd ios && xcodebuild -workspace OPSQLiteExample.xcworkspace -scheme debug -configuration Debug -sdk iphonesimulator CC=clang CPLUSPLUS=clang++ LD=clang LDPLUSPLUS=clang++ GCC_OPTIMIZATION_LEVEL=0 GCC_PRECOMPILE_PREFIX_HEADER=YES ASSETCATALOG_COMPILER_OPTIMIZATION=time DEBUG_INFORMATION_FORMAT=dwarf COMPILER_INDEX_STORE_ENABLE=NO"
},
Expand Down Expand Up @@ -60,7 +61,7 @@
"crsqlite": false,
"performanceMode": "2",
"iosSqlite": false,
"fts5": false,
"fts5": true,
"libsql": false
}
}
7 changes: 5 additions & 2 deletions example/src/tests/queries.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
type DB,
type SQLBatchTuple,
} from '@op-engineering/op-sqlite';
import {beforeEach, describe, it} from './MochaRNAdapter';
import {beforeEach, describe, it, itOnly} from './MochaRNAdapter';
import chai from 'chai';

const expect = chai.expect;
Expand Down Expand Up @@ -50,7 +50,7 @@ export function queriesTests() {
expect(res.rowsAffected).to.equal(0);
});

it('Open a libsql database replicated to turso', async () => {
itOnly('Open a libsql database replicated to turso', async () => {
const remoteDb = openSync({
url: 'libsql://foo-ospfranco.turso.io',
authToken:
Expand All @@ -59,6 +59,9 @@ export function queriesTests() {
});

const res = remoteDb.execute('SELECT 1');

remoteDb.sync();

expect(res.rowsAffected).to.equal(0);
});
}
Expand Down
15 changes: 9 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,20 @@ export const {
export type QueryResult = {
insertId?: number;
rowsAffected: number;
res?: any[];
rows?: {
/** Raw array with all dataset */
_array: any[];
/** The lengh of the dataset */
/** The length of the dataset */
length: number;
/** A convenience function to acess the index based the row object
/** A convenience function to access the index based the row object
* @param idx the row index
* @returns the row structure identified by column names
*/
item: (idx: number) => any;
};
/**
* Query metadata, avaliable only for select query results
* Query metadata, available only for select query results
*/
metadata?: ColumnMetadata[];
};
Expand All @@ -81,12 +82,12 @@ export type QueryResult = {
* Describes some information about columns fetched by the query
*/
export type ColumnMetadata = {
/** The name used for this column for this resultset */
/** The name used for this column for this result set */
name: string;
/** The declared column type for this column, when fetched directly from a table or a View resulting from a table column. "UNKNOWN" for dynamic values, like function returned ones. */
type: string;
/**
* The index for this column for this resultset*/
* The index for this column for this result set*/
index: number;
};

Expand Down Expand Up @@ -186,6 +187,7 @@ export type DB = {
}[];
callback: (response: any) => void;
}) => () => void;
sync: () => void;
};

type OPSQLiteProxy = {
Expand All @@ -211,7 +213,6 @@ const locks: Record<
> = {};

// Enhance some host functions

// Add 'item' function to result object to allow the sqlite-storage typeorm driver to work
function enhanceQueryResult(result: QueryResult): void {
// Add 'item' function to result object to allow the sqlite-storage typeorm driver to work
Expand All @@ -222,6 +223,7 @@ function enhanceQueryResult(result: QueryResult): void {
item: (idx: number) => result.rows?._array[idx],
};
} else {
result.res = result.rows._array;
result.rows.item = (idx: number) => result.rows?._array[idx];
}
}
Expand Down Expand Up @@ -268,6 +270,7 @@ function enhanceDB(db: DB, options: any): DB {
executeRawAsync: db.executeRawAsync,
getDbPath: db.getDbPath,
reactiveExecute: db.reactiveExecute,
sync: db.sync,
close: () => {
db.close();
delete locks[options.url];
Expand Down

0 comments on commit 645bdb4

Please sign in to comment.