@cashu/coco-expo-sqlite 1.0.0 → 2.0.0-rc.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/README.md CHANGED
@@ -1,10 +1,11 @@
1
1
  # @cashu/coco-expo-sqlite
2
2
 
3
- > ⚠️ Release candidate: Coco is stabilizing for v1, but breaking changes may
4
- > still land before the final 1.0 release. Pin versions in production.
5
-
6
3
  Expo SQLite storage adapter for Coco in React Native and Expo applications.
7
4
 
5
+ The public entry point is `SqliteRepositories`. Open the `expo-sqlite` database
6
+ in your application, pass that already-opened database instance to the adapter,
7
+ and keep ownership of the database lifecycle.
8
+
8
9
  ## Install
9
10
 
10
11
  ```bash
@@ -16,11 +17,11 @@ npx expo install expo-sqlite
16
17
 
17
18
  ```ts
18
19
  import { initializeCoco } from '@cashu/coco-core';
19
- import { ExpoSqliteRepositories } from '@cashu/coco-expo-sqlite';
20
+ import { SqliteRepositories } from '@cashu/coco-expo-sqlite';
20
21
  import { openDatabaseAsync } from 'expo-sqlite';
21
22
 
22
23
  const database = await openDatabaseAsync('coco.db');
23
- const repositories = new ExpoSqliteRepositories({ database });
24
+ const repositories = new SqliteRepositories({ database });
24
25
  await repositories.init();
25
26
 
