0.3.5 共享功能修复
This commit is contained in:
@@ -2,6 +2,18 @@ import { mutation, query } from "./_generated/server";
|
||||
import { v } from "convex/values";
|
||||
import { nowIso } from "./_utils/time";
|
||||
import { generateId } from "./_utils/id";
|
||||
import type { MutationCtx, QueryCtx } from "./_generated/server";
|
||||
|
||||
async function assertWorkspaceMember(ctx: QueryCtx | MutationCtx, userId: string, workspaceId: string) {
|
||||
const membership = await ctx.db
|
||||
.query("workspace_members")
|
||||
.withIndex("by_workspace_user", (q) => q.eq("workspace_id", workspaceId).eq("user_id", userId))
|
||||
.first();
|
||||
if (!membership) {
|
||||
throw new Error("无权访问该工作空间");
|
||||
}
|
||||
return membership;
|
||||
}
|
||||
|
||||
// Query: 获取单个表格
|
||||
export const get = query({
|
||||
@@ -102,6 +114,40 @@ export const listByDocument = query({
|
||||
},
|
||||
});
|
||||
|
||||
export const listByWorkspaceForSearch = query({
|
||||
args: {
|
||||
userId: v.string(),
|
||||
workspaceId: v.string(),
|
||||
includeArchived: v.optional(v.boolean()),
|
||||
limit: v.optional(v.number()),
|
||||
},
|
||||
handler: async (ctx, args) => {
|
||||
await assertWorkspaceMember(ctx, args.userId, args.workspaceId);
|
||||
|
||||
const includeArchived = Boolean(args.includeArchived);
|
||||
const limit = Math.max(1, Math.min(3000, Math.floor(args.limit ?? 3000)));
|
||||
|
||||
const rows = await ctx.db
|
||||
.query("document_tables")
|
||||
.withIndex("by_workspace", (q) => q.eq("workspace_id", args.workspaceId))
|
||||
.order("desc")
|
||||
.take(limit * 2);
|
||||
|
||||
return rows
|
||||
.filter((t) => (includeArchived ? true : !t.is_archived))
|
||||
.slice(0, limit)
|
||||
.map((t) => ({
|
||||
id: t.id,
|
||||
workspace_id: t.workspace_id,
|
||||
document_id: t.document_id,
|
||||
title: t.title,
|
||||
is_archived: t.is_archived,
|
||||
created_at: t.created_at,
|
||||
updated_at: t.updated_at,
|
||||
}));
|
||||
},
|
||||
});
|
||||
|
||||
// Mutation: 创建表格
|
||||
export const create = mutation({
|
||||
args: {
|
||||
@@ -287,3 +333,36 @@ export const getRows = query({
|
||||
.map((r) => r.row_data);
|
||||
},
|
||||
});
|
||||
|
||||
export const listRowsByWorkspaceForSearch = query({
|
||||
args: {
|
||||
userId: v.string(),
|
||||
workspaceId: v.string(),
|
||||
limit: v.optional(v.number()),
|
||||
},
|
||||
handler: async (ctx, args) => {
|
||||
await assertWorkspaceMember(ctx, args.userId, args.workspaceId);
|
||||
|
||||
const limit = Math.max(1, Math.min(8000, Math.floor(args.limit ?? 8000)));
|
||||
|
||||
const rows = await ctx.db
|
||||
.query("document_table_rows")
|
||||
.withIndex("by_workspace", (q) => q.eq("workspace_id", args.workspaceId))
|
||||
.order("desc")
|
||||
.take(limit * 2);
|
||||
|
||||
return rows
|
||||
.filter((r) => !r.is_deleted)
|
||||
.slice(0, limit)
|
||||
.map((r) => ({
|
||||
id: r.id,
|
||||
workspace_id: r.workspace_id,
|
||||
document_id: r.document_id,
|
||||
table_id: r.table_id,
|
||||
row_index: r.row_index,
|
||||
row_hash: r.row_hash ?? null,
|
||||
created_at: r.created_at ?? null,
|
||||
updated_at: r.updated_at ?? null,
|
||||
}));
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user