1.0.31 • Published 5 months ago

angular-web-sqlite v1.0.31

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

angular-web-sqlite

angular-web-sqlite is an Angular service that wraps the @sqlite.org/sqlite-wasm module. This Angular service takes care of the declaration and communication with a web worker, essential for utilizing the Origin Private File System (OPFS) storage back-end.

Installation

Install the library using npm:

npm install angular-web-sqlite

Angular Configuration

The installation of the module modifies the angular.json file to incorporate precompiled files.

This setup guarantees that the essential assets—the SQLite working script (sqlite-worker.js) and files from the @sqlite.org/sqlite-wasm module, utilized by the library internally—are included in the output directory during the build process.

Ensure that the 'sqlite-client' folder is copied to the project's output directory, www.

  "projects": {
    "app": {
      ...
      "architect": {
        ...
         "build": {
          ...
          "options": {
            "assets": [
              ...
              {
                "glob": "**/*.js",
                "input": "./node_modules/angular-web-sqlite/src/lib/assets",
                "output": "./sqlite-client/"
              },
              {
                "glob": "**/*",
                "input": "./node_modules/@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm/",
                "output": "./sqlite-client/"
              }
            ]
          }
          ...
       },
        "serve": {
          ...
          "options": {
            ...
            "headers": {
              "Cross-Origin-Opener-Policy": "same-origin",
              "Cross-Origin-Embedder-Policy": "require-corp"
            }
          }
       }
      }
    }
   }

Methods

init(dbName: string): Promise<any>                              //Initializes the SQLite database with the specified name.

executeSql(sql: string, params: any[]): Promise<any>            //Executes a single SQL query with optional parameters.

batchSql(sqls: [index: number]: [string, any[]]): Promise<any>  //Executes a batch of SQL statements as a transaction.

Usage

import { WebSqlite } from 'angular-web-sqlite';

@Injectable()
export class YourService {

  constructor(
    private webSqlite: WebSqlite
  ) {  }

  async initializeDatabase(dbName: string) {
    await this.webSqlite.init(dbName);
  }

  async executeQuery() {
    const sql = 'SELECT * FROM your_table';
    const params = [];
    const result = await this.webSqlite.executeSql(sql, params);
    // Process the result as needed
  }

  async batchSqlOperations() {
    const sqls = [
        ["CREATE TABLE IF NOT EXISTS your_table (a TEXT, b TEXT)", []],
        ["CREATE TABLE IF NOT EXISTS your_table2 (c TEXT, d TEXT)", []],
        ....];
    const result = await this.webSqlite.batchSql(sqls);
    // Process the result as needed
  }
}
1.0.26

5 months ago

1.0.25

5 months ago

1.0.29

5 months ago

1.0.28

5 months ago

1.0.27

5 months ago

1.0.31

5 months ago

1.0.30

5 months ago

1.0.24

6 months ago

1.0.23

6 months ago

1.0.22

6 months ago

1.0.21

6 months ago

1.0.20

6 months ago

1.0.19

6 months ago

1.0.18

6 months ago

1.0.17

6 months ago

1.0.16

6 months ago

1.0.15

6 months ago

1.0.14

6 months ago

1.0.13

6 months ago

1.0.12

6 months ago

1.0.11

6 months ago

1.0.10

6 months ago

1.0.9

6 months ago

1.0.8

6 months ago

1.0.7

6 months ago

1.0.6

6 months ago

1.0.5

6 months ago

1.0.4

6 months ago

1.0.3

6 months ago

1.0.2

6 months ago

1.0.1

6 months ago

1.0.0

6 months ago