Skip to content

Commit

Permalink
Merge pull request #3182 from opral/lixdk-123-lix-sdk-use-null-t-inst…
Browse files Browse the repository at this point in the history
…ead-of-undefined-t

refactor: use null
  • Loading branch information
samuelstroschein authored Oct 18, 2024
2 parents e399964 + e50bcf2 commit 19a0f7b
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 23 deletions.
10 changes: 5 additions & 5 deletions lix/packages/sdk/src/database/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ type SnapshotTable = {
* - For a csv cell change, the value would be the new cell value.
* - For an inlang message change, the value would be the new message.
*/
content?: Record<string, any>;
content: Record<string, any> | null;
};

// TODO #185 rename content to snapshot_content
Expand All @@ -104,8 +104,8 @@ export type Conflict = Selectable<ConflictTable>;
export type NewConflict = Insertable<ConflictTable>;
export type ConflictUpdate = Updateable<ConflictTable>;
type ConflictTable = {
meta?: Record<string, any>;
reason?: string;
meta: Record<string, any> | null;
reason: string | null;
change_id: ChangeTable["id"];
conflicting_change_id: ChangeTable["id"];
/**
Expand All @@ -114,7 +114,7 @@ type ConflictTable = {
* Can be the change_id, conflicting_change_id, or another change_id
* that resulted from a merge.
*/
resolved_change_id?: ChangeTable["id"];
resolved_change_id: ChangeTable["id"] | null;
};

// ------ discussions ------
Expand All @@ -139,7 +139,7 @@ export type NewComment = Insertable<CommentTable>;
export type CommentUpdate = Updateable<CommentTable>;
type CommentTable = {
id: Generated<string>;
parent_id?: string;
parent_id: string | null;
discussion_id: string;
created_at: Generated<string>;
body: string;
Expand Down
9 changes: 4 additions & 5 deletions lix/packages/sdk/src/merge/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,10 @@ export async function merge(args: {
await trx
.insertInto("change")
.values(
// https://github.com/opral/inlang-message-sdk/issues/123
sourceChangesWithSnapshot.map((change) => {
delete change.content;
return change;
}),
sourceChangesWithSnapshot.map((c) => ({
...c,
content: undefined,
})),
)
// ignore if already exists
.onConflict((oc) => oc.doNothing())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export function isInSimulatedCurrentBranch(
.where("conflict.resolved_change_id", "is", null),
),
// change is in a conflict and is the resolved one
// @ts-expect-error - no idea why
eb("change.id", "in", (subquery) =>
subquery.selectFrom("conflict").select("conflict.resolved_change_id"),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ test("it should throw if the change id does not belong to the conflict", async (
conflict: {
change_id: "change1",
conflicting_change_id: "change2",
meta: undefined,
reason: undefined,
resolved_change_id: undefined,
meta: null,
reason: null,
resolved_change_id: null,
},
selectChangeId: "change3",
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ test("it should throw if the to be resolved with change already exists", async (
parentIds: [changes[0]!.id],
newChange: {
...changes[0]!,
content: mockSnapshots[0]?.content,
content: mockSnapshots[0]?.content ?? null,
},
}),
).rejects.toThrowError(ChangeAlreadyExistsError);
Expand Down Expand Up @@ -310,11 +310,7 @@ test("resolving a conflict with a new change should insert the change and mark t

await lix.db
.insertInto("snapshot")
.values(
mockSnapshots.map((s) => {
return { content: s.content };
}),
)
.values(mockSnapshots.map((s) => ({ content: s.content })))
.returningAll()
.execute();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ export async function resolveConflictWithNewChange(args: {
});

const snapshotContent = args.newChange.content!;
delete args.newChange.content;

await args.lix.db.transaction().execute(async (trx) => {
await trx
Expand All @@ -87,12 +86,12 @@ export async function resolveConflictWithNewChange(args: {
.returningAll()
.executeTakeFirstOrThrow();

delete args.newChange.content;

const insertedChange = await trx
.insertInto("change")
.values({
...args.newChange,
// @ts-expect-error - newChange is a change with a snapshot
content: undefined,
snapshot_id: snapshot.id,
})
.returning("id")
Expand Down

0 comments on commit 19a0f7b

Please sign in to comment.