forked from rocicorp/replicache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.ts
More file actions
49 lines (43 loc) · 1.16 KB
/
Copy pathdata.ts
File metadata and controls
49 lines (43 loc) · 1.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
import Replicache, {WriteTransaction} from 'replicache';
import {
dataLayerAuth,
diffServerURL,
diffServerAuth,
batchURL,
} from './settings';
import type {EventIdentity, Event, EventUpdate} from './types';
export const rep = new Replicache({
diffServerURL,
dataLayerAuth,
diffServerAuth,
batchURL,
});
function eventKey(event: EventIdentity) {
return `/event/${event.id}`;
}
export const addEvent = rep.register(
'addEvent',
async (tx: WriteTransaction, event: Event) => {
await tx.put(eventKey(event), event);
},
);
export const updateEvent = rep.register(
'updateEvent',
async (tx: WriteTransaction, update: EventUpdate) => {
const event = (await tx.get(eventKey(update))) as Event | null;
if (!event) {
console.warn(`Possible conflict - event ${update.id} not found`);
return;
}
event.summary = update.summary ?? event.summary;
event.start = update.start ?? event.start;
event.end = update.end ?? event.end;
await tx.put(eventKey(event), event);
},
);
export const deleteEvent = rep.register(
'deleteEvent',
async (tx: WriteTransaction, id: EventIdentity) => {
await tx.del(eventKey(id));
},
);