tinyplex / tinybase

The reactive data store for local‑first apps.
https://tinybase.org
MIT License
3.71k stars 77 forks source link

expo-sqlite-next-persister not working #122

Closed tobiascornille closed 8 months ago

tobiascornille commented 9 months ago

Describe the bug

If I try to use createExpoSqliteNextPersister and follow the changes as here, the connection to the database does not seem to work. In the console I can only see INFO BEGIN undefined being logged.

Tagging @alanjhughes @brentvatne since they submitted the expo-sqlite-next-persister PR https://github.com/tinyplex/tinybase/pull/114

Your Example Website or App

No response

Steps to Reproduce the Bug or Issue

Here are some code snippets: Working:

import * as SQLite from "expo-sqlite";

export const dbName = "test.db";

export const db = SQLite.openDatabase(dbName);

export function initDatabase() {
  console.error("Initializing db", db);
  db.exec(
    [
      {
        sql: `CREATE TABLE IF NOT EXISTS "todo" ("id" PRIMARY KEY, "text", "completed" INTEGER DEFAULT 0);`,
        args: [],
      },
    ],
    false,
    () => {},
  );
}
  const store = useStore();

  useEffect(() => {
    initDatabase();
  }, []);

  const persister = useCreatePersister(
    store,
    (store) =>
      createExpoSqlitePersister(
        store,
        db,
        {
          mode: "tabular",
          tables: {
            load: { todo: { tableId: "todo", rowIdColumnName: "id" } },
            save: { todo: { tableName: "todo", rowIdColumnName: "id" } },
          },
        },
        console.info,
      ),
    [db],
    async (persister) => {
      await persister.startAutoLoad();
      await persister.startAutoSave();
    },
  );

Not working:

import * as SQLite from "expo-sqlite/next";

export const dbName = "test.db";

export const db = SQLite.openDatabaseSync(dbName);

export function initDatabase() {
  console.error("Initializing db", db);
  db.execAsync(
    `CREATE TABLE IF NOT EXISTS "todo" ("id" PRIMARY KEY, "text", "completed" INTEGER DEFAULT 0);`,
  );
}
  useEffect(() => {
    initDatabase();
  }, []);

  const persister = useCreatePersister(
    store,
    (store) =>
      createExpoSqliteNextPersister(
        store,
        db,
        {
          mode: "tabular",
          tables: {
            load: { todo: { tableId: "todo", rowIdColumnName: "id" } },
            save: { todo: { tableName: "todo", rowIdColumnName: "id" } },
          },
        },
        console.info,
      ),
    [db],
    async (persister) => {
      await persister.startAutoLoad();
      await persister.startAutoSave();
    },
  );

Expected behavior

No response

Screenshots or Videos

No response

Platform

"expo": "^50.0.0-preview.7", "expo-sqlite": "~13.2.0", "react-native": "0.73.1", "tinybase": "4.5.10",

I'm testing on an Android Emulator with Android 14.0

Additional context

No response

brentvatne commented 9 months ago

we made some changes to the expo-sqlite/next API during the beta, we'll need to update the persister. cc @alanjhughes

jamesgpearce commented 8 months ago

Standing by to accept the PR! It would be cool to unbreak this. Thanks!

alanjhughes commented 8 months ago

Working on it at the moment. Should be ready soon, just investigating an issue.