-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathexample.ts
More file actions
97 lines (89 loc) · 2.38 KB
/
example.ts
File metadata and controls
97 lines (89 loc) · 2.38 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
import { query, mutation as rawMutation } from "./_generated/server";
import { components } from "./_generated/api";
import { TableHistory } from "@convex-dev/table-history";
import { v } from "convex/values";
import { paginationOptsValidator } from "convex/server";
import { Triggers } from "convex-helpers/server/triggers";
import { DataModel } from "./_generated/dataModel";
import {
customCtx,
customMutation,
} from "convex-helpers/server/customFunctions";
import { PaginatedQueryReference } from "convex/react";
import { api } from "./_generated/api";
const userAuditLog = new TableHistory<DataModel, "users">(
components.userAuditLog,
{
serializability: "wallclock",
}
);
const triggers = new Triggers<DataModel>();
triggers.register("users", userAuditLog.trigger());
export const mutation = customMutation(rawMutation, customCtx(triggers.wrapDB));
export const getProfile = query({
args: {},
handler: async (ctx) => {
const user = await ctx.db.query("users").first();
return user;
},
});
export const updateProfile = mutation({
args: {
name: v.string(),
email: v.string(),
},
handler: async (ctx, args) => {
const user = await ctx.db.query("users").first();
if (!user) {
await ctx.db.insert("users", {
name: args.name,
email: args.email,
});
} else {
await ctx.db.patch(user._id, {
name: args.name,
email: args.email,
});
}
},
});
export const listHistory = query({
args: {
maxTs: v.number(),
paginationOpts: paginationOptsValidator,
},
handler: async (ctx, args) => {
return await userAuditLog.listHistory(ctx, args.maxTs, args.paginationOpts);
},
});
export const listDocumentHistory = query({
args: {
id: v.id("users"),
maxTs: v.number(),
paginationOpts: paginationOptsValidator,
},
handler: async (ctx, args) => {
return await userAuditLog.listDocumentHistory(
ctx,
args.id,
args.maxTs,
args.paginationOpts
);
},
});
const _typeAssertion: PaginatedQueryReference = api.example.listDocumentHistory;
export const listSnapshot = query({
args: {
snapshotTs: v.number(),
currentTs: v.number(),
paginationOpts: paginationOptsValidator,
},
handler: async (ctx, args) => {
return await userAuditLog.listSnapshot(
ctx,
args.snapshotTs,
args.currentTs,
args.paginationOpts
);
},
});