@manablox/db 0.1.0 → 0.2.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.
Files changed (53) hide show
  1. package/README.md +21 -0
  2. package/drizzle.config.ts +1 -1
  3. package/migrations/0005_menus.sql +44 -0
  4. package/migrations/0006_roles.sql +13 -0
  5. package/migrations/0007_apikey-permissions.sql +4 -0
  6. package/migrations/0008_workflows.sql +49 -0
  7. package/migrations/meta/0005_snapshot.json +2504 -0
  8. package/migrations/meta/0006_snapshot.json +2605 -0
  9. package/migrations/meta/0007_snapshot.json +2605 -0
  10. package/migrations/meta/0008_snapshot.json +2986 -0
  11. package/migrations/meta/_journal.json +28 -0
  12. package/package.json +10 -5
  13. package/src/cli/create-db.ts +30 -0
  14. package/src/cli/migrate.ts +2 -9
  15. package/src/client.ts +8 -1
  16. package/src/errors.ts +50 -0
  17. package/src/index.ts +8 -2
  18. package/src/migrate.ts +21 -0
  19. package/src/pagination.ts +52 -0
  20. package/src/query.ts +1 -5
  21. package/src/repositories/asset-usage.ts +1 -1
  22. package/src/repositories/asset.ts +13 -21
  23. package/src/repositories/content-type.ts +1 -1
  24. package/src/repositories/content.ts +150 -97
  25. package/src/repositories/index.ts +12 -0
  26. package/src/repositories/menu.ts +235 -0
  27. package/src/repositories/role.ts +85 -0
  28. package/src/repositories/space.ts +7 -2
  29. package/src/repositories/user.ts +171 -25
  30. package/src/repositories/webhook.ts +46 -0
  31. package/src/repositories/workflow.ts +306 -0
  32. package/src/schema/assets.ts +108 -0
  33. package/src/schema/auth.ts +166 -0
  34. package/src/schema/content-types.ts +31 -0
  35. package/src/schema/content.ts +133 -0
  36. package/src/schema/index.ts +38 -0
  37. package/src/schema/menus.ts +61 -0
  38. package/src/schema/relations.ts +64 -0
  39. package/src/schema/spaces.ts +20 -0
  40. package/src/schema/webhooks.ts +46 -0
  41. package/src/schema/workflows.ts +92 -0
  42. package/{test/helpers.ts → src/testing-fixtures.ts} +21 -35
  43. package/src/testing.ts +105 -0
  44. package/test/asset-usage.test.ts +3 -3
  45. package/test/menu.test.ts +126 -0
  46. package/test/publish.test.ts +31 -3
  47. package/test/query.test.ts +33 -3
  48. package/test/role.test.ts +81 -0
  49. package/test/tree.test.ts +3 -3
  50. package/test/user.test.ts +126 -0
  51. package/test/webhook.test.ts +48 -0
  52. package/vitest.config.ts +0 -2
  53. package/src/schema.ts +0 -513
package/README.md ADDED
@@ -0,0 +1,21 @@
1
+ # `@manablox/db`
2
+
3
+ The storage layer: the Drizzle schema (one file per entity under `src/schema/`), the migrations, one repository per table group, the filter and sort builder that turns a `ContentFilter` into SQL, and the helpers tests use to get a migrated database.
4
+
5
+ ## Exports
6
+
7
+ - `createDatabase`, `createRepositories`, `Repositories`
8
+ - `ContentRepository` and friends — every query the services run
9
+ - `buildContentWhere`, `buildOrderBy` — the allow-listed query builder
10
+ - `rethrowUniqueViolation` — a Postgres unique violation as a validation error
11
+ - `@manablox/db/testing` — `createTestDatabase()`, a clone of a migrated template
12
+
13
+ ## Depends on
14
+
15
+ @manablox/core, drizzle-orm, postgres
16
+
17
+ ## Test
18
+
19
+ ```sh
20
+ pnpm --filter @manablox/db test
21
+ ```
package/drizzle.config.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { defineConfig } from 'drizzle-kit';
2
2
 
