@miniaxolotl/grove-server 1.2.3 → 1.4.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/worker.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,
@@ -1830,7 +1850,9 @@ var forgetSchema = z4.object({
1830
1850
  }).strict();
1831
1851
  var projectSchema = z4.object({
1832
1852
  name: z4.string().trim().min(1).max(120),
1833
- identity: z4.string().trim().min(1).max(500)
1853
+ identity: z4.string().trim().min(1).max(500),
1854
+ parentId: uuidSchema.nullable().optional(),
1855
+ expectedVersion: z4.number().int().positive().optional()
1834
1856
  }).strict();
1835
1857
  var grantSchema = z4.object({
1836
1858
  name: z4.string().trim().min(1).max(100),
@@ -2646,9 +2668,13 @@ ${input.query}`.slice(-1e3) : input.query;
2646
2668
  }
2647
2669
  async projects(principal) {
2648
2670
  return this.access(principal, "memory:read", async ({ trx, grant }) => {
2649
- const q = trx("projects").select("id", "name", "identity", "createdAt").orderBy("name").limit(100);
2671
+ const q = trx("projects").select("id", "name", "identity", "parentId", "version", "createdAt").orderBy("name").limit(100);
2650
2672
  if (grant && !grant.allProjects) q.whereIn("id", grant.projectIds);
2651
- return q;
2673
+ const rows = await q;
2674
+ return rows.map((row) => ({
2675
+ ...row,
2676
+ parentId: grant && !grant.allProjects && !grant.projectIds.includes(row.parentId) ? null : row.parentId
2677
+ }));
2652
2678
  });
2653
2679
  }
2654
2680
  async project(principal, input, id) {
@@ -2656,23 +2682,71 @@ ${input.query}`.slice(-1e3) : input.query;
2656
2682
  principal,
2657
2683
  "memory:write",
2658
2684
  async ({ trx, account, grant }) => {
2685
+ const fields = ["id", "name", "identity", "parentId", "version"];
2686
+ const present = (row2) => ({
2687
+ ...row2,
2688
+ parentId: grant && !grant.allProjects && !grant.projectIds.includes(row2.parentId ?? "") ? null : row2.parentId
2689
+ });
2659
2690
  if (id) {
2660
2691
  this.scope(grant, id);
2661
- const [row2] = await trx("projects").where({ id }).update({ name: input.name }).returning(["id", "name", "identity"]);
2662
- if (!row2) throw new GroveError("not_found", "Project not found", 404);
2663
- return row2;
2692
+ const existing2 = await trx("projects").where({ id }).first();
2693
+ if (!existing2)
2694
+ throw new GroveError("not_found", "Notebook not found", 404);
2695
+ if (input.expectedVersion !== void 0 && existing2.version !== input.expectedVersion)
2696
+ throw new GroveError(
2697
+ "version_conflict",
2698
+ "This notebook changed. Reopen its settings to review the latest version.",
2699
+ 409
2700
+ );
2701
+ if (input.parentId) {
2702
+ this.scope(grant, input.parentId);
2703
+ const projects = await trx("projects").select("id", "parentId");
2704
+ const parents = new Map(
2705
+ projects.map((p) => [p.id, p.parentId])
2706
+ );
2707
+ if (!parents.has(input.parentId))
2708
+ throw new GroveError(
2709
+ "not_found",
2710
+ "Parent notebook not found",
2711
+ 404
2712
+ );
2713
+ const seen = /* @__PURE__ */ new Set([id]);
2714
+ let parent = input.parentId;
2715
+ while (parent) {
2716
+ if (seen.has(parent))
2717
+ throw new GroveError(
2718
+ "invalid_request",
2719
+ "A notebook cannot be placed inside itself or its sub-notebooks."
2720
+ );
2721
+ seen.add(parent);
2722
+ parent = parents.get(parent) ?? null;
2723
+ }
2724
+ }
2725
+ const [row2] = await trx("projects").where({ id }).update({
2726
+ name: input.name,
2727
+ ...input.parentId !== void 0 ? { parentId: input.parentId } : {},
2728
+ version: trx.raw("version + 1")
2729
+ }).returning(fields);
2730
+ return present(row2);
2664
2731
  }
2665
2732
  const existing = await trx("projects").where({ identity: input.identity }).first();
2666
2733
  if (existing) {
2667
2734
  this.scope(grant, existing.id);
2668
- return existing;
2735
+ return present(existing);
2669
2736
  }
2670
2737
  if (grant && !grant.allProjects) throw forbidden();
2738
+ if (input.parentId && !await trx("projects").where({ id: input.parentId }).first())
2739
+ throw new GroveError("not_found", "Parent notebook not found", 404);
2671
2740
  const total = await trx("projects").count("id as count").first();
2672
2741
  if (Number(total?.count) >= policy.maxProjects)
2673
2742
  throw new GroveError("project_limit", "Project limit reached", 429);
2674
- const [row] = await trx("projects").insert({ ...input, accountId: account.id }).onConflict(["accountId", "identity"]).merge({ name: input.name }).returning(["id", "name", "identity"]);
2675
- return row;
2743
+ const [row] = await trx("projects").insert({
2744
+ name: input.name,
2745
+ identity: input.identity,
2746
+ parentId: input.parentId ?? null,
2747
+ accountId: account.id
2748
+ }).onConflict(["accountId", "identity"]).merge({ name: input.name }).returning(fields);
2749
+ return present(row);
2676
2750
  }
2677
2751
  );
2678
2752
  }