diff --git a/store/drizzle/0001_damp_captain_midlands.sql b/store/drizzle/0001_damp_captain_midlands.sql new file mode 100644 index 0000000..58f595f --- /dev/null +++ b/store/drizzle/0001_damp_captain_midlands.sql @@ -0,0 +1,14 @@ +ALTER TABLE "nodes" ADD PRIMARY KEY ("id");--> statement-breakpoint +ALTER TABLE "users" ALTER COLUMN "id" SET DATA TYPE uuid;--> statement-breakpoint +ALTER TABLE "users" ALTER COLUMN "id" SET DEFAULT gen_random_uuid();--> statement-breakpoint +ALTER TABLE "users" ALTER COLUMN "name" SET NOT NULL;--> statement-breakpoint +ALTER TABLE "nodes" ADD COLUMN "userId" varchar NOT NULL;--> statement-breakpoint +ALTER TABLE "nodes" ADD COLUMN "systemId" varchar NOT NULL;--> statement-breakpoint +ALTER TABLE "nodes" ADD COLUMN "nodeId" varchar NOT NULL;--> statement-breakpoint +ALTER TABLE "nodes" ADD COLUMN "hash" varchar(8) NOT NULL;--> statement-breakpoint +ALTER TABLE "nodes" ADD COLUMN "previous" integer;--> statement-breakpoint +CREATE INDEX "user_id_idx" ON "nodes" USING btree ("userId");--> statement-breakpoint +CREATE INDEX "system_id_idx" ON "nodes" USING btree ("systemId");--> statement-breakpoint +CREATE INDEX "node_id_idx" ON "nodes" USING btree ("nodeId");--> statement-breakpoint +CREATE INDEX "hash_idx" ON "nodes" USING btree ("hash");--> statement-breakpoint +ALTER TABLE "users" ADD CONSTRAINT "users_name_unique" UNIQUE("name"); \ No newline at end of file diff --git a/store/drizzle/0002_chemical_whiplash.sql b/store/drizzle/0002_chemical_whiplash.sql new file mode 100644 index 0000000..f1b6f04 --- /dev/null +++ b/store/drizzle/0002_chemical_whiplash.sql @@ -0,0 +1,3 @@ +ALTER TABLE "nodes" ALTER COLUMN "previous" SET DATA TYPE varchar(8);--> statement-breakpoint +ALTER TABLE "nodes" ADD CONSTRAINT "nodes_userId_users_id_fk" FOREIGN KEY ("userId") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint +ALTER TABLE "nodes" ADD CONSTRAINT "node_previous_fk" FOREIGN KEY ("previous") REFERENCES "public"."nodes"("hash") ON DELETE no action ON UPDATE no action; \ No newline at end of file diff --git a/store/drizzle/meta/0001_snapshot.json b/store/drizzle/meta/0001_snapshot.json new file mode 100644 index 0000000..b403bf0 --- /dev/null +++ b/store/drizzle/meta/0001_snapshot.json @@ -0,0 +1,175 @@ +{ + "id": "3c2af8b1-1824-4003-a4f0-d3bfc27c235d", + "prevId": "53dea8d7-01be-4983-ac75-9de9c9a7f592", + "version": "7", + "dialect": "postgresql", + "tables": { + "public.users": { + "name": "users", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "users_name_unique": { + "name": "users_name_unique", + "nullsNotDistinct": false, + "columns": [ + "name" + ] + } + }, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.nodes": { + "name": "nodes", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "userId": { + "name": "userId", + "type": "varchar", + "primaryKey": false, + "notNull": true + }, + "systemId": { + "name": "systemId", + "type": "varchar", + "primaryKey": false, + "notNull": true + }, + "nodeId": { + "name": "nodeId", + "type": "varchar", + "primaryKey": false, + "notNull": true + }, + "content": { + "name": "content", + "type": "bytea", + "primaryKey": false, + "notNull": true + }, + "definition": { + "name": "definition", + "type": "json", + "primaryKey": false, + "notNull": true + }, + "hash": { + "name": "hash", + "type": "varchar(8)", + "primaryKey": false, + "notNull": true + }, + "previous": { + "name": "previous", + "type": "integer", + "primaryKey": false, + "notNull": false + } + }, + "indexes": { + "user_id_idx": { + "name": "user_id_idx", + "columns": [ + { + "expression": "userId", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "system_id_idx": { + "name": "system_id_idx", + "columns": [ + { + "expression": "systemId", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "node_id_idx": { + "name": "node_id_idx", + "columns": [ + { + "expression": "nodeId", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "hash_idx": { + "name": "hash_idx", + "columns": [ + { + "expression": "hash", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + } + }, + "enums": {}, + "schemas": {}, + "sequences": {}, + "roles": {}, + "policies": {}, + "views": {}, + "_meta": { + "columns": {}, + "schemas": {}, + "tables": {} + } +} \ No newline at end of file diff --git a/store/drizzle/meta/0002_snapshot.json b/store/drizzle/meta/0002_snapshot.json new file mode 100644 index 0000000..76fe849 --- /dev/null +++ b/store/drizzle/meta/0002_snapshot.json @@ -0,0 +1,202 @@ +{ + "id": "d950b459-4e3e-4a24-82d8-918eee2d8379", + "prevId": "3c2af8b1-1824-4003-a4f0-d3bfc27c235d", + "version": "7", + "dialect": "postgresql", + "tables": { + "public.users": { + "name": "users", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "users_name_unique": { + "name": "users_name_unique", + "nullsNotDistinct": false, + "columns": [ + "name" + ] + } + }, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.nodes": { + "name": "nodes", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "userId": { + "name": "userId", + "type": "varchar", + "primaryKey": false, + "notNull": true + }, + "systemId": { + "name": "systemId", + "type": "varchar", + "primaryKey": false, + "notNull": true + }, + "nodeId": { + "name": "nodeId", + "type": "varchar", + "primaryKey": false, + "notNull": true + }, + "content": { + "name": "content", + "type": "bytea", + "primaryKey": false, + "notNull": true + }, + "definition": { + "name": "definition", + "type": "json", + "primaryKey": false, + "notNull": true + }, + "hash": { + "name": "hash", + "type": "varchar(8)", + "primaryKey": false, + "notNull": true + }, + "previous": { + "name": "previous", + "type": "varchar(8)", + "primaryKey": false, + "notNull": false + } + }, + "indexes": { + "user_id_idx": { + "name": "user_id_idx", + "columns": [ + { + "expression": "userId", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "system_id_idx": { + "name": "system_id_idx", + "columns": [ + { + "expression": "systemId", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "node_id_idx": { + "name": "node_id_idx", + "columns": [ + { + "expression": "nodeId", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "hash_idx": { + "name": "hash_idx", + "columns": [ + { + "expression": "hash", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "nodes_userId_users_id_fk": { + "name": "nodes_userId_users_id_fk", + "tableFrom": "nodes", + "tableTo": "users", + "columnsFrom": [ + "userId" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "node_previous_fk": { + "name": "node_previous_fk", + "tableFrom": "nodes", + "tableTo": "nodes", + "columnsFrom": [ + "previous" + ], + "columnsTo": [ + "hash" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + } + }, + "enums": {}, + "schemas": {}, + "sequences": {}, + "roles": {}, + "policies": {}, + "views": {}, + "_meta": { + "columns": {}, + "schemas": {}, + "tables": {} + } +} \ No newline at end of file diff --git a/store/drizzle/meta/_journal.json b/store/drizzle/meta/_journal.json index cae9842..0cd75e0 100644 --- a/store/drizzle/meta/_journal.json +++ b/store/drizzle/meta/_journal.json @@ -8,6 +8,20 @@ "when": 1734446124519, "tag": "0000_dark_squirrel_girl", "breakpoints": true + }, + { + "idx": 1, + "version": "7", + "when": 1734693909872, + "tag": "0001_damp_captain_midlands", + "breakpoints": true + }, + { + "idx": 2, + "version": "7", + "when": 1734694545302, + "tag": "0002_chemical_whiplash", + "breakpoints": true } ] } \ No newline at end of file diff --git a/store/src/routes/node/node.schema.ts b/store/src/routes/node/node.schema.ts index 2e648c5..8579921 100644 --- a/store/src/routes/node/node.schema.ts +++ b/store/src/routes/node/node.schema.ts @@ -1,5 +1,6 @@ import { customType, + foreignKey, index, integer, json, @@ -21,27 +22,21 @@ const bytea = customType<{ export const nodeTable = pgTable("nodes", { id: serial().primaryKey(), - userId: varchar().notNull(), + userId: varchar().notNull().references(() => usersTable.id), systemId: varchar().notNull(), nodeId: varchar().notNull(), content: bytea().notNull(), definition: json().notNull(), hash: varchar({ length: 8 }).notNull(), - previous: integer(), + previous: varchar({ length: 8 }), }, (table) => [ + foreignKey({ + columns: [table.previous], + foreignColumns: [table.hash], + name: "node_previous_fk", + }), index("user_id_idx").on(table.userId), index("system_id_idx").on(table.systemId), index("node_id_idx").on(table.nodeId), index("hash_idx").on(table.hash), ]); - -export const nodeRelations = relations(nodeTable, ({ one }) => ({ - userId: one(usersTable, { - fields: [nodeTable.userId], - references: [usersTable.id], - }), - previous: one(nodeTable, { - fields: [nodeTable.previous], - references: [nodeTable.id], - }), -}));