Skip to content

Commit

Permalink
Create and modify documents
Browse files Browse the repository at this point in the history
  • Loading branch information
csansoon committed Jul 22, 2024
1 parent aa90d09 commit 3fd39fb
Show file tree
Hide file tree
Showing 22 changed files with 610 additions and 219 deletions.
11 changes: 9 additions & 2 deletions apps/web/src/actions/documents/create.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use server'

import { createDocumentVersion } from '@latitude-data/core'
import { createNewDocument, findCommitByUuid } from '@latitude-data/core'
import { z } from 'zod'

import { withProject } from '../procedures'
Expand All @@ -15,6 +15,13 @@ export const createDocumentVersionAction = withProject
{ type: 'json' },
)
.handler(async ({ input }) => {
const result = await createDocumentVersion(input)
const commit = await findCommitByUuid({
projectId: input.projectId,
uuid: input.commitUuid,
}).then((r) => r.unwrap())
const result = await createNewDocument({
commitId: commit.id,
path: input.path,
})
return result.unwrap()
})
6 changes: 3 additions & 3 deletions apps/web/src/app/(private)/_data-access/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { cache } from 'react'

import {
findCommit as originalfindCommit,
findCommitByUuid as originalfindCommit,
findProject as originalFindProject,
getFirstProject as originalGetFirstProject,
type FindCommitProps,
type FindCommitByUuidProps,
type FindProjectProps,
} from '@latitude-data/core'

Expand All @@ -27,7 +27,7 @@ export const findProject = cache(
)

