-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
337 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,39 @@ | ||
import { DocumentVersion, Result } from '@latitude-data/core' | ||
import { findCommitById, getDocumentsAtCommit } from '$core/data-access' | ||
import { Result, Transaction, TypedResult } from '$core/lib' | ||
import { BadRequestError } from '$core/lib/errors' | ||
|
||
import { | ||
getDraft, | ||
getMergedAndDraftDocuments, | ||
replaceCommitChanges, | ||
resolveDocumentChanges, | ||
} from './shared' | ||
import { DocumentVersion, documentVersions } from '$core/schema' | ||
|
||
export async function createNewDocument({ | ||
commitId, | ||
path, | ||
content, | ||
}: { | ||
commitId: number | ||
path: string | ||
}) { | ||
try { | ||
const draft = (await getDraft(commitId)).unwrap() | ||
|
||
const [mergedDocuments, draftDocuments] = ( | ||
await getMergedAndDraftDocuments({ | ||
draft, | ||
}) | ||
).unwrap() | ||
content?: string | ||
}): Promise<TypedResult<DocumentVersion, Error>> { | ||
return await Transaction.call(async (tx) => { | ||
const commit = (await findCommitById({ id: commitId }, tx)).unwrap() | ||
if (commit.mergedAt !== null) { | ||
return Result.error(new BadRequestError('Cannot modify a merged commit')) | ||
} | ||
|
||
if (path && draftDocuments.find((d) => d.path === path)) { | ||
const currentDocs = (await getDocumentsAtCommit({ commitId }, tx)).unwrap() | ||
if (currentDocs.find((d) => d.path === path)) { | ||
return Result.error( | ||
new BadRequestError('A document with the same path already exists'), | ||
) | ||
} | ||
|
||
draftDocuments.push({ | ||
path, | ||
content: '', | ||
} as DocumentVersion) | ||
|
||
const documentsToUpdate = await resolveDocumentChanges({ | ||
originalDocuments: mergedDocuments, | ||
newDocuments: draftDocuments, | ||
}) | ||
|
||
const newDraftDocuments = ( | ||
await replaceCommitChanges({ | ||
const newDoc = await tx | ||
.insert(documentVersions) | ||
.values({ | ||
commitId, | ||
documentChanges: documentsToUpdate, | ||
path, | ||
content: content ?? '', | ||
}) | ||
).unwrap() | ||
.returning() | ||
|
||
return Result.ok(newDraftDocuments.find((d) => d.path === path)!) | ||
} catch (error) { | ||
return Result.error(error as Error) | ||
} | ||
return Result.ok(newDoc[0]!) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './create' | ||
export * from './update' | ||
export * from './recomputeChanges' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import type { CompileError } from '@latitude-data/compiler' | ||
import { database } from '$core/client' | ||
import { findCommitById } from '$core/data-access' | ||
import { Result, TypedResult } from '$core/lib' | ||
import { BadRequestError } from '$core/lib/errors' | ||
import { DocumentVersion } from '$core/schema' | ||
|
||
import { | ||
getMergedAndDraftDocuments, | ||
replaceCommitChanges, | ||
resolveDocumentChanges, | ||
} from './utils' | ||
|
||
type RecomputedChanges = { | ||
documents: DocumentVersion[] | ||
errors: { [documentUuid: string]: CompileError[] } | ||
} | ||
|
||
export async function recomputeChanges( | ||
{ | ||
commitId, | ||
}: { | ||
commitId: number | ||
}, | ||
tx = database, | ||
): Promise<TypedResult<RecomputedChanges, Error>> { | ||
try { | ||
const draft = (await findCommitById({ id: commitId }, tx)).unwrap() | ||
if (draft.mergedAt !== null) { | ||
return Result.error( | ||
new BadRequestError('Cannot recompute changes in a merged commit'), | ||
) | ||
} | ||
|
||
const [mergedDocuments, draftDocuments] = ( | ||
await getMergedAndDraftDocuments( | ||
{ | ||
draft, | ||
}, | ||
tx, | ||
) | ||
).unwrap() | ||
|
||
const { documents: documentsToUpdate, errors } = | ||
await resolveDocumentChanges({ | ||
originalDocuments: mergedDocuments, | ||
newDocuments: draftDocuments, | ||
}) | ||
|
||
const newDraftDocuments = ( | ||
await replaceCommitChanges( | ||
{ | ||
commitId, | ||
documentChanges: documentsToUpdate, | ||
}, | ||
tx, | ||
) | ||
).unwrap() | ||
|
||
return Result.ok({ documents: newDraftDocuments, errors }) | ||
} catch (error) { | ||
return Result.error(error as Error) | ||
} | ||
} |
Oops, something went wrong.