3
3
  export default defineConfig({
4
- schema: './src/schema.ts',
4
+ schema: './src/schema/index.ts',
5
5
  out: './migrations',
6
6
  dialect: 'postgresql',
7
7
  dbCredentials: {
@@ -0,0 +1,44 @@
1
+ CREATE TABLE "menu_items" (
2
+ "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
3
+ "menu_id" uuid NOT NULL,
4
+ "parent_id" uuid,
5
+ "position" integer DEFAULT 0 NOT NULL,
6
+ "localization_id" uuid,
7
+ "label" text,
8
+ "url" text
9
+ );
10
+ --> statement-breakpoint
11
+ CREATE TABLE "menus" (
12
+ "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
13
+ "space_id" uuid NOT NULL,
14
+ "name" text NOT NULL,
15
+ "machine_name" text NOT NULL,
16
+ "description" text,
17
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
18
+ "updated_at" timestamp with time zone DEFAULT now() NOT NULL,
19
+ CONSTRAINT "menus_space_machine_name_key" UNIQUE("space_id","machine_name")
20
+ );
21
+ --> statement-breakpoint
22
+ ALTER TABLE "menu_items" ADD CONSTRAINT "menu_items_menu_id_menus_id_fk" FOREIGN KEY ("menu_id") REFERENCES "public"."menus"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
23
+ ALTER TABLE "menu_items" ADD CONSTRAINT "menu_items_parent_id_menu_items_id_fk" FOREIGN KEY ("parent_id") REFERENCES "public"."menu_items"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
24
+ ALTER TABLE "menus" ADD CONSTRAINT "menus_space_id_spaces_id_fk" FOREIGN KEY ("space_id") REFERENCES "public"."spaces"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
25
+ CREATE INDEX "menu_items_menu_idx" ON "menu_items" USING btree ("menu_id","parent_id","position");--> statement-breakpoint
26
+ CREATE INDEX "menu_items_localization_idx" ON "menu_items" USING btree ("localization_id");--> statement-breakpoint
27
+ -- Every document that was flagged "show in menu" becomes an entry of a "Main" menu in
28
+ -- its space, one entry per document (all translations share it), nested the way the
29
+ -- documents nest in the tree. Entry ids equal the documents' localization ids for the
30
+ -- duration of the migration so parents can be linked in one pass.
31
+ INSERT INTO "menus" ("space_id", "name", "machine_name", "description")
32
+ SELECT "space_id", 'Main', 'main', 'Created from the former "show in menu" flag.'
33
+ FROM "contents" WHERE "visible_in_menu" GROUP BY "space_id";--> statement-breakpoint
34
+ INSERT INTO "menu_items" ("id", "menu_id", "parent_id", "position", "localization_id")
35
+ SELECT DISTINCT ON (c."localization_id") c."localization_id", m."id", NULL, c."position", c."localization_id"
36
+ FROM "contents" c JOIN "menus" m ON m."space_id" = c."space_id" AND m."machine_name" = 'main'
37
+ WHERE c."visible_in_menu"
38
+ ORDER BY c."localization_id", c."locale";--> statement-breakpoint
39
+ UPDATE "menu_items" mi SET "parent_id" = p."localization_id"
40
+ FROM "contents" c JOIN "contents" p ON p."id" = c."parent_id"
41
+ WHERE mi."id" = c."localization_id"
42
+ AND EXISTS (SELECT 1 FROM "menu_items" x WHERE x."id" = p."localization_id" AND x."menu_id" = mi."menu_id");--> statement-breakpoint
43
+ ALTER TABLE "contents" DROP COLUMN "visible_in_menu";--> statement-breakpoint
44
+ ALTER TABLE "published_contents" DROP COLUMN "visible_in_menu";
@@ -0,0 +1,13 @@
1
+ CREATE TABLE "roles" (
2
+ "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
3
+ "space_id" uuid NOT NULL,
4
+ "name" text NOT NULL,
5
+ "machine_name" text NOT NULL,
6
+ "description" text,
7
+ "permissions" jsonb DEFAULT '[]'::jsonb NOT NULL,
8
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
9
+ "updated_at" timestamp with time zone DEFAULT now() NOT NULL
10
+ );
11
+ --> statement-breakpoint
12
+ ALTER TABLE "roles" ADD CONSTRAINT "roles_space_id_spaces_id_fk" FOREIGN KEY ("space_id") REFERENCES "public"."spaces"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
13
+ CREATE UNIQUE INDEX "roles_space_machine_name_key" ON "roles" USING btree ("space_id","machine_name");
@@ -0,0 +1,4 @@
1
+ -- The column was declared by better-auth's key schema and never written; it now holds
2
+ -- the key's grants as a JSON array. Anything that is not one becomes null.
3
+ ALTER TABLE "apikeys" ALTER COLUMN "permissions" SET DATA TYPE jsonb
4
+ USING (CASE WHEN "permissions" ~ '^\s*\[' THEN "permissions"::jsonb ELSE NULL END);
@@ -0,0 +1,49 @@
1
+ CREATE TABLE "push_subscriptions" (
2
+ "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
3
+ "user_id" uuid NOT NULL,
4
+ "endpoint" text NOT NULL,
5
+ "keys" jsonb NOT NULL,
6
+ "user_agent" text,
7
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
8
+ "last_used_at" timestamp with time zone,
9
+ CONSTRAINT "push_subscriptions_endpoint_key" UNIQUE("endpoint")
10
+ );
11
+ --> statement-breakpoint
12
+ CREATE TABLE "workflow_runs" (
13
+ "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
14
+ "workflow_id" uuid NOT NULL,
15
+ "space_id" uuid NOT NULL,
16
+ "trigger" text NOT NULL,
17
+ "status" text DEFAULT 'queued' NOT NULL,
18
+ "context" jsonb NOT NULL,
19
+ "cursor" jsonb DEFAULT '[]'::jsonb NOT NULL,
20
+ "log" jsonb DEFAULT '[]'::jsonb NOT NULL,
21
+ "error" text,
22
+ "resume_at" timestamp with time zone,
23
+ "started_at" timestamp with time zone,
24
+ "finished_at" timestamp with time zone,
25
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL
26
+ );
27
+ --> statement-breakpoint
28
+ CREATE TABLE "workflows" (
29
+ "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
30
+ "space_id" uuid NOT NULL,
31
+ "name" text NOT NULL,
32
+ "description" text,
33
+ "enabled" boolean DEFAULT false NOT NULL,
34
+ "trigger" jsonb NOT NULL,
35
+ "steps" jsonb DEFAULT '[]'::jsonb NOT NULL,
36
+ "last_scheduled_at" timestamp with time zone,
37
+ "last_run_at" timestamp with time zone,
38
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
39
+ "updated_at" timestamp with time zone DEFAULT now() NOT NULL
40
+ );
41
+ --> statement-breakpoint
42
+ ALTER TABLE "push_subscriptions" ADD CONSTRAINT "push_subscriptions_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
43
+ ALTER TABLE "workflow_runs" ADD CONSTRAINT "workflow_runs_workflow_id_workflows_id_fk" FOREIGN KEY ("workflow_id") REFERENCES "public"."workflows"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
44
+ ALTER TABLE "workflow_runs" ADD CONSTRAINT "workflow_runs_space_id_spaces_id_fk" FOREIGN KEY ("space_id") REFERENCES "public"."spaces"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
45
+ ALTER TABLE "workflows" ADD CONSTRAINT "workflows_space_id_spaces_id_fk" FOREIGN KEY ("space_id") REFERENCES "public"."spaces"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
46
+ CREATE INDEX "push_subscriptions_user_idx" ON "push_subscriptions" USING btree ("user_id");--> statement-breakpoint
47
+ CREATE INDEX "workflow_runs_workflow_idx" ON "workflow_runs" USING btree ("workflow_id","created_at");--> statement-breakpoint
48
+ CREATE INDEX "workflow_runs_resume_idx" ON "workflow_runs" USING btree ("status","resume_at");--> statement-breakpoint
49
+ CREATE INDEX "workflows_space_idx" ON "workflows" USING btree ("space_id","enabled");