diff --git a/cpp/DBHostObject.cpp b/cpp/DBHostObject.cpp index 49e33e52..07eba55a 100644 --- a/cpp/DBHostObject.cpp +++ b/cpp/DBHostObject.cpp @@ -5,6 +5,7 @@ #else #include "bridge.h" #endif +#include "logs.h" #include "macros.h" #include "utils.h" #include @@ -26,7 +27,7 @@ void DBHostObject::auto_register_update_hook() { return; } - auto hook = [this](std::string dbName, std::string table_name, + auto hook = [this](std::string name, std::string table_name, std::string operation, int rowId) { if (update_hook_callback != nullptr) { std::vector params; @@ -37,17 +38,17 @@ void DBHostObject::auto_register_update_hook() { if (operation != "DELETE") { std::string query = "SELECT * FROM " + table_name + " where rowid = " + std::to_string(rowId) + ";"; - opsqlite_execute(dbName, query, ¶ms, &results, metadata); + opsqlite_execute(name, query, ¶ms, &results, metadata); } jsCallInvoker->invokeAsync( [this, results = std::make_shared>(results), - callback = update_hook_callback, tableName = std::move(table_name), + callback = update_hook_callback, table_name, operation = std::move(operation), &rowId] { auto res = jsi::Object(rt); res.setProperty(rt, "table", - jsi::String::createFromUtf8(rt, tableName)); + jsi::String::createFromUtf8(rt, table_name)); res.setProperty(rt, "operation", jsi::String::createFromUtf8(rt, operation)); res.setProperty(rt, "rowId", jsi::Value(rowId)); @@ -63,7 +64,6 @@ void DBHostObject::auto_register_update_hook() { } for (const auto &query_ptr : reactive_queries) { - auto query = query_ptr.get(); if (query->discriminators.size() == 0) { continue; @@ -75,7 +75,6 @@ void DBHostObject::auto_register_update_hook() { if (discriminator.table != table_name) { continue; } - // Table has matched // If no ids are specified, then we should fire diff --git a/cpp/libsql/bridge.cpp b/cpp/libsql/bridge.cpp index 9f29c87b..30b85332 100644 --- a/cpp/libsql/bridge.cpp +++ b/cpp/libsql/bridge.cpp @@ -42,15 +42,8 @@ std::string opsqlite_get_db_path(std::string const &db_name, return location + "/" + db_name; } -#ifdef OP_SQLITE_USE_SQLCIPHER -BridgeResult opsqlite_open(std::string const &dbName, - std::string const &last_path, - std::string const &crsqlitePath, - std::string const &encryptionKey) { -#else BridgeResult opsqlite_libsql_open(std::string const &name, std::string const &last_path) { -#endif std::string path = opsqlite_get_db_path(name, last_path); int status = 0; @@ -72,11 +65,6 @@ BridgeResult opsqlite_libsql_open(std::string const &name, db_map[name] = {.db = db, .c = c}; -#ifdef OP_SQLITE_USE_SQLCIPHER - opsqlite_execute(dbName, "PRAGMA key = '" + encryptionKey + "'", nullptr, - nullptr, nullptr); -#endif - return {.type = SQLiteOk, .affectedRows = 0}; } diff --git a/example/Gemfile.lock b/example/Gemfile.lock index aeccdb9d..18f7769d 100644 --- a/example/Gemfile.lock +++ b/example/Gemfile.lock @@ -5,15 +5,10 @@ GEM base64 nkf rexml - activesupport (7.1.3.2) - base64 - bigdecimal + activesupport (7.0.8.1) concurrent-ruby (~> 1.0, >= 1.0.2) - connection_pool (>= 2.2.5) - drb i18n (>= 1.6, < 2) minitest (>= 5.1) - mutex_m tzinfo (~> 2.0) addressable (2.8.6) public_suffix (>= 2.0.2, < 6.0) @@ -22,12 +17,11 @@ GEM json (>= 1.5.1) atomos (0.1.3) base64 (0.2.0) - bigdecimal (3.1.7) claide (1.1.0) - cocoapods (1.15.2) + cocoapods (1.14.3) addressable (~> 2.8) claide (>= 1.0.2, < 2.0) - cocoapods-core (= 1.15.2) + cocoapods-core (= 1.14.3) cocoapods-deintegrate (>= 1.0.3, < 2.0) cocoapods-downloader (>= 2.1, < 3.0) cocoapods-plugins (>= 1.0.0, < 2.0) @@ -42,7 +36,7 @@ GEM nap (~> 1.0) ruby-macho (>= 2.3.0, < 3.0) xcodeproj (>= 1.23.0, < 2.0) - cocoapods-core (1.15.2) + cocoapods-core (1.14.3) activesupport (>= 5.0, < 8) addressable (~> 2.8) algoliasearch (~> 1.0) @@ -63,8 +57,6 @@ GEM cocoapods-try (1.2.0) colored2 (3.1.2) concurrent-ruby (1.2.3) - connection_pool (2.4.1) - drb (2.2.1) escape (0.0.4) ethon (0.16.0) ffi (>= 1.15.0) @@ -78,7 +70,6 @@ GEM json (2.7.2) minitest (5.22.3) molinillo (0.8.0) - mutex_m (0.2.0) nanaimo (0.3.0) nap (1.1.0) netrc (0.11.0) @@ -102,7 +93,11 @@ PLATFORMS ruby DEPENDENCIES - cocoapods + activesupport (>= 6.1.7.5, < 7.1.0) + cocoapods (>= 1.13, < 1.15) + +RUBY VERSION + ruby 3.3.1p55 BUNDLED WITH 2.5.9 diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index 5f56d5d1..ecd71612 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -8,9 +8,11 @@ PODS: - hermes-engine/Pre-built (= 0.74.0) - hermes-engine/Pre-built (0.74.0) - op-sqlite (6.0.3): + - OpenSSL-Universal - React - React-callinvoker - React-Core + - OpenSSL-Universal (3.1.5004) - RCT-Folly (2024.01.01.00): - boost - DoubleConversion @@ -1234,6 +1236,7 @@ DEPENDENCIES: SPEC REPOS: trunk: + - OpenSSL-Universal - SocketRocket EXTERNAL SOURCES: @@ -1358,7 +1361,8 @@ SPEC CHECKSUMS: fmt: 4c2741a687cc09f0634a2e2c72a838b99f1ff120 glog: c5d68082e772fa1c511173d6b30a9de2c05a69a2 hermes-engine: 6eae7edb2f563ee41d7c1f91f4f2e57c26d8a5c3 - op-sqlite: 6a6fb7bd336a2cac366408dd07c7f6c5a9d56e90 + op-sqlite: 557247bb21ec97c23ff45a012cc1cbea2c42bcaf + OpenSSL-Universal: 0db2e81615ad95efc90ce13a638986858da38c0d RCT-Folly: 045d6ecaa59d826c5736dfba0b2f4083ff8d79df RCTDeprecation: 3ca8b6c36bfb302e1895b72cfe7db0de0c92cd47 RCTRequired: 9fc183af555fd0c89a366c34c1ae70b7e03b1dc5 diff --git a/example/package.json b/example/package.json index 1a246359..6d6e9e4a 100644 --- a/example/package.json +++ b/example/package.json @@ -56,11 +56,11 @@ "node": ">=18" }, "op-sqlite": { - "sqlcipher": false, + "sqlcipher": true, "crsqlite": false, "performanceMode": "1", "iosSqlite": false, "fts5": true, - "libsql": true + "libsql": false } } diff --git a/example/src/tests/queries.spec.ts b/example/src/tests/queries.spec.ts index 6e016cca..5bbe69f9 100644 --- a/example/src/tests/queries.spec.ts +++ b/example/src/tests/queries.spec.ts @@ -477,7 +477,6 @@ export function queriesTests() { await new Promise(done => { setTimeout(() => done(), 50); }); - console.warn('mark1'); tx.execute('SELECT * FROM User;'); ranCallback = true; }); diff --git a/example/src/tests/reactive.spec.ts b/example/src/tests/reactive.spec.ts index b74c5e8e..167fdec7 100644 --- a/example/src/tests/reactive.spec.ts +++ b/example/src/tests/reactive.spec.ts @@ -2,8 +2,10 @@ import {isLibsql, open, type DB} from '@op-engineering/op-sqlite'; import chai from 'chai'; import {beforeEach, describe, it} from './MochaRNAdapter'; import {sleep} from './utils'; +import Chance from 'chance'; const expect = chai.expect; +const chance = new Chance(); let db: DB; export function reactiveTests() { @@ -165,11 +167,11 @@ export function reactiveTests() { [1, 'John', 30, 1000, 'Johnny'], ); - await sleep(20); + await sleep(0); db.execute('UPDATE User SET name = ? WHERE id = ?;', ['Foo', 1]); - await sleep(20); + await sleep(0); expect(firstReactiveRan).to.be.false; expect(secondReactiveRan).to.be.false; @@ -180,5 +182,56 @@ export function reactiveTests() { unsubscribe2(); unsubscribe3(); }); + + it('Update hook and reactive queries work at the same time', async () => { + let promiseResolve: any; + let promise = new Promise(resolve => { + promiseResolve = resolve; + }); + db.updateHook(({operation}) => { + promiseResolve?.(operation); + }); + + let emittedUser = null; + const unsubscribe = db.reactiveExecute({ + query: 'SELECT * FROM User;', + arguments: [], + fireOn: [ + { + table: 'User', + }, + ], + callback: data => { + emittedUser = data.rows._array[0]; + }, + }); + + const id = chance.integer({ + min: 1, + max: 100000, + }); + const name = chance.name(); + const age = chance.integer(); + const networth = chance.floating(); + db.execute( + 'INSERT INTO User (id, name, age, networth, nickname) VALUES (?, ?, ?, ?, ?);', + [id, name, age, networth, 'Johnny'], + ); + + const operation = await promise; + + await sleep(0); + + expect(operation).to.equal('INSERT'); + expect(emittedUser).to.deep.eq({ + id, + name, + age, + networth, + nickname: 'Johnny', + }); + + unsubscribe(); + }); }); }