diff --git a/apps/web/src/actions/documents/create.ts b/apps/web/src/actions/documents/create.ts index 892c8a68f..c01598feb 100644 --- a/apps/web/src/actions/documents/create.ts +++ b/apps/web/src/actions/documents/create.ts @@ -3,22 +3,20 @@ import { createDocumentVersion, DocumentType } from '@latitude-data/core' import { z } from 'zod' -import { authProcedure } from '../procedures' +import { withProject } from '../procedures' -export const createDocumentVersionAction = authProcedure +export const createDocumentVersionAction = withProject .createServerAction() .input( z.object({ - commitUuid: z.string(), - documentType: z - .enum([ - 'folder' as DocumentType.Folder, - 'document' as DocumentType.Document, - ]) - .optional(), name: z.string(), + commitUuid: z.string(), parentId: z.number().optional(), + documentType: z.nativeEnum(DocumentType).optional(), }), { type: 'json' }, ) - .handler(async ({ input }) => createDocumentVersion(input)) + .handler(async ({ input }) => { + const result = await createDocumentVersion(input) + return result.unwrap() + }) diff --git a/apps/web/src/actions/procedures/index.ts b/apps/web/src/actions/procedures/index.ts index bc65c03f3..e5add055f 100644 --- a/apps/web/src/actions/procedures/index.ts +++ b/apps/web/src/actions/procedures/index.ts @@ -1,4 +1,6 @@ import { getCurrentUser } from '$/services/auth/getCurrentUser' +import { findProject } from '@latitude-data/core' +import { z } from 'zod' import { createServerActionProcedure } from 'zsa' /** @@ -10,3 +12,15 @@ export const authProcedure = createServerActionProcedure().handler(async () => { const data = (await getCurrentUser()).unwrap() return { session: data.session!, workspace: data.workspace, user: data.user } }) + +export const withProject = createServerActionProcedure(authProcedure) + .input(z.object({ projectId: z.number() })) + .handler(async ({ input, ctx }) => { + const project = ( + await findProject({ + projectId: input.projectId, + workspaceId: ctx.workspace.id, + }) + ).unwrap() + return { ...ctx, project } + }) diff --git a/apps/web/src/app/api/commits/[commitUuid]/documents/route.ts b/apps/web/src/app/api/commits/[commitUuid]/documents/route.ts index 3a1112fa2..41a125cf1 100644 --- a/apps/web/src/app/api/commits/[commitUuid]/documents/route.ts +++ b/apps/web/src/app/api/commits/[commitUuid]/documents/route.ts @@ -7,9 +7,9 @@ export async function GET( ) { try { const staged = Boolean(req.nextUrl.searchParams.get('staged') || false) - const nodes = await materializeDocumentsAtCommit({ commitUuid, staged }) + const documents = await materializeDocumentsAtCommit({ commitUuid, staged }) - return NextResponse.json(nodes) + return NextResponse.json(documents) } catch (err: unknown) { const error = err as Error return NextResponse.json({ error: error.message }, { status: 500 }) diff --git a/apps/web/src/app/page.tsx b/apps/web/src/app/page.tsx deleted file mode 100644 index b9f103bff..000000000 --- a/apps/web/src/app/page.tsx +++ /dev/null @@ -1,7 +0,0 @@ -export default async function Home() { - return ( -
-

Main body

-
- ) -} diff --git a/apps/web/src/components/Sidebar/DocumentTree/index.tsx b/apps/web/src/components/Sidebar/DocumentTree/index.tsx index 6b0d526ab..3f2f1d2cf 100644 --- a/apps/web/src/components/Sidebar/DocumentTree/index.tsx +++ b/apps/web/src/components/Sidebar/DocumentTree/index.tsx @@ -4,7 +4,7 @@ import { faker } from '@faker-js/faker' import type { DocumentType, DocumentVersion } from '@latitude-data/core' import useDocumentVersions from '$/stores/documentVersions' -import toTree, { Node } from '../toTree' +import { Node, useTree } from '../toTree' function generateName() { return faker.science.chemicalElement().name @@ -61,20 +61,24 @@ function TreeNode({ node, level = 0 }: { node: Node; level?: number }) { )} )} - {node.children.map((node) => ( - + {node.children.map((node, idx) => ( + ))} ) } -export default function DocumentTree({ nodes }: { nodes: DocumentVersion[] }) { - const { data } = useDocumentVersions( +export default function DocumentTree({ + documents: serverDocuments, +}: { + documents: DocumentVersion[] +}) { + const { documents } = useDocumentVersions( { staged: true }, - { fallbackData: nodes }, + { fallbackData: serverDocuments }, ) - const rootNode = toTree(data) + const rootNode = useTree({ documents }) return } diff --git a/apps/web/src/components/Sidebar/index.tsx b/apps/web/src/components/Sidebar/index.tsx index b125434fd..c03362159 100644 --- a/apps/web/src/components/Sidebar/index.tsx +++ b/apps/web/src/components/Sidebar/index.tsx @@ -3,7 +3,7 @@ import { HEAD_COMMIT, materializeDocumentsAtCommit } from '@latitude-data/core' import DocumentTree, { CreateNode } from './DocumentTree' export default async function Sidebar() { - const nodes = await materializeDocumentsAtCommit({ + const documents = await materializeDocumentsAtCommit({ commitUuid: HEAD_COMMIT, staged: true, }) @@ -16,7 +16,7 @@ export default async function Sidebar() { - + ) } diff --git a/apps/web/src/components/Sidebar/toTree.ts b/apps/web/src/components/Sidebar/toTree.ts index 5e3965a02..42134a861 100644 --- a/apps/web/src/components/Sidebar/toTree.ts +++ b/apps/web/src/components/Sidebar/toTree.ts @@ -1,3 +1,5 @@ +import { useMemo } from 'react' + import { DocumentVersion } from '@latitude-data/core' export class Node { @@ -16,24 +18,26 @@ export class Node { } } -export default function toTree(docs: DocumentVersion[]) { - function iterate(node: Node) { - let children - if (node.isRoot) { - children = Object.values(docs) - .filter((doc) => !doc.parentId) - .map((doc) => new Node(doc)) - } else { - children = docs - .filter((doc) => doc.parentId === node.doc!.id) - .map((doc) => new Node(doc)) - } +export function useTree({ documents }: { documents: DocumentVersion[] }) { + return useMemo(() => { + function iterate(node: Node) { + let children + if (node.isRoot) { + children = Object.values(documents) + .filter((doc) => !doc.parentId) + .map((doc) => new Node(doc)) + } else { + children = documents + .filter((doc) => doc.parentId === node.doc!.id) + .map((doc) => new Node(doc)) + } - node.children = children - node.children.forEach(iterate) + node.children = children + node.children.forEach(iterate) - return node - } + return node + } - return iterate(new Node(undefined, [], true)) + return iterate(new Node(undefined, [], true)) + }, [documents]) } diff --git a/apps/web/src/stores/documentVersions.ts b/apps/web/src/stores/documentVersions.ts index 0a0cb03ed..6afd8e2a7 100644 --- a/apps/web/src/stores/documentVersions.ts +++ b/apps/web/src/stores/documentVersions.ts @@ -1,9 +1,14 @@ 'use client' -import { DocumentVersion, HEAD_COMMIT } from '@latitude-data/core' +import { useCallback } from 'react' + +import { DocumentVersion } from '@latitude-data/core' +import { HEAD_COMMIT, type DocumentType } from '@latitude-data/core/browser' import { createDocumentVersionAction } from '$/actions/documents/create' import useSWR, { SWRConfiguration } from 'swr' +import { useServerAction } from 'zsa-react' +const FIXME_HARDCODED_PROJECT_ID = 1 export default function useDocumentVersions( { commitUuid = HEAD_COMMIT, @@ -19,30 +24,37 @@ export default function useDocumentVersions( new URLSearchParams({ staged: String(staged), }).toString() - const { mutate, data, ...rest } = useSWR( + + const { mutate, data, ...rest } = useSWR( key, (url: string) => fetch(url).then((res) => res.json()), opts, ) - const create = async (payload: { - commitUuid?: string - name: string - documentType?: DocumentVersion['documentType'] - parentId?: number - }) => { - try { - const doc = await createDocumentVersionAction({ + const documents = data ?? [] + const { execute } = useServerAction(createDocumentVersionAction) + const create = useCallback( + async (payload: { + commitUuid?: string + name: string + documentType?: DocumentType + parentId?: number + }) => { + const [document] = await execute({ ...payload, + projectId: FIXME_HARDCODED_PROJECT_ID, name: payload.name!, commitUuid: payload.commitUuid || HEAD_COMMIT, }) - mutate([...data, doc]) + const prev = documents ?? [] - return doc - } catch (err) { - console.error(err) - } - } + if (document) { + mutate([...prev, document]) + } + + return document + }, + [execute, mutate, documents], + ) - return { ...rest, key, data, create, mutate } + return { ...rest, key, documents, create, mutate } } diff --git a/apps/web/tsconfig.json b/apps/web/tsconfig.json index 19119173b..0265ac74f 100644 --- a/apps/web/tsconfig.json +++ b/apps/web/tsconfig.json @@ -12,6 +12,9 @@ "baseUrl": ".", "paths": { "$/*": ["./src/*"], + "@latitude-data/core": ["../../packages/core/src/*"], + "@latitude-data/jobs": ["../../packages/jobs/src/*"], + "@latitude-data/web-ui": ["../../packages/web-ui/src/*"], "$core/*": ["../../packages/core/src/*"], "$jobs/*": ["../../packages/jobs/src/*"], "$ui/*": ["../../packages/web-ui/src/*"], diff --git a/packages/core/drizzle/0000_nice_daimon_hellstrom.sql b/packages/core/drizzle/0000_nice_daimon_hellstrom.sql new file mode 100644 index 000000000..a8fe5a34e --- /dev/null +++ b/packages/core/drizzle/0000_nice_daimon_hellstrom.sql @@ -0,0 +1,10 @@ +CREATE SCHEMA "latitude"; +--> statement-breakpoint +CREATE TABLE IF NOT EXISTS "latitude"."document_hierarchies" ( + "id" bigserial PRIMARY KEY NOT NULL, + "parent_id" bigint, + "depth" integer NOT NULL, + "child_id" bigint NOT NULL, + "created_at" timestamp DEFAULT now() NOT NULL, + "updated_at" timestamp DEFAULT now() NOT NULL +); diff --git a/packages/core/drizzle/0001_ambitious_mesmero.sql b/packages/core/drizzle/0001_ambitious_mesmero.sql deleted file mode 100644 index bb7d5a322..000000000 --- a/packages/core/drizzle/0001_ambitious_mesmero.sql +++ /dev/null @@ -1,82 +0,0 @@ --- Trigger to update closure tree on new node created -CREATE OR REPLACE FUNCTION document_versions_insert_trigger() -RETURNS TRIGGER AS -$BODY$ -BEGIN - INSERT INTO "latitude"."document_hierarchies" (parent_id, child_id, depth) - SELECT p.parent_id, NEW.id, p.depth + 1 - FROM "latitude"."document_hierarchies" AS p - WHERE p.child_id = NEW.parent_id; - - INSERT INTO "latitude"."document_hierarchies" (parent_id, child_id, depth) - SELECT NEW.id, NEW.id, 0; - - RETURN NEW; -END; -$BODY$ -LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER after_document_versions_insert -AFTER INSERT ON "latitude"."document_versions" -FOR EACH ROW - EXECUTE FUNCTION document_versions_insert_trigger(); - - --- Trigger to update closure tree on deleting a node -CREATE OR REPLACE FUNCTION document_versions_delete_trigger() -RETURNS TRIGGER AS -$BODY$ -BEGIN - DELETE FROM "latitude"."document_hierarchies" - WHERE child_id IN (SELECT child_id FROM "latitude"."document_hierarchies" WHERE parent_id = OLD.id) - AND parent_id NOT IN (SELECT child_id FROM "latitude"."document_hierarchies" WHERE parent_id = OLD.id); - - DELETE FROM "latitude"."document_hierarchies" - WHERE child_id = OLD.id AND parent_id = OLD.id; - - RETURN OLD; -END; -$BODY$ -LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER after_document_versions_delete -AFTER DELETE ON "latitude"."document_versions" -FOR EACH ROW - EXECUTE FUNCTION document_versions_delete_trigger(); - - --- Trigger to update closure tree on reparenting -CREATE OR REPLACE FUNCTION document_versions_update_trigger() -RETURNS TRIGGER AS -$BODY$ -BEGIN - IF NEW.parent_id IS NOT DISTINCT FROM OLD.parent_id THEN - RETURN NEW; - END IF; - - DELETE FROM "latitude"."document_hierarchies" - WHERE child_id IN (SELECT child_id FROM "latitude"."document_hierarchies" WHERE parent_id = NEW.id) - AND parent_id NOT IN (SELECT child_id FROM "latitude"."document_hierarchies" WHERE parent_id = NEW.id); - - -- rebuild relations for all document children - INSERT INTO "latitude"."document_hierarchies" (parent_id, child_id, depth) - SELECT p.parent_id, c.child_id, p.depth + c.depth + 1 - FROM "latitude"."document_hierarchies" AS p - CROSS JOIN "latitude"."document_hierarchies" AS c - WHERE c.parent_id = NEW.id - AND p.child_id = NEW.parent_id; - - RETURN NEW; -END; -$BODY$ -LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER after_document_versions_update -AFTER UPDATE ON "latitude"."document_versions" -FOR EACH ROW - EXECUTE FUNCTION document_versions_update_trigger(); - - - - - diff --git a/packages/core/drizzle/0001_living_madripoor.sql b/packages/core/drizzle/0001_living_madripoor.sql new file mode 100644 index 000000000..3409b43f3 --- /dev/null +++ b/packages/core/drizzle/0001_living_madripoor.sql @@ -0,0 +1,76 @@ +CREATE TABLE IF NOT EXISTS "latitude"."users" ( + "id" text PRIMARY KEY NOT NULL, + "name" text, + "email" text NOT NULL, + "encrypted_password" text NOT NULL, + "created_at" timestamp DEFAULT now() NOT NULL, + "updated_at" timestamp DEFAULT now() NOT NULL, + CONSTRAINT "users_email_unique" UNIQUE("email") +); +--> statement-breakpoint +CREATE TABLE IF NOT EXISTS "latitude"."sessions" ( + "id" text PRIMARY KEY NOT NULL, + "user_id" text NOT NULL, + "expires_at" timestamp with time zone NOT NULL, + "created_at" timestamp DEFAULT now() NOT NULL, + "updated_at" timestamp DEFAULT now() NOT NULL +); +--> statement-breakpoint +CREATE TABLE IF NOT EXISTS "latitude"."workspaces" ( + "id" bigserial PRIMARY KEY NOT NULL, + "name" varchar(256) NOT NULL, + "creator_id" text, + "created_at" timestamp DEFAULT now() NOT NULL, + "updated_at" timestamp DEFAULT now() NOT NULL +); +--> statement-breakpoint +CREATE TABLE IF NOT EXISTS "latitude"."memberships" ( + "workspace_id" bigint NOT NULL, + "user_id" text NOT NULL, + "created_at" timestamp DEFAULT now() NOT NULL, + "updated_at" timestamp DEFAULT now() NOT NULL, + CONSTRAINT "memberships_workspace_id_user_id_pk" PRIMARY KEY("workspace_id","user_id") +); +--> statement-breakpoint +CREATE TABLE IF NOT EXISTS "latitude"."api_keys" ( + "id" bigserial PRIMARY KEY NOT NULL, + "uuid" uuid NOT NULL, + "workspace_id" bigint NOT NULL, + "name" varchar(256), + "created_at" timestamp DEFAULT now() NOT NULL, + "updated_at" timestamp DEFAULT now() NOT NULL, + "deleted_at" timestamp, + CONSTRAINT "api_keys_uuid_unique" UNIQUE("uuid") +); +--> statement-breakpoint +DO $$ BEGIN + ALTER TABLE "latitude"."sessions" ADD CONSTRAINT "sessions_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "latitude"."users"("id") ON DELETE cascade ON UPDATE no action; +EXCEPTION + WHEN duplicate_object THEN null; +END $$; +--> statement-breakpoint +DO $$ BEGIN + ALTER TABLE "latitude"."workspaces" ADD CONSTRAINT "workspaces_creator_id_users_id_fk" FOREIGN KEY ("creator_id") REFERENCES "latitude"."users"("id") ON DELETE set null ON UPDATE no action; +EXCEPTION + WHEN duplicate_object THEN null; +END $$; +--> statement-breakpoint +DO $$ BEGIN + ALTER TABLE "latitude"."memberships" ADD CONSTRAINT "memberships_workspace_id_workspaces_id_fk" FOREIGN KEY ("workspace_id") REFERENCES "latitude"."workspaces"("id") ON DELETE cascade ON UPDATE no action; +EXCEPTION + WHEN duplicate_object THEN null; +END $$; +--> statement-breakpoint +DO $$ BEGIN + ALTER TABLE "latitude"."memberships" ADD CONSTRAINT "memberships_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "latitude"."users"("id") ON DELETE cascade ON UPDATE no action; +EXCEPTION + WHEN duplicate_object THEN null; +END $$; +--> statement-breakpoint +DO $$ BEGIN + ALTER TABLE "latitude"."api_keys" ADD CONSTRAINT "api_keys_workspace_id_workspaces_id_fk" FOREIGN KEY ("workspace_id") REFERENCES "latitude"."workspaces"("id") ON DELETE no action ON UPDATE no action; +EXCEPTION + WHEN duplicate_object THEN null; +END $$; +--> statement-breakpoint +CREATE INDEX IF NOT EXISTS "workspace_id_idx" ON "latitude"."api_keys" USING btree ("workspace_id"); \ No newline at end of file diff --git a/packages/core/drizzle/0000_daffy_madrox.sql b/packages/core/drizzle/0002_windy_wind_dancer.sql similarity index 51% rename from packages/core/drizzle/0000_daffy_madrox.sql rename to packages/core/drizzle/0002_windy_wind_dancer.sql index 6c9f71997..91dae5bd0 100644 --- a/packages/core/drizzle/0000_daffy_madrox.sql +++ b/packages/core/drizzle/0002_windy_wind_dancer.sql @@ -1,20 +1,15 @@ -CREATE SCHEMA "latitude"; ---> statement-breakpoint DO $$ BEGIN - CREATE TYPE "public"."document_type" AS ENUM('document', 'folder'); + CREATE TYPE "latitude"."document_type" AS ENUM('document', 'folder'); EXCEPTION WHEN duplicate_object THEN null; END $$; --> statement-breakpoint -CREATE TABLE IF NOT EXISTS "latitude"."api_keys" ( +CREATE TABLE IF NOT EXISTS "latitude"."projects" ( "id" bigserial PRIMARY KEY NOT NULL, - "uuid" uuid NOT NULL, + "name" varchar(256) NOT NULL, "workspace_id" bigint NOT NULL, - "name" varchar(256), "created_at" timestamp DEFAULT now() NOT NULL, - "updated_at" timestamp DEFAULT now() NOT NULL, - "deleted_at" timestamp, - CONSTRAINT "api_keys_uuid_unique" UNIQUE("uuid") + "updated_at" timestamp DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE IF NOT EXISTS "latitude"."commits" ( @@ -23,21 +18,12 @@ CREATE TABLE IF NOT EXISTS "latitude"."commits" ( "next_commit_id" bigint, "title" varchar(256), "description" text, - "workspace_id" bigint NOT NULL, + "project_id" bigint NOT NULL, "created_at" timestamp DEFAULT now() NOT NULL, "updated_at" timestamp DEFAULT now() NOT NULL, CONSTRAINT "commits_uuid_unique" UNIQUE("uuid") ); --> statement-breakpoint -CREATE TABLE IF NOT EXISTS "latitude"."document_hierarchies" ( - "id" bigserial PRIMARY KEY NOT NULL, - "parent_id" bigint, - "depth" integer NOT NULL, - "child_id" bigint NOT NULL, - "created_at" timestamp DEFAULT now() NOT NULL, - "updated_at" timestamp DEFAULT now() NOT NULL -); ---> statement-breakpoint CREATE TABLE IF NOT EXISTS "latitude"."document_snapshots" ( "id" bigserial PRIMARY KEY NOT NULL, "commit_id" bigint NOT NULL, @@ -49,7 +35,7 @@ CREATE TABLE IF NOT EXISTS "latitude"."document_snapshots" ( CREATE TABLE IF NOT EXISTS "latitude"."document_versions" ( "id" bigserial PRIMARY KEY NOT NULL, "name" varchar NOT NULL, - "document_type" "document_type" DEFAULT 'document' NOT NULL, + "document_type" "latitude"."document_type" DEFAULT 'document' NOT NULL, "content" text, "hash" varchar, "deleted_at" timestamp, @@ -60,42 +46,8 @@ CREATE TABLE IF NOT EXISTS "latitude"."document_versions" ( "updated_at" timestamp DEFAULT now() NOT NULL ); --> statement-breakpoint -CREATE TABLE IF NOT EXISTS "latitude"."memberships" ( - "workspace_id" bigint NOT NULL, - "user_id" text NOT NULL, - "created_at" timestamp DEFAULT now() NOT NULL, - "updated_at" timestamp DEFAULT now() NOT NULL, - CONSTRAINT "memberships_workspace_id_user_id_pk" PRIMARY KEY("workspace_id","user_id") -); ---> statement-breakpoint -CREATE TABLE IF NOT EXISTS "latitude"."sessions" ( - "id" text PRIMARY KEY NOT NULL, - "user_id" text NOT NULL, - "expires_at" timestamp with time zone NOT NULL, - "created_at" timestamp DEFAULT now() NOT NULL, - "updated_at" timestamp DEFAULT now() NOT NULL -); ---> statement-breakpoint -CREATE TABLE IF NOT EXISTS "latitude"."users" ( - "id" text PRIMARY KEY NOT NULL, - "name" text, - "email" text NOT NULL, - "encrypted_password" text NOT NULL, - "created_at" timestamp DEFAULT now() NOT NULL, - "updated_at" timestamp DEFAULT now() NOT NULL, - CONSTRAINT "users_email_unique" UNIQUE("email") -); ---> statement-breakpoint -CREATE TABLE IF NOT EXISTS "latitude"."workspaces" ( - "id" bigserial PRIMARY KEY NOT NULL, - "name" varchar(256) NOT NULL, - "creator_id" text, - "created_at" timestamp DEFAULT now() NOT NULL, - "updated_at" timestamp DEFAULT now() NOT NULL -); ---> statement-breakpoint DO $$ BEGIN - ALTER TABLE "latitude"."api_keys" ADD CONSTRAINT "api_keys_workspace_id_workspaces_id_fk" FOREIGN KEY ("workspace_id") REFERENCES "latitude"."workspaces"("id") ON DELETE no action ON UPDATE no action; + ALTER TABLE "latitude"."projects" ADD CONSTRAINT "projects_workspace_id_workspaces_id_fk" FOREIGN KEY ("workspace_id") REFERENCES "latitude"."workspaces"("id") ON DELETE cascade ON UPDATE no action; EXCEPTION WHEN duplicate_object THEN null; END $$; @@ -107,7 +59,7 @@ EXCEPTION END $$; --> statement-breakpoint DO $$ BEGIN - ALTER TABLE "latitude"."commits" ADD CONSTRAINT "commits_workspace_id_workspaces_id_fk" FOREIGN KEY ("workspace_id") REFERENCES "latitude"."workspaces"("id") ON DELETE cascade ON UPDATE no action; + ALTER TABLE "latitude"."commits" ADD CONSTRAINT "commits_project_id_workspaces_id_fk" FOREIGN KEY ("project_id") REFERENCES "latitude"."workspaces"("id") ON DELETE cascade ON UPDATE no action; EXCEPTION WHEN duplicate_object THEN null; END $$; @@ -136,31 +88,7 @@ EXCEPTION WHEN duplicate_object THEN null; END $$; --> statement-breakpoint -DO $$ BEGIN - ALTER TABLE "latitude"."memberships" ADD CONSTRAINT "memberships_workspace_id_workspaces_id_fk" FOREIGN KEY ("workspace_id") REFERENCES "latitude"."workspaces"("id") ON DELETE cascade ON UPDATE no action; -EXCEPTION - WHEN duplicate_object THEN null; -END $$; ---> statement-breakpoint -DO $$ BEGIN - ALTER TABLE "latitude"."memberships" ADD CONSTRAINT "memberships_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "latitude"."users"("id") ON DELETE cascade ON UPDATE no action; -EXCEPTION - WHEN duplicate_object THEN null; -END $$; ---> statement-breakpoint -DO $$ BEGIN - ALTER TABLE "latitude"."sessions" ADD CONSTRAINT "sessions_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "latitude"."users"("id") ON DELETE cascade ON UPDATE no action; -EXCEPTION - WHEN duplicate_object THEN null; -END $$; ---> statement-breakpoint -DO $$ BEGIN - ALTER TABLE "latitude"."workspaces" ADD CONSTRAINT "workspaces_creator_id_users_id_fk" FOREIGN KEY ("creator_id") REFERENCES "latitude"."users"("id") ON DELETE set null ON UPDATE no action; -EXCEPTION - WHEN duplicate_object THEN null; -END $$; ---> statement-breakpoint -CREATE INDEX IF NOT EXISTS "workspace_id_idx" ON "latitude"."api_keys" USING btree ("workspace_id");--> statement-breakpoint +CREATE INDEX IF NOT EXISTS "workspace_idx" ON "latitude"."projects" USING btree ("workspace_id");--> statement-breakpoint CREATE INDEX IF NOT EXISTS "commit_next_commit_idx" ON "latitude"."commits" USING btree ("next_commit_id");--> statement-breakpoint CREATE INDEX IF NOT EXISTS "prompt_commit_idx" ON "latitude"."document_snapshots" USING btree ("commit_id");--> statement-breakpoint CREATE INDEX IF NOT EXISTS "document_snapshot_document_version_idx" ON "latitude"."document_snapshots" USING btree ("document_version_id"); \ No newline at end of file diff --git a/packages/core/drizzle/0001_old_rattler.sql b/packages/core/drizzle/0003_cold_spirit.sql similarity index 99% rename from packages/core/drizzle/0001_old_rattler.sql rename to packages/core/drizzle/0003_cold_spirit.sql index 64c0cd143..3172dba49 100644 --- a/packages/core/drizzle/0001_old_rattler.sql +++ b/packages/core/drizzle/0003_cold_spirit.sql @@ -75,7 +75,3 @@ CREATE OR REPLACE TRIGGER after_document_versions_update AFTER UPDATE ON "latitude"."document_versions" FOR EACH ROW EXECUTE FUNCTION document_versions_update_trigger(); - - - - diff --git a/packages/core/drizzle/meta/0000_snapshot.json b/packages/core/drizzle/meta/0000_snapshot.json index bb146b8d3..77f94abda 100644 --- a/packages/core/drizzle/meta/0000_snapshot.json +++ b/packages/core/drizzle/meta/0000_snapshot.json @@ -1,216 +1,9 @@ { - "id": "2fd909d0-9cd7-4e6a-81de-0203fb8041a6", + "id": "60f41b01-d3ec-4de1-82b1-3e7ec942c75a", "prevId": "00000000-0000-0000-0000-000000000000", "version": "7", "dialect": "postgresql", "tables": { - "latitude.api_keys": { - "name": "api_keys", - "schema": "latitude", - "columns": { - "id": { - "name": "id", - "type": "bigserial", - "primaryKey": true, - "notNull": true - }, - "uuid": { - "name": "uuid", - "type": "uuid", - "primaryKey": false, - "notNull": true - }, - "workspace_id": { - "name": "workspace_id", - "type": "bigint", - "primaryKey": false, - "notNull": true - }, - "name": { - "name": "name", - "type": "varchar(256)", - "primaryKey": false, - "notNull": false - }, - "created_at": { - "name": "created_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - }, - "updated_at": { - "name": "updated_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - }, - "deleted_at": { - "name": "deleted_at", - "type": "timestamp", - "primaryKey": false, - "notNull": false - } - }, - "indexes": { - "workspace_id_idx": { - "name": "workspace_id_idx", - "columns": [ - { - "expression": "workspace_id", - "isExpression": false, - "asc": true, - "nulls": "last" - } - ], - "isUnique": false, - "concurrently": false, - "method": "btree", - "with": {} - } - }, - "foreignKeys": { - "api_keys_workspace_id_workspaces_id_fk": { - "name": "api_keys_workspace_id_workspaces_id_fk", - "tableFrom": "api_keys", - "tableTo": "workspaces", - "schemaTo": "latitude", - "columnsFrom": [ - "workspace_id" - ], - "columnsTo": [ - "id" - ], - "onDelete": "no action", - "onUpdate": "no action" - } - }, - "compositePrimaryKeys": {}, - "uniqueConstraints": { - "api_keys_uuid_unique": { - "name": "api_keys_uuid_unique", - "nullsNotDistinct": false, - "columns": [ - "uuid" - ] - } - } - }, - "latitude.commits": { - "name": "commits", - "schema": "latitude", - "columns": { - "id": { - "name": "id", - "type": "bigserial", - "primaryKey": true, - "notNull": true - }, - "uuid": { - "name": "uuid", - "type": "uuid", - "primaryKey": false, - "notNull": true, - "default": "gen_random_uuid()" - }, - "next_commit_id": { - "name": "next_commit_id", - "type": "bigint", - "primaryKey": false, - "notNull": false - }, - "title": { - "name": "title", - "type": "varchar(256)", - "primaryKey": false, - "notNull": false - }, - "description": { - "name": "description", - "type": "text", - "primaryKey": false, - "notNull": false - }, - "workspace_id": { - "name": "workspace_id", - "type": "bigint", - "primaryKey": false, - "notNull": true - }, - "created_at": { - "name": "created_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - }, - "updated_at": { - "name": "updated_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - } - }, - "indexes": { - "commit_next_commit_idx": { - "name": "commit_next_commit_idx", - "columns": [ - { - "expression": "next_commit_id", - "isExpression": false, - "asc": true, - "nulls": "last" - } - ], - "isUnique": false, - "concurrently": false, - "method": "btree", - "with": {} - } - }, - "foreignKeys": { - "commits_next_commit_id_commits_id_fk": { - "name": "commits_next_commit_id_commits_id_fk", - "tableFrom": "commits", - "tableTo": "commits", - "schemaTo": "latitude", - "columnsFrom": [ - "next_commit_id" - ], - "columnsTo": [ - "id" - ], - "onDelete": "restrict", - "onUpdate": "no action" - }, - "commits_workspace_id_workspaces_id_fk": { - "name": "commits_workspace_id_workspaces_id_fk", - "tableFrom": "commits", - "tableTo": "workspaces", - "schemaTo": "latitude", - "columnsFrom": [ - "workspace_id" - ], - "columnsTo": [ - "id" - ], - "onDelete": "cascade", - "onUpdate": "no action" - } - }, - "compositePrimaryKeys": {}, - "uniqueConstraints": { - "commits_uuid_unique": { - "name": "commits_uuid_unique", - "nullsNotDistinct": false, - "columns": [ - "uuid" - ] - } - } - }, "latitude.document_hierarchies": { "name": "document_hierarchies", "schema": "latitude", @@ -258,473 +51,9 @@ "foreignKeys": {}, "compositePrimaryKeys": {}, "uniqueConstraints": {} - }, - "latitude.document_snapshots": { - "name": "document_snapshots", - "schema": "latitude", - "columns": { - "id": { - "name": "id", - "type": "bigserial", - "primaryKey": true, - "notNull": true - }, - "commit_id": { - "name": "commit_id", - "type": "bigint", - "primaryKey": false, - "notNull": true - }, - "document_version_id": { - "name": "document_version_id", - "type": "bigint", - "primaryKey": false, - "notNull": true - }, - "created_at": { - "name": "created_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - }, - "updated_at": { - "name": "updated_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - } - }, - "indexes": { - "prompt_commit_idx": { - "name": "prompt_commit_idx", - "columns": [ - { - "expression": "commit_id", - "isExpression": false, - "asc": true, - "nulls": "last" - } - ], - "isUnique": false, - "concurrently": false, - "method": "btree", - "with": {} - }, - "document_snapshot_document_version_idx": { - "name": "document_snapshot_document_version_idx", - "columns": [ - { - "expression": "document_version_id", - "isExpression": false, - "asc": true, - "nulls": "last" - } - ], - "isUnique": false, - "concurrently": false, - "method": "btree", - "with": {} - } - }, - "foreignKeys": { - "document_snapshots_commit_id_commits_id_fk": { - "name": "document_snapshots_commit_id_commits_id_fk", - "tableFrom": "document_snapshots", - "tableTo": "commits", - "schemaTo": "latitude", - "columnsFrom": [ - "commit_id" - ], - "columnsTo": [ - "id" - ], - "onDelete": "restrict", - "onUpdate": "no action" - }, - "document_snapshots_document_version_id_document_versions_id_fk": { - "name": "document_snapshots_document_version_id_document_versions_id_fk", - "tableFrom": "document_snapshots", - "tableTo": "document_versions", - "schemaTo": "latitude", - "columnsFrom": [ - "document_version_id" - ], - "columnsTo": [ - "id" - ], - "onDelete": "restrict", - "onUpdate": "no action" - } - }, - "compositePrimaryKeys": {}, - "uniqueConstraints": {} - }, - "latitude.document_versions": { - "name": "document_versions", - "schema": "latitude", - "columns": { - "id": { - "name": "id", - "type": "bigserial", - "primaryKey": true, - "notNull": true - }, - "name": { - "name": "name", - "type": "varchar", - "primaryKey": false, - "notNull": true - }, - "document_type": { - "name": "document_type", - "type": "document_type", - "typeSchema": "public", - "primaryKey": false, - "notNull": true, - "default": "'document'" - }, - "content": { - "name": "content", - "type": "text", - "primaryKey": false, - "notNull": false - }, - "hash": { - "name": "hash", - "type": "varchar", - "primaryKey": false, - "notNull": false - }, - "deleted_at": { - "name": "deleted_at", - "type": "timestamp", - "primaryKey": false, - "notNull": false - }, - "document_uuid": { - "name": "document_uuid", - "type": "uuid", - "primaryKey": false, - "notNull": true, - "default": "gen_random_uuid()" - }, - "parent_id": { - "name": "parent_id", - "type": "bigint", - "primaryKey": false, - "notNull": false - }, - "commit_id": { - "name": "commit_id", - "type": "bigint", - "primaryKey": false, - "notNull": true - }, - "created_at": { - "name": "created_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - }, - "updated_at": { - "name": "updated_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - } - }, - "indexes": {}, - "foreignKeys": { - "document_versions_parent_id_document_versions_id_fk": { - "name": "document_versions_parent_id_document_versions_id_fk", - "tableFrom": "document_versions", - "tableTo": "document_versions", - "schemaTo": "latitude", - "columnsFrom": [ - "parent_id" - ], - "columnsTo": [ - "id" - ], - "onDelete": "cascade", - "onUpdate": "no action" - }, - "document_versions_commit_id_commits_id_fk": { - "name": "document_versions_commit_id_commits_id_fk", - "tableFrom": "document_versions", - "tableTo": "commits", - "schemaTo": "latitude", - "columnsFrom": [ - "commit_id" - ], - "columnsTo": [ - "id" - ], - "onDelete": "no action", - "onUpdate": "no action" - } - }, - "compositePrimaryKeys": {}, - "uniqueConstraints": {} - }, - "latitude.memberships": { - "name": "memberships", - "schema": "latitude", - "columns": { - "workspace_id": { - "name": "workspace_id", - "type": "bigint", - "primaryKey": false, - "notNull": true - }, - "user_id": { - "name": "user_id", - "type": "text", - "primaryKey": false, - "notNull": true - }, - "created_at": { - "name": "created_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - }, - "updated_at": { - "name": "updated_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - } - }, - "indexes": {}, - "foreignKeys": { - "memberships_workspace_id_workspaces_id_fk": { - "name": "memberships_workspace_id_workspaces_id_fk", - "tableFrom": "memberships", - "tableTo": "workspaces", - "schemaTo": "latitude", - "columnsFrom": [ - "workspace_id" - ], - "columnsTo": [ - "id" - ], - "onDelete": "cascade", - "onUpdate": "no action" - }, - "memberships_user_id_users_id_fk": { - "name": "memberships_user_id_users_id_fk", - "tableFrom": "memberships", - "tableTo": "users", - "schemaTo": "latitude", - "columnsFrom": [ - "user_id" - ], - "columnsTo": [ - "id" - ], - "onDelete": "cascade", - "onUpdate": "no action" - } - }, - "compositePrimaryKeys": { - "memberships_workspace_id_user_id_pk": { - "name": "memberships_workspace_id_user_id_pk", - "columns": [ - "workspace_id", - "user_id" - ] - } - }, - "uniqueConstraints": {} - }, - "latitude.sessions": { - "name": "sessions", - "schema": "latitude", - "columns": { - "id": { - "name": "id", - "type": "text", - "primaryKey": true, - "notNull": true - }, - "user_id": { - "name": "user_id", - "type": "text", - "primaryKey": false, - "notNull": true - }, - "expires_at": { - "name": "expires_at", - "type": "timestamp with time zone", - "primaryKey": false, - "notNull": true - }, - "created_at": { - "name": "created_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - }, - "updated_at": { - "name": "updated_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - } - }, - "indexes": {}, - "foreignKeys": { - "sessions_user_id_users_id_fk": { - "name": "sessions_user_id_users_id_fk", - "tableFrom": "sessions", - "tableTo": "users", - "schemaTo": "latitude", - "columnsFrom": [ - "user_id" - ], - "columnsTo": [ - "id" - ], - "onDelete": "cascade", - "onUpdate": "no action" - } - }, - "compositePrimaryKeys": {}, - "uniqueConstraints": {} - }, - "latitude.users": { - "name": "users", - "schema": "latitude", - "columns": { - "id": { - "name": "id", - "type": "text", - "primaryKey": true, - "notNull": true - }, - "name": { - "name": "name", - "type": "text", - "primaryKey": false, - "notNull": false - }, - "email": { - "name": "email", - "type": "text", - "primaryKey": false, - "notNull": true - }, - "encrypted_password": { - "name": "encrypted_password", - "type": "text", - "primaryKey": false, - "notNull": true - }, - "created_at": { - "name": "created_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - }, - "updated_at": { - "name": "updated_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - } - }, - "indexes": {}, - "foreignKeys": {}, - "compositePrimaryKeys": {}, - "uniqueConstraints": { - "users_email_unique": { - "name": "users_email_unique", - "nullsNotDistinct": false, - "columns": [ - "email" - ] - } - } - }, - "latitude.workspaces": { - "name": "workspaces", - "schema": "latitude", - "columns": { - "id": { - "name": "id", - "type": "bigserial", - "primaryKey": true, - "notNull": true - }, - "name": { - "name": "name", - "type": "varchar(256)", - "primaryKey": false, - "notNull": true - }, - "creator_id": { - "name": "creator_id", - "type": "text", - "primaryKey": false, - "notNull": false - }, - "created_at": { - "name": "created_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - }, - "updated_at": { - "name": "updated_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - } - }, - "indexes": {}, - "foreignKeys": { - "workspaces_creator_id_users_id_fk": { - "name": "workspaces_creator_id_users_id_fk", - "tableFrom": "workspaces", - "tableTo": "users", - "schemaTo": "latitude", - "columnsFrom": [ - "creator_id" - ], - "columnsTo": [ - "id" - ], - "onDelete": "set null", - "onUpdate": "no action" - } - }, - "compositePrimaryKeys": {}, - "uniqueConstraints": {} - } - }, - "enums": { - "public.document_type": { - "name": "document_type", - "schema": "public", - "values": [ - "document", - "folder" - ] } }, + "enums": {}, "schemas": { "latitude": "latitude" }, diff --git a/packages/core/drizzle/meta/0001_snapshot.json b/packages/core/drizzle/meta/0001_snapshot.json index 8f323665b..bea4b57c2 100644 --- a/packages/core/drizzle/meta/0001_snapshot.json +++ b/packages/core/drizzle/meta/0001_snapshot.json @@ -1,11 +1,11 @@ { - "id": "bf621189-e101-43a7-84cf-387add46cfae", - "prevId": "2fd909d0-9cd7-4e6a-81de-0203fb8041a6", + "id": "cdf027b2-ec77-4802-8085-bfdcde91f089", + "prevId": "60f41b01-d3ec-4de1-82b1-3e7ec942c75a", "version": "7", "dialect": "postgresql", "tables": { - "latitude.api_keys": { - "name": "api_keys", + "latitude.document_hierarchies": { + "name": "document_hierarchies", "schema": "latitude", "columns": { "id": { @@ -14,126 +14,20 @@ "primaryKey": true, "notNull": true }, - "uuid": { - "name": "uuid", - "type": "uuid", - "primaryKey": false, - "notNull": true - }, - "workspace_id": { - "name": "workspace_id", + "parent_id": { + "name": "parent_id", "type": "bigint", "primaryKey": false, - "notNull": true - }, - "name": { - "name": "name", - "type": "varchar(256)", - "primaryKey": false, "notNull": false }, - "created_at": { - "name": "created_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - }, - "updated_at": { - "name": "updated_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - }, - "deleted_at": { - "name": "deleted_at", - "type": "timestamp", + "depth": { + "name": "depth", + "type": "integer", "primaryKey": false, - "notNull": false - } - }, - "indexes": { - "workspace_id_idx": { - "name": "workspace_id_idx", - "columns": [ - { - "expression": "workspace_id", - "isExpression": false, - "asc": true, - "nulls": "last" - } - ], - "isUnique": false, - "with": {}, - "method": "btree", - "concurrently": false - } - }, - "foreignKeys": { - "api_keys_workspace_id_workspaces_id_fk": { - "name": "api_keys_workspace_id_workspaces_id_fk", - "tableFrom": "api_keys", - "columnsFrom": [ - "workspace_id" - ], - "tableTo": "workspaces", - "schemaTo": "latitude", - "columnsTo": [ - "id" - ], - "onUpdate": "no action", - "onDelete": "no action" - } - }, - "compositePrimaryKeys": {}, - "uniqueConstraints": { - "api_keys_uuid_unique": { - "name": "api_keys_uuid_unique", - "columns": [ - "uuid" - ], - "nullsNotDistinct": false - } - } - }, - "latitude.commits": { - "name": "commits", - "schema": "latitude", - "columns": { - "id": { - "name": "id", - "type": "bigserial", - "primaryKey": true, "notNull": true }, - "uuid": { - "name": "uuid", - "type": "uuid", - "primaryKey": false, - "notNull": true, - "default": "gen_random_uuid()" - }, - "next_commit_id": { - "name": "next_commit_id", - "type": "bigint", - "primaryKey": false, - "notNull": false - }, - "title": { - "name": "title", - "type": "varchar(256)", - "primaryKey": false, - "notNull": false - }, - "description": { - "name": "description", - "type": "text", - "primaryKey": false, - "notNull": false - }, - "workspace_id": { - "name": "workspace_id", + "child_id": { + "name": "child_id", "type": "bigint", "primaryKey": false, "notNull": true @@ -153,89 +47,36 @@ "default": "now()" } }, - "indexes": { - "commit_next_commit_idx": { - "name": "commit_next_commit_idx", - "columns": [ - { - "expression": "next_commit_id", - "isExpression": false, - "asc": true, - "nulls": "last" - } - ], - "isUnique": false, - "with": {}, - "method": "btree", - "concurrently": false - } - }, - "foreignKeys": { - "commits_next_commit_id_commits_id_fk": { - "name": "commits_next_commit_id_commits_id_fk", - "tableFrom": "commits", - "columnsFrom": [ - "next_commit_id" - ], - "tableTo": "commits", - "schemaTo": "latitude", - "columnsTo": [ - "id" - ], - "onUpdate": "no action", - "onDelete": "restrict" - }, - "commits_workspace_id_workspaces_id_fk": { - "name": "commits_workspace_id_workspaces_id_fk", - "tableFrom": "commits", - "columnsFrom": [ - "workspace_id" - ], - "tableTo": "workspaces", - "schemaTo": "latitude", - "columnsTo": [ - "id" - ], - "onUpdate": "no action", - "onDelete": "cascade" - } - }, + "indexes": {}, + "foreignKeys": {}, "compositePrimaryKeys": {}, - "uniqueConstraints": { - "commits_uuid_unique": { - "name": "commits_uuid_unique", - "columns": [ - "uuid" - ], - "nullsNotDistinct": false - } - } + "uniqueConstraints": {} }, - "latitude.document_hierarchies": { - "name": "document_hierarchies", + "latitude.users": { + "name": "users", "schema": "latitude", "columns": { "id": { "name": "id", - "type": "bigserial", + "type": "text", "primaryKey": true, "notNull": true }, - "parent_id": { - "name": "parent_id", - "type": "bigint", + "name": { + "name": "name", + "type": "text", "primaryKey": false, "notNull": false }, - "depth": { - "name": "depth", - "type": "integer", + "email": { + "name": "email", + "type": "text", "primaryKey": false, "notNull": true }, - "child_id": { - "name": "child_id", - "type": "bigint", + "encrypted_password": { + "name": "encrypted_password", + "type": "text", "primaryKey": false, "notNull": true }, @@ -257,27 +98,35 @@ "indexes": {}, "foreignKeys": {}, "compositePrimaryKeys": {}, - "uniqueConstraints": {} + "uniqueConstraints": { + "users_email_unique": { + "name": "users_email_unique", + "nullsNotDistinct": false, + "columns": [ + "email" + ] + } + } }, - "latitude.document_snapshots": { - "name": "document_snapshots", + "latitude.sessions": { + "name": "sessions", "schema": "latitude", "columns": { "id": { "name": "id", - "type": "bigserial", + "type": "text", "primaryKey": true, "notNull": true }, - "commit_id": { - "name": "commit_id", - "type": "bigint", + "user_id": { + "name": "user_id", + "type": "text", "primaryKey": false, "notNull": true }, - "document_version_id": { - "name": "document_version_id", - "type": "bigint", + "expires_at": { + "name": "expires_at", + "type": "timestamp with time zone", "primaryKey": false, "notNull": true }, @@ -296,73 +145,28 @@ "default": "now()" } }, - "indexes": { - "prompt_commit_idx": { - "name": "prompt_commit_idx", - "columns": [ - { - "expression": "commit_id", - "isExpression": false, - "asc": true, - "nulls": "last" - } - ], - "isUnique": false, - "with": {}, - "method": "btree", - "concurrently": false - }, - "document_snapshot_document_version_idx": { - "name": "document_snapshot_document_version_idx", - "columns": [ - { - "expression": "document_version_id", - "isExpression": false, - "asc": true, - "nulls": "last" - } - ], - "isUnique": false, - "with": {}, - "method": "btree", - "concurrently": false - } - }, + "indexes": {}, "foreignKeys": { - "document_snapshots_commit_id_commits_id_fk": { - "name": "document_snapshots_commit_id_commits_id_fk", - "tableFrom": "document_snapshots", - "columnsFrom": [ - "commit_id" - ], - "tableTo": "commits", + "sessions_user_id_users_id_fk": { + "name": "sessions_user_id_users_id_fk", + "tableFrom": "sessions", + "tableTo": "users", "schemaTo": "latitude", - "columnsTo": [ - "id" - ], - "onUpdate": "no action", - "onDelete": "restrict" - }, - "document_snapshots_document_version_id_document_versions_id_fk": { - "name": "document_snapshots_document_version_id_document_versions_id_fk", - "tableFrom": "document_snapshots", "columnsFrom": [ - "document_version_id" + "user_id" ], - "tableTo": "document_versions", - "schemaTo": "latitude", "columnsTo": [ "id" ], - "onUpdate": "no action", - "onDelete": "restrict" + "onDelete": "cascade", + "onUpdate": "no action" } }, "compositePrimaryKeys": {}, "uniqueConstraints": {} }, - "latitude.document_versions": { - "name": "document_versions", + "latitude.workspaces": { + "name": "workspaces", "schema": "latitude", "columns": { "id": { @@ -373,55 +177,16 @@ }, "name": { "name": "name", - "type": "varchar", + "type": "varchar(256)", "primaryKey": false, "notNull": true }, - "document_type": { - "name": "document_type", - "type": "document_type", - "typeSchema": "public", - "primaryKey": false, - "notNull": true, - "default": "'document'" - }, - "content": { - "name": "content", + "creator_id": { + "name": "creator_id", "type": "text", "primaryKey": false, "notNull": false }, - "hash": { - "name": "hash", - "type": "varchar", - "primaryKey": false, - "notNull": false - }, - "deleted_at": { - "name": "deleted_at", - "type": "timestamp", - "primaryKey": false, - "notNull": false - }, - "document_uuid": { - "name": "document_uuid", - "type": "uuid", - "primaryKey": false, - "notNull": true, - "default": "gen_random_uuid()" - }, - "parent_id": { - "name": "parent_id", - "type": "bigint", - "primaryKey": false, - "notNull": false - }, - "commit_id": { - "name": "commit_id", - "type": "bigint", - "primaryKey": false, - "notNull": true - }, "created_at": { "name": "created_at", "type": "timestamp", @@ -439,33 +204,19 @@ }, "indexes": {}, "foreignKeys": { - "document_versions_parent_id_document_versions_id_fk": { - "name": "document_versions_parent_id_document_versions_id_fk", - "tableFrom": "document_versions", - "columnsFrom": [ - "parent_id" - ], - "tableTo": "document_versions", + "workspaces_creator_id_users_id_fk": { + "name": "workspaces_creator_id_users_id_fk", + "tableFrom": "workspaces", + "tableTo": "users", "schemaTo": "latitude", - "columnsTo": [ - "id" - ], - "onUpdate": "no action", - "onDelete": "cascade" - }, - "document_versions_commit_id_commits_id_fk": { - "name": "document_versions_commit_id_commits_id_fk", - "tableFrom": "document_versions", "columnsFrom": [ - "commit_id" + "creator_id" ], - "tableTo": "commits", - "schemaTo": "latitude", "columnsTo": [ "id" ], - "onUpdate": "no action", - "onDelete": "no action" + "onDelete": "set null", + "onUpdate": "no action" } }, "compositePrimaryKeys": {}, @@ -507,30 +258,30 @@ "memberships_workspace_id_workspaces_id_fk": { "name": "memberships_workspace_id_workspaces_id_fk", "tableFrom": "memberships", + "tableTo": "workspaces", + "schemaTo": "latitude", "columnsFrom": [ "workspace_id" ], - "tableTo": "workspaces", - "schemaTo": "latitude", "columnsTo": [ "id" ], - "onUpdate": "no action", - "onDelete": "cascade" + "onDelete": "cascade", + "onUpdate": "no action" }, "memberships_user_id_users_id_fk": { "name": "memberships_user_id_users_id_fk", "tableFrom": "memberships", + "tableTo": "users", + "schemaTo": "latitude", "columnsFrom": [ "user_id" ], - "tableTo": "users", - "schemaTo": "latitude", "columnsTo": [ "id" ], - "onUpdate": "no action", - "onDelete": "cascade" + "onDelete": "cascade", + "onUpdate": "no action" } }, "compositePrimaryKeys": { @@ -544,91 +295,34 @@ }, "uniqueConstraints": {} }, - "latitude.sessions": { - "name": "sessions", + "latitude.api_keys": { + "name": "api_keys", "schema": "latitude", "columns": { "id": { "name": "id", - "type": "text", + "type": "bigserial", "primaryKey": true, "notNull": true }, - "user_id": { - "name": "user_id", - "type": "text", - "primaryKey": false, - "notNull": true - }, - "expires_at": { - "name": "expires_at", - "type": "timestamp with time zone", + "uuid": { + "name": "uuid", + "type": "uuid", "primaryKey": false, "notNull": true }, - "created_at": { - "name": "created_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - }, - "updated_at": { - "name": "updated_at", - "type": "timestamp", + "workspace_id": { + "name": "workspace_id", + "type": "bigint", "primaryKey": false, - "notNull": true, - "default": "now()" - } - }, - "indexes": {}, - "foreignKeys": { - "sessions_user_id_users_id_fk": { - "name": "sessions_user_id_users_id_fk", - "tableFrom": "sessions", - "columnsFrom": [ - "user_id" - ], - "tableTo": "users", - "schemaTo": "latitude", - "columnsTo": [ - "id" - ], - "onUpdate": "no action", - "onDelete": "cascade" - } - }, - "compositePrimaryKeys": {}, - "uniqueConstraints": {} - }, - "latitude.users": { - "name": "users", - "schema": "latitude", - "columns": { - "id": { - "name": "id", - "type": "text", - "primaryKey": true, "notNull": true }, "name": { "name": "name", - "type": "text", + "type": "varchar(256)", "primaryKey": false, "notNull": false }, - "email": { - "name": "email", - "type": "text", - "primaryKey": false, - "notNull": true - }, - "encrypted_password": { - "name": "encrypted_password", - "type": "text", - "primaryKey": false, - "notNull": true - }, "created_at": { "name": "created_at", "type": "timestamp", @@ -642,89 +336,60 @@ "primaryKey": false, "notNull": true, "default": "now()" + }, + "deleted_at": { + "name": "deleted_at", + "type": "timestamp", + "primaryKey": false, + "notNull": false } }, - "indexes": {}, - "foreignKeys": {}, - "compositePrimaryKeys": {}, - "uniqueConstraints": { - "users_email_unique": { - "name": "users_email_unique", + "indexes": { + "workspace_id_idx": { + "name": "workspace_id_idx", "columns": [ - "email" + { + "expression": "workspace_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } ], - "nullsNotDistinct": false - } - } - }, - "latitude.workspaces": { - "name": "workspaces", - "schema": "latitude", - "columns": { - "id": { - "name": "id", - "type": "bigserial", - "primaryKey": true, - "notNull": true - }, - "name": { - "name": "name", - "type": "varchar(256)", - "primaryKey": false, - "notNull": true - }, - "creator_id": { - "name": "creator_id", - "type": "text", - "primaryKey": false, - "notNull": false - }, - "created_at": { - "name": "created_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" - }, - "updated_at": { - "name": "updated_at", - "type": "timestamp", - "primaryKey": false, - "notNull": true, - "default": "now()" + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} } }, - "indexes": {}, "foreignKeys": { - "workspaces_creator_id_users_id_fk": { - "name": "workspaces_creator_id_users_id_fk", - "tableFrom": "workspaces", + "api_keys_workspace_id_workspaces_id_fk": { + "name": "api_keys_workspace_id_workspaces_id_fk", + "tableFrom": "api_keys", + "tableTo": "workspaces", + "schemaTo": "latitude", "columnsFrom": [ - "creator_id" + "workspace_id" ], - "tableTo": "users", - "schemaTo": "latitude", "columnsTo": [ "id" ], - "onUpdate": "no action", - "onDelete": "set null" + "onDelete": "no action", + "onUpdate": "no action" } }, "compositePrimaryKeys": {}, - "uniqueConstraints": {} - } - }, - "enums": { - "public.document_type": { - "name": "document_type", - "schema": "public", - "values": [ - "document", - "folder" - ] + "uniqueConstraints": { + "api_keys_uuid_unique": { + "name": "api_keys_uuid_unique", + "nullsNotDistinct": false, + "columns": [ + "uuid" + ] + } + } } }, + "enums": {}, "schemas": { "latitude": "latitude" }, diff --git a/packages/core/drizzle/meta/0002_snapshot.json b/packages/core/drizzle/meta/0002_snapshot.json new file mode 100644 index 000000000..198d69a59 --- /dev/null +++ b/packages/core/drizzle/meta/0002_snapshot.json @@ -0,0 +1,809 @@ +{ + "id": "cf3e829e-52c2-47fb-89f4-5dd28e3c3d43", + "prevId": "cdf027b2-ec77-4802-8085-bfdcde91f089", + "version": "7", + "dialect": "postgresql", + "tables": { + "latitude.document_hierarchies": { + "name": "document_hierarchies", + "schema": "latitude", + "columns": { + "id": { + "name": "id", + "type": "bigserial", + "primaryKey": true, + "notNull": true + }, + "parent_id": { + "name": "parent_id", + "type": "bigint", + "primaryKey": false, + "notNull": false + }, + "depth": { + "name": "depth", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "child_id": { + "name": "child_id", + "type": "bigint", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "latitude.users": { + "name": "users", + "schema": "latitude", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "email": { + "name": "email", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "encrypted_password": { + "name": "encrypted_password", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "users_email_unique": { + "name": "users_email_unique", + "nullsNotDistinct": false, + "columns": [ + "email" + ] + } + } + }, + "latitude.sessions": { + "name": "sessions", + "schema": "latitude", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "expires_at": { + "name": "expires_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "sessions_user_id_users_id_fk": { + "name": "sessions_user_id_users_id_fk", + "tableFrom": "sessions", + "tableTo": "users", + "schemaTo": "latitude", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "latitude.workspaces": { + "name": "workspaces", + "schema": "latitude", + "columns": { + "id": { + "name": "id", + "type": "bigserial", + "primaryKey": true, + "notNull": true + }, + "name": { + "name": "name", + "type": "varchar(256)", + "primaryKey": false, + "notNull": true + }, + "creator_id": { + "name": "creator_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "workspaces_creator_id_users_id_fk": { + "name": "workspaces_creator_id_users_id_fk", + "tableFrom": "workspaces", + "tableTo": "users", + "schemaTo": "latitude", + "columnsFrom": [ + "creator_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "latitude.memberships": { + "name": "memberships", + "schema": "latitude", + "columns": { + "workspace_id": { + "name": "workspace_id", + "type": "bigint", + "primaryKey": false, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "memberships_workspace_id_workspaces_id_fk": { + "name": "memberships_workspace_id_workspaces_id_fk", + "tableFrom": "memberships", + "tableTo": "workspaces", + "schemaTo": "latitude", + "columnsFrom": [ + "workspace_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "memberships_user_id_users_id_fk": { + "name": "memberships_user_id_users_id_fk", + "tableFrom": "memberships", + "tableTo": "users", + "schemaTo": "latitude", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": { + "memberships_workspace_id_user_id_pk": { + "name": "memberships_workspace_id_user_id_pk", + "columns": [ + "workspace_id", + "user_id" + ] + } + }, + "uniqueConstraints": {} + }, + "latitude.api_keys": { + "name": "api_keys", + "schema": "latitude", + "columns": { + "id": { + "name": "id", + "type": "bigserial", + "primaryKey": true, + "notNull": true + }, + "uuid": { + "name": "uuid", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "workspace_id": { + "name": "workspace_id", + "type": "bigint", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "varchar(256)", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "deleted_at": { + "name": "deleted_at", + "type": "timestamp", + "primaryKey": false, + "notNull": false + } + }, + "indexes": { + "workspace_id_idx": { + "name": "workspace_id_idx", + "columns": [ + { + "expression": "workspace_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "api_keys_workspace_id_workspaces_id_fk": { + "name": "api_keys_workspace_id_workspaces_id_fk", + "tableFrom": "api_keys", + "tableTo": "workspaces", + "schemaTo": "latitude", + "columnsFrom": [ + "workspace_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "api_keys_uuid_unique": { + "name": "api_keys_uuid_unique", + "nullsNotDistinct": false, + "columns": [ + "uuid" + ] + } + } + }, + "latitude.projects": { + "name": "projects", + "schema": "latitude", + "columns": { + "id": { + "name": "id", + "type": "bigserial", + "primaryKey": true, + "notNull": true + }, + "name": { + "name": "name", + "type": "varchar(256)", + "primaryKey": false, + "notNull": true + }, + "workspace_id": { + "name": "workspace_id", + "type": "bigint", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "workspace_idx": { + "name": "workspace_idx", + "columns": [ + { + "expression": "workspace_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "projects_workspace_id_workspaces_id_fk": { + "name": "projects_workspace_id_workspaces_id_fk", + "tableFrom": "projects", + "tableTo": "workspaces", + "schemaTo": "latitude", + "columnsFrom": [ + "workspace_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "latitude.commits": { + "name": "commits", + "schema": "latitude", + "columns": { + "id": { + "name": "id", + "type": "bigserial", + "primaryKey": true, + "notNull": true + }, + "uuid": { + "name": "uuid", + "type": "uuid", + "primaryKey": false, + "notNull": true, + "default": "gen_random_uuid()" + }, + "next_commit_id": { + "name": "next_commit_id", + "type": "bigint", + "primaryKey": false, + "notNull": false + }, + "title": { + "name": "title", + "type": "varchar(256)", + "primaryKey": false, + "notNull": false + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "project_id": { + "name": "project_id", + "type": "bigint", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "commit_next_commit_idx": { + "name": "commit_next_commit_idx", + "columns": [ + { + "expression": "next_commit_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "commits_next_commit_id_commits_id_fk": { + "name": "commits_next_commit_id_commits_id_fk", + "tableFrom": "commits", + "tableTo": "commits", + "schemaTo": "latitude", + "columnsFrom": [ + "next_commit_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "restrict", + "onUpdate": "no action" + }, + "commits_project_id_workspaces_id_fk": { + "name": "commits_project_id_workspaces_id_fk", + "tableFrom": "commits", + "tableTo": "workspaces", + "schemaTo": "latitude", + "columnsFrom": [ + "project_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "commits_uuid_unique": { + "name": "commits_uuid_unique", + "nullsNotDistinct": false, + "columns": [ + "uuid" + ] + } + } + }, + "latitude.document_snapshots": { + "name": "document_snapshots", + "schema": "latitude", + "columns": { + "id": { + "name": "id", + "type": "bigserial", + "primaryKey": true, + "notNull": true + }, + "commit_id": { + "name": "commit_id", + "type": "bigint", + "primaryKey": false, + "notNull": true + }, + "document_version_id": { + "name": "document_version_id", + "type": "bigint", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "prompt_commit_idx": { + "name": "prompt_commit_idx", + "columns": [ + { + "expression": "commit_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "document_snapshot_document_version_idx": { + "name": "document_snapshot_document_version_idx", + "columns": [ + { + "expression": "document_version_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "document_snapshots_commit_id_commits_id_fk": { + "name": "document_snapshots_commit_id_commits_id_fk", + "tableFrom": "document_snapshots", + "tableTo": "commits", + "schemaTo": "latitude", + "columnsFrom": [ + "commit_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "restrict", + "onUpdate": "no action" + }, + "document_snapshots_document_version_id_document_versions_id_fk": { + "name": "document_snapshots_document_version_id_document_versions_id_fk", + "tableFrom": "document_snapshots", + "tableTo": "document_versions", + "schemaTo": "latitude", + "columnsFrom": [ + "document_version_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "restrict", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "latitude.document_versions": { + "name": "document_versions", + "schema": "latitude", + "columns": { + "id": { + "name": "id", + "type": "bigserial", + "primaryKey": true, + "notNull": true + }, + "name": { + "name": "name", + "type": "varchar", + "primaryKey": false, + "notNull": true + }, + "document_type": { + "name": "document_type", + "type": "document_type", + "typeSchema": "latitude", + "primaryKey": false, + "notNull": true, + "default": "'document'" + }, + "content": { + "name": "content", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "hash": { + "name": "hash", + "type": "varchar", + "primaryKey": false, + "notNull": false + }, + "deleted_at": { + "name": "deleted_at", + "type": "timestamp", + "primaryKey": false, + "notNull": false + }, + "document_uuid": { + "name": "document_uuid", + "type": "uuid", + "primaryKey": false, + "notNull": true, + "default": "gen_random_uuid()" + }, + "parent_id": { + "name": "parent_id", + "type": "bigint", + "primaryKey": false, + "notNull": false + }, + "commit_id": { + "name": "commit_id", + "type": "bigint", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "document_versions_parent_id_document_versions_id_fk": { + "name": "document_versions_parent_id_document_versions_id_fk", + "tableFrom": "document_versions", + "tableTo": "document_versions", + "schemaTo": "latitude", + "columnsFrom": [ + "parent_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "document_versions_commit_id_commits_id_fk": { + "name": "document_versions_commit_id_commits_id_fk", + "tableFrom": "document_versions", + "tableTo": "commits", + "schemaTo": "latitude", + "columnsFrom": [ + "commit_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + } + }, + "enums": { + "latitude.document_type": { + "name": "document_type", + "schema": "latitude", + "values": [ + "document", + "folder" + ] + } + }, + "schemas": { + "latitude": "latitude" + }, + "_meta": { + "columns": {}, + "schemas": {}, + "tables": {} + } +} \ No newline at end of file diff --git a/packages/core/drizzle/meta/0003_snapshot.json b/packages/core/drizzle/meta/0003_snapshot.json new file mode 100644 index 000000000..ea4a979fd --- /dev/null +++ b/packages/core/drizzle/meta/0003_snapshot.json @@ -0,0 +1,809 @@ +{ + "id": "eedd6d42-c764-44b7-9e9e-2428f529d3dc", + "prevId": "cf3e829e-52c2-47fb-89f4-5dd28e3c3d43", + "version": "7", + "dialect": "postgresql", + "tables": { + "latitude.document_hierarchies": { + "name": "document_hierarchies", + "schema": "latitude", + "columns": { + "id": { + "name": "id", + "type": "bigserial", + "primaryKey": true, + "notNull": true + }, + "parent_id": { + "name": "parent_id", + "type": "bigint", + "primaryKey": false, + "notNull": false + }, + "depth": { + "name": "depth", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "child_id": { + "name": "child_id", + "type": "bigint", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "latitude.users": { + "name": "users", + "schema": "latitude", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "email": { + "name": "email", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "encrypted_password": { + "name": "encrypted_password", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "users_email_unique": { + "name": "users_email_unique", + "columns": [ + "email" + ], + "nullsNotDistinct": false + } + } + }, + "latitude.sessions": { + "name": "sessions", + "schema": "latitude", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "expires_at": { + "name": "expires_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "sessions_user_id_users_id_fk": { + "name": "sessions_user_id_users_id_fk", + "tableFrom": "sessions", + "columnsFrom": [ + "user_id" + ], + "tableTo": "users", + "schemaTo": "latitude", + "columnsTo": [ + "id" + ], + "onUpdate": "no action", + "onDelete": "cascade" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "latitude.workspaces": { + "name": "workspaces", + "schema": "latitude", + "columns": { + "id": { + "name": "id", + "type": "bigserial", + "primaryKey": true, + "notNull": true + }, + "name": { + "name": "name", + "type": "varchar(256)", + "primaryKey": false, + "notNull": true + }, + "creator_id": { + "name": "creator_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "workspaces_creator_id_users_id_fk": { + "name": "workspaces_creator_id_users_id_fk", + "tableFrom": "workspaces", + "columnsFrom": [ + "creator_id" + ], + "tableTo": "users", + "schemaTo": "latitude", + "columnsTo": [ + "id" + ], + "onUpdate": "no action", + "onDelete": "set null" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "latitude.memberships": { + "name": "memberships", + "schema": "latitude", + "columns": { + "workspace_id": { + "name": "workspace_id", + "type": "bigint", + "primaryKey": false, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "memberships_workspace_id_workspaces_id_fk": { + "name": "memberships_workspace_id_workspaces_id_fk", + "tableFrom": "memberships", + "columnsFrom": [ + "workspace_id" + ], + "tableTo": "workspaces", + "schemaTo": "latitude", + "columnsTo": [ + "id" + ], + "onUpdate": "no action", + "onDelete": "cascade" + }, + "memberships_user_id_users_id_fk": { + "name": "memberships_user_id_users_id_fk", + "tableFrom": "memberships", + "columnsFrom": [ + "user_id" + ], + "tableTo": "users", + "schemaTo": "latitude", + "columnsTo": [ + "id" + ], + "onUpdate": "no action", + "onDelete": "cascade" + } + }, + "compositePrimaryKeys": { + "memberships_workspace_id_user_id_pk": { + "name": "memberships_workspace_id_user_id_pk", + "columns": [ + "workspace_id", + "user_id" + ] + } + }, + "uniqueConstraints": {} + }, + "latitude.api_keys": { + "name": "api_keys", + "schema": "latitude", + "columns": { + "id": { + "name": "id", + "type": "bigserial", + "primaryKey": true, + "notNull": true + }, + "uuid": { + "name": "uuid", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "workspace_id": { + "name": "workspace_id", + "type": "bigint", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "varchar(256)", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "deleted_at": { + "name": "deleted_at", + "type": "timestamp", + "primaryKey": false, + "notNull": false + } + }, + "indexes": { + "workspace_id_idx": { + "name": "workspace_id_idx", + "columns": [ + { + "expression": "workspace_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "with": {}, + "method": "btree", + "concurrently": false + } + }, + "foreignKeys": { + "api_keys_workspace_id_workspaces_id_fk": { + "name": "api_keys_workspace_id_workspaces_id_fk", + "tableFrom": "api_keys", + "columnsFrom": [ + "workspace_id" + ], + "tableTo": "workspaces", + "schemaTo": "latitude", + "columnsTo": [ + "id" + ], + "onUpdate": "no action", + "onDelete": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "api_keys_uuid_unique": { + "name": "api_keys_uuid_unique", + "columns": [ + "uuid" + ], + "nullsNotDistinct": false + } + } + }, + "latitude.projects": { + "name": "projects", + "schema": "latitude", + "columns": { + "id": { + "name": "id", + "type": "bigserial", + "primaryKey": true, + "notNull": true + }, + "name": { + "name": "name", + "type": "varchar(256)", + "primaryKey": false, + "notNull": true + }, + "workspace_id": { + "name": "workspace_id", + "type": "bigint", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "workspace_idx": { + "name": "workspace_idx", + "columns": [ + { + "expression": "workspace_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "with": {}, + "method": "btree", + "concurrently": false + } + }, + "foreignKeys": { + "projects_workspace_id_workspaces_id_fk": { + "name": "projects_workspace_id_workspaces_id_fk", + "tableFrom": "projects", + "columnsFrom": [ + "workspace_id" + ], + "tableTo": "workspaces", + "schemaTo": "latitude", + "columnsTo": [ + "id" + ], + "onUpdate": "no action", + "onDelete": "cascade" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "latitude.commits": { + "name": "commits", + "schema": "latitude", + "columns": { + "id": { + "name": "id", + "type": "bigserial", + "primaryKey": true, + "notNull": true + }, + "uuid": { + "name": "uuid", + "type": "uuid", + "primaryKey": false, + "notNull": true, + "default": "gen_random_uuid()" + }, + "next_commit_id": { + "name": "next_commit_id", + "type": "bigint", + "primaryKey": false, + "notNull": false + }, + "title": { + "name": "title", + "type": "varchar(256)", + "primaryKey": false, + "notNull": false + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "project_id": { + "name": "project_id", + "type": "bigint", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "commit_next_commit_idx": { + "name": "commit_next_commit_idx", + "columns": [ + { + "expression": "next_commit_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "with": {}, + "method": "btree", + "concurrently": false + } + }, + "foreignKeys": { + "commits_next_commit_id_commits_id_fk": { + "name": "commits_next_commit_id_commits_id_fk", + "tableFrom": "commits", + "columnsFrom": [ + "next_commit_id" + ], + "tableTo": "commits", + "schemaTo": "latitude", + "columnsTo": [ + "id" + ], + "onUpdate": "no action", + "onDelete": "restrict" + }, + "commits_project_id_workspaces_id_fk": { + "name": "commits_project_id_workspaces_id_fk", + "tableFrom": "commits", + "columnsFrom": [ + "project_id" + ], + "tableTo": "workspaces", + "schemaTo": "latitude", + "columnsTo": [ + "id" + ], + "onUpdate": "no action", + "onDelete": "cascade" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "commits_uuid_unique": { + "name": "commits_uuid_unique", + "columns": [ + "uuid" + ], + "nullsNotDistinct": false + } + } + }, + "latitude.document_snapshots": { + "name": "document_snapshots", + "schema": "latitude", + "columns": { + "id": { + "name": "id", + "type": "bigserial", + "primaryKey": true, + "notNull": true + }, + "commit_id": { + "name": "commit_id", + "type": "bigint", + "primaryKey": false, + "notNull": true + }, + "document_version_id": { + "name": "document_version_id", + "type": "bigint", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "prompt_commit_idx": { + "name": "prompt_commit_idx", + "columns": [ + { + "expression": "commit_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "with": {}, + "method": "btree", + "concurrently": false + }, + "document_snapshot_document_version_idx": { + "name": "document_snapshot_document_version_idx", + "columns": [ + { + "expression": "document_version_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "with": {}, + "method": "btree", + "concurrently": false + } + }, + "foreignKeys": { + "document_snapshots_commit_id_commits_id_fk": { + "name": "document_snapshots_commit_id_commits_id_fk", + "tableFrom": "document_snapshots", + "columnsFrom": [ + "commit_id" + ], + "tableTo": "commits", + "schemaTo": "latitude", + "columnsTo": [ + "id" + ], + "onUpdate": "no action", + "onDelete": "restrict" + }, + "document_snapshots_document_version_id_document_versions_id_fk": { + "name": "document_snapshots_document_version_id_document_versions_id_fk", + "tableFrom": "document_snapshots", + "columnsFrom": [ + "document_version_id" + ], + "tableTo": "document_versions", + "schemaTo": "latitude", + "columnsTo": [ + "id" + ], + "onUpdate": "no action", + "onDelete": "restrict" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "latitude.document_versions": { + "name": "document_versions", + "schema": "latitude", + "columns": { + "id": { + "name": "id", + "type": "bigserial", + "primaryKey": true, + "notNull": true + }, + "name": { + "name": "name", + "type": "varchar", + "primaryKey": false, + "notNull": true + }, + "document_type": { + "name": "document_type", + "type": "document_type", + "typeSchema": "latitude", + "primaryKey": false, + "notNull": true, + "default": "'document'" + }, + "content": { + "name": "content", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "hash": { + "name": "hash", + "type": "varchar", + "primaryKey": false, + "notNull": false + }, + "deleted_at": { + "name": "deleted_at", + "type": "timestamp", + "primaryKey": false, + "notNull": false + }, + "document_uuid": { + "name": "document_uuid", + "type": "uuid", + "primaryKey": false, + "notNull": true, + "default": "gen_random_uuid()" + }, + "parent_id": { + "name": "parent_id", + "type": "bigint", + "primaryKey": false, + "notNull": false + }, + "commit_id": { + "name": "commit_id", + "type": "bigint", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "document_versions_parent_id_document_versions_id_fk": { + "name": "document_versions_parent_id_document_versions_id_fk", + "tableFrom": "document_versions", + "columnsFrom": [ + "parent_id" + ], + "tableTo": "document_versions", + "schemaTo": "latitude", + "columnsTo": [ + "id" + ], + "onUpdate": "no action", + "onDelete": "cascade" + }, + "document_versions_commit_id_commits_id_fk": { + "name": "document_versions_commit_id_commits_id_fk", + "tableFrom": "document_versions", + "columnsFrom": [ + "commit_id" + ], + "tableTo": "commits", + "schemaTo": "latitude", + "columnsTo": [ + "id" + ], + "onUpdate": "no action", + "onDelete": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + } + }, + "enums": { + "latitude.document_type": { + "name": "document_type", + "schema": "latitude", + "values": [ + "document", + "folder" + ] + } + }, + "schemas": { + "latitude": "latitude" + }, + "_meta": { + "columns": {}, + "schemas": {}, + "tables": {} + } +} \ No newline at end of file diff --git a/packages/core/drizzle/meta/_journal.json b/packages/core/drizzle/meta/_journal.json index 4a13dfd63..ba3a397a1 100644 --- a/packages/core/drizzle/meta/_journal.json +++ b/packages/core/drizzle/meta/_journal.json @@ -5,15 +5,29 @@ { "idx": 0, "version": "7", - "when": 1721052037424, - "tag": "0000_daffy_madrox", + "when": 1721122441285, + "tag": "0000_nice_daimon_hellstrom", "breakpoints": true }, { "idx": 1, "version": "7", - "when": 1721052039598, - "tag": "0001_ambitious_mesmero", + "when": 1721122652708, + "tag": "0001_living_madripoor", + "breakpoints": true + }, + { + "idx": 2, + "version": "7", + "when": 1721124730397, + "tag": "0002_windy_wind_dancer", + "breakpoints": true + }, + { + "idx": 3, + "version": "7", + "when": 1721124762014, + "tag": "0003_cold_spirit", "breakpoints": true } ] diff --git a/packages/core/package.json b/packages/core/package.json index b9fc573c5..f8bcd0426 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -5,7 +5,8 @@ "main": "src/index.ts", "type": "module", "exports": { - ".": "./src/index.ts" + ".": "./src/index.ts", + "./browser": "./src/browser.ts" }, "scripts": { "db:generate": "drizzle-kit generate", diff --git a/packages/core/src/browser.ts b/packages/core/src/browser.ts new file mode 100644 index 000000000..f87cf0102 --- /dev/null +++ b/packages/core/src/browser.ts @@ -0,0 +1 @@ +export * from './constants' diff --git a/packages/core/src/constants.ts b/packages/core/src/constants.ts index 14f9cafaa..67402396c 100644 --- a/packages/core/src/constants.ts +++ b/packages/core/src/constants.ts @@ -2,3 +2,8 @@ export const LATITUDE_DOCS_URL = '' export const LATITUDE_EMAIL = '' export const LATITUDE_SLACK_URL = 'https://join.slack.com/t/trylatitude/shared_invite/zt-17dyj4elt-rwM~h2OorAA3NtgmibhnLA' +export const HEAD_COMMIT = 'HEAD' +export enum DocumentType { + Document = 'document', + Folder = 'folder', +} diff --git a/packages/core/src/data-access/commits.ts b/packages/core/src/data-access/commits.ts index 164fbdf79..e605596ad 100644 --- a/packages/core/src/data-access/commits.ts +++ b/packages/core/src/data-access/commits.ts @@ -1,26 +1,20 @@ -import { commits, database, HEAD_COMMIT } from '@latitude-data/core' +import { database } from '$core/client' +import { HEAD_COMMIT } from '$core/constants' +import { commits } from '$core/schema' import { desc, eq, isNull } from 'drizzle-orm' -export async function findCommit({ uuid }: { uuid?: string }, tx = database) { - const selectCondition = (uuid?: Exclude) => { - if (!uuid) return isNull(commits.nextCommitId) +const selectCondition = (uuid?: Exclude) => { + if (!uuid) return isNull(commits.nextCommitId) - return eq(commits.uuid, uuid) - } + return eq(commits.uuid, uuid) +} +export async function findCommit({ uuid }: { uuid?: string }, tx = database) { if (uuid === HEAD_COMMIT) { - return ( - await tx.select().from(commits).orderBy(desc(commits.id)).limit(1) - )[0] + return tx.query.commits.findFirst({ orderBy: desc(commits.id) }) } - return ( - await tx - .select({ id: commits.id }) - .from(commits) - .where(selectCondition(uuid)) - .limit(1) - )[0] + return tx.query.commits.findFirst({ where: selectCondition(uuid) }) } export async function listCommits() { diff --git a/packages/core/src/data-access/documentSnapshots.ts b/packages/core/src/data-access/documentSnapshots.ts index 60522c2ae..c5699962c 100644 --- a/packages/core/src/data-access/documentSnapshots.ts +++ b/packages/core/src/data-access/documentSnapshots.ts @@ -11,7 +11,7 @@ export async function listdocumentSnapshots() { .from(documentSnapshots) .innerJoin( documentVersions, - eq(documentSnapshots.DocumentVersionId, documentVersions.id), + eq(documentSnapshots.documentVersionId, documentVersions.id), ) return documents diff --git a/packages/core/src/data-access/documentVersions.ts b/packages/core/src/data-access/documentVersions.ts index 4e978123c..e1ad4f3e3 100644 --- a/packages/core/src/data-access/documentVersions.ts +++ b/packages/core/src/data-access/documentVersions.ts @@ -23,6 +23,7 @@ export async function getDocumentsAtCommit(commitUuid: string) { .select({ id: commits.id }) .from(commits) .where(eq(commits.uuid, commitUuid)) + if (referenceCommitId.length === 0) return [] const commitIdsBeforeReferenceCommit = await database diff --git a/packages/core/src/data-access/index.ts b/packages/core/src/data-access/index.ts index 3b5920aea..f381efc40 100644 --- a/packages/core/src/data-access/index.ts +++ b/packages/core/src/data-access/index.ts @@ -1,8 +1,9 @@ import { Database } from '@latitude-data/core' +export * from './users' +export * from './projects' export * from './commits' export * from './documentSnapshots' export * from './documentVersions' -export * from './users' export type AppContext = { db: Database } diff --git a/packages/core/src/data-access/projects.ts b/packages/core/src/data-access/projects.ts new file mode 100644 index 000000000..b9f92c341 --- /dev/null +++ b/packages/core/src/data-access/projects.ts @@ -0,0 +1,31 @@ +import { database } from '$core/client' +import { Result } from '$core/lib' +import { projects } from '$core/schema' +import { and, eq } from 'drizzle-orm' + +class ProjectNotFoundError extends Error { + constructor() { + super('Project not found') + } +} + +export async function findProject({ + projectId, + workspaceId, +}: { + projectId: number + workspaceId: number +}) { + const project = await database.query.projects.findFirst({ + where: and( + eq(projects.workspaceId, workspaceId), + eq(projects.id, projectId), + ), + }) + + if (!project) { + return Result.error(new ProjectNotFoundError()) + } + + return Result.ok(project!) +} diff --git a/packages/core/src/schema/index.ts b/packages/core/src/schema/index.ts index b8c1cea6f..b4b6e3087 100644 --- a/packages/core/src/schema/index.ts +++ b/packages/core/src/schema/index.ts @@ -1,12 +1,18 @@ export * from './db-schema' + +// Closure tree hierarchy +// More info: https://fueled.com/the-cache/posts/backend/closure-table/ +export * from './models/documentHierarchies' + +// Tenancy tables +export * from './models/users' +export * from './models/sessions' +export * from './models/workspaces' +export * from './models/memberships' export * from './models/apiKeys' -export * from './models/apiKeys' + +// Document tables +export * from './models/projects' export * from './models/commits' -export * from './models/documentHierarchies' export * from './models/documentSnapshots' export * from './models/documentVersions' -export * from './models/memberships' -export * from './models/sessions' -export * from './models/types' -export * from './models/users' -export * from './models/workspaces' diff --git a/packages/core/src/schema/models/commits.ts b/packages/core/src/schema/models/commits.ts index 80dbf10e7..a40dd5418 100644 --- a/packages/core/src/schema/models/commits.ts +++ b/packages/core/src/schema/models/commits.ts @@ -9,13 +9,7 @@ import { varchar, } from 'drizzle-orm/pg-core' -import { - DocumentSnapshot, - documentSnapshots, - latitudeSchema, - Workspace, - workspaces, -} from '..' +import { documentSnapshots, latitudeSchema, projects, workspaces } from '..' import { timestamps } from '../schemaHelpers' export const commits = latitudeSchema.table( @@ -32,7 +26,7 @@ export const commits = latitudeSchema.table( ), title: varchar('title', { length: 256 }), description: text('description'), - workspaceId: bigint('workspace_id', { mode: 'number' }) + projectId: bigint('project_id', { mode: 'number' }) .notNull() .references(() => workspaces.id, { onDelete: 'cascade' }), ...timestamps(), @@ -44,10 +38,10 @@ export const commits = latitudeSchema.table( export const commitRelations = relations(commits, ({ one, many }) => ({ snapshots: many(documentSnapshots, { relationName: 'snapshots' }), - workspace: one(workspaces), + project: one(projects, { + fields: [commits.projectId], + references: [projects.id], + }), })) -export type Commit = InferSelectModel & { - snapshots: DocumentSnapshot[] - workspace: Workspace -} +export type Commit = InferSelectModel diff --git a/packages/core/src/schema/models/documentSnapshots.ts b/packages/core/src/schema/models/documentSnapshots.ts index e0e513ec7..b9765a0ad 100644 --- a/packages/core/src/schema/models/documentSnapshots.ts +++ b/packages/core/src/schema/models/documentSnapshots.ts @@ -17,7 +17,7 @@ export const documentSnapshots = latitudeSchema.table( commitId: bigint('commit_id', { mode: 'number' }) .references((): AnyPgColumn => commits.id, { onDelete: 'restrict' }) .notNull(), - DocumentVersionId: bigint('document_version_id', { mode: 'number' }) + documentVersionId: bigint('document_version_id', { mode: 'number' }) .references((): AnyPgColumn => documentVersions.id, { onDelete: 'restrict', }) @@ -26,8 +26,8 @@ export const documentSnapshots = latitudeSchema.table( }, (doc) => ({ commitIdx: index('prompt_commit_idx').on(doc.commitId), - DocumentVersionIdx: index('document_snapshot_document_version_idx').on( - doc.DocumentVersionId, + documentVersionIdx: index('document_snapshot_document_version_idx').on( + doc.documentVersionId, ), }), ) @@ -41,7 +41,7 @@ export const documentSnapshotRelations = relations( references: [commits.id], }), version: one(documentVersions, { - fields: [documentSnapshots.DocumentVersionId], + fields: [documentSnapshots.documentVersionId], references: [documentVersions.id], }), }), diff --git a/packages/core/src/schema/models/documentVersions.ts b/packages/core/src/schema/models/documentVersions.ts index 661d3cc19..2d8786345 100644 --- a/packages/core/src/schema/models/documentVersions.ts +++ b/packages/core/src/schema/models/documentVersions.ts @@ -1,9 +1,9 @@ +import { DocumentType } from '$core/constants' import { InferSelectModel, relations } from 'drizzle-orm' import { AnyPgColumn, bigint, bigserial, - pgEnum, text, timestamp, uuid, @@ -14,9 +14,8 @@ import { z } from 'zod' import { latitudeSchema } from '../db-schema' import { timestamps } from '../schemaHelpers' import { commits } from './commits' -import { DocumentType } from './types' -export const documentTypeEnum = pgEnum('document_type', [ +export const documentTypeEnum = latitudeSchema.enum('document_type', [ DocumentType.Document, DocumentType.Folder, ]) diff --git a/packages/core/src/schema/models/projects.ts b/packages/core/src/schema/models/projects.ts new file mode 100644 index 000000000..16395fd5d --- /dev/null +++ b/packages/core/src/schema/models/projects.ts @@ -0,0 +1,29 @@ +import { InferSelectModel, relations } from 'drizzle-orm' +import { bigint, bigserial, index, varchar } from 'drizzle-orm/pg-core' + +import { latitudeSchema, workspaces } from '..' +import { timestamps } from '../schemaHelpers' + +export const projects = latitudeSchema.table( + 'projects', + { + id: bigserial('id', { mode: 'number' }).notNull().primaryKey(), + name: varchar('name', { length: 256 }).notNull(), + workspaceId: bigint('workspace_id', { mode: 'number' }) + .notNull() + .references(() => workspaces.id, { onDelete: 'cascade' }), + ...timestamps(), + }, + (table) => ({ + nextCommitIdx: index('workspace_idx').on(table.workspaceId), + }), +) + +export const projectRelations = relations(projects, ({ one }) => ({ + workspace: one(workspaces, { + fields: [projects.workspaceId], + references: [workspaces.id], + }), +})) + +export type Project = InferSelectModel diff --git a/packages/core/src/schema/models/types.ts b/packages/core/src/schema/models/types.ts deleted file mode 100644 index 646feb9bd..000000000 --- a/packages/core/src/schema/models/types.ts +++ /dev/null @@ -1,4 +0,0 @@ -export enum DocumentType { - Document = 'document', - Folder = 'folder', -} diff --git a/packages/core/src/services/commits/create.ts b/packages/core/src/services/commits/create.ts index c2338d85e..fcf2ba803 100644 --- a/packages/core/src/services/commits/create.ts +++ b/packages/core/src/services/commits/create.ts @@ -1,16 +1,23 @@ -import { Commit, commits, database, Result } from '@latitude-data/core' +import { + Commit, + commits, + Database, + database, + Result, + Transaction, +} from '@latitude-data/core' -export default async function createCommit(tx = database) { - try { - return Result.ok( - ( - await tx - .insert(commits) - .values({} as Commit) - .returning() - )[0], - ) - } catch (err) { - return Result.error(err as Error) - } +export default async function createCommit({ + projectId, + db = database, +}: { + projectId: number + db?: Database +}) { + return Transaction.call(async (tx) => { + const result = await tx.insert(commits).values({ projectId }).returning() + const commit = result[0] + + return Result.ok(commit!) + }, db) } diff --git a/packages/core/src/services/documentVersions/create.ts b/packages/core/src/services/documentVersions/create.ts index bdecda4ff..0635c86d0 100644 --- a/packages/core/src/services/documentVersions/create.ts +++ b/packages/core/src/services/documentVersions/create.ts @@ -1,63 +1,67 @@ import { - database, + DocumentVersion, documentVersions, findCommit, Result, + Transaction, type DocumentType, } from '@latitude-data/core' import createCommit from '../commits/create' +function createDocument({ + name, + commitId, + parentId, + documentType, +}: { + name: string + commitId: number + parentId?: number + documentType?: DocumentType +}) { + return Transaction.call(async (tx) => { + const result = await tx + .insert(documentVersions) + .values({ + name, + commitId, + parentId, + documentType, + }) + .returning() + const documentVersion = result[0] + return Result.ok(documentVersion!) + }) +} + export async function createDocumentVersion({ + projectId, name, commitUuid, documentType, parentId, }: { + projectId: number name: string commitUuid: string documentType?: DocumentType parentId?: number }) { - const data = { name, parentId, documentType } + let commit = await findCommit({ uuid: commitUuid }) + return Transaction.call(async (tx) => { + if (!commit) { + const resultCommit = await createCommit({ projectId, db: tx }) + if (resultCommit.error) return resultCommit - return database.transaction(async (tx) => { - const foundCommit = await findCommit({ uuid: commitUuid }) - if (foundCommit) { - try { - return Result.ok( - ( - await tx - .insert(documentVersions) - .values({ - ...data, - commitId: foundCommit.id, - }) - .returning() - )[0], - ) - } catch (err) { - return Result.error(err as Error) - } + commit = resultCommit.value } - const res = await createCommit(tx) - if (!res.ok) return res - - try { - return Result.ok( - ( - await tx - .insert(documentVersions) - .values({ - ...data, - commitId: res.unwrap()!.id, - }) - .returning() - )[0], - ) - } catch (err) { - return Result.error(err as Error) - } + return createDocument({ + name, + commitId: commit.id, + parentId, + documentType, + }) }) } diff --git a/packages/core/src/services/documentVersions/materializeAtCommit.ts b/packages/core/src/services/documentVersions/materializeAtCommit.ts index 07045a5a0..a7490097e 100644 --- a/packages/core/src/services/documentVersions/materializeAtCommit.ts +++ b/packages/core/src/services/documentVersions/materializeAtCommit.ts @@ -1,12 +1,11 @@ import { uniqBy } from 'lodash-es' +import { HEAD_COMMIT } from '$core/constants' import { getDocumentsAtCommit, listdocumentSnapshots, listStagedDocuments, -} from '@latitude-data/core' - -export const HEAD_COMMIT = 'HEAD' +} from '$core/data-access' export async function materializeDocumentsAtCommit({ commitUuid = HEAD_COMMIT,