SolidJSS
SolidJSโ€ข8mo agoโ€ข
1 reply
jesseb34r

how to use solid router with tauri and a sqlite database

I am making a Tauri app that has a pretty basic database to hold lists and projects created by the user. I currently have a singleton database class with functions for database actions like this:
import Database from "@tauri-apps/plugin-sql"

export class MyDB {
  private static instance: MyDB | null = null;
  private db: Database;

  private constructor(db: Database) {
    this.db = db;
  }

  static async db(): Promise<MyDB> {
    if (!MyDB.instance) {
      const db = await Database.load("sqlite:data.db");
      MyDB.instance = new MyDB(db);
    }
    return MyDB.instance;
   }

  // DB actions
  async create_project(name: string): Promise<number> {
    const result = this.db.execute(
      "INSERT INTO projects (name) VALUES (?)",
      name,
    }
    if (result.lastInsertId !== undefined) {
      return result.lastInsertId;
    } else {
      throw new Error("Failed to get last insert ID");
    }
  }
  //... rest of actions
}

and then I have been calling this in my components by doing something like this:
async function handle_new_project(name: string): number {
  const db = MyDB.db();
  const project_id = await db.create_project(name);
  return project_id;
}


This has worked so far but I feel like there has got to be a way to use solid router functions like createAsync, action, and query to do this better but I am not really familiar with these at all. How can I set this stuff up to have better usage in my components? Right now, I'm hitting some complications trying to have a list of projects that I fetch and display with a <For each={projects} /> but I can't figure out how to do refetching and stuff in a good way.
Was this page helpful?