@miniaxolotl/grove-server 1.2.2 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/server.js CHANGED
@@ -500,6 +500,25 @@ var backgroundServices = {
500
500
  }
501
501
  };
502
502
 
503
+ // lib/database/src/notebook-migration.ts
504
+ var nestedNotebooks = {
505
+ name: "20261020_008_nested_notebooks",
506
+ async up(db) {
507
+ await db.raw(`
508
+ ALTER TABLE projects
509
+ ADD COLUMN parent_id uuid,
510
+ ADD COLUMN version integer NOT NULL DEFAULT 1 CHECK (version > 0),
511
+ ADD CONSTRAINT projects_parent_owner FOREIGN KEY (account_id, parent_id)
512
+ REFERENCES projects(account_id, id) ON DELETE SET NULL (parent_id),
513
+ ADD CONSTRAINT projects_not_own_parent CHECK (parent_id IS DISTINCT FROM id);
514
+ CREATE INDEX projects_parent ON projects(account_id, parent_id);
515
+ `);
516
+ },
517
+ async down() {
518
+ throw new Error("Use a forward migration");
519
+ }
520
+ };
521
+
503
522
  // lib/database/src/migrations.ts
504
523
  var migrations = [
505
524
  {
@@ -709,7 +728,8 @@ var migrations = [
709
728
  },
710
729
  modelProviders,
711
730
  memoryLifetimes,
712
- backgroundServices
731
+ backgroundServices,
732
+ nestedNotebooks
713
733
  ];
714
734
  var migrationSource = {
715
735
  getMigrations: async () => migrations,
@@ -903,7 +923,9 @@ var forgetSchema = z.object({
903
923
  }).strict();
904
924
  var projectSchema = z.object({
905
925
  name: z.string().trim().min(1).max(120),
906
- identity: z.string().trim().min(1).max(500)
926
+ identity: z.string().trim().min(1).max(500),
927
+ parentId: uuidSchema.nullable().optional(),
928
+ expectedVersion: z.number().int().positive().optional()
907
929
  }).strict();
908
930
  var grantSchema = z.object({
909
931
  name: z.string().trim().min(1).max(100),
@@ -2854,9 +2876,13 @@ ${input.query}`.slice(-1e3) : input.query;
2854
2876
  }
2855
2877
  async projects(principal) {
2856
2878
  return this.access(principal, "memory:read", async ({ trx, grant }) => {
2857
- const q = trx("projects").select("id", "name", "identity", "createdAt").orderBy("name").limit(100);
2879
+ const q = trx("projects").select("id", "name", "identity", "parentId", "version", "createdAt").orderBy("name").limit(100);
2858
2880
  if (grant && !grant.allProjects) q.whereIn("id", grant.projectIds);
2859
- return q;
2881
+ const rows = await q;
2882
+ return rows.map((row) => ({
2883
+ ...row,
2884
+ parentId: grant && !grant.allProjects && !grant.projectIds.includes(row.parentId) ? null : row.parentId
2885
+ }));
2860
2886
  });
2861
2887
  }
2862
2888
  async project(principal, input, id) {
@@ -2864,23 +2890,71 @@ ${input.query}`.slice(-1e3) : input.query;
2864
2890
  principal,
2865
2891
  "memory:write",
2866
2892
  async ({ trx, account, grant }) => {
2893
+ const fields = ["id", "name", "identity", "parentId", "version"];
2894
+ const present = (row2) => ({
2895
+ ...row2,
2896
+ parentId: grant && !grant.allProjects && !grant.projectIds.includes(row2.parentId ?? "") ? null : row2.parentId
2897
+ });
2867
2898
  if (id) {
2868
2899
  this.scope(grant, id);
2869
- const [row2] = await trx("projects").where({ id }).update({ name: input.name }).returning(["id", "name", "identity"]);
2870
- if (!row2) throw new GroveError("not_found", "Project not found", 404);
2871
- return row2;
2900
+ const existing2 = await trx("projects").where({ id }).first();
2901
+ if (!existing2)
2902
+ throw new GroveError("not_found", "Notebook not found", 404);
2903
+ if (input.expectedVersion !== void 0 && existing2.version !== input.expectedVersion)
2904
+ throw new GroveError(
2905
+ "version_conflict",
2906
+ "This notebook changed. Reopen its settings to review the latest version.",
2907
+ 409
2908
+ );
2909
+ if (input.parentId) {
2910
+ this.scope(grant, input.parentId);
2911
+ const projects = await trx("projects").select("id", "parentId");
2912
+ const parents = new Map(
2913
+ projects.map((p) => [p.id, p.parentId])
2914
+ );
2915
+ if (!parents.has(input.parentId))
2916
+ throw new GroveError(
2917
+ "not_found",
2918
+ "Parent notebook not found",
2919
+ 404
2920
+ );
2921
+ const seen = /* @__PURE__ */ new Set([id]);
2922
+ let parent = input.parentId;
2923
+ while (parent) {
2924
+ if (seen.has(parent))
2925
+ throw new GroveError(
2926
+ "invalid_request",
2927
+ "A notebook cannot be placed inside itself or its sub-notebooks."
2928
+ );
2929
+ seen.add(parent);
2930
+ parent = parents.get(parent) ?? null;
2931
+ }
2932
+ }
2933
+ const [row2] = await trx("projects").where({ id }).update({
2934
+ name: input.name,
2935
+ ...input.parentId !== void 0 ? { parentId: input.parentId } : {},
2936
+ version: trx.raw("version + 1")
2937
+ }).returning(fields);
2938
+ return present(row2);
2872
2939
  }
2873
2940
  const existing = await trx("projects").where({ identity: input.identity }).first();
2874
2941
  if (existing) {
2875
2942
  this.scope(grant, existing.id);
2876
- return existing;
2943
+ return present(existing);
2877
2944
  }
2878
2945
  if (grant && !grant.allProjects) throw forbidden();
2946
+ if (input.parentId && !await trx("projects").where({ id: input.parentId }).first())
2947
+ throw new GroveError("not_found", "Parent notebook not found", 404);
2879
2948
  const total = await trx("projects").count("id as count").first();
2880
2949
  if (Number(total?.count) >= policy.maxProjects)
2881
2950
  throw new GroveError("project_limit", "Project limit reached", 429);
2882
- const [row] = await trx("projects").insert({ ...input, accountId: account.id }).onConflict(["accountId", "identity"]).merge({ name: input.name }).returning(["id", "name", "identity"]);
2883
- return row;
2951
+ const [row] = await trx("projects").insert({
2952
+ name: input.name,
2953
+ identity: input.identity,
2954
+ parentId: input.parentId ?? null,
2955
+ accountId: account.id
2956
+ }).onConflict(["accountId", "identity"]).merge({ name: input.name }).returning(fields);
2957
+ return present(row);
2884
2958
  }
2885
2959
  );
2886
2960
  }