26
27
  const manager = await initializeCoco({
@@ -29,7 +30,25 @@ const manager = await initializeCoco({
29
30
  });
30
31
  ```
31
32
 
33
+ The adapter does not close a database that it did not open. Keep the Expo
34
+ database lifecycle under your application code.
35
+
36
+ ## Public API
37
+
38
+ - Import `SqliteRepositories` and `SqliteRepositoriesOptions` from
39
+ `@cashu/coco-expo-sqlite`.
40
+ - Pass an already-opened `expo-sqlite` database with the `database` option.
41
+ - Call `repositories.init()` before using the manager so schema creation and
42
+ migrations run.
43
+ - `ExpoSqliteRepositories` remains available as an alias for
44
+ `SqliteRepositories`.
45
+ - `ExpoSqliteRepositoriesOptions` remains available as an alias for
46
+ `SqliteRepositoriesOptions`.
47
+ - Migration helpers, database wrapper classes, and individual repository classes
48
+ are internal implementation details and are not public adapter exports.
49
+
32
50
  ## Notes
33
51
 
34
52
  - Pass an already opened `expo-sqlite` database instance via the `database` option.
35
53
  - The adapter ensures schema creation and migrations when you call `init()`.
54
+ - `ExpoSqliteRepositories` remains available as a migration alias.
package/dist/index.d.ts CHANGED
@@ -1,298 +1,33 @@
1
- import { AuthSession, AuthSessionRepository, CoreProof, Counter, CounterRepository, HistoryEntry, KeyRingRepository, Keypair, Keyset, KeysetRepository, MeltHistoryEntry, MeltOperationRepository, MeltQuote, MeltQuoteRepository, Mint, MintHistoryEntry, MintOperationRepository, MintQuote, MintQuoteRepository, MintRepository, ProofRepository, ProofState, ReceiveHistoryEntry, ReceiveHistoryState, ReceiveOperation, ReceiveOperationRepository, ReceiveOperationState, Repositories, RepositoryTransactionScope, SendHistoryEntry, SendOperation, SendOperationRepository, SendOperationState } from "@cashu/coco-core";
1
+ import { Repositories, RepositoryTransactionScope } from "@cashu/coco-core/adapter";
2
2
  import { SQLiteDatabase } from "expo-sqlite";
3
3
 
4
- //#region src/db.d.ts
5
- type ExpoSqliteDatabaseLike = SQLiteDatabase;
6
- interface ExpoSqliteDbOptions {
7
- database: ExpoSqliteDatabaseLike;
8
- }
9
- /**
10
- * Shared state for transaction management across all ExpoSqliteDb instances.
11
- * All instances created from the same root share this state to coordinate transactions.
12
- */
13
- interface ExpoSqliteDbRootState {
14
- /** The underlying expo-sqlite Database instance */
15
- readonly db: ExpoSqliteDatabaseLike;
16
- /** Promise chain used to serialize concurrent transactions */
17
- transactionQueue: Promise<void>;
18
- /** Unique identifier for the currently active transaction scope (null if no transaction) */
19
- currentScope: symbol | null;
20
- /** Current nesting depth of transactions (1 = top-level, 2+ = nested) */
21
- scopeDepth: number;
22
- /** Number of top-level transactions currently active or queued */
23
- pendingTransactionCount: number;
24
- }
25
- /**
26
- * Wrapper around expo-sqlite Database providing transaction management.
27
- *
28
- * Transaction behavior:
29
- * - Nested transactions within the same scope are "rolled up" - they reuse the parent transaction
30
- * - Concurrent transactions from different scopes are queued and executed serially
31
- * - Each top-level transaction gets a unique scope token for identification
32
- */
33
- declare class ExpoSqliteDb {
34
- private readonly root;
35
- private readonly operationDb;
36
- /** Unique identifier for this instance's transaction scope (null for root instances) */
37
- private readonly scopeToken;
38
- constructor(optionsOrRoot: ExpoSqliteDbOptions | ExpoSqliteDbRootState, scopeToken?: symbol | null, operationDb?: ExpoSqliteDatabaseLike);
39
- get raw(): ExpoSqliteDatabaseLike;
40
- private shouldWaitForTransaction;
41
- private waitForActiveTransaction;
42
- exec(sql: string): Promise<void>;
43
- run(sql: string, params?: unknown[]): Promise<{
44
- lastID: number;
45
- changes: number;
46
- }>;
47
- get<T = unknown>(sql: string, params?: unknown[]): Promise<T | undefined>;
48
- all<T = unknown>(sql: string, params?: unknown[]): Promise<T[]>;
49
- /**
50
- * Execute a function within a database transaction.
51
- *
52
- * Transaction Semantics:
53
- *
54
- * 1. NESTED TRANSACTIONS (same scope):
55
- * When transaction() is called from within an active transaction using the same
56
- * scoped ExpoSqliteDb instance, the inner call is treated as part of the outer transaction.
57
- * No new BEGIN/COMMIT is issued - the transaction is "rolled up".
58
- *
59
- * 2. CONCURRENT TRANSACTIONS (different scopes):
60
- * When transaction() is called while another transaction is active but from a
61
- * different scope (e.g., parallel calls), the new transaction waits in a queue.
62
- * Transactions execute serially to prevent "cannot start a transaction within a transaction".
63
- *
64
- * 3. ERROR HANDLING:
65
- * If the transaction function throws an error, ROLLBACK is executed and the error
66
- * is re-thrown. The transaction queue is properly released even on error, allowing
67
- * subsequent transactions to proceed.
68
- *
69
- * @param fn - Function to execute within the transaction, receives a scoped ExpoSqliteDb instance
70
- * @returns Promise that resolves with the return value of fn
71
- * @throws Re-throws any error from fn after rolling back the transaction
72
- */
73
- transaction<T>(fn: (tx: ExpoSqliteDb) => Promise<T>): Promise<T>;
74
- }
75
- //#endregion
76
- //#region src/schema.d.ts
77
- interface Migration {
78
- id: string;
79
- sql?: string;
80
- run?: (db: ExpoSqliteDb) => Promise<void>;
81
- }
82
- declare const MIGRATIONS: readonly Migration[];
83
- /**
84
- * Ensures the database schema is up to date by running all pending migrations.
85
- */
86
- declare function ensureSchema(db: ExpoSqliteDb): Promise<void>;
87
- /**
88
- * Run migrations up to (but not including) a specific migration ID.
89
- * If stopBeforeId is not provided, runs all migrations.
90
- * Used for testing migration behavior.
91
- */
92
- declare function ensureSchemaUpTo(db: ExpoSqliteDb, stopBeforeId?: string): Promise<void>;
93
- //#endregion
94
- //#region src/repositories/MintRepository.d.ts
95
- declare class ExpoMintRepository implements MintRepository {
96
- private readonly db;
97
- constructor(db: ExpoSqliteDb);
98
- isTrustedMint(mintUrl: string): Promise<boolean>;
99
- getMintByUrl(mintUrl: string): Promise<Mint>;
100
- getAllMints(): Promise<Mint[]>;
101
- getAllTrustedMints(): Promise<Mint[]>;
102
- addNewMint(mint: Mint): Promise<void>;
103
- addOrUpdateMint(mint: Mint): Promise<void>;
104
- updateMint(mint: Mint): Promise<void>;
105
- setMintTrusted(mintUrl: string, trusted: boolean): Promise<void>;
106
- deleteMint(mintUrl: string): Promise<void>;
107
- }
108
- //#endregion
109
- //#region src/repositories/KeysetRepository.d.ts
110
- declare class ExpoKeysetRepository implements KeysetRepository {
111
- private readonly db;
112
- constructor(db: ExpoSqliteDb);
113
- getKeysetsByMintUrl(mintUrl: string): Promise<Keyset[]>;
114
- getKeysetById(mintUrl: string, id: string): Promise<Keyset | null>;
115
- updateKeyset(keyset: Omit<Keyset, 'keypairs' | 'updatedAt'>): Promise<void>;
116
- addKeyset(keyset: Omit<Keyset, 'updatedAt'>): Promise<void>;
117
- deleteKeyset(mintUrl: string, keysetId: string): Promise<void>;
118
- }
119
- //#endregion
120
- //#region src/repositories/KeyRingRepository.d.ts
121
- declare class ExpoKeyRingRepository implements KeyRingRepository {
122
- private readonly db;
123
- constructor(db: ExpoSqliteDb);
124
- getPersistedKeyPair(publicKey: string): Promise<Keypair | null>;
125
- setPersistedKeyPair(keyPair: Keypair): Promise<void>;
126
- deletePersistedKeyPair(publicKey: string): Promise<void>;
127
- getAllPersistedKeyPairs(): Promise<Keypair[]>;
128
- getLatestKeyPair(): Promise<Keypair | null>;
129
- getLastDerivationIndex(): Promise<number>;
130
- }
131
- //#endregion
132
- //#region src/repositories/CounterRepository.d.ts
133
- declare class ExpoCounterRepository implements CounterRepository {
134
- private readonly db;
135
- constructor(db: ExpoSqliteDb);
136
- getCounter(mintUrl: string, keysetId: string): Promise<Counter | null>;
137
- setCounter(mintUrl: string, keysetId: string, counter: number): Promise<void>;
138
- }
139
- //#endregion
140
- //#region src/repositories/ProofRepository.d.ts
141
- declare class ExpoProofRepository implements ProofRepository {
142
- private readonly db;
143
- constructor(db: ExpoSqliteDb);
144
- saveProofs(mintUrl: string, proofs: CoreProof[]): Promise<void>;
145
- getReadyProofs(mintUrl: string): Promise<CoreProof[]>;
146
- getInflightProofs(mintUrls?: string[]): Promise<CoreProof[]>;
147
- getAllReadyProofs(): Promise<CoreProof[]>;
148
- getProofsByKeysetId(mintUrl: string, keysetId: string): Promise<CoreProof[]>;
149
- setProofState(mintUrl: string, secrets: string[], state: ProofState): Promise<void>;
150
- deleteProofs(mintUrl: string, secrets: string[]): Promise<void>;
151
- wipeProofsByKeysetId(mintUrl: string, keysetId: string): Promise<void>;
152
- reserveProofs(mintUrl: string, secrets: string[], operationId: string): Promise<void>;
153
- releaseProofs(mintUrl: string, secrets: string[]): Promise<void>;
154
- setCreatedByOperation(mintUrl: string, secrets: string[], operationId: string): Promise<void>;
155
- getProofBySecret(mintUrl: string, secret: string): Promise<CoreProof | null>;
156
- getProofsBySecrets(mintUrl: string, secrets: string[]): Promise<CoreProof[]>;
157
- getProofsByOperationId(mintUrl: string, operationId: string): Promise<CoreProof[]>;
158
- getAvailableProofs(mintUrl: string): Promise<CoreProof[]>;
159
- getReservedProofs(): Promise<CoreProof[]>;
160
- }
161
- //#endregion
162
- //#region src/repositories/MintQuoteRepository.d.ts
163
- declare class ExpoMintQuoteRepository implements MintQuoteRepository {
164
- private readonly db;
165
- constructor(db: ExpoSqliteDb);
166
- getMintQuote(mintUrl: string, quoteId: string): Promise<MintQuote | null>;
167
- addMintQuote(quote: MintQuote): Promise<void>;
168
- setMintQuoteState(mintUrl: string, quoteId: string, state: MintQuote['state']): Promise<void>;
169
- getPendingMintQuotes(): Promise<MintQuote[]>;
170
- }
171
- //#endregion
172
- //#region src/repositories/MeltQuoteRepository.d.ts
173
- declare class ExpoMeltQuoteRepository implements MeltQuoteRepository {
174
- private readonly db;
175
- constructor(db: ExpoSqliteDb);
176
- getMeltQuote(mintUrl: string, quoteId: string): Promise<MeltQuote | null>;
177
- addMeltQuote(quote: MeltQuote): Promise<void>;
178
- setMeltQuoteState(mintUrl: string, quoteId: string, state: MeltQuote['state']): Promise<void>;
179
- getPendingMeltQuotes(): Promise<MeltQuote[]>;
180
- }
181
- //#endregion
182
- //#region src/repositories/HistoryRepository.d.ts
183
- type MintQuoteState = MintHistoryEntry['state'];
184
- type MeltQuoteState = MeltHistoryEntry['state'];
185
- type SendHistoryState = SendHistoryEntry['state'];
186
- type NewHistoryEntry = Omit<MintHistoryEntry, 'id'> | Omit<MeltHistoryEntry, 'id'> | Omit<SendHistoryEntry, 'id'> | Omit<ReceiveHistoryEntry, 'id'>;
187
- type UpdatableHistoryEntry = Omit<MintHistoryEntry, 'id' | 'createdAt'> | Omit<MeltHistoryEntry, 'id' | 'createdAt'> | Omit<SendHistoryEntry, 'id' | 'createdAt'> | Omit<ReceiveHistoryEntry, 'id' | 'createdAt'>;
188
- declare class ExpoHistoryRepository {
189
- private readonly db;
190
- constructor(db: ExpoSqliteDb);
191
- getMintHistoryEntry(mintUrl: string, quoteId: string): Promise<MintHistoryEntry | null>;
192
- getMeltHistoryEntry(mintUrl: string, quoteId: string): Promise<MeltHistoryEntry | null>;
193
- getSendHistoryEntry(mintUrl: string, operationId: string): Promise<SendHistoryEntry | null>;
194
- getReceiveHistoryEntry(mintUrl: string, operationId: string): Promise<ReceiveHistoryEntry | null>;
195
- getPaginatedHistoryEntries(limit: number, offset: number): Promise<HistoryEntry[]>;
196
- getHistoryEntryById(id: string): Promise<HistoryEntry | null>;
197
- addHistoryEntry(history: NewHistoryEntry): Promise<HistoryEntry>;
198
- updateHistoryMintEntry(mintUrl: string, quoteId: string, state: MintQuoteState): Promise<HistoryEntry>;
199
- updateHistoryMeltEntry(mintUrl: string, quoteId: string, state: MeltQuoteState): Promise<HistoryEntry>;
200
- updateHistoryEntry(history: UpdatableHistoryEntry): Promise<HistoryEntry>;
201
- updateSendHistoryState(mintUrl: string, operationId: string, state: SendHistoryState): Promise<void>;
202
- updateReceiveHistoryState(mintUrl: string, operationId: string, state: ReceiveHistoryState): Promise<void>;
203
- deleteHistoryEntry(mintUrl: string, quoteId: string): Promise<void>;
204
- private getById;
205
- private rowToEntry;
206
- }
207
- //#endregion
208
- //#region src/repositories/SendOperationRepository.d.ts
209
- declare class ExpoSendOperationRepository implements SendOperationRepository {
210
- private readonly db;
211
- constructor(db: ExpoSqliteDb);
212
- create(operation: SendOperation): Promise<void>;
213
- update(operation: SendOperation): Promise<void>;
214
- getById(id: string): Promise<SendOperation | null>;
215
- getByState(state: SendOperationState): Promise<SendOperation[]>;
216
- getPending(): Promise<SendOperation[]>;
217
- getByMintUrl(mintUrl: string): Promise<SendOperation[]>;
218
- delete(id: string): Promise<void>;
219
- }
220
- //#endregion
221
- //#region src/repositories/MeltOperationRepository.d.ts
222
- type MeltOperation = NonNullable<Awaited<ReturnType<MeltOperationRepository['getById']>>>;
223
- type MeltOperationState = Parameters<MeltOperationRepository['getByState']>[0];
224
- declare class ExpoMeltOperationRepository implements MeltOperationRepository {
225
- private readonly db;
226
- constructor(db: ExpoSqliteDb);
227
- create(operation: MeltOperation): Promise<void>;
228
- update(operation: MeltOperation): Promise<void>;
229
- getById(id: string): Promise<MeltOperation | null>;
230
- getByState(state: MeltOperationState): Promise<MeltOperation[]>;
231
- getPending(): Promise<MeltOperation[]>;
232
- getByMintUrl(mintUrl: string): Promise<MeltOperation[]>;
233
- getByQuoteId(mintUrl: string, quoteId: string): Promise<MeltOperation[]>;
234
- delete(id: string): Promise<void>;
235
- }
236
- //#endregion
237
- //#region src/repositories/AuthSessionRepository.d.ts
238
- declare class ExpoAuthSessionRepository implements AuthSessionRepository {
239
- private readonly db;
240
- constructor(db: ExpoSqliteDb);
241
- getSession(mintUrl: string): Promise<AuthSession | null>;
242
- saveSession(session: AuthSession): Promise<void>;
243
- deleteSession(mintUrl: string): Promise<void>;
244
- getAllSessions(): Promise<AuthSession[]>;
245
- }
246
- //#endregion
247
- //#region src/repositories/MintOperationRepository.d.ts
248
- type MintOperation = NonNullable<Awaited<ReturnType<MintOperationRepository['getById']>>>;
249
- type MintOperationState = Parameters<MintOperationRepository['getByState']>[0];
250
- declare class ExpoMintOperationRepository implements MintOperationRepository {
251
- private readonly db;
252
- constructor(db: ExpoSqliteDb);
253
- create(operation: MintOperation): Promise<void>;
254
- update(operation: MintOperation): Promise<void>;
255
- getById(id: string): Promise<MintOperation | null>;
256
- getByState(state: MintOperationState): Promise<MintOperation[]>;
257
- getPending(): Promise<MintOperation[]>;
258
- getByMintUrl(mintUrl: string): Promise<MintOperation[]>;
259
- getByQuoteId(mintUrl: string, quoteId: string): Promise<MintOperation[]>;
260
- delete(id: string): Promise<void>;
261
- }
262
- //#endregion
263
- //#region src/repositories/ReceiveOperationRepository.d.ts
264
- declare class ExpoReceiveOperationRepository implements ReceiveOperationRepository {
265
- private readonly db;
266
- constructor(db: ExpoSqliteDb);
267
- create(operation: ReceiveOperation): Promise<void>;
268
- update(operation: ReceiveOperation): Promise<void>;
269
- getById(id: string): Promise<ReceiveOperation | null>;
270
- getByState(state: ReceiveOperationState): Promise<ReceiveOperation[]>;
271
- getPending(): Promise<ReceiveOperation[]>;
272
- getByMintUrl(mintUrl: string): Promise<ReceiveOperation[]>;
273
- delete(id: string): Promise<void>;
274
- }
275
- //#endregion
276
4
  //#region src/index.d.ts
277
- interface ExpoSqliteRepositoriesOptions extends ExpoSqliteDbOptions {}
278
- declare class ExpoSqliteRepositories implements Repositories {
279
- readonly mintRepository: MintRepository;
280
- readonly keyRingRepository: KeyRingRepository;
281
- readonly counterRepository: CounterRepository;
282
- readonly keysetRepository: KeysetRepository;
283
- readonly proofRepository: ProofRepository;
284
- readonly mintQuoteRepository: MintQuoteRepository;
285
- readonly meltQuoteRepository: MeltQuoteRepository;
286
- readonly historyRepository: ExpoHistoryRepository;
287
- readonly sendOperationRepository: SendOperationRepository;
288
- readonly meltOperationRepository: MeltOperationRepository;
289
- readonly authSessionRepository: AuthSessionRepository;
290
- readonly mintOperationRepository: MintOperationRepository;
291
- readonly receiveOperationRepository: ReceiveOperationRepository;
292
- readonly db: ExpoSqliteDb;
293
- constructor(options: ExpoSqliteRepositoriesOptions);
5
+ interface SqliteRepositoriesOptions {
6
+ database: SQLiteDatabase;
7
+ }
8
+ declare class SqliteRepositories implements Repositories {
9
+ readonly mintRepository: Repositories['mintRepository'];
10
+ readonly keyRingRepository: Repositories['keyRingRepository'];
11
+ readonly counterRepository: Repositories['counterRepository'];
12
+ readonly keysetRepository: Repositories['keysetRepository'];
13
+ readonly proofRepository: Repositories['proofRepository'];
14
+ readonly meltQuoteRepository: Repositories['meltQuoteRepository'];
15
+ readonly mintQuoteRepository: Repositories['mintQuoteRepository'];
16
+ readonly legacyMintQuoteRepository: Repositories['legacyMintQuoteRepository'];
17
+ readonly historyRepository: Repositories['historyRepository'];
18
+ readonly sendOperationRepository: Repositories['sendOperationRepository'];
19
+ readonly meltOperationRepository: Repositories['meltOperationRepository'];
20
+ readonly authSessionRepository: Repositories['authSessionRepository'];
21
+ readonly mintOperationRepository: Repositories['mintOperationRepository'];
22
+ readonly receiveOperationRepository: Repositories['receiveOperationRepository'];
23
+ readonly paymentRequestReceiveOperationRepository: Repositories['paymentRequestReceiveOperationRepository'];
24
+ readonly paymentRequestReceiveAttemptRepository: Repositories['paymentRequestReceiveAttemptRepository'];
25
+ private readonly db;
26
+ private readonly repositories;
27
+ constructor(options: SqliteRepositoriesOptions);
294
28
  init(): Promise<void>;
295
29
  withTransaction<T>(fn: (repos: RepositoryTransactionScope) => Promise<T>): Promise<T>;
296
30
  }
31
+ type ExpoSqliteRepositoriesOptions = SqliteRepositoriesOptions;
297
32
  //#endregion
298
- export { ExpoAuthSessionRepository, ExpoCounterRepository, ExpoHistoryRepository, ExpoKeyRingRepository, ExpoKeysetRepository, ExpoMeltOperationRepository, ExpoMeltQuoteRepository, ExpoMintOperationRepository, ExpoMintQuoteRepository, ExpoMintRepository, ExpoProofRepository, ExpoReceiveOperationRepository, ExpoSendOperationRepository, ExpoSqliteDb, ExpoSqliteRepositories, ExpoSqliteRepositoriesOptions, MIGRATIONS, type Migration, ensureSchema, ensureSchemaUpTo };
33
+ export { SqliteRepositories as ExpoSqliteRepositories, SqliteRepositories, ExpoSqliteRepositoriesOptions, SqliteRepositoriesOptions };