export const findCommit = cache(
async ({ uuid, projectId }: FindCommitProps) => {
async ({ uuid, projectId }: FindCommitByUuidProps) => {
const result = await originalfindCommit({ uuid, projectId })
const commit = result.unwrap()

Expand Down
6 changes: 3 additions & 3 deletions apps/web/src/components/Sidebar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getDocumentsAtCommit } from '@latitude-data/core'
import { findCommitByUuid, getDocumentsAtCommit } from '@latitude-data/core'

import DocumentTree, { CreateNode } from './DocumentTree'

Expand All @@ -9,9 +9,9 @@ export default async function Sidebar({
commitUuid: string
projectId: number
}) {
const commit = await findCommitByUuid({ projectId, uuid: commitUuid })
const documentsResult = await getDocumentsAtCommit({
projectId,
commitUuid,
commitId: commit.unwrap().id,
})
const documents = documentsResult.unwrap()

Expand Down
1 change: 0 additions & 1 deletion packages/compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"rollup": "^4.10.0",
"rollup-plugin-dts": "^6.1.1",
"typescript": "^5.2.2",
"vite-tsconfig-paths": "^4.3.2",
"vitest": "^1.2.2"
}
}
13 changes: 11 additions & 2 deletions packages/compiler/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import tsconfigPaths from 'vite-tsconfig-paths'
import { dirname } from 'path'
import { fileURLToPath } from 'url'

import { defineConfig } from 'vitest/config'

const filename = fileURLToPath(import.meta.url)
const root = dirname(filename)

export default defineConfig({
plugins: [tsconfigPaths()],
resolve: {
alias: {
$compiler: `${root}/src`,
},
},
test: {
globals: true,
environment: 'node',
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"prettier": "prettier --write src/**/*.ts"
},
"dependencies": {
"@latitude-data/compiler": "workspace:^",
"@latitude-data/env": "workspace:^",
"@t3-oss/env-core": "^0.10.1",
"bcrypt": "^5.1.1",
Expand All @@ -41,7 +42,6 @@
"eslint-plugin-drizzle": "^0.2.3",
"pg-transactional-tests": "^1.0.9",
"supertest": "^7.0.0",
"vite-tsconfig-paths": "^4.3.2",
"vitest": "^2.0.3"
}
}
39 changes: 12 additions & 27 deletions packages/core/src/data-access/commits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ export async function findHeadCommit(
return Result.ok(headCommit)
}

export type FindCommitProps = {
export type FindCommitByUuidProps = {
uuid: string
projectId?: number
}
export async function findCommit(
{ projectId, uuid }: FindCommitProps,
export async function findCommitByUuid(
{ projectId, uuid }: FindCommitByUuidProps,
tx = database,
): Promise<TypedResult<Commit, LatitudeError>> {
if (uuid === HEAD_COMMIT) {
Expand All @@ -49,34 +49,19 @@ export async function findCommit(
return Result.ok(commit)
}

export async function listCommits() {
return database.select().from(commits)
}

export async function getCommitMergedAt(
{ projectId, commitUuid }: { projectId: number; commitUuid: string },
export async function findCommitById(
{ id }: { id: number },
tx = database,
): Promise<TypedResult<Date | null, LatitudeError>> {
if (commitUuid === HEAD_COMMIT) {
const result = await tx
.select({ mergedAt: commits.mergedAt })
.from(commits)
.where(and(eq(commits.projectId, projectId), isNotNull(commits.mergedAt)))
.orderBy(desc(commits.mergedAt))
.limit(1)

if (!result.length) {
return Result.error(new NotFoundError('No head commit found'))
}
const headCommit = result[0]!
return Result.ok(headCommit.mergedAt!)
}

): Promise<TypedResult<Commit, LatitudeError>> {
const commit = await tx.query.commits.findFirst({
where: eq(commits.uuid, commitUuid),
where: eq(commits.id, id),
})

if (!commit) return Result.error(new NotFoundError('Commit not found'))

return Result.ok(commit.mergedAt)
return Result.ok(commit)
}

export async function listCommits() {
return database.select().from(commits)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { database } from '$core/client'
import { NotFoundError, Result } from '$core/lib'

import { findCommitByUuid } from '../commits'
import { getDocumentsAtCommit } from './getDocumentsAtCommit'

export async function getDocumentByPath(
Expand All @@ -16,7 +17,9 @@ export async function getDocumentByPath(
db = database,
) {
try {
const result = await getDocumentsAtCommit({ projectId, commitUuid }, db)
const commit = await findCommitByUuid({ projectId, uuid: commitUuid }, db)
if (commit.error) return commit
const result = await getDocumentsAtCommit({ commitId: commit.value.id }, db)
const documents = result.unwrap()
const document = documents.find((doc) => doc.path === path)
if (!document) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { HEAD_COMMIT } from '$core/constants'
import { modifyExistingDocument } from '$core/services'
import { mergeCommit } from '$core/services/commits/merge'
import useTestDatabase from '$core/tests/useTestDatabase'
import { describe, expect, it } from 'vitest'

import { findCommitByUuid } from '../commits'
import { getDocumentsAtCommit } from './getDocumentsAtCommit'

useTestDatabase()
Expand All @@ -17,8 +19,7 @@ describe('getDocumentsAtCommit', () => {
await mergeCommit({ commitId: commit.id })

const result = await getDocumentsAtCommit({
commitUuid: commit.uuid,
projectId: project.id,
commitId: commit.id,
})
const documents = result.unwrap()

Expand All @@ -34,57 +35,51 @@ describe('getDocumentsAtCommit', () => {
commit: commit1,
content: 'VERSION 1',
})
await mergeCommit({ commitId: commit1.id }).then((r) => r.unwrap())

const { commit: commit2 } = await ctx.factories.createDraft({ project })
await ctx.factories.createDocumentVersion({
commit: commit2,
await modifyExistingDocument({
commitId: commit2.id,
documentUuid: doc.documentUuid,
content: 'VERSION 2',
})
}).then((r) => r.unwrap())

const { commit: commit3 } = await ctx.factories.createDraft({ project })
await ctx.factories.createDocumentVersion({
commit: commit3,
await modifyExistingDocument({
commitId: commit3.id,
documentUuid: doc.documentUuid,
content: 'VERSION 3',
})
content: 'VERSION 3 (draft)',
}).then((r) => r.unwrap())

// Commit 1 is merged AFTER commit 2
// Commit 3 is not merged
await mergeCommit({ commitId: commit2.id })
await mergeCommit({ commitId: commit1.id })
await mergeCommit({ commitId: commit2.id }).then((r) => r.unwrap())

const commit1Result = await getDocumentsAtCommit({
commitUuid: commit1.uuid,
projectId: project.id,
})
const commit1Docs = commit1Result.unwrap()
const commit1Docs = await getDocumentsAtCommit({
commitId: commit1.id,
}).then((r) => r.unwrap())
expect(commit1Docs.length).toBe(1)
expect(commit1Docs[0]!.content).toBe('VERSION 1')

const commit2Result = await getDocumentsAtCommit({
commitUuid: commit2.uuid,
projectId: project.id,
})
const commit2Docs = commit2Result.unwrap()
const commit2Docs = await getDocumentsAtCommit({
commitId: commit2.id,
}).then((r) => r.unwrap())
expect(commit2Docs.length).toBe(1)
expect(commit2Docs[0]!.content).toBe('VERSION 2')

const commit3Result = await getDocumentsAtCommit({
commitUuid: commit3.uuid,
projectId: project.id,
})
const commit3Docs = commit3Result.unwrap()
const commit3Docs = await getDocumentsAtCommit({
commitId: commit3.id,
}).then((r) => r.unwrap())
expect(commit3Docs.length).toBe(1)
expect(commit3Docs[0]!.content).toBe('VERSION 3')
expect(commit3Docs[0]!.content).toBe('VERSION 3 (draft)')

const headResult = await getDocumentsAtCommit({
commitUuid: HEAD_COMMIT,
const headCommit = await findCommitByUuid({
projectId: project.id,
})
const headDocs = headResult.unwrap()
uuid: HEAD_COMMIT,
}).then((r) => r.unwrap())
const headDocs = await getDocumentsAtCommit({
commitId: headCommit.id,
}).then((r) => r.unwrap())
expect(headDocs.length).toBe(1)
expect(headDocs[0]!.content).toBe('VERSION 1')
expect(headDocs[0]!.content).toBe('VERSION 2')
})

it('returns documents that were last modified in a previous commit', async (ctx) => {
Expand All @@ -95,55 +90,52 @@ describe('getDocumentsAtCommit', () => {
commit: commit1,
content: 'Doc 1 commit 1',
})
await mergeCommit({ commitId: commit1.id })
await mergeCommit({ commitId: commit1.id }).then((r) => r.unwrap())

const { commit: commit2 } = await ctx.factories.createDraft({ project })
const { documentVersion: doc2 } = await ctx.factories.createDocumentVersion(
{ commit: commit2, content: 'Doc 2 commit 2' },
)
await mergeCommit({ commitId: commit2.id })

await mergeCommit({ commitId: commit2.id }).then((r) => r.unwrap())

const { commit: commit3 } = await ctx.factories.createDraft({ project })
await ctx.factories.createDocumentVersion({
commit: commit3,
await modifyExistingDocument({
commitId: commit3.id,
documentUuid: doc2.documentUuid,
content: 'Doc 2 commit 3 (draft)',
})
}).then((r) => r.unwrap())

const commit1Result = await getDocumentsAtCommit({
commitUuid: commit1.uuid,
projectId: project.id,
})
const commit1Docs = commit1Result.unwrap()
const commit1Docs = await getDocumentsAtCommit({
commitId: commit1.id,
}).then((r) => r.unwrap())
expect(commit1Docs.length).toBe(1)
const commit1DocContents = commit1Docs.map((d) => d.content)
expect(commit1DocContents).toContain('Doc 1 commit 1')

const commit2Result = await getDocumentsAtCommit({
commitUuid: commit2.uuid,
projectId: project.id,
})
const commit2Docs = commit2Result.unwrap()
const commit2Docs = await getDocumentsAtCommit({
commitId: commit2.id,
}).then((r) => r.unwrap())
expect(commit2Docs.length).toBe(2)
const commit2DocContents = commit2Docs.map((d) => d.content)
expect(commit2DocContents).toContain('Doc 1 commit 1')
expect(commit2DocContents).toContain('Doc 2 commit 2')

const commit3Result = await getDocumentsAtCommit({
commitUuid: commit3.uuid,
projectId: project.id,
})
const commit3Docs = commit3Result.unwrap()
const commit3Docs = await getDocumentsAtCommit({
commitId: commit3.id,
}).then((r) => r.unwrap())
expect(commit3Docs.length).toBe(2)
const commit3DocContents = commit3Docs.map((d) => d.content)
expect(commit3DocContents).toContain('Doc 1 commit 1')
expect(commit3DocContents).toContain('Doc 2 commit 3 (draft)')

const headResult = await getDocumentsAtCommit({
commitUuid: HEAD_COMMIT,
const headCommit = await findCommitByUuid({
projectId: project.id,
uuid: HEAD_COMMIT,
})
const headDocs = headResult.unwrap()
const headDocs = await getDocumentsAtCommit({
commitId: headCommit.unwrap().id,
}).then((r) => r.unwrap())
expect(headDocs.length).toBe(2)
const headDocContents = headDocs.map((d) => d.content)
expect(headDocContents).toContain('Doc 1 commit 1')
Expand Down
Loading

0 comments on commit 3fd39fb

Please sign in to comment.