deno.land / x / replicache@v10.0.0-beta.0 / db / test-helpers.ts

test-helpers.ts
نووسراو ببینە
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
import {LogContext} from '@rocicorp/logger';import {expect} from '@esm-bundle/chai';import type * as dag from '../dag/mod';import {Commit, DEFAULT_HEAD_NAME, Meta} from './commit';import {readCommit, whenceHead} from './read';import {initDB, Write, readIndexesForWrite} from './write';import type {JSONValue} from '../json';
export type Chain = Commit<Meta>[];
export async function addGenesis( chain: Chain, store: dag.Store,): Promise<Chain> { expect(chain).to.have.length(0); const commit = await createGenesis(store); chain.push(commit); return chain;}
export async function createGenesis(store: dag.Store): Promise<Commit<Meta>> { await store.withWrite(async w => { await initDB(w, DEFAULT_HEAD_NAME); }); return await store.withRead(async read => { const [, commit] = await readCommit(whenceHead(DEFAULT_HEAD_NAME), read); return commit; });}
// Local commit has mutator name and args according to its index in the// chain.export async function addLocal(chain: Chain, store: dag.Store): Promise<Chain> { expect(chain).to.have.length.greaterThan(0); const i = chain.length; const commit = await createLocal([[`local`, `${i}`]], store, i);
chain.push(commit); return chain;}
export async function createLocal( entries: [string, JSONValue][], store: dag.Store, i: number,): Promise<Commit<Meta>> { const lc = new LogContext(); await store.withWrite(async dagWrite => { const w = await Write.newLocal( whenceHead(DEFAULT_HEAD_NAME), `mutator_name_${i}`, [i], null, dagWrite, 42, ); for (const [key, val] of entries) { await w.put(lc, key, val); } await w.commit(DEFAULT_HEAD_NAME); }); return await store.withRead(async dagRead => { const [, commit] = await readCommit(whenceHead(DEFAULT_HEAD_NAME), dagRead); return commit; });}
export async function addIndexChange( chain: Chain, store: dag.Store,): Promise<Chain> { expect(chain).to.have.length.greaterThan(0); const i = chain.length; const commit = await createIndex(i + '', 'local', '', store); chain.push(commit); return chain;}
export async function createIndex( name: string, prefix: string, jsonPointer: string, store: dag.Store,): Promise<Commit<Meta>> { const lc = new LogContext(); await store.withWrite(async dagWrite => { const w = await Write.newIndexChange( whenceHead(DEFAULT_HEAD_NAME), dagWrite, ); await w.createIndex(lc, name, prefix, jsonPointer); await w.commit(DEFAULT_HEAD_NAME); }); return store.withRead(async dagRead => { const [, commit] = await readCommit(whenceHead(DEFAULT_HEAD_NAME), dagRead); return commit; });}
// See also sync.test_helpers for add_sync_snapshot, which can't go here because// it depends on details of sync and sync depends on db.
// The optional map for the commit is treated as key, value pairs.export async function addSnapshot( chain: Chain, store: dag.Store, map: [string, JSONValue][] | undefined,): Promise<Chain> { expect(chain).to.have.length.greaterThan(0); const lc = new LogContext(); const cookie = `cookie_${chain.length}`; await store.withWrite(async dagWrite => { const w = await Write.newSnapshot( whenceHead(DEFAULT_HEAD_NAME), chain[chain.length - 1].nextMutationID, cookie, dagWrite, readIndexesForWrite(chain[chain.length - 1]), );
if (map) { for (const [k, v] of map) { await w.put(lc, k, v); } } await w.commit(DEFAULT_HEAD_NAME); }); return store.withRead(async dagRead => { const [, commit] = await readCommit(whenceHead(DEFAULT_HEAD_NAME), dagRead); chain.push(commit); return chain; });}
replicache

Version Info

Tagged at
2 years ago