Skip to content

Commit

Permalink
refactor: Extract toBlob() from lix.* object opral/lix-sdk#196
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelstroschein committed Dec 18, 2024
1 parent 402cd70 commit be9effa
Show file tree
Hide file tree
Showing 16 changed files with 88 additions and 54 deletions.
10 changes: 10 additions & 0 deletions .changeset/three-worms-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"@lix-js/sdk": patch
---

Extract `toBlob()` from `lix.*` object https://github.com/opral/lix-sdk/issues/196

```diff
- await lix.toBlob()
+ await toBlob({ lix })
```
5 changes: 3 additions & 2 deletions packages/lix-sdk/src/lix/merge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { createChangeSet } from "../change-set/create-change-set.js";
import { createDiscussion } from "../discussion/create-discussion.js";
import { createComment } from "../discussion/create-comment.js";
import { fileQueueSettled } from "../file-queue/file-queue-settled.js";
import { toBlob } from "./to-blob.js";

test("it should copy changes from the sourceLix into the targetLix that do not exist in targetLix yet", async () => {
const mockSnapshots = [
Expand Down Expand Up @@ -691,7 +692,7 @@ test("it should copy discussion and related comments and mappings", async () =>

// The files have to be there before we merge
const lix2 = await openLixInMemory({
blob: await lix1.toBlob(),
blob: await toBlob({ lix: lix1 }),
providePlugins: [mockPlugin],
});

Expand Down Expand Up @@ -816,7 +817,7 @@ test.skip("it should copy change sets and merge memberships", async () => {
});

const sourceLix = await openLixInMemory({
blob: await targetLix.toBlob(),
blob: await toBlob({ lix: targetLix }),
});

// expand the change set to contain another change
Expand Down
6 changes: 5 additions & 1 deletion packages/lix-sdk/src/lix/open-lix-in-memory.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { test, expect } from "vitest";
import { openLixInMemory } from "./open-lix-in-memory.js";
import { toBlob } from "./to-blob.js";

test("it should open a lix in memory", async () => {
const lix = await openLixInMemory({});
Expand All @@ -10,6 +11,7 @@ test("it should open a lix in memory", async () => {

test("it should open a lix in memory from a blob", async () => {
const lix1 = await openLixInMemory({});

await lix1.db
.insertInto("file")
.values({
Expand All @@ -18,8 +20,10 @@ test("it should open a lix in memory from a blob", async () => {
data: new TextEncoder().encode("hello"),
})
.execute();
const lix2 = await openLixInMemory({ blob: await lix1.toBlob() });

const lix2 = await openLixInMemory({ blob: await toBlob({ lix: lix1 }) });
const files = await lix2.db.selectFrom("file").selectAll().execute();

expect(files).toEqual([
expect.objectContaining({
id: "1",
Expand Down
3 changes: 2 additions & 1 deletion packages/lix-sdk/src/lix/open-lix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { expect, test } from "vitest";
import { openLixInMemory } from "./open-lix-in-memory.js";
import { newLixFile } from "./new-lix.js";
import type { LixPlugin } from "../plugin/lix-plugin.js";
import { toBlob } from "./to-blob.js";

test("providing plugins should be possible", async () => {
const mockPlugin: LixPlugin = {
Expand All @@ -24,7 +25,7 @@ test("providing key values should be possible", async () => {

// testing overwriting key values
const lix1 = await openLixInMemory({
blob: await lix.toBlob(),
blob: await toBlob({ lix }),
keyValues: [{ key: "key", value: "value2" }],
});

Expand Down
18 changes: 2 additions & 16 deletions packages/lix-sdk/src/lix/open-lix.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type { LixPlugin } from "../plugin/lix-plugin.js";
import { loadPlugins } from "../plugin/load-plugin.js";
import { contentFromDatabase, type SqliteDatabase } from "sqlite-wasm-kysely";
import { type SqliteDatabase } from "sqlite-wasm-kysely";
import { initDb } from "../database/init-db.js";
import { initFileQueueProcess } from "../file-queue/file-queue-process.js";
import { fileQueueSettled } from "../file-queue/file-queue-settled.js";
import type { Kysely } from "kysely";
import type { LixDatabaseSchema } from "../database/schema.js";
import { initSyncProcess } from "../sync/sync-process.js";
Expand All @@ -22,11 +21,9 @@ export type Lix = {
*/
sqlite: SqliteDatabase;
db: Kysely<LixDatabaseSchema>;
toBlob: () => Promise<Blob>;
plugin: {
getAll: () => Promise<LixPlugin[]>;
};
close: () => Promise<void>;
};

/**
Expand Down Expand Up @@ -77,27 +74,16 @@ export async function openLix(args: {
getAll: async () => plugins,
};

const toBlob = async () => {
await fileQueueSettled({ lix: { db } });
return new Blob([contentFromDatabase(args.database)]);
};

initFileQueueProcess({
lix: { db, plugin, sqlite: args.database },
rawDatabase: args.database,
});

initSyncProcess({ lix: { db, plugin, toBlob } });
initSyncProcess({ lix: { db, plugin, sqlite: args.database } });

return {
db,
sqlite: args.database,
toBlob,
plugin,
close: async () => {
await fileQueueSettled({ lix: { db } });
// args.database.close();
// await db.destroy();
},
};
}
14 changes: 14 additions & 0 deletions packages/lix-sdk/src/lix/to-blob.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { contentFromDatabase } from "sqlite-wasm-kysely";
import type { Lix } from "./open-lix.js";

/**
* Convert the lix to a blob.
*
* @example
* const blob = await toBlob({ lix })
*/
export async function toBlob(args: {
lix: Pick<Lix, "db" | "sqlite">;
}): Promise<Blob> {
return new Blob([contentFromDatabase(args.lix.sqlite)]);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { test, expect } from "vitest";
import { createLsaInMemoryEnvironment } from "./create-in-memory-environment.js";
import { openLixInMemory } from "../../lix/open-lix-in-memory.js";
import { toBlob } from "../../lix/to-blob.js";

test("opening a lix works", async () => {
const environment = createLsaInMemoryEnvironment();
Expand All @@ -14,7 +15,7 @@ test("opening a lix works", async () => {
.executeTakeFirstOrThrow();

// initialize the env with the lix file
environment.setLix({ id: lixId, blob: await mockLix.toBlob() });
environment.setLix({ id: lixId, blob: await toBlob({ lix: mockLix }) });

const open0 = await environment.openLix({ id: lixId });

Expand Down Expand Up @@ -61,7 +62,7 @@ test("it handles concurrent connections", async () => {
.executeTakeFirstOrThrow();

// initialize the env with the lix file
environment.setLix({ id: lixId, blob: await mockLix.toBlob() });
environment.setLix({ id: lixId, blob: await toBlob({ lix: mockLix }) });

const open0 = await environment.openLix({ id: lixId });
const open1 = await environment.openLix({ id: lixId });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { openLixInMemory } from "../../lix/open-lix-in-memory.js";
import type { Lix } from "../../lix/open-lix.js";
import { toBlob } from "../../lix/to-blob.js";
import type { LsaEnvironment } from "./environment.js";

/**
Expand Down Expand Up @@ -100,7 +101,7 @@ export const createLsaInMemoryEnvironment = (): LsaEnvironment => {
if (connections.size === 0) {
// TODO no concurrency guarantees
const lix = openLixes.get(args.id);
const blob = await lix!.toBlob();
const blob = await toBlob({ lix: lix! });
await lix?.close();
openConnections.delete(args.id);
openLixes.delete(args.id);
Expand Down
5 changes: 3 additions & 2 deletions packages/lix-sdk/src/server-api-handler/routes/get-v1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createServerApiHandler } from "../create-server-api-handler.js";
import { newLixFile } from "../../lix/new-lix.js";
import { openLixInMemory } from "../../lix/open-lix-in-memory.js";
import { createLsaInMemoryEnvironment } from "../environment/create-in-memory-environment.js";
import { toBlob } from "../../lix/to-blob.js";

test("it should fetch the lix file from the server", async () => {
const lix = await openLixInMemory({
Expand All @@ -26,7 +27,7 @@ test("it should fetch the lix file from the server", async () => {
await lsaHandler(
new Request("http://localhost:3000/lsa/new-v1", {
method: "POST",
body: await lix.toBlob(),
body: await toBlob({ lix }),
})
);

Expand Down Expand Up @@ -116,7 +117,7 @@ test("#lix_sync is set to true", async () => {
const response0 = await lsaHandler(
new Request("http://localhost:3000/lsa/new-v1", {
method: "POST",
body: await lix.toBlob(),
body: await toBlob({ lix }),
})
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createServerApiHandler } from "../create-server-api-handler.js";
import { newLixFile } from "../../lix/new-lix.js";
import { openLixInMemory } from "../../lix/open-lix-in-memory.js";
import { createLsaInMemoryEnvironment } from "../environment/create-in-memory-environment.js";
import { toBlob } from "../../lix/to-blob.js";

test("it should store the lix file", async () => {
const initLix = await openLixInMemory({
Expand All @@ -22,7 +23,7 @@ test("it should store the lix file", async () => {
const response = await lsaHandler(
new Request("http://localhost:3000/lsa/new-v1", {
method: "POST",
body: await initLix.toBlob(),
body: await toBlob({ lix: initLix }),
})
);
const json = await response.json();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { createServerApiHandler } from "../create-server-api-handler.js";
import { mockJsonSnapshot } from "../../snapshot/mock-json-snapshot.js";
import { mockChange } from "../../change/mock-change.js";
import { createLsaInMemoryEnvironment } from "../environment/create-in-memory-environment.js";
import { toBlob } from "../../lix/to-blob.js";

type RequestBody =
LixServerApi.paths["/lsa/pull-v1"]["post"]["requestBody"]["content"]["application/json"];
Expand All @@ -27,7 +28,7 @@ test("it should pull rows successfully", async () => {

const environment = createLsaInMemoryEnvironment();

await environment.setLix({ id: id.value, blob: await lix.toBlob() });
await environment.setLix({ id: id.value, blob: await toBlob({ lix }) });

const lsaHandler = await createServerApiHandler({ environment });

Expand Down Expand Up @@ -79,7 +80,7 @@ test("it should specifically be able to handle snapshots which use json binary a
.execute();

const environment = createLsaInMemoryEnvironment();
await environment.setLix({ id, blob: await lix.toBlob() });
await environment.setLix({ id, blob: await toBlob({ lix }) });

const lsa = await createServerApiHandler({ environment });

Expand Down Expand Up @@ -172,7 +173,7 @@ test("it should handle empty tables gracefully", async () => {
.executeTakeFirstOrThrow();

const environment = createLsaInMemoryEnvironment();
await environment.setLix({ id, blob: await lix.toBlob() });
await environment.setLix({ id, blob: await toBlob({ lix }) });

const lsa = await createServerApiHandler({ environment });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { createVersion } from "../../version/create-version.js";
import { switchVersion } from "../../version/switch-version.js";
import { pullFromServer } from "../../sync/pull-from-server.js";
import { createLsaInMemoryEnvironment } from "../environment/create-in-memory-environment.js";
import { toBlob } from "../../lix/to-blob.js";

test("it should push data successfully", async () => {
const lix = await openLixInMemory({});
Expand All @@ -19,7 +20,7 @@ test("it should push data successfully", async () => {
.executeTakeFirstOrThrow();

const environment = createLsaInMemoryEnvironment();
await environment.setLix({ id, blob: await lix.toBlob() });
await environment.setLix({ id, blob: await toBlob({ lix }) });

const lsaHandler = await createServerApiHandler({ environment });

Expand Down Expand Up @@ -130,7 +131,7 @@ test("it should return 400 for a failed insert operation", async () => {

const environment = createLsaInMemoryEnvironment();

environment.setLix({ id, blob: await lix.toBlob() });
environment.setLix({ id, blob: await toBlob({ lix }) });

const lsa = await createServerApiHandler({ environment });

Expand Down Expand Up @@ -169,7 +170,7 @@ test.skip("it should detect conflicts", async () => {

// initialize client
const lixOnClient = await openLixInMemory({
blob: await lixOnServer.toBlob(),
blob: await toBlob({ lix: lixOnServer }),
});

const lixId = await lixOnServer.db
Expand All @@ -192,7 +193,7 @@ test.skip("it should detect conflicts", async () => {

await environment.setLix({
id: lixId.value,
blob: await lixOnServer.toBlob(),
blob: await toBlob({ lix: lixOnServer }),
});

// client has/creates value1 for mock_key
Expand Down
Loading

0 comments on commit be9effa

Please sign in to comment.