diff --git a/apps/api/src/common/Paths.ts b/apps/api/src/common/Paths.ts index 5d2976fe9..32956d395 100644 --- a/apps/api/src/common/Paths.ts +++ b/apps/api/src/common/Paths.ts @@ -8,9 +8,9 @@ export default { Base: '/api', V1: { Base: '/v1', - Chat: { - Base: '/chat', - Completions: '/completions', + Commits: { + Base: '/commits', + Prompt: '/:commitUuid/*', }, }, }, diff --git a/apps/api/src/common/RouteError.ts b/apps/api/src/common/RouteError.ts index a9b5a8888..11d4cc2ab 100644 --- a/apps/api/src/common/RouteError.ts +++ b/apps/api/src/common/RouteError.ts @@ -1,4 +1,4 @@ -import HttpStatusCodes from '@src/common/HttpStatusCodes' +import HttpStatusCodes from '$src/common/HttpStatusCodes' /** * Error with status code and message diff --git a/apps/api/src/index.ts b/apps/api/src/index.ts index 896e196ba..2c41dae16 100644 --- a/apps/api/src/index.ts +++ b/apps/api/src/index.ts @@ -1,7 +1,9 @@ -import EnvVars from '@src/common/EnvVars' +import EnvVars from '$src/common/EnvVars' import server from './server' const SERVER_START_MSG = 'Express server started on port: ' + EnvVars.PORT -server.listen(EnvVars.PORT, () => console.info(SERVER_START_MSG)) +server.listen(EnvVars.PORT, () => { + console.info(SERVER_START_MSG) +}) diff --git a/apps/api/src/middlewares/validate.ts b/apps/api/src/middlewares/validate.ts index 78030723c..dec7503e1 100644 --- a/apps/api/src/middlewares/validate.ts +++ b/apps/api/src/middlewares/validate.ts @@ -1,5 +1,5 @@ import { Result } from '@latitude-data/core' -import { UnprocessableEntityError } from '@src/common/errors' +import { UnprocessableEntityError } from '$src/common/errors' import { NextFunction, Request, Response } from 'express' import { ZodError, ZodSchema } from 'zod' diff --git a/apps/api/src/routes/api/index.ts b/apps/api/src/routes/api/index.ts index 548c85834..e9372ff7b 100644 --- a/apps/api/src/routes/api/index.ts +++ b/apps/api/src/routes/api/index.ts @@ -1,4 +1,4 @@ -import Paths from '@src/common/Paths' +import Paths from '$src/common/Paths' import { Router } from 'express' import v1Router from './v1' diff --git a/apps/api/src/routes/api/v1/chat/completions.test.ts b/apps/api/src/routes/api/v1/chat/completions.test.ts deleted file mode 100644 index 2dc85e0de..000000000 --- a/apps/api/src/routes/api/v1/chat/completions.test.ts +++ /dev/null @@ -1,31 +0,0 @@ -import HttpStatusCodes from '@src/common/HttpStatusCodes' -import app from '@src/server' -import { Response } from 'express' -import supertest, { Test } from 'supertest' -import TestAgent from 'supertest/lib/agent' -import apiCb from 'test/support/apiCb' -import Paths from 'test/support/Paths' -import { beforeAll, describe, expect, it } from 'vitest' - -// Tests -describe('CompletionsRouter', () => { - let agent: TestAgent - - beforeAll(() => { - agent = supertest.agent(app) - }) - - describe(`"GET:${Paths.Api.V1.Chat.Completions}"`, () => { - const api = (cb: Function) => - agent.get(Paths.Api.V1.Chat.Completions).end(apiCb(cb)) - - // Success - it('should return 200', () => - new Promise((done) => { - api((res: Response) => { - expect(res.status).toBe(HttpStatusCodes.OK) - done() - }) - })) - }) -}) diff --git a/apps/api/src/routes/api/v1/chat/index.ts b/apps/api/src/routes/api/v1/chat/index.ts deleted file mode 100644 index f0a161364..000000000 --- a/apps/api/src/routes/api/v1/chat/index.ts +++ /dev/null @@ -1,10 +0,0 @@ -import Paths from '@src/common/Paths' -import { Router } from 'express' - -import { completions } from './routes' - -const router = Router() - -router.use(Paths.Api.V1.Chat.Completions, completions) - -export default router diff --git a/apps/api/src/routes/api/v1/chat/routes.ts b/apps/api/src/routes/api/v1/chat/routes.ts deleted file mode 100644 index 2d481fc73..000000000 --- a/apps/api/src/routes/api/v1/chat/routes.ts +++ /dev/null @@ -1,6 +0,0 @@ -import HttpStatusCodes from '@src/common/HttpStatusCodes' -import { Request, Response } from 'express' - -export function completions(_: Request, res: Response) { - return res.status(HttpStatusCodes.OK).json() -} diff --git a/apps/api/src/routes/api/v1/commits/index.ts b/apps/api/src/routes/api/v1/commits/index.ts new file mode 100644 index 000000000..aa8412b38 --- /dev/null +++ b/apps/api/src/routes/api/v1/commits/index.ts @@ -0,0 +1,10 @@ +import Paths from '$src/common/Paths' +import { Router } from 'express' + +import { promptRoute } from './routes' + +const router = Router() + +router.get(Paths.Api.V1.Commits.Prompt, promptRoute) + +export default router diff --git a/apps/api/src/routes/api/v1/commits/routes.ts b/apps/api/src/routes/api/v1/commits/routes.ts new file mode 100644 index 000000000..45213bd71 --- /dev/null +++ b/apps/api/src/routes/api/v1/commits/routes.ts @@ -0,0 +1,39 @@ +import { materializeDocumentsAtCommit, Node, toTree } from '@latitude-data/core' +import HttpStatusCodes from '$src/common/HttpStatusCodes' +import { BadRequestError, NotFoundError } from '$core/lib/errors' +import { Request, Response } from 'express' + +function findNode({ rootNode, path }: { rootNode: Node; path: string }) { + const pathParts = path.split('/') + + let currentNode = rootNode + let currentPart = pathParts.shift() + while (currentPart) { + const child = currentNode.children.find( + // NOTE: sanitaze name before comparing + (child) => child.doc?.name === currentPart, + ) + if (!child) return null + + currentNode = child + currentPart = pathParts.shift() + } + + return currentNode +} + +export async function promptRoute(req: Request, res: Response) { + const commitUuid = req.params.commitUuid + const path = req.params[0] + if (!path) throw new BadRequestError('Invalid prompt path') + + const result = await materializeDocumentsAtCommit({ commitUuid: commitUuid! }) + const rootNode = toTree(result.unwrap()) + const node = findNode({ rootNode, path }) + + if (!node) { + throw new NotFoundError('Prompt not found') + } else { + return res.status(HttpStatusCodes.OK).json(node.doc) + } +} diff --git a/apps/api/src/routes/api/v1/index.ts b/apps/api/src/routes/api/v1/index.ts index d2db7c9b5..b30e78734 100644 --- a/apps/api/src/routes/api/v1/index.ts +++ b/apps/api/src/routes/api/v1/index.ts @@ -1,10 +1,10 @@ -import Paths from '@src/common/Paths' +import Paths from '$src/common/Paths' import { Router } from 'express' -import chatRouter from './chat' +import commitsRouter from './commits' const router = Router() -router.use(Paths.Api.V1.Chat.Base, chatRouter) +router.use(Paths.Api.V1.Commits.Base, commitsRouter) export default router diff --git a/apps/api/src/routes/index.ts b/apps/api/src/routes/index.ts index 8a071e423..d9f75e3f9 100644 --- a/apps/api/src/routes/index.ts +++ b/apps/api/src/routes/index.ts @@ -1,4 +1,4 @@ -import Paths from '@src/common/Paths' +import Paths from '$src/common/Paths' import { Router } from 'express' import apiRouter from './api' diff --git a/apps/api/src/server.ts b/apps/api/src/server.ts index 1326a4529..883c79975 100644 --- a/apps/api/src/server.ts +++ b/apps/api/src/server.ts @@ -8,10 +8,10 @@ import morgan from 'morgan' import 'express-async-errors' -import EnvVars from '@src/common/EnvVars' -import HttpStatusCodes from '@src/common/HttpStatusCodes' -import { NodeEnvs } from '@src/common/misc' -import BaseRouter from '@src/routes' +import EnvVars from '$src/common/EnvVars' +import HttpStatusCodes from '$src/common/HttpStatusCodes' +import { NodeEnvs } from '$src/common/misc' +import BaseRouter from '$src/routes' import { LatitudeError, UnprocessableEntityError } from './common/errors' @@ -35,10 +35,6 @@ app.use('/', BaseRouter) // Add error handler app.use((err: Error, _: Request, res: Response, __: NextFunction) => { - if (EnvVars.NODE_ENV !== NodeEnvs.Test.valueOf()) { - console.error(err.message, true) - } - if (err instanceof UnprocessableEntityError) { return res.status(err.statusCode).json({ name: err.name, diff --git a/apps/api/src/services/elastic/client.ts b/apps/api/src/services/elastic/client.ts index 174850ace..5e8dd07d3 100644 --- a/apps/api/src/services/elastic/client.ts +++ b/apps/api/src/services/elastic/client.ts @@ -1,5 +1,5 @@ import { Client } from '@elastic/elasticsearch' -import EnvVars from '@src/common/EnvVars' +import EnvVars from '$src/common/EnvVars' export default new Client({ node: EnvVars.ELASTIC_URL, diff --git a/apps/api/test/support/Paths.ts b/apps/api/test/support/Paths.ts index 7390da496..a7484d59a 100644 --- a/apps/api/test/support/Paths.ts +++ b/apps/api/test/support/Paths.ts @@ -2,7 +2,7 @@ * Convert paths to full for testing. */ -import Paths from '@src/common/Paths' +import Paths from '$src/common/Paths' import jetPaths from 'jet-paths' export default jetPaths(Paths) diff --git a/apps/api/tsconfig.json b/apps/api/tsconfig.json index 435934330..92f9c48c0 100644 --- a/apps/api/tsconfig.json +++ b/apps/api/tsconfig.json @@ -4,7 +4,7 @@ "outDir": "dist", "baseUrl": "./", "paths": { - "@src/*": ["src/*"], + "$src/*": ["src/*"], "$core/*": ["../../packages/core/src/*"] }, "useUnknownInCatchVariables": false diff --git a/apps/web/src/actions/commits/create.ts b/apps/web/src/actions/commits/create.ts new file mode 100644 index 000000000..7a381a597 --- /dev/null +++ b/apps/web/src/actions/commits/create.ts @@ -0,0 +1,12 @@ +'use server' + +import { createCommit } from '@latitude-data/core' + +import { withProject } from '../procedures' + +export const createCommitAction = withProject + .createServerAction() + .handler(async ({ input }) => { + const result = await createCommit({ projectId: input.projectId }) + return result.unwrap() + }) diff --git a/apps/web/src/actions/commits/fetch.ts b/apps/web/src/actions/commits/fetch.ts new file mode 100644 index 000000000..c0e5424b8 --- /dev/null +++ b/apps/web/src/actions/commits/fetch.ts @@ -0,0 +1,9 @@ +'use server' + +import { listCommits } from '@latitude-data/core' + +import { withProject } from '../procedures' + +export const getCommitsAction = withProject + .createServerAction() + .handler(() => listCommits()) diff --git a/apps/web/src/actions/documents/fetch.ts b/apps/web/src/actions/documents/fetch.ts new file mode 100644 index 000000000..02da2117c --- /dev/null +++ b/apps/web/src/actions/documents/fetch.ts @@ -0,0 +1,17 @@ +'use server' + +import { materializeDocumentsAtCommit } from '@latitude-data/core' +import { z } from 'zod' + +import { withProject } from '../procedures' + +export const getDocumentsAtCommitAction = withProject + .createServerAction() + .input(z.object({ commitUuid: z.string() })) + .handler(async ({ input }) => { + const result = await materializeDocumentsAtCommit({ + commitUuid: input.commitUuid, + }) + + return result.unwrap() + }) diff --git a/apps/web/src/actions/procedures/index.ts b/apps/web/src/actions/procedures/index.ts index 70c25bc8e..cca9af8c6 100644 --- a/apps/web/src/actions/procedures/index.ts +++ b/apps/web/src/actions/procedures/index.ts @@ -16,11 +16,12 @@ export const authProcedure = createServerActionProcedure().handler(async () => { 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() + const res = await findProject({ + projectId: input.projectId, + workspaceId: ctx.workspace.id, + }) + + const project = res.unwrap() + return { ...ctx, project } }) diff --git a/apps/web/src/components/Sidebar/DocumentTree/index.tsx b/apps/web/src/components/Sidebar/DocumentTree/index.tsx index 8676c76de..2f48ba4cd 100644 --- a/apps/web/src/components/Sidebar/DocumentTree/index.tsx +++ b/apps/web/src/components/Sidebar/DocumentTree/index.tsx @@ -1,11 +1,17 @@ 'use client' import { faker } from '@faker-js/faker' -import type { DocumentType, DocumentVersion } from '@latitude-data/core' +import type { + Commit, + DocumentType, + DocumentVersion, + Node, +} from '@latitude-data/core' import { useCurrentCommit, useCurrentProject } from '@latitude-data/web-ui' +import useCommits from '$/stores/commits' import useDocumentVersions from '$/stores/documentVersions' -import { Node, useTree } from '../toTree' +import { useTree } from './useTree' function generateName() { return faker.science.chemicalElement().name @@ -91,17 +97,37 @@ function TreeNode({ node, level = 0 }: { node: Node; level?: number }) { } export default function DocumentTree({ + commits: serverCommits, documents: serverDocuments, }: { + commits: Commit[] documents: DocumentVersion[] }) { const { commit } = useCurrentCommit() const { project } = useCurrentProject() - const { documents } = useDocumentVersions( + const { data: documents } = useDocumentVersions( { commitUuid: commit.uuid, projectId: project.id }, { fallbackData: serverDocuments }, ) + const { create } = useCommits( + { projectId: project.id }, + { + fallbackData: serverCommits, + }, + ) + const rootNode = useTree({ documents }) - return + return ( +
+ +
+
+

Create document

+ +
+ +
+
+ ) } diff --git a/apps/web/src/components/Sidebar/DocumentTree/useTree.ts b/apps/web/src/components/Sidebar/DocumentTree/useTree.ts new file mode 100644 index 000000000..d7ca14496 --- /dev/null +++ b/apps/web/src/components/Sidebar/DocumentTree/useTree.ts @@ -0,0 +1,7 @@ +import { useMemo } from 'react' + +import { DocumentVersion, toTree } from '@latitude-data/core' + +export function useTree({ documents }: { documents: DocumentVersion[] }) { + return useMemo(() => toTree(documents), [documents]) +} diff --git a/apps/web/src/components/Sidebar/index.tsx b/apps/web/src/components/Sidebar/index.tsx index 82a93b76a..3f3c004c8 100644 --- a/apps/web/src/components/Sidebar/index.tsx +++ b/apps/web/src/components/Sidebar/index.tsx @@ -1,29 +1,14 @@ -import { materializeDocumentsAtCommit } from '@latitude-data/core' +import { listCommits, materializeDocumentsAtCommit } from '@latitude-data/core' -import DocumentTree, { CreateNode } from './DocumentTree' +import DocumentTree from './DocumentTree' -export default async function Sidebar({ - commitUuid, - projectId, -}: { - commitUuid: string - projectId: number -}) { - const documentsResult = await materializeDocumentsAtCommit({ - projectId, +export default async function Sidebar({ commitUuid }: { commitUuid: string }) { + const docsResult = await materializeDocumentsAtCommit({ commitUuid, }) - const documents = documentsResult.unwrap() - return ( -
-
-

Prompts

-
- -
-
- -
- ) + // TODO: wrap data-access reads in transaction blocks and make use of result + const commits = await listCommits() + + return } diff --git a/apps/web/src/stores/commits.ts b/apps/web/src/stores/commits.ts new file mode 100644 index 000000000..71426c2cd --- /dev/null +++ b/apps/web/src/stores/commits.ts @@ -0,0 +1,39 @@ +import { useCallback } from 'react' + +import type { Commit } from '@latitude-data/core' +import { createCommitAction } from '$/actions/commits/create' +import { getCommitsAction } from '$/actions/commits/fetch' +import useSWR, { SWRConfiguration } from 'swr' +import { useServerAction } from 'zsa-react' + +export default function useCommits( + { projectId }: { projectId: number }, + opts?: SWRConfiguration, +) { + const key = '/api/commits' + const { execute } = useServerAction(createCommitAction) + const { + data = [], + mutate, + ...rest + } = useSWR( + key, + async () => { + const [commits] = await getCommitsAction({ projectId }) + return commits! + }, + opts, + ) + + const create = useCallback(async () => { + const [commit] = await execute({ + projectId: 1, + }) + + mutate([...data, commit as Commit]) + + return commit + }, [execute]) + + return { data, create, ...rest } +} diff --git a/apps/web/src/stores/documentVersions.ts b/apps/web/src/stores/documentVersions.ts index 8b8cda338..77a43af4e 100644 --- a/apps/web/src/stores/documentVersions.ts +++ b/apps/web/src/stores/documentVersions.ts @@ -5,8 +5,8 @@ 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 { getDocumentsAtCommitAction } from '$/actions/documents/fetch' import useSWR, { SWRConfiguration } from 'swr' -import { useServerAction } from 'zsa-react' export default function useDocumentVersions( { @@ -20,35 +20,57 @@ export default function useDocumentVersions( ) { const key = `/api/projects/${projectId}/commits/${commitUuid ?? HEAD_COMMIT}/documents` - const { mutate, data, ...rest } = useSWR( + const { + mutate, + data = [], + ...rest + } = useSWR( key, - (url: string) => fetch(url).then((res) => res.json()), + async () => { + const [data, err] = await getDocumentsAtCommitAction({ + projectId, + commitUuid, + }) + + if (err) { + console.error(err) + + return [] + } + + console.log(data) + + return data! + }, opts, ) - const documents = data ?? [] - const { execute } = useServerAction(createDocumentVersionAction) const create = useCallback( async (payload: { name: string documentType?: DocumentType parentId?: number }) => { - const [document] = await execute({ + const [document, err] = await createDocumentVersionAction({ ...payload, projectId, name: payload.name!, commitUuid: commitUuid || HEAD_COMMIT, }) - const prev = documents ?? [] + + if (err) { + console.error(err) + + return + } if (document) { - mutate([...prev, document]) + mutate([...data, document]) } return document }, - [execute, mutate, documents], + [mutate, data], ) - return { ...rest, key, documents, create, mutate } + return { ...rest, key, data, create, mutate } } diff --git a/packages/core/drizzle/0000_nice_daimon_hellstrom.sql b/packages/core/drizzle/0000_nice_daimon_hellstrom.sql deleted file mode 100644 index a8fe5a34e..000000000 --- a/packages/core/drizzle/0000_nice_daimon_hellstrom.sql +++ /dev/null @@ -1,10 +0,0 @@ -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/0002_windy_wind_dancer.sql b/packages/core/drizzle/0000_secret_argent.sql similarity index 55% rename from packages/core/drizzle/0002_windy_wind_dancer.sql rename to packages/core/drizzle/0000_secret_argent.sql index 91dae5bd0..a895eeaf3 100644 --- a/packages/core/drizzle/0002_windy_wind_dancer.sql +++ b/packages/core/drizzle/0000_secret_argent.sql @@ -1,9 +1,65 @@ +CREATE SCHEMA "latitude"; +--> statement-breakpoint DO $$ BEGIN CREATE TYPE "latitude"."document_type" AS ENUM('document', 'folder'); EXCEPTION WHEN duplicate_object THEN null; END $$; --> 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"."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 CREATE TABLE IF NOT EXISTS "latitude"."projects" ( "id" bigserial PRIMARY KEY NOT NULL, "name" varchar(256) NOT NULL, @@ -46,6 +102,36 @@ CREATE TABLE IF NOT EXISTS "latitude"."document_versions" ( "updated_at" timestamp DEFAULT now() NOT NULL ); --> 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 DO $$ BEGIN 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 @@ -88,6 +174,7 @@ 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 diff --git a/packages/core/drizzle/0001_living_madripoor.sql b/packages/core/drizzle/0001_living_madripoor.sql deleted file mode 100644 index 3409b43f3..000000000 --- a/packages/core/drizzle/0001_living_madripoor.sql +++ /dev/null @@ -1,76 +0,0 @@ -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/0003_cold_spirit.sql b/packages/core/drizzle/0001_yummy_carmella_unuscione.sql similarity index 99% rename from packages/core/drizzle/0003_cold_spirit.sql rename to packages/core/drizzle/0001_yummy_carmella_unuscione.sql index 3172dba49..4972bf03f 100644 --- a/packages/core/drizzle/0003_cold_spirit.sql +++ b/packages/core/drizzle/0001_yummy_carmella_unuscione.sql @@ -75,3 +75,4 @@ 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/0002_living_silver_samurai.sql b/packages/core/drizzle/0002_living_silver_samurai.sql new file mode 100644 index 000000000..c4367fcd3 --- /dev/null +++ b/packages/core/drizzle/0002_living_silver_samurai.sql @@ -0,0 +1 @@ +ALTER TABLE "latitude"."document_versions" ADD COLUMN "merged_at" timestamp; \ No newline at end of file diff --git a/packages/core/drizzle/0003_curious_anthem.sql b/packages/core/drizzle/0003_curious_anthem.sql new file mode 100644 index 000000000..0e423ed25 --- /dev/null +++ b/packages/core/drizzle/0003_curious_anthem.sql @@ -0,0 +1,4 @@ +ALTER TABLE "latitude"."commits" ADD COLUMN "merged_at" timestamp;--> statement-breakpoint +ALTER TABLE "latitude"."commits" DROP COLUMN IF EXISTS "title";--> statement-breakpoint +ALTER TABLE "latitude"."commits" DROP COLUMN IF EXISTS "description";--> statement-breakpoint +ALTER TABLE "latitude"."document_versions" DROP COLUMN IF EXISTS "merged_at"; \ No newline at end of file diff --git a/packages/core/drizzle/0005_fair_karnak.sql b/packages/core/drizzle/0005_fair_karnak.sql new file mode 100644 index 000000000..b7d9ee8b9 --- /dev/null +++ b/packages/core/drizzle/0005_fair_karnak.sql @@ -0,0 +1 @@ +DROP TABLE "latitude"."document_snapshots"; \ No newline at end of file diff --git a/packages/core/drizzle/meta/0000_snapshot.json b/packages/core/drizzle/meta/0000_snapshot.json index 77f94abda..9d53aaa15 100644 --- a/packages/core/drizzle/meta/0000_snapshot.json +++ b/packages/core/drizzle/meta/0000_snapshot.json @@ -1,5 +1,5 @@ { - "id": "60f41b01-d3ec-4de1-82b1-3e7ec942c75a", + "id": "71b94dcd-cf78-4053-a436-ce78f7b7cb6e", "prevId": "00000000-0000-0000-0000-000000000000", "version": "7", "dialect": "postgresql", @@ -51,9 +51,753 @@ "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" + ] } }, - "enums": {}, "schemas": { "latitude": "latitude" }, diff --git a/packages/core/drizzle/meta/0001_snapshot.json b/packages/core/drizzle/meta/0001_snapshot.json index bea4b57c2..06efc59e0 100644 --- a/packages/core/drizzle/meta/0001_snapshot.json +++ b/packages/core/drizzle/meta/0001_snapshot.json @@ -1,6 +1,6 @@ { - "id": "cdf027b2-ec77-4802-8085-bfdcde91f089", - "prevId": "60f41b01-d3ec-4de1-82b1-3e7ec942c75a", + "id": "606e730c-c777-43ae-a4a7-9f6ac099fd16", + "prevId": "71b94dcd-cf78-4053-a436-ce78f7b7cb6e", "version": "7", "dialect": "postgresql", "tables": { @@ -101,10 +101,10 @@ "uniqueConstraints": { "users_email_unique": { "name": "users_email_unique", - "nullsNotDistinct": false, "columns": [ "email" - ] + ], + "nullsNotDistinct": false } } }, @@ -150,16 +150,16 @@ "sessions_user_id_users_id_fk": { "name": "sessions_user_id_users_id_fk", "tableFrom": "sessions", - "tableTo": "users", - "schemaTo": "latitude", "columnsFrom": [ "user_id" ], + "tableTo": "users", + "schemaTo": "latitude", "columnsTo": [ "id" ], - "onDelete": "cascade", - "onUpdate": "no action" + "onUpdate": "no action", + "onDelete": "cascade" } }, "compositePrimaryKeys": {}, @@ -207,16 +207,16 @@ "workspaces_creator_id_users_id_fk": { "name": "workspaces_creator_id_users_id_fk", "tableFrom": "workspaces", - "tableTo": "users", - "schemaTo": "latitude", "columnsFrom": [ "creator_id" ], + "tableTo": "users", + "schemaTo": "latitude", "columnsTo": [ "id" ], - "onDelete": "set null", - "onUpdate": "no action" + "onUpdate": "no action", + "onDelete": "set null" } }, "compositePrimaryKeys": {}, @@ -258,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" ], - "onDelete": "cascade", - "onUpdate": "no action" + "onUpdate": "no action", + "onDelete": "cascade" }, "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" ], - "onDelete": "cascade", - "onUpdate": "no action" + "onUpdate": "no action", + "onDelete": "cascade" } }, "compositePrimaryKeys": { @@ -356,40 +356,448 @@ } ], "isUnique": false, - "concurrently": false, + "with": {}, "method": "btree", - "with": {} + "concurrently": false } }, "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" ], + "tableTo": "workspaces", + "schemaTo": "latitude", "columnsTo": [ "id" ], - "onDelete": "no action", - "onUpdate": "no action" + "onUpdate": "no action", + "onDelete": "no action" } }, "compositePrimaryKeys": {}, "uniqueConstraints": { "api_keys_uuid_unique": { "name": "api_keys_uuid_unique", - "nullsNotDistinct": false, "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" + ] } }, - "enums": {}, "schemas": { "latitude": "latitude" }, diff --git a/packages/core/drizzle/meta/0002_snapshot.json b/packages/core/drizzle/meta/0002_snapshot.json index 198d69a59..f52de35f9 100644 --- a/packages/core/drizzle/meta/0002_snapshot.json +++ b/packages/core/drizzle/meta/0002_snapshot.json @@ -1,6 +1,6 @@ { - "id": "cf3e829e-52c2-47fb-89f4-5dd28e3c3d43", - "prevId": "cdf027b2-ec77-4802-8085-bfdcde91f089", + "id": "c94e53b5-fe51-4bbb-8a09-949b6c2f0052", + "prevId": "606e730c-c777-43ae-a4a7-9f6ac099fd16", "version": "7", "dialect": "postgresql", "tables": { @@ -732,6 +732,12 @@ "primaryKey": false, "notNull": false }, + "merged_at": { + "name": "merged_at", + "type": "timestamp", + "primaryKey": false, + "notNull": false + }, "commit_id": { "name": "commit_id", "type": "bigint", diff --git a/packages/core/drizzle/meta/0003_snapshot.json b/packages/core/drizzle/meta/0003_snapshot.json index ea4a979fd..f7c405184 100644 --- a/packages/core/drizzle/meta/0003_snapshot.json +++ b/packages/core/drizzle/meta/0003_snapshot.json @@ -1,6 +1,6 @@ { - "id": "eedd6d42-c764-44b7-9e9e-2428f529d3dc", - "prevId": "cf3e829e-52c2-47fb-89f4-5dd28e3c3d43", + "id": "017fe841-6f7b-4c38-9132-6b16cb643384", + "prevId": "c94e53b5-fe51-4bbb-8a09-949b6c2f0052", "version": "7", "dialect": "postgresql", "tables": { @@ -101,10 +101,10 @@ "uniqueConstraints": { "users_email_unique": { "name": "users_email_unique", + "nullsNotDistinct": false, "columns": [ "email" - ], - "nullsNotDistinct": false + ] } } }, @@ -150,16 +150,16 @@ "sessions_user_id_users_id_fk": { "name": "sessions_user_id_users_id_fk", "tableFrom": "sessions", + "tableTo": "users", + "schemaTo": "latitude", "columnsFrom": [ "user_id" ], - "tableTo": "users", - "schemaTo": "latitude", "columnsTo": [ "id" ], - "onUpdate": "no action", - "onDelete": "cascade" + "onDelete": "cascade", + "onUpdate": "no action" } }, "compositePrimaryKeys": {}, @@ -207,16 +207,16 @@ "workspaces_creator_id_users_id_fk": { "name": "workspaces_creator_id_users_id_fk", "tableFrom": "workspaces", + "tableTo": "users", + "schemaTo": "latitude", "columnsFrom": [ "creator_id" ], - "tableTo": "users", - "schemaTo": "latitude", "columnsTo": [ "id" ], - "onUpdate": "no action", - "onDelete": "set null" + "onDelete": "set null", + "onUpdate": "no action" } }, "compositePrimaryKeys": {}, @@ -258,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": { @@ -356,35 +356,35 @@ } ], "isUnique": false, - "with": {}, + "concurrently": false, "method": "btree", - "concurrently": false + "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" ], - "tableTo": "workspaces", - "schemaTo": "latitude", "columnsTo": [ "id" ], - "onUpdate": "no action", - "onDelete": "no action" + "onDelete": "no action", + "onUpdate": "no action" } }, "compositePrimaryKeys": {}, "uniqueConstraints": { "api_keys_uuid_unique": { "name": "api_keys_uuid_unique", + "nullsNotDistinct": false, "columns": [ "uuid" - ], - "nullsNotDistinct": false + ] } } }, @@ -437,25 +437,25 @@ } ], "isUnique": false, - "with": {}, + "concurrently": false, "method": "btree", - "concurrently": false + "with": {} } }, "foreignKeys": { "projects_workspace_id_workspaces_id_fk": { "name": "projects_workspace_id_workspaces_id_fk", "tableFrom": "projects", + "tableTo": "workspaces", + "schemaTo": "latitude", "columnsFrom": [ "workspace_id" ], - "tableTo": "workspaces", - "schemaTo": "latitude", "columnsTo": [ "id" ], - "onUpdate": "no action", - "onDelete": "cascade" + "onDelete": "cascade", + "onUpdate": "no action" } }, "compositePrimaryKeys": {}, @@ -484,15 +484,9 @@ "primaryKey": false, "notNull": false }, - "title": { - "name": "title", - "type": "varchar(256)", - "primaryKey": false, - "notNull": false - }, - "description": { - "name": "description", - "type": "text", + "merged_at": { + "name": "merged_at", + "type": "timestamp", "primaryKey": false, "notNull": false }, @@ -529,49 +523,49 @@ } ], "isUnique": false, - "with": {}, + "concurrently": false, "method": "btree", - "concurrently": false + "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" ], - "tableTo": "commits", - "schemaTo": "latitude", "columnsTo": [ "id" ], - "onUpdate": "no action", - "onDelete": "restrict" + "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" ], - "tableTo": "workspaces", - "schemaTo": "latitude", "columnsTo": [ "id" ], - "onUpdate": "no action", - "onDelete": "cascade" + "onDelete": "cascade", + "onUpdate": "no action" } }, "compositePrimaryKeys": {}, "uniqueConstraints": { "commits_uuid_unique": { "name": "commits_uuid_unique", + "nullsNotDistinct": false, "columns": [ "uuid" - ], - "nullsNotDistinct": false + ] } } }, @@ -624,9 +618,9 @@ } ], "isUnique": false, - "with": {}, + "concurrently": false, "method": "btree", - "concurrently": false + "with": {} }, "document_snapshot_document_version_idx": { "name": "document_snapshot_document_version_idx", @@ -639,39 +633,39 @@ } ], "isUnique": false, - "with": {}, + "concurrently": false, "method": "btree", - "concurrently": false + "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" ], - "tableTo": "commits", - "schemaTo": "latitude", "columnsTo": [ "id" ], - "onUpdate": "no action", - "onDelete": "restrict" + "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" ], - "tableTo": "document_versions", - "schemaTo": "latitude", "columnsTo": [ "id" ], - "onUpdate": "no action", - "onDelete": "restrict" + "onDelete": "restrict", + "onUpdate": "no action" } }, "compositePrimaryKeys": {}, @@ -758,30 +752,30 @@ "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" ], - "tableTo": "document_versions", - "schemaTo": "latitude", "columnsTo": [ "id" ], - "onUpdate": "no action", - "onDelete": "cascade" + "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" ], - "tableTo": "commits", - "schemaTo": "latitude", "columnsTo": [ "id" ], - "onUpdate": "no action", - "onDelete": "no action" + "onDelete": "no action", + "onUpdate": "no action" } }, "compositePrimaryKeys": {}, diff --git a/packages/core/drizzle/meta/0004_snapshot.json b/packages/core/drizzle/meta/0004_snapshot.json index d12a8c198..a53f0bce2 100644 --- a/packages/core/drizzle/meta/0004_snapshot.json +++ b/packages/core/drizzle/meta/0004_snapshot.json @@ -102,9 +102,7 @@ "users_email_unique": { "name": "users_email_unique", "nullsNotDistinct": false, - "columns": [ - "email" - ] + "columns": ["email"] } } }, @@ -152,12 +150,8 @@ "tableFrom": "sessions", "tableTo": "users", "schemaTo": "latitude", - "columnsFrom": [ - "user_id" - ], - "columnsTo": [ - "id" - ], + "columnsFrom": ["user_id"], + "columnsTo": ["id"], "onDelete": "cascade", "onUpdate": "no action" } @@ -209,12 +203,8 @@ "tableFrom": "workspaces", "tableTo": "users", "schemaTo": "latitude", - "columnsFrom": [ - "creator_id" - ], - "columnsTo": [ - "id" - ], + "columnsFrom": ["creator_id"], + "columnsTo": ["id"], "onDelete": "set null", "onUpdate": "no action" } @@ -260,12 +250,8 @@ "tableFrom": "memberships", "tableTo": "workspaces", "schemaTo": "latitude", - "columnsFrom": [ - "workspace_id" - ], - "columnsTo": [ - "id" - ], + "columnsFrom": ["workspace_id"], + "columnsTo": ["id"], "onDelete": "cascade", "onUpdate": "no action" }, @@ -274,12 +260,8 @@ "tableFrom": "memberships", "tableTo": "users", "schemaTo": "latitude", - "columnsFrom": [ - "user_id" - ], - "columnsTo": [ - "id" - ], + "columnsFrom": ["user_id"], + "columnsTo": ["id"], "onDelete": "cascade", "onUpdate": "no action" } @@ -287,10 +269,7 @@ "compositePrimaryKeys": { "memberships_workspace_id_user_id_pk": { "name": "memberships_workspace_id_user_id_pk", - "columns": [ - "workspace_id", - "user_id" - ] + "columns": ["workspace_id", "user_id"] } }, "uniqueConstraints": {} @@ -367,12 +346,8 @@ "tableFrom": "api_keys", "tableTo": "workspaces", "schemaTo": "latitude", - "columnsFrom": [ - "workspace_id" - ], - "columnsTo": [ - "id" - ], + "columnsFrom": ["workspace_id"], + "columnsTo": ["id"], "onDelete": "no action", "onUpdate": "no action" } @@ -382,9 +357,7 @@ "api_keys_uuid_unique": { "name": "api_keys_uuid_unique", "nullsNotDistinct": false, - "columns": [ - "uuid" - ] + "columns": ["uuid"] } } }, @@ -448,12 +421,8 @@ "tableFrom": "projects", "tableTo": "workspaces", "schemaTo": "latitude", - "columnsFrom": [ - "workspace_id" - ], - "columnsTo": [ - "id" - ], + "columnsFrom": ["workspace_id"], + "columnsTo": ["id"], "onDelete": "cascade", "onUpdate": "no action" } @@ -546,12 +515,8 @@ "tableFrom": "commits", "tableTo": "projects", "schemaTo": "latitude", - "columnsFrom": [ - "project_id" - ], - "columnsTo": [ - "id" - ], + "columnsFrom": ["project_id"], + "columnsTo": ["id"], "onDelete": "cascade", "onUpdate": "no action" } @@ -561,9 +526,7 @@ "commits_uuid_unique": { "name": "commits_uuid_unique", "nullsNotDistinct": false, - "columns": [ - "uuid" - ] + "columns": ["uuid"] } } }, @@ -642,12 +605,8 @@ "tableFrom": "document_snapshots", "tableTo": "commits", "schemaTo": "latitude", - "columnsFrom": [ - "commit_id" - ], - "columnsTo": [ - "id" - ], + "columnsFrom": ["commit_id"], + "columnsTo": ["id"], "onDelete": "restrict", "onUpdate": "no action" }, @@ -656,12 +615,8 @@ "tableFrom": "document_snapshots", "tableTo": "document_versions", "schemaTo": "latitude", - "columnsFrom": [ - "document_version_id" - ], - "columnsTo": [ - "id" - ], + "columnsFrom": ["document_version_id"], + "columnsTo": ["id"], "onDelete": "restrict", "onUpdate": "no action" } @@ -752,12 +707,8 @@ "tableFrom": "document_versions", "tableTo": "document_versions", "schemaTo": "latitude", - "columnsFrom": [ - "parent_id" - ], - "columnsTo": [ - "id" - ], + "columnsFrom": ["parent_id"], + "columnsTo": ["id"], "onDelete": "cascade", "onUpdate": "no action" }, @@ -766,12 +717,8 @@ "tableFrom": "document_versions", "tableTo": "commits", "schemaTo": "latitude", - "columnsFrom": [ - "commit_id" - ], - "columnsTo": [ - "id" - ], + "columnsFrom": ["commit_id"], + "columnsTo": ["id"], "onDelete": "no action", "onUpdate": "no action" } @@ -784,10 +731,7 @@ "latitude.document_type": { "name": "document_type", "schema": "latitude", - "values": [ - "document", - "folder" - ] + "values": ["document", "folder"] } }, "schemas": { @@ -798,4 +742,4 @@ "schemas": {}, "tables": {} } -} \ No newline at end of file +} diff --git a/packages/core/drizzle/meta/0005_snapshot.json b/packages/core/drizzle/meta/0005_snapshot.json new file mode 100644 index 000000000..2d69c7386 --- /dev/null +++ b/packages/core/drizzle/meta/0005_snapshot.json @@ -0,0 +1,699 @@ +{ + "id": "bd4c979a-776a-45f0-acc9-3f809b99dd43", + "prevId": "d9cf6cd2-f6a9-41e1-8acc-984d00e7afcd", + "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()" + }, + "title": { + "name": "title", + "type": "varchar(256)", + "primaryKey": false, + "notNull": false + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "merged_at": { + "name": "merged_at", + "type": "timestamp", + "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": { + "project_commit_order_idx": { + "name": "project_commit_order_idx", + "columns": [ + { + "expression": "merged_at", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "project_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "commits_project_id_projects_id_fk": { + "name": "commits_project_id_projects_id_fk", + "tableFrom": "commits", + "tableTo": "projects", + "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_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/_journal.json b/packages/core/drizzle/meta/_journal.json index 5696de412..302720ea6 100644 --- a/packages/core/drizzle/meta/_journal.json +++ b/packages/core/drizzle/meta/_journal.json @@ -5,29 +5,29 @@ { "idx": 0, "version": "7", - "when": 1721122441285, - "tag": "0000_nice_daimon_hellstrom", + "when": 1721140912240, + "tag": "0000_secret_argent", "breakpoints": true }, { "idx": 1, "version": "7", - "when": 1721122652708, - "tag": "0001_living_madripoor", + "when": 1721140914896, + "tag": "0001_yummy_carmella_unuscione", "breakpoints": true }, { "idx": 2, "version": "7", - "when": 1721124730397, - "tag": "0002_windy_wind_dancer", + "when": 1721144280427, + "tag": "0002_living_silver_samurai", "breakpoints": true }, { "idx": 3, "version": "7", - "when": 1721124762014, - "tag": "0003_cold_spirit", + "when": 1721144395480, + "tag": "0003_curious_anthem", "breakpoints": true }, { @@ -36,6 +36,13 @@ "when": 1721290010434, "tag": "0004_cold_ben_parker", "breakpoints": true + }, + { + "idx": 5, + "version": "7", + "when": 1721371418530, + "tag": "0005_fair_karnak", + "breakpoints": true } ] } \ No newline at end of file diff --git a/packages/core/package.json b/packages/core/package.json index 4a395ee4c..ee5b99e75 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -15,7 +15,7 @@ "db:migrate:test": "NODE_ENV=test pnpm run db:migrate", "db:drop": "drizzle-kit drop", "db:check": "drizzle-kit check", - "db:studio": "drizzle-kit studio --port 3002", + "db:studio": "drizzle-kit studio --verbose", "lint": "eslint src", "tc": "tsc --noEmit", "test": "pnpm run db:migrate:test && vitest run --pool=forks", diff --git a/packages/core/src/data-access/commits.ts b/packages/core/src/data-access/commits.ts index 9cc60af27..414aed52a 100644 --- a/packages/core/src/data-access/commits.ts +++ b/packages/core/src/data-access/commits.ts @@ -26,13 +26,14 @@ export async function findHeadCommit( export type FindCommitProps = { uuid: string - projectId: number + projectId?: number } export async function findCommit( { projectId, uuid }: FindCommitProps, tx = database, ): Promise> { - if (uuid === HEAD_COMMIT) return findHeadCommit({ projectId }, tx) + if (uuid === HEAD_COMMIT && projectId) + return findHeadCommit({ projectId }, tx) const commit = await tx.query.commits.findFirst({ where: eq(commits.uuid, uuid), diff --git a/packages/core/src/data-access/documentSnapshots.ts b/packages/core/src/data-access/documentSnapshots.ts deleted file mode 100644 index c5699962c..000000000 --- a/packages/core/src/data-access/documentSnapshots.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { - database, - documentSnapshots, - documentVersions, -} from '@latitude-data/core' -import { eq } from 'drizzle-orm' - -export async function listdocumentSnapshots() { - const documents = await database - .select() - .from(documentSnapshots) - .innerJoin( - documentVersions, - eq(documentSnapshots.documentVersionId, documentVersions.id), - ) - - return documents -} diff --git a/packages/core/src/data-access/index.ts b/packages/core/src/data-access/index.ts index f381efc40..4151f9ec6 100644 --- a/packages/core/src/data-access/index.ts +++ b/packages/core/src/data-access/index.ts @@ -1,9 +1,3 @@ -import { Database } from '@latitude-data/core' - export * from './users' export * from './projects' export * from './commits' -export * from './documentSnapshots' -export * from './documentVersions' - -export type AppContext = { db: Database } diff --git a/packages/core/src/schema/index.ts b/packages/core/src/schema/index.ts index b4b6e3087..8c127ff6a 100644 --- a/packages/core/src/schema/index.ts +++ b/packages/core/src/schema/index.ts @@ -1,7 +1,5 @@ export * from './db-schema' -// Closure tree hierarchy -// More info: https://fueled.com/the-cache/posts/backend/closure-table/ export * from './models/documentHierarchies' // Tenancy tables @@ -14,5 +12,4 @@ export * from './models/apiKeys' // Document tables export * from './models/projects' export * from './models/commits' -export * from './models/documentSnapshots' export * from './models/documentVersions' diff --git a/packages/core/src/schema/models/commits.ts b/packages/core/src/schema/models/commits.ts index 3d8bf1dcb..3bd548e68 100644 --- a/packages/core/src/schema/models/commits.ts +++ b/packages/core/src/schema/models/commits.ts @@ -9,7 +9,7 @@ import { varchar, } from 'drizzle-orm/pg-core' -import { documentSnapshots, latitudeSchema, projects } from '..' +import { latitudeSchema, projects } from '..' import { timestamps } from '../schemaHelpers' export const commits = latitudeSchema.table( @@ -22,10 +22,10 @@ export const commits = latitudeSchema.table( .default(sql`gen_random_uuid()`), title: varchar('title', { length: 256 }), description: text('description'), + mergedAt: timestamp('merged_at'), projectId: bigint('project_id', { mode: 'number' }) .notNull() .references(() => projects.id, { onDelete: 'cascade' }), - mergedAt: timestamp('merged_at'), ...timestamps(), }, (table) => ({ @@ -36,8 +36,7 @@ export const commits = latitudeSchema.table( }), ) -export const commitRelations = relations(commits, ({ one, many }) => ({ - snapshots: many(documentSnapshots, { relationName: 'snapshots' }), +export const commitRelations = relations(commits, ({ one }) => ({ project: one(projects, { fields: [commits.projectId], references: [projects.id], diff --git a/packages/core/src/schema/models/documentSnapshots.ts b/packages/core/src/schema/models/documentSnapshots.ts deleted file mode 100644 index b9765a0ad..000000000 --- a/packages/core/src/schema/models/documentSnapshots.ts +++ /dev/null @@ -1,53 +0,0 @@ -import { InferSelectModel, relations } from 'drizzle-orm' -import { AnyPgColumn, bigint, bigserial, index } from 'drizzle-orm/pg-core' - -import { - Commit, - commits, - DocumentVersion, - documentVersions, - latitudeSchema, -} from '..' -import { timestamps } from '../schemaHelpers' - -export const documentSnapshots = latitudeSchema.table( - 'document_snapshots', - { - id: bigserial('id', { mode: 'number' }).notNull().primaryKey(), - commitId: bigint('commit_id', { mode: 'number' }) - .references((): AnyPgColumn => commits.id, { onDelete: 'restrict' }) - .notNull(), - documentVersionId: bigint('document_version_id', { mode: 'number' }) - .references((): AnyPgColumn => documentVersions.id, { - onDelete: 'restrict', - }) - .notNull(), - ...timestamps(), - }, - (doc) => ({ - commitIdx: index('prompt_commit_idx').on(doc.commitId), - documentVersionIdx: index('document_snapshot_document_version_idx').on( - doc.documentVersionId, - ), - }), -) - -export const documentSnapshotRelations = relations( - documentSnapshots, - ({ one }) => ({ - commit: one(commits, { - relationName: 'snapshots', - fields: [documentSnapshots.commitId], - references: [commits.id], - }), - version: one(documentVersions, { - fields: [documentSnapshots.documentVersionId], - references: [documentVersions.id], - }), - }), -) - -export type DocumentSnapshot = InferSelectModel & { - commit: Commit - version: DocumentVersion -} diff --git a/packages/core/src/services/commits/create.ts b/packages/core/src/services/commits/create.ts index 712ef82d9..d70346862 100644 --- a/packages/core/src/services/commits/create.ts +++ b/packages/core/src/services/commits/create.ts @@ -1,19 +1,15 @@ import { Commit, commits, - Database, database, Result, Transaction, } from '@latitude-data/core' -export default async function createCommit({ - commit, +export async function createCommit( + commit: Omit, 'id'>, db = database, -}: { - commit: Omit, 'id'> - db?: Database -}) { +) { return Transaction.call(async (tx) => { const result = await tx .insert(commits) diff --git a/packages/core/src/services/documentVersions/create.ts b/packages/core/src/services/documentVersions/create.ts index be8438c65..442d89a81 100644 --- a/packages/core/src/services/documentVersions/create.ts +++ b/packages/core/src/services/documentVersions/create.ts @@ -1,4 +1,5 @@ import { + database, DocumentVersion, documentVersions, findCommit, @@ -8,21 +9,24 @@ import { } from '@latitude-data/core' import { ForbiddenError } from '$core/lib/errors' -function createDocument({ - name, - commitId, - parentId, - documentType, - documentUuid, - content, -}: { - name: string - commitId: number - parentId?: number - documentType?: DocumentType - documentUuid?: string - content?: string -}) { +function createDocument( + { + name, + commitId, + parentId, + documentType, + documentUuid, + content, + }: { + name: string + commitId: number + parentId?: number + documentType?: DocumentType + documentUuid?: string + content?: string + }, + db = database, +) { return Transaction.call(async (tx) => { const result = await tx .insert(documentVersions) @@ -35,9 +39,11 @@ function createDocument({ content, }) .returning() + const documentVersion = result[0] + return Result.ok(documentVersion!) - }) + }, db) } export async function createDocumentVersion({ diff --git a/packages/core/src/services/documentVersions/index.ts b/packages/core/src/services/documentVersions/index.ts index f51d7fc92..29e571a2f 100644 --- a/packages/core/src/services/documentVersions/index.ts +++ b/packages/core/src/services/documentVersions/index.ts @@ -1,2 +1,3 @@ export * from './create' export * from './materializeAtCommit' +export * from './toTree' diff --git a/packages/core/src/services/documentVersions/materializeAtCommit.ts b/packages/core/src/services/documentVersions/materializeAtCommit.ts index e0c9f91bc..d3f99e55c 100644 --- a/packages/core/src/services/documentVersions/materializeAtCommit.ts +++ b/packages/core/src/services/documentVersions/materializeAtCommit.ts @@ -1,20 +1,30 @@ +import { database } from '$core/client' import { HEAD_COMMIT } from '$core/constants' -import { getDocumentsAtCommit, listdocumentSnapshots } from '$core/data-access' +import { findCommit } from '$core/data-access' import { Result } from '$core/lib' +import { NotFoundError } from '$core/lib/errors' +import { commits, documentVersions } from '$core/schema' +import { and, desc, eq, isNotNull, lte } from 'drizzle-orm' export async function materializeDocumentsAtCommit({ commitUuid = HEAD_COMMIT, - projectId, }: { commitUuid: string - projectId: number }) { - if (commitUuid === HEAD_COMMIT) { - const snapshots = (await listdocumentSnapshots()).map( - (snap) => snap.document_versions, - ) - return Result.ok(snapshots) - } else { - return await getDocumentsAtCommit({ commitUuid, projectId }) + const commit = await findCommit({ uuid: commitUuid }) + if (!commit || !commit.mergedAt) { + return Result.error(new NotFoundError('Commit not found')) } + + const docsInCommits = await database + .selectDistinct() + .from(documentVersions) + .innerJoin(commits, eq(commits.id, documentVersions.commitId)) + .where( + and(isNotNull(commits.mergedAt), lte(commits.mergedAt, commit.mergedAt!)), + ) + .groupBy(documentVersions.documentUuid, documentVersions.id, commits.id) + .orderBy(desc(commits.mergedAt)) + + return Result.ok(docsInCommits.map((doc) => doc.document_versions)) } diff --git a/packages/core/src/services/documentVersions/toTree.ts b/packages/core/src/services/documentVersions/toTree.ts new file mode 100644 index 000000000..4e35c7a42 --- /dev/null +++ b/packages/core/src/services/documentVersions/toTree.ts @@ -0,0 +1,39 @@ +import { DocumentVersion } from '@latitude-data/core' + +export class Node { + public doc?: DocumentVersion + public children: Node[] = [] + public isRoot: boolean = false + + constructor( + doc?: DocumentVersion, + children: Node[] = [], + isRoot: boolean = false, + ) { + this.doc = doc + this.children = children + this.isRoot = isRoot + } +} + +export 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)) + } + + node.children = children + node.children.forEach(iterate) + + return node + } + + return iterate(new Node(undefined, [], true)) +} diff --git a/packages/core/src/services/index.ts b/packages/core/src/services/index.ts index a76d75b1c..213c3ca32 100644 --- a/packages/core/src/services/index.ts +++ b/packages/core/src/services/index.ts @@ -1,3 +1,4 @@ export * from './users' export * from './workspaces' export * from './documentVersions' +export * from './commits/create'