Repository navigation
Expand file tree
/
Copy pathtransactions.ts
More file actions
108 lines (96 loc) · 2.77 KB
/
Copy pathtransactions.ts
File metadata and controls
108 lines (96 loc) · 2.77 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
import type {JSONValue, ToJSON} from './json.js';
import type {ScanItem} from './scan-item.js';
import type {ScanOptions} from './scan-options.js';
import type {Invoke, ScanRequest} from './repm-invoker.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.
*/
scan(options?: ScanOptions): Promise<Iterable<ScanItem>>;
}
export class ReadTransactionImpl implements ReadTransaction {
protected readonly _transactionId: number;
protected readonly _invoke: Invoke;
constructor(invoke: Invoke, transactionId: number) {
this._invoke = invoke;
this._transactionId = transactionId;
}
async get(key: string): Promise<JSONValue | undefined> {
const result = await this._invoke('get', {
transactionId: this._transactionId,
key: key,
});
if (!result.has) {
return undefined;
}
return result.value;
}
async has(key: string): Promise<boolean> {
const result = await this._invoke('has', {
transactionId: this._transactionId,
key: key,
});
return result['has'];
}
async scan({prefix = '', start, limit = 50}: ScanOptions = {}): Promise<
Iterable<ScanItem>
> {
const args: ScanRequest = {
transactionId: this._transactionId,
limit: limit,
};
if (prefix !== undefined) {
args.prefix = prefix;
}
if (start !== undefined) {
args.start = start;
}
return await this._invoke('scan', args);
}
}
/**
* 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> {
await this._invoke('put', {
transactionId: this._transactionId,
key: key,
value: value,
});
}
async del(key: string): Promise<boolean> {
const result = await this._invoke('del', {
transactionId: this._transactionId,
key: key,
});
return result['ok'];
}
}