-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.ts
293 lines (268 loc) · 7.9 KB
/
db.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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import { ulid } from "./deps.ts";
import { getSubCollectionNames, omit } from "./utils.ts";
import { CollectionAccess, CollectionMeta } from "./types.ts";
const setCollection = (kv: Deno.Kv) =>
async (
name: string,
schema: Record<string, unknown>,
access: CollectionAccess[] = [],
) => {
await kv.set(["collection-meta", name], { schema, access });
};
const getCollection = (kv: Deno.Kv) =>
async (
name: string,
): Promise<CollectionMeta | undefined> => {
const { key, value } = await kv.get<Omit<CollectionMeta, "name">>([
"collection-meta",
name,
]);
if (value) {
return { ...value, name: String(key[1]) };
}
return undefined;
};
const listCollections = (kv: Deno.Kv) => async () => {
const result: CollectionMeta[] = [];
for await (
const item of kv.list<Omit<CollectionMeta, "name">>({
prefix: ["collection-meta"],
})
) {
result.push({ ...item.value, name: String(item.key[1]) });
}
return result;
};
const deleteCollection = (kv: Deno.Kv) => async (name: string) => {
for await (const { key } of kv.list({ prefix: ["collection", name] })) {
await kv.delete(key);
}
return kv.delete(["collection-meta", name]);
};
const addCollectionAccess =
(kv: Deno.Kv) => async (name: string, accessItem: CollectionAccess) => {
const collection = await getCollection(kv)(name);
if (!collection) return false;
const access = collection.access.filter((d) => d.key !== accessItem.key);
access.push(accessItem);
await setCollection(kv)(name, collection.schema, access);
return true;
};
const removeCollectionAccess =
(kv: Deno.Kv) => async (name: string, key: string) => {
const collection = await getCollection(kv)(name);
if (!collection) return false;
const access = collection.access.filter((d) => d.key !== key);
await setCollection(kv)(name, collection.schema, access);
return true;
};
const insertCollectionItem =
(kv: Deno.Kv) =>
async <T = Record<string, unknown>>(
{ collection, data, id, skipSubCollections }: {
collection: CollectionMeta;
data: T;
id?: string;
skipSubCollections?: boolean;
},
) => {
const _id = id || ulid();
const subCollections = getSubCollectionNames(collection);
await kv.set(
["collection", collection.name, _id],
omit(subCollections, data),
);
const result: Record<string, unknown> = {
_id,
...omit(subCollections, data),
};
if (!skipSubCollections) {
for await (const key of subCollections) {
const values = [];
// @ts-ignore ?
const items: Record<string, unknown>[] = Array.isArray(data[key])
// @ts-ignore ?
? data[key]
: [];
for await (const item of items) {
const sub_id = ulid();
await kv.set(["collection", collection.name, _id, key, sub_id], item);
values.push({ _id: sub_id, ...item });
}
result[key] = values;
}
}
return result;
};
const addCollectionItem = (kv: Deno.Kv) =>
<T = Record<string, unknown>>(
collection: CollectionMeta,
data: T,
) => insertCollectionItem(kv)({ collection, data });
const addSubCollectionItem =
(kv: Deno.Kv) =>
async <T = Record<string, unknown>>(
collection: CollectionMeta,
_id: string,
key: string,
data: T,
) => {
const subCollections = getSubCollectionNames(collection);
if (!subCollections.includes(key)) return undefined;
const _sub_id = ulid();
await kv.set(["collection", collection.name, _id, key, _sub_id], data);
return { _id: _sub_id, ...data };
};
const getCollectionItem =
(kv: Deno.Kv) =>
async <T = Record<string, unknown>>(name: string, _id: string) => {
const res = await kv.get<T>(["collection", name, _id]);
if (res.value) {
return { _id, ...res.value };
}
return undefined;
};
const getSubCollectionItem =
(kv: Deno.Kv) =>
async <T = Record<string, unknown>>(
collection: CollectionMeta,
_id: string,
key: string,
_sub_id: string,
) => {
const subCollections = getSubCollectionNames(collection);
if (!subCollections.includes(key)) {
return undefined;
}
const res = await kv.get<T>([
"collection",
collection.name,
_id,
key,
_sub_id,
]);
if (res.value) {
return { _id, ...res.value };
}
return undefined;
};
const setCollectionItem = (kv: Deno.Kv) =>
async <T = Record<string, unknown>>(
collection: CollectionMeta,
_id: string,
data: T,
put?: boolean,
) => {
const item = await getCollectionItem(kv)(collection.name, _id);
if (!item) return undefined;
const { _id: _, ...rest } = item;
const toSave = put ? data : { ...rest, ...data };
return insertCollectionItem(kv)({
collection,
data: toSave,
id: _id,
skipSubCollections: true,
});
};
const setSubCollectionItem =
(kv: Deno.Kv) =>
async <T = Record<string, unknown>>(
collection: CollectionMeta,
_id: string,
key: string,
_sub_id: string,
data: T,
put?: boolean,
) => {
const item = await getSubCollectionItem(kv)(collection, _id, key, _sub_id);
if (!item) return undefined;
const { _id: _, ...rest } = item;
const toSave = put ? data : { ...rest, ...data };
await kv.set(
["collection", collection.name, _id, key, _sub_id],
toSave,
);
return { _id: _sub_id, ...toSave };
};
const deleteCollectionItem =
(kv: Deno.Kv) => async (name: string, _id: string) => {
for await (const item of kv.list({ prefix: ["collection", name, _id] })) {
await kv.delete(item.key);
}
};
const deleteSubCollectionItem = (kv: Deno.Kv) =>
async (
collection: CollectionMeta,
_id: string,
key: string,
_sub_id: string,
) => {
const subCollections = getSubCollectionNames(collection);
if (!subCollections.includes(key)) {
return undefined;
}
await kv.delete(["collection", collection.name, _id, key, _sub_id]);
};
const listCollectionItems =
(kv: Deno.Kv) =>
async <T = Record<string, unknown>>(
name: string,
filter: (d: T, i: number) => boolean = () => true,
) => {
const result: (T & { _id: string })[] = [];
let i = 0;
for await (const item of kv.list<T>({ prefix: ["collection", name] })) {
if (filter(item.value, i) && item.key.length === 3) {
result.push({ ...item.value, _id: String(item.key[2]) });
}
i++;
}
return result;
};
const listSubCollectionItems =
(kv: Deno.Kv) =>
async <T = Record<string, unknown>>(
collection: CollectionMeta,
_id: string,
key: string,
filter: (d: T, i: number) => boolean = () => true,
) => {
const subCollections = getSubCollectionNames(collection);
if (!subCollections.includes(key)) {
return undefined;
}
const result: (T & { _id: string })[] = [];
let i = 0;
for await (
const item of kv.list<T>({
prefix: ["collection", collection.name, _id, key],
})
) {
if (filter(item.value, i) && item.key.length === 5) {
result.push({ ...item.value, _id: String(item.key[4]) });
}
i++;
}
return result;
};
export default async (path?: string) => {
const kv = await Deno.openKv(path);
return {
setCollection: setCollection(kv),
getCollection: getCollection(kv),
listCollections: listCollections(kv),
deleteCollection: deleteCollection(kv),
addCollectionAccess: addCollectionAccess(kv),
removeCollectionAccess: removeCollectionAccess(kv),
getCollectionItem: getCollectionItem(kv),
getSubCollectionItem: getSubCollectionItem(kv),
addCollectionItem: addCollectionItem(kv),
addSubCollectionItem: addSubCollectionItem(kv),
setCollectionItem: setCollectionItem(kv),
setSubCollectionItem: setSubCollectionItem(kv),
deleteCollectionItem: deleteCollectionItem(kv),
deleteSubCollectionItem: deleteSubCollectionItem(kv),
listCollectionItems: listCollectionItems(kv),
listSubCollectionItems: listSubCollectionItems(kv),
};
};