@tauri-apps/plugin-sql - v2.3.0
    Preparing search index...

    Class default

    Database

    The Database class serves as the primary interface for communicating with the rust side of the sql plugin.

    Index

    Constructors

    Properties

    Methods

    Constructors

    Properties

    path: string

    Methods

    • load

      A static initializer which connects to the underlying database and returns a Database instance once a connection to the database is established.

      Sqlite

      The path is relative to tauri::path::BaseDirectory::App and must start with sqlite:.

      Parameters

      • path: string

      Returns Promise<default>

      const db = await Database.load("sqlite:test.db");
      
    • get

      A static initializer which synchronously returns an instance of the Database class while deferring the actual database connection until the first invocation or selection on the database.

      Sqlite

      The path is relative to tauri::path::BaseDirectory::App and must start with sqlite:.

      Parameters

      • path: string

      Returns default

      const db = Database.get("sqlite:test.db");
      
    • execute

      Passes a SQL expression to the database for execution.

      Parameters

      • query: string
      • OptionalbindValues: unknown[]

      Returns Promise<QueryResult>

      // for sqlite & postgres
      // INSERT example
      const result = await db.execute(
      "INSERT into todos (id, title, status) VALUES ($1, $2, $3)",
      [ todos.id, todos.title, todos.status ]
      );
      // UPDATE example
      const result = await db.execute(
      "UPDATE todos SET title = $1, completed = $2 WHERE id = $3",
      [ todos.title, todos.status, todos.id ]
      );

      // for mysql
      // INSERT example
      const result = await db.execute(
      "INSERT into todos (id, title, status) VALUES (?, ?, ?)",
      [ todos.id, todos.title, todos.status ]
      );
      // UPDATE example
      const result = await db.execute(
      "UPDATE todos SET title = ?, completed = ? WHERE id = ?",
      [ todos.title, todos.status, todos.id ]
      );
    • select

      Passes in a SELECT query to the database for execution.

      Type Parameters

      • T

      Parameters

      • query: string
      • OptionalbindValues: unknown[]

      Returns Promise<T>

      // for sqlite & postgres
      const result = await db.select(
      "SELECT * from todos WHERE id = $1", [ id ]
      );

      // for mysql
      const result = await db.select(
      "SELECT * from todos WHERE id = ?", [ id ]
      );
    • close

      Closes the database connection pool.

      Parameters

      • Optionaldb: string

        Optionally state the name of a database if you are managing more than one. Otherwise, all database pools will be in scope.

      Returns Promise<boolean>

      const success = await db.close()