1.0.1 • Published 5 years ago

react-native-ppr-database v1.0.1

Weekly downloads
1
License
ISC
Repository
-
Last release
5 years ago

react-native-ppr-database

Require:

  • "react-native": "*",
  • "react-native-sqlite-storage": "*"

BaseDatabase

Inside the class's methods, this.database is initialized and used to exectutel SQL actions. You should use this field too.

We have already table KeyValue to manage database version. You should read the source code to reference.

Method

MethodParameterDescription
constructorname: string, currentDBVersion: numberInitialize the database informations includes database name and database version
openDatabasevoidOpen the database and validate the database version, re-create all tables in database if the database version changed
createTablestx: SQLitePluginTransactionCreate all tables in the database
dropTablestx: SQLitePluginTransactionDrop all tables in the database
initializeDatabasevoidCall dropTables, then call createTables. This method is called if openDatabase detect the database version changed

Usage

import BaseDatabase, { SQLQueryCreator } from "react-native-ppr-database";

// * table schema template *
// you should use this template if you want to use SQLQueryCreator
const Animal = {
  name: "Animal",
  primaryKey: "id",
  properties: {
    "id": "text",
    "name": "text"
  }
};

// inherit from Database to modify
class DerivedDatabase extends BaseDatabase {
  // create more tables in `createTables` method.
  createTables(tx) {
    super.createTables(tx);
    tx.executeSql(SQLQueryCreator.createTable(Animal));
  }

  // drop more tables in `dropTables` method.
  dropTables(tx) {
    super.dropTables(tx);
    tx.executeSql(SQLQueryCreator.dropTable(Animal));
  }
}