feat: add convex state functions for snapshot persistence
This commit is contained in:
9
convex/schema.ts
Normal file
9
convex/schema.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { defineSchema, defineTable } from "convex/server";
|
||||
import { v } from "convex/values";
|
||||
|
||||
export default defineSchema({
|
||||
state_snapshots: defineTable({
|
||||
snapshot: v.any(),
|
||||
updatedAt: v.string(),
|
||||
}),
|
||||
});
|
||||
45
convex/state.ts
Normal file
45
convex/state.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { mutation, query } from "./_generated/server";
|
||||
import { v } from "convex/values";
|
||||
|
||||
export const getLatestSnapshot = query({
|
||||
args: {},
|
||||
handler: async (ctx) => {
|
||||
const latest = await ctx.db
|
||||
.query("state_snapshots")
|
||||
.order("desc")
|
||||
.first();
|
||||
|
||||
return latest
|
||||
? {
|
||||
snapshot: latest.snapshot,
|
||||
updatedAt: latest.updatedAt,
|
||||
}
|
||||
: null;
|
||||
},
|
||||
});
|
||||
|
||||
export const saveSnapshot = mutation({
|
||||
args: {
|
||||
snapshot: v.any(),
|
||||
updatedAt: v.string(),
|
||||
},
|
||||
handler: async (ctx, args) => {
|
||||
const latest = await ctx.db
|
||||
.query("state_snapshots")
|
||||
.order("desc")
|
||||
.first();
|
||||
|
||||
if (latest) {
|
||||
await ctx.db.patch(latest._id, {
|
||||
snapshot: args.snapshot,
|
||||
updatedAt: args.updatedAt,
|
||||
});
|
||||
return latest._id;
|
||||
}
|
||||
|
||||
return ctx.db.insert("state_snapshots", {
|
||||
snapshot: args.snapshot,
|
||||
updatedAt: args.updatedAt,
|
||||
});
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user