@cashu/coco-expo-sqlite 1.0.0-rc.5 → 1.0.0

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/dist/index.cjs CHANGED
@@ -1,6 +1,10 @@
1
1
  let _cashu_coco_core = require("@cashu/coco-core");
2
2
 
3
3
  //#region src/db.ts
4
+ function isWebRuntime() {
5
+ const runtime = globalThis;
6
+ return typeof runtime.window === "object" && typeof runtime.document === "object";
7
+ }
4
8
  /**
5
9
  * Wrapper around expo-sqlite Database providing transaction management.
6
10
  *
@@ -11,40 +15,54 @@ let _cashu_coco_core = require("@cashu/coco-core");
11
15
  */
12
16
  var ExpoSqliteDb = class ExpoSqliteDb {
13
17
  root;
18
+ operationDb;
14
19
  /** Unique identifier for this instance's transaction scope (null for root instances) */
15
20
  scopeToken;
16
- constructor(optionsOrRoot, scopeToken = null) {
21
+ constructor(optionsOrRoot, scopeToken = null, operationDb) {
17
22
  if ("database" in optionsOrRoot) {
18
23
  this.root = {
19
24
  db: optionsOrRoot.database,
20
25
  transactionQueue: Promise.resolve(),
21
26
  currentScope: null,
22
- scopeDepth: 0
27
+ scopeDepth: 0,
28
+ pendingTransactionCount: 0
23
29
  };
24
30
  this.scopeToken = null;
31
+ this.operationDb = optionsOrRoot.database;
25
32
  } else {
26
33
  this.root = optionsOrRoot;
27
34
  this.scopeToken = scopeToken;
35
+ this.operationDb = operationDb ?? optionsOrRoot.db;
28
36
  }
29
37
  }
30
38
  get raw() {
31
- return this.root.db;
39
+ return this.operationDb;
40
+ }
41
+ shouldWaitForTransaction() {
42
+ return this.root.pendingTransactionCount > 0 && (!this.scopeToken || this.root.currentScope !== this.scopeToken);
43
+ }
44
+ async waitForActiveTransaction() {
45
+ while (this.root.pendingTransactionCount > 0 && (!this.scopeToken || this.root.currentScope !== this.scopeToken)) await this.root.transactionQueue;
32
46
  }
33
47
  async exec(sql) {
34
- await this.root.db.execAsync(sql);
48
+ if (this.shouldWaitForTransaction()) await this.waitForActiveTransaction();
49
+ await this.operationDb.execAsync(sql);
35
50
  }
36
51
  async run(sql, params = []) {
37
- const result = await this.root.db.runAsync(sql, ...params ?? []);
52
+ if (this.shouldWaitForTransaction()) await this.waitForActiveTransaction();
53
+ const result = await this.operationDb.runAsync(sql, ...params ?? []);
38
54
  return {
39
55
  lastID: result.lastInsertRowId ?? 0,
40
56
  changes: result.changes ?? 0
41
57
  };
42
58
  }
43
59
  async get(sql, params = []) {
44
- return await this.root.db.getFirstAsync(sql, ...params ?? []) ?? void 0;
60
+ if (this.shouldWaitForTransaction()) await this.waitForActiveTransaction();
61
+ return await this.operationDb.getFirstAsync(sql, ...params ?? []) ?? void 0;
45
62
  }
46
63
  async all(sql, params = []) {
47
- return await this.root.db.getAllAsync(sql, ...params ?? []) ?? [];
64
+ if (this.shouldWaitForTransaction()) await this.waitForActiveTransaction();
65
+ return await this.operationDb.getAllAsync(sql, ...params ?? []) ?? [];
48
66
  }
49
67
  /**
50
68
  * Execute a function within a database transaction.
@@ -81,24 +99,33 @@ var ExpoSqliteDb = class ExpoSqliteDb {
81
99
  }
82
100
  }
83
101
  const scopeToken = Symbol("expo-sqlite-transaction");
84
- const scopedDb = new ExpoSqliteDb(root, scopeToken);
85
102
  const previousTransaction = root.transactionQueue;
86
103
  let resolver;
87
104
  root.transactionQueue = new Promise((resolve) => {
88
105
  resolver = resolve;
89
106
  });
107
+ root.pendingTransactionCount++;
90
108
  try {
91
109
  await previousTransaction;
92
110
  const dbAny = root.db;
93
111
  root.currentScope = scopeToken;
94
112
  root.scopeDepth = 1;
113
+ if (typeof dbAny.withExclusiveTransactionAsync === "function" && !isWebRuntime()) {
114
+ let result;
115
+ await dbAny.withExclusiveTransactionAsync(async (txn) => {
116
+ result = await fn(new ExpoSqliteDb(root, scopeToken, txn));
117
+ });
118
+ return result;
119
+ }
95
120
  if (typeof dbAny.withTransactionAsync === "function") {
121
+ const scopedDb = new ExpoSqliteDb(root, scopeToken);
96
122
  let result;
97
123
  await dbAny.withTransactionAsync(async () => {
98
124
  result = await fn(scopedDb);
99
125
  });
100
126
  return result;
101
127
  }
128
+ const scopedDb = new ExpoSqliteDb(root, scopeToken);
102
129
  await dbAny.execAsync("BEGIN");
103
130
  try {
104
131
  const res = await fn(scopedDb);
@@ -113,6 +140,7 @@ var ExpoSqliteDb = class ExpoSqliteDb {
113
140
  } finally {
114
141
  root.scopeDepth = 0;
115
142
  root.currentScope = null;
143
+ root.pendingTransactionCount--;
116
144
  resolver();
117
145
  }
118
146
  }
@@ -123,6 +151,31 @@ function getUnixTimeSeconds() {
123
151
 
124
152
  //#endregion
125
153
  //#region src/schema.ts
154
+ const SEND_OPERATION_METHOD_MIGRATION_IDS = ["012_send_operations_method", "013_send_operations_method"];
155
+ const RECEIVE_OPERATION_MIGRATION_IDS = ["013_receive_operations", "012_receive_operations"];
156
+ async function tableExists(db, tableName) {
157
+ return !!await db.get(`SELECT name FROM sqlite_master WHERE type = 'table' AND name = ?`, [tableName]);
158
+ }
159
+ async function getTableColumns(db, tableName) {
160
+ const rows = await db.all(`PRAGMA table_info(${tableName})`);
161
+ return new Set(rows.map((row) => row.name));
162
+ }
163
+ async function insertMigrationIds(db, ids) {
164
+ const appliedAt = getUnixTimeSeconds();
165
+ for (const id of ids) await db.run("INSERT OR IGNORE INTO coco_cashu_migrations (id, appliedAt) VALUES (?, ?)", [id, appliedAt]);
166
+ }
167
+ async function addSendOperationMethodColumns(db) {
168
+ const columns = await getTableColumns(db, "coco_cashu_send_operations");
169
+ if (!columns.has("method")) await db.run(`ALTER TABLE coco_cashu_send_operations ADD COLUMN method TEXT NOT NULL DEFAULT 'default'`);
170
+ if (!columns.has("methodDataJson")) await db.run(`ALTER TABLE coco_cashu_send_operations ADD COLUMN methodDataJson TEXT NOT NULL DEFAULT '{}'`);
171
+ }
172
+ async function reconcileMigrationAliases(db) {
173
+ if (await tableExists(db, "coco_cashu_send_operations")) {
174
+ const sendColumns = await getTableColumns(db, "coco_cashu_send_operations");
175
+ if (sendColumns.has("method") && sendColumns.has("methodDataJson")) await insertMigrationIds(db, SEND_OPERATION_METHOD_MIGRATION_IDS);
176
+ }
177
+ if (await tableExists(db, "coco_cashu_receive_operations")) await insertMigrationIds(db, RECEIVE_OPERATION_MIGRATION_IDS);
178
+ }
126
179
  const MIGRATIONS = [
127
180
  {
128
181
  id: "001_initial",
@@ -401,10 +454,7 @@ const MIGRATIONS = [
401
454
  },
402
455
  {
403
456
  id: "012_send_operations_method",
404
- sql: `
405
- ALTER TABLE coco_cashu_send_operations ADD COLUMN method TEXT NOT NULL DEFAULT 'default';
406
- ALTER TABLE coco_cashu_send_operations ADD COLUMN methodDataJson TEXT NOT NULL DEFAULT '{}';
407
- `
457
+ run: addSendOperationMethodColumns
408
458
  },
409
459
  {
410
460
  id: "013_receive_operations",
@@ -657,6 +707,7 @@ async function ensureSchemaUpTo(db, stopBeforeId) {
657
707
  appliedAt INTEGER NOT NULL
658
708
  );
659
709
  `);
710
+ await reconcileMigrationAliases(db);
660
711
  const appliedRows = await db.all("SELECT id FROM coco_cashu_migrations ORDER BY id ASC");
661
712
  const applied = new Set(appliedRows.map((r) => r.id));
662
713
  for (const migration of MIGRATIONS) {
@@ -668,6 +719,7 @@ async function ensureSchemaUpTo(db, stopBeforeId) {
668
719
  await tx.run("INSERT INTO coco_cashu_migrations (id, appliedAt) VALUES (?, ?)", [migration.id, getUnixTimeSeconds()]);
669
720
  });
670
721
  }
722
+ await reconcileMigrationAliases(db);
671
723
  }
672
724
 
673
725
  //#endregion
package/dist/index.d.cts CHANGED
@@ -19,6 +19,8 @@ interface ExpoSqliteDbRootState {
19
19
  currentScope: symbol | null;
20
20
  /** Current nesting depth of transactions (1 = top-level, 2+ = nested) */
21
21
  scopeDepth: number;
22
+ /** Number of top-level transactions currently active or queued */
23
+ pendingTransactionCount: number;
22
24
  }
23
25
  /**
24
26
  * Wrapper around expo-sqlite Database providing transaction management.
@@ -30,10 +32,13 @@ interface ExpoSqliteDbRootState {
30
32
  */
31
33
  declare class ExpoSqliteDb {
32
34
  private readonly root;
35
+ private readonly operationDb;
33
36
  /** Unique identifier for this instance's transaction scope (null for root instances) */
34
37
  private readonly scopeToken;
35
- constructor(optionsOrRoot: ExpoSqliteDbOptions | ExpoSqliteDbRootState, scopeToken?: symbol | null);
38
+ constructor(optionsOrRoot: ExpoSqliteDbOptions | ExpoSqliteDbRootState, scopeToken?: symbol | null, operationDb?: ExpoSqliteDatabaseLike);
36
39
  get raw(): ExpoSqliteDatabaseLike;
40
+ private shouldWaitForTransaction;
41
+ private waitForActiveTransaction;
37
42
  exec(sql: string): Promise<void>;
38
43
  run(sql: string, params?: unknown[]): Promise<{
39
44
  lastID: number;
package/dist/index.d.ts CHANGED
@@ -19,6 +19,8 @@ interface ExpoSqliteDbRootState {
19
19
  currentScope: symbol | null;
20
20
  /** Current nesting depth of transactions (1 = top-level, 2+ = nested) */
21
21
  scopeDepth: number;
22
+ /** Number of top-level transactions currently active or queued */
23
+ pendingTransactionCount: number;
22
24
  }
23
25
  /**
24
26
  * Wrapper around expo-sqlite Database providing transaction management.
@@ -30,10 +32,13 @@ interface ExpoSqliteDbRootState {
30
32
  */
31
33
  declare class ExpoSqliteDb {
32
34
  private readonly root;
35
+ private readonly operationDb;
33
36
  /** Unique identifier for this instance's transaction scope (null for root instances) */
34
37
  private readonly scopeToken;
35
- constructor(optionsOrRoot: ExpoSqliteDbOptions | ExpoSqliteDbRootState, scopeToken?: symbol | null);
38
+ constructor(optionsOrRoot: ExpoSqliteDbOptions | ExpoSqliteDbRootState, scopeToken?: symbol | null, operationDb?: ExpoSqliteDatabaseLike);
36
39
  get raw(): ExpoSqliteDatabaseLike;
40
+ private shouldWaitForTransaction;
41
+ private waitForActiveTransaction;
37
42
  exec(sql: string): Promise<void>;
38
43
  run(sql: string, params?: unknown[]): Promise<{
39
44
  lastID: number;
package/dist/index.js CHANGED
@@ -1,6 +1,10 @@
1
1
  import { normalizeMintUrl } from "@cashu/coco-core";
2
2
 
3
3
  //#region src/db.ts
4
+ function isWebRuntime() {
5
+ const runtime = globalThis;
6
+ return typeof runtime.window === "object" && typeof runtime.document === "object";
7
+ }
4
8
  /**
5
9
  * Wrapper around expo-sqlite Database providing transaction management.
6
10
  *
@@ -11,40 +15,54 @@ import { normalizeMintUrl } from "@cashu/coco-core";
11
15
  */
12
16
  var ExpoSqliteDb = class ExpoSqliteDb {
13
17
  root;
18
+ operationDb;
14
19
  /** Unique identifier for this instance's transaction scope (null for root instances) */
15
20
  scopeToken;
16
- constructor(optionsOrRoot, scopeToken = null) {
21
+ constructor(optionsOrRoot, scopeToken = null, operationDb) {
17
22
  if ("database" in optionsOrRoot) {
18
23
  this.root = {
19
24
  db: optionsOrRoot.database,
20
25
  transactionQueue: Promise.resolve(),
21
26
  currentScope: null,
22
- scopeDepth: 0
27
+ scopeDepth: 0,
28
+ pendingTransactionCount: 0
23
29
  };
24
30
  this.scopeToken = null;
31
+ this.operationDb = optionsOrRoot.database;
25
32
  } else {
26
33
  this.root = optionsOrRoot;
27
34
  this.scopeToken = scopeToken;
35
+ this.operationDb = operationDb ?? optionsOrRoot.db;
28
36
  }
29
37
  }
30
38
  get raw() {
31
- return this.root.db;
39
+ return this.operationDb;
40
+ }
41
+ shouldWaitForTransaction() {
42
+ return this.root.pendingTransactionCount > 0 && (!this.scopeToken || this.root.currentScope !== this.scopeToken);
43
+ }
44
+ async waitForActiveTransaction() {
45
+ while (this.root.pendingTransactionCount > 0 && (!this.scopeToken || this.root.currentScope !== this.scopeToken)) await this.root.transactionQueue;
32
46
  }
33
47
  async exec(sql) {
34
- await this.root.db.execAsync(sql);
48
+ if (this.shouldWaitForTransaction()) await this.waitForActiveTransaction();
49
+ await this.operationDb.execAsync(sql);
35
50
  }
36
51
  async run(sql, params = []) {
37
- const result = await this.root.db.runAsync(sql, ...params ?? []);
52
+ if (this.shouldWaitForTransaction()) await this.waitForActiveTransaction();
53
+ const result = await this.operationDb.runAsync(sql, ...params ?? []);
38
54
  return {
39
55
  lastID: result.lastInsertRowId ?? 0,
40
56
  changes: result.changes ?? 0
41
57
  };
42
58
  }
43
59
  async get(sql, params = []) {
44
- return await this.root.db.getFirstAsync(sql, ...params ?? []) ?? void 0;
60
+ if (this.shouldWaitForTransaction()) await this.waitForActiveTransaction();
61
+ return await this.operationDb.getFirstAsync(sql, ...params ?? []) ?? void 0;
45
62
  }
46
63
  async all(sql, params = []) {
47
- return await this.root.db.getAllAsync(sql, ...params ?? []) ?? [];
64
+ if (this.shouldWaitForTransaction()) await this.waitForActiveTransaction();
65
+ return await this.operationDb.getAllAsync(sql, ...params ?? []) ?? [];
48
66
  }
49
67
  /**
50
68
  * Execute a function within a database transaction.
@@ -81,24 +99,33 @@ var ExpoSqliteDb = class ExpoSqliteDb {
81
99
  }
82
100
  }
83
101
  const scopeToken = Symbol("expo-sqlite-transaction");
84
- const scopedDb = new ExpoSqliteDb(root, scopeToken);
85
102
  const previousTransaction = root.transactionQueue;
86
103
  let resolver;
87
104
  root.transactionQueue = new Promise((resolve) => {
88
105
  resolver = resolve;
89
106
  });
107
+ root.pendingTransactionCount++;
90
108
  try {
91
109
  await previousTransaction;
92
110
  const dbAny = root.db;
93
111
  root.currentScope = scopeToken;
94
112
  root.scopeDepth = 1;
113
+ if (typeof dbAny.withExclusiveTransactionAsync === "function" && !isWebRuntime()) {
114
+ let result;
115
+ await dbAny.withExclusiveTransactionAsync(async (txn) => {
116
+ result = await fn(new ExpoSqliteDb(root, scopeToken, txn));
117
+ });
118
+ return result;
119
+ }
95
120
  if (typeof dbAny.withTransactionAsync === "function") {
121
+ const scopedDb = new ExpoSqliteDb(root, scopeToken);
96
122
  let result;
97
123
  await dbAny.withTransactionAsync(async () => {
98
124
  result = await fn(scopedDb);
99
125
  });
100
126
  return result;
101
127
  }
128
+ const scopedDb = new ExpoSqliteDb(root, scopeToken);
102
129
  await dbAny.execAsync("BEGIN");
103
130
  try {
104
131
  const res = await fn(scopedDb);
@@ -113,6 +140,7 @@ var ExpoSqliteDb = class ExpoSqliteDb {
113
140
  } finally {
114
141
  root.scopeDepth = 0;
115
142
  root.currentScope = null;
143
+ root.pendingTransactionCount--;
116
144
  resolver();
117
145
  }
118
146
  }
@@ -123,6 +151,31 @@ function getUnixTimeSeconds() {
123
151
 
124
152
  //#endregion
125
153
  //#region src/schema.ts
154
+ const SEND_OPERATION_METHOD_MIGRATION_IDS = ["012_send_operations_method", "013_send_operations_method"];
155
+ const RECEIVE_OPERATION_MIGRATION_IDS = ["013_receive_operations", "012_receive_operations"];
156
+ async function tableExists(db, tableName) {
157
+ return !!await db.get(`SELECT name FROM sqlite_master WHERE type = 'table' AND name = ?`, [tableName]);
158
+ }
159
+ async function getTableColumns(db, tableName) {
160
+ const rows = await db.all(`PRAGMA table_info(${tableName})`);
161
+ return new Set(rows.map((row) => row.name));
162
+ }
163
+ async function insertMigrationIds(db, ids) {
164
+ const appliedAt = getUnixTimeSeconds();
165
+ for (const id of ids) await db.run("INSERT OR IGNORE INTO coco_cashu_migrations (id, appliedAt) VALUES (?, ?)", [id, appliedAt]);
166
+ }
167
+ async function addSendOperationMethodColumns(db) {
168
+ const columns = await getTableColumns(db, "coco_cashu_send_operations");
169
+ if (!columns.has("method")) await db.run(`ALTER TABLE coco_cashu_send_operations ADD COLUMN method TEXT NOT NULL DEFAULT 'default'`);
170
+ if (!columns.has("methodDataJson")) await db.run(`ALTER TABLE coco_cashu_send_operations ADD COLUMN methodDataJson TEXT NOT NULL DEFAULT '{}'`);
171
+ }
172
+ async function reconcileMigrationAliases(db) {
173
+ if (await tableExists(db, "coco_cashu_send_operations")) {
174
+ const sendColumns = await getTableColumns(db, "coco_cashu_send_operations");
175
+ if (sendColumns.has("method") && sendColumns.has("methodDataJson")) await insertMigrationIds(db, SEND_OPERATION_METHOD_MIGRATION_IDS);
176
+ }
177
+ if (await tableExists(db, "coco_cashu_receive_operations")) await insertMigrationIds(db, RECEIVE_OPERATION_MIGRATION_IDS);
178
+ }
126
179
  const MIGRATIONS = [
127
180
  {
128
181
  id: "001_initial",
@@ -401,10 +454,7 @@ const MIGRATIONS = [
401
454
  },
402
455
  {
403
456
  id: "012_send_operations_method",
404
- sql: `
405
- ALTER TABLE coco_cashu_send_operations ADD COLUMN method TEXT NOT NULL DEFAULT 'default';
406
- ALTER TABLE coco_cashu_send_operations ADD COLUMN methodDataJson TEXT NOT NULL DEFAULT '{}';
407
- `
457
+ run: addSendOperationMethodColumns
408
458
  },
409
459
  {
410
460
  id: "013_receive_operations",
@@ -657,6 +707,7 @@ async function ensureSchemaUpTo(db, stopBeforeId) {
657
707
  appliedAt INTEGER NOT NULL
658
708
  );
659
709
  `);
710
+ await reconcileMigrationAliases(db);
660
711
  const appliedRows = await db.all("SELECT id FROM coco_cashu_migrations ORDER BY id ASC");
661
712
  const applied = new Set(appliedRows.map((r) => r.id));
662
713
  for (const migration of MIGRATIONS) {
@@ -668,6 +719,7 @@ async function ensureSchemaUpTo(db, stopBeforeId) {
668
719
  await tx.run("INSERT INTO coco_cashu_migrations (id, appliedAt) VALUES (?, ?)", [migration.id, getUnixTimeSeconds()]);
669
720
  });
670
721
  }
722
+ await reconcileMigrationAliases(db);
671
723
  }
672
724
 
673
725
  //#endregion
package/package.json CHANGED
@@ -1,7 +1,9 @@
1
1
  {
2
2
  "name": "@cashu/coco-expo-sqlite",
3
- "version": "1.0.0-rc.5",
4
- "module": "index.ts",
3
+ "version": "1.0.0",
4
+ "main": "dist/index.cjs",
5
+ "module": "dist/index.js",
6
+ "types": "dist/index.d.ts",
5
7
  "type": "module",
6
8
  "repository": {
7
9
  "type": "git",
@@ -13,7 +15,7 @@
13
15
  "README.md"
14
16
  ],
15
17
  "devDependencies": {
16
- "@cashu/coco-adapter-tests": "1.0.0-rc.5"
18
+ "@cashu/coco-adapter-tests": "1.0.0"
17
19
  },
18
20
  "exports": {
19
21
  ".": {
@@ -31,6 +33,6 @@
31
33
  "peerDependencies": {
32
34
  "typescript": "^5",
33
35
  "expo-sqlite": "*",
34
- "@cashu/coco-core": "1.0.0-rc.5"
36
+ "@cashu/coco-core": "1.0.0"
35
37
  }
36
38
  }