TanStackT
TanStack4w ago
2 replies
sad-indigo

Should I expect collections with the same id to store the same data ?

Hello folks ! Should I expect collections with the same id to store the same data ?

type TestItem = { id: string; name: string };

Deno.test("collections with same id share store", () => {
  const collection1 = createCollection<TestItem, string>({
    id: "shared-test-3",
    getKey: (item) => item.id,
    sync: {
      sync: ({ markReady }) => {
        markReady();
      },
    },
    onInsert: async () => {},
  });

  const collection2 = createCollection<TestItem, string>({
    id: "shared-test-3",
    getKey: (item) => item.id,
    sync: {
      sync: ({ markReady }) => {
        markReady();
      },
    },
    onInsert: async () => {},
  });

  console.log(Object.keys(collection1));

  // Use utils to write directly to bypass transaction
  collection1.insert({ id: "1", name: "test" });

  // Check if collection2 sees it
  const item1 = collection1.get("1");
  const item2 = collection2.get("1");

  console.log("collection1.get('1'):", item1);
  console.log("collection2.get('1'):", item2);

  assertEquals(collection1 === collection2, true, "Collections with same id should be same instance");
});
Was this page helpful?