Files
convex/convex/github/action.ts

68 lines
1.7 KiB
TypeScript

import { query, mutation, action } from "../_generated/server.js";
import { v } from "convex/values";
export const getList = query({
args: {},
handler: async (ctx) => {
const results = await ctx.db.query("github_starred").collect();
return results;
},
});
export const createStarred = mutation({
args: {
repo_id: v.number(),
title: v.string(),
description: v.string(),
author: v.string(),
stars: v.number(),
last_updated: v.string(),
link: v.string(),
auto: v.optional(v.string()),
summary: v.optional(v.string()),
tags: v.optional(v.array(v.string())),
},
handler: async (ctx, values) => {
const repo_id = values.repo_id;
const existing = await ctx.db.query("github_starred").withIndex("by_repo_id", (q) =>
q.eq("repo_id", repo_id)
).first();
if (existing) {
// 更新已有记录
const updated = await ctx.db.patch("github_starred", existing._id, values);
return updated;
}
// 创建新记录
const result = await ctx.db.insert("github_starred", values);
return result;
}
});
export const updateById = mutation({
args: {
id: v.id("github_starred"),
repo_id: v.number(),
title: v.string(),
description: v.string(),
author: v.string(),
stars: v.number(),
last_updated: v.string(),
link: v.string(),
auto: v.optional(v.string()),
summary: v.optional(v.string()),
},
handler: async (ctx, args) => {
const { id, ...rest } = args;
await ctx.db.patch(id, rest);
},
});
export const deleteById = mutation({
args: {
id: v.id("github_starred"),
},
handler: async (ctx, args) => {
await ctx.db.delete(args.id);
},
});