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
162 lines (142 loc) · 4.16 KB
/
Copy pathtransactions.ts
File metadata and controls
162 lines (142 loc) · 4.16 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
import type {JSONValue, ToJSON} from './json.js';
import type {
Invoke,
OpenTransactionRequest,
CommitTransactionResponse,
} from './repm-invoker.js';
import type {ScanOptions} from './scan-options.js';
import {ScanResult} from './scan-iterator.js';
import {TransactionClosedError} 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`.
*
* It the `ScanResult` is used after the `ReadTransaction` has been closed it
* will throw a {@link TransactionClosedError}.
*/
scan(options?: ScanOptions): ScanResult;
/**
* Convenience for scan() that reads all results into an array.
*/
scanAll(options?: ScanOptions): Promise<[string, JSONValue][]>;
}
export function throwIfClosed(tx: {closed: boolean}): void {
if (tx.closed) {
throw new TransactionClosedError();
}
}
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({prefix = '', start}: ScanOptions = {}): ScanResult {
return new ScanResult(prefix, start, this._invoke, () => this, false);
}
async scanAll(options: ScanOptions = {}): Promise<[string, JSONValue][]> {
const it = this.scan(options).entries();
const result = [];
for await (const pair of it) {
result.push(pair);
}
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>;
}
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 commit(): Promise<CommitTransactionResponse> {
this._closed = true;
const commitRes = await this._invoke('commitTransaction', {
transactionId: this.id,
});
return commitRes;
}
}