-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
convex.ts
82 lines (76 loc) · 2.25 KB
/
convex.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
/* eslint-disable spaced-comment */
// eslint-disable-next-line import/no-extraneous-dependencies
import {
internalQueryGeneric as internalQuery,
internalMutationGeneric as internalMutation,
} from "convex/server";
// eslint-disable-next-line import/no-extraneous-dependencies
import { GenericId, v } from "convex/values";
export const get = /*#__PURE__*/ internalQuery({
args: {
id: /*#__PURE__*/ v.string(),
},
handler: async (ctx, args) => {
const result = await ctx.db.get(args.id as GenericId<string>);
return result;
},
});
export const insert = /*#__PURE__*/ internalMutation({
args: {
table: /*#__PURE__*/ v.string(),
document: /*#__PURE__*/ v.any(),
},
handler: async (ctx, args) => {
await ctx.db.insert(args.table, args.document);
},
});
export const lookup = /*#__PURE__*/ internalQuery({
args: {
table: /*#__PURE__*/ v.string(),
index: /*#__PURE__*/ v.string(),
keyField: /*#__PURE__*/ v.string(),
key: /*#__PURE__*/ v.string(),
},
handler: async (ctx, args) => {
const result = await ctx.db
.query(args.table)
.withIndex(args.index, (q) => q.eq(args.keyField, args.key))
.collect();
return result;
},
});
export const upsert = /*#__PURE__*/ internalMutation({
args: {
table: /*#__PURE__*/ v.string(),
index: /*#__PURE__*/ v.string(),
keyField: /*#__PURE__*/ v.string(),
key: /*#__PURE__*/ v.string(),
document: /*#__PURE__*/ v.any(),
},
handler: async (ctx, args) => {
const existing = await ctx.db
.query(args.table)
.withIndex(args.index, (q) => q.eq(args.keyField, args.key))
.unique();
if (existing !== null) {
await ctx.db.replace(existing._id, args.document);
} else {
await ctx.db.insert(args.table, args.document);
}
},
});
export const deleteMany = /*#__PURE__*/ internalMutation({
args: {
table: /*#__PURE__*/ v.string(),
index: /*#__PURE__*/ v.string(),
keyField: /*#__PURE__*/ v.string(),
key: /*#__PURE__*/ v.string(),
},
handler: async (ctx, args) => {
const existing = await ctx.db
.query(args.table)
.withIndex(args.index, (q) => q.eq(args.keyField, args.key))
.collect();
await Promise.all(existing.map((doc) => ctx.db.delete(doc._id)));
},
});