forked from rocicorp/replicache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransactions.ts
More file actions
224 lines (200 loc) · 5.74 KB
/
Copy pathtransactions.ts
File metadata and controls
224 lines (200 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import type {JSONValue, ToJSON} from './json.js';
import type {
Invoke,
OpenTransactionRequest,
CommitTransactionResponse,
} from './repm-invoker.js';
import type {KeyTypeForScanOptions, ScanOptions} from './scan-options.js';
import {ScanResult} from './scan-iterator.js';
import {throwIfClosed} from './transaction-closed-error.js';
/**
* ReadTransactions are used with `Replicache.query` and allows read operations
* on the database.
*/
export interface ReadTransaction {
/**
* Get a single value from the database. If the key is not present this
* returns `undefined`.
*/
get(key: string): Promise<JSONValue | undefined>;
/**
* Determines if a single key is present in the database.
*/
has(key: string): Promise<boolean>;
/**
* Gets many values from the database. This returns a `ScanResult` which
* implements `AsyncIterable`. It also has methods to iterate over the `keys`
* and `entries`.
*
* If `options` has an `indexName`, then this does a scan over an index with
* that name. A scan over an index uses a tuple for the key consisting of
* `[secondary: string, primary: string]`.
*
* If the `ScanResult` is used after the `ReadTransaction` has been closed it
* will throw a {@link TransactionClosedError}.
*/
scan<O extends ScanOptions, K extends KeyTypeForScanOptions<O>>(
options?: O,
): ScanResult<K>;
/**
* Convenience form of `scan()` which returns all the entries as an array.
*/
scanAll<O extends ScanOptions, K extends KeyTypeForScanOptions<O>>(
options?: O,
): Promise<[K, JSONValue][]>;
}
export class ReadTransactionImpl implements ReadTransaction {
private _transactionId = -1;
protected readonly _invoke: Invoke;
protected _closed = false;
constructor(invoke: Invoke) {
this._invoke = invoke;
}
async get(key: string): Promise<JSONValue | undefined> {
throwIfClosed(this);
const result = await this._invoke('get', {
transactionId: this._transactionId,
key,
});
if (!result.has) {
return undefined;
}
return JSON.parse(result.value);
}
async has(key: string): Promise<boolean> {
throwIfClosed(this);
const result = await this._invoke('has', {
transactionId: this._transactionId,
key,
});
return result['has'];
}
scan<O extends ScanOptions, K extends KeyTypeForScanOptions<O>>(
options?: O,
): ScanResult<K> {
const {prefix = '', startKey, startKeyExclusive, limit, indexName} =
options || {};
return new ScanResult(
prefix,
startKey,
startKeyExclusive,
limit,
indexName,
this._invoke,
() => this,
false,
);
}
async scanAll<O extends ScanOptions, K extends KeyTypeForScanOptions<O>>(
options?: O,
): Promise<[K, JSONValue][]> {
type E = [K, JSONValue];
const it = this.scan(options).entries();
const result: E[] = [];
for await (const pair of it) {
result.push(pair as E);
}
return result;
}
get id(): number {
return this._transactionId;
}
get closed(): boolean {
return this._closed;
}
async open(args: OpenTransactionRequest): Promise<void> {
const {transactionId} = await this._invoke('openTransaction', args);
this._transactionId = transactionId;
}
async close(): Promise<void> {
try {
this._closed = true;
await this._invoke('closeTransaction', {
transactionId: this._transactionId,
});
} catch (ex) {
console.error('Failed to close transaction', ex);
}
}
}
/**
* WriteTransactions are used with `Replicache.register` and allows read and
* write operations on the database.
*/
export interface WriteTransaction extends ReadTransaction {
/**
* Sets a single value in the database. The `value` will be encoded using
* `JSON.stringify`.
*/
put(key: string, value: JSONValue | ToJSON): Promise<void>;
/**
* Removes a key and its value from the database. Returns true if there was a
* key to remove.
*/
del(key: string): Promise<boolean>;
/**
* Creates a persistent secondary index in Replicache which can be used with scan.
*
* If the named index already exists with the same definition this returns success
* immediately. If the named index already exists, but with a different definition
* an error is returned.
*/
createIndex({
name,
keyPrefix,
jsonPointer,
}: CreateIndexOptions): Promise<void>;
/**
* Drops an index previously created with {@link createIndex}.
* @param name
*/
dropIndex(name: string): Promise<void>;
}
interface CreateIndexOptions {
name: string;
keyPrefix?: string;
jsonPointer: string;
}
export class WriteTransactionImpl
extends ReadTransactionImpl
implements WriteTransaction {
async put(key: string, value: JSONValue | ToJSON): Promise<void> {
throwIfClosed(this);
await this._invoke('put', {
transactionId: this.id,
key,
value: JSON.stringify(value),
});
}
async del(key: string): Promise<boolean> {
throwIfClosed(this);
const result = await this._invoke('del', {
transactionId: this.id,
key,
});
return result.ok;
}
async createIndex(options: CreateIndexOptions): Promise<void> {
throwIfClosed(this);
await this._invoke('createIndex', {
transactionId: this.id,
name: options.name,
keyPrefix: options.keyPrefix || '',
jsonPointer: options.jsonPointer,
});
}
async dropIndex(name: string): Promise<void> {
throwIfClosed(this);
await this._invoke('dropIndex', {
transactionId: this.id,
name,
});
}
async commit(): Promise<CommitTransactionResponse> {
this._closed = true;
const commitRes = await this._invoke('commitTransaction', {
transactionId: this.id,
});
return commitRes;
}
}