Negotiate Pushing Milestone
    Wondering what’s next for npm?Check out our public roadmap! »

    react-native-sqlite-tool
    TypeScript icon, indicating that this package has built-in type declarations

    1.0.4 • Public • Published

    react-native-sqlite-tool

    This repository is based on react-native-sqlite-storage, you should install and link react-native-sqlite-storage first

    Installation

     npm install --save react-native-sqlite-tool

    Then follow the instructions to use react-native-sqlite-tool;

    Steps

    1. Create a class for one table

      import { SqlLite } from "react-native-sqlite-tool";
      //if you use typescript
      export interface T {
          //primary key
          id?: string;
          value?: string;
      }
      //Set primaryKey dbName tableName createTableSql
      class DemoSql extends SqlLite<T> {
          primaryKey = "id";
          dbName = "default.db";
          tableName = "todo";
          createTableSql = `CREATE TABLE IF NOT EXISTS todo (
              id  varchar(32) NOT NULL,
              value varchar(255) DEFAULT NULL,
              PRIMARY KEY (id)
          )`;
          constructor() {
              super();
              //important
              this.init();
          }
      }
      
      export const DemoSqlInstance = new DemoSql();
    2. You can use follow apis

      insert(item: T): Promise;

      import { demoSqlInstance } from "./Demo";
      export interface T {
          //primary key
          id?: string;
          value?: string;
      }
      class Todo {
          insert(item: T) {
              return demoSqlInstance.insert(item);
          }
      }

      updateByCondition(newParams: T, conditionParams: T): Promise;

      const item={value:"new value"}
      demoSqlInstance.updateByCondition(item,{id:"123"})

      updateByPrimaryKey(newParams:T): Promise

      const item = { id: "123", value: "new value" }
      demoSqlInstance.updateByPrimaryKey(item);

      deleteByPrimaryKey(primaryValue: any): Promise

      demoSqlInstance.deleteByPrimaryKey("123")

      selectByPrimaryKey(primaryValue: any): Promise<T | null>

      demoSqlInstance.selectByPrimaryKey("123")

      select(selectSql?: string, orderSql?: string): Promise<[] | T[]>

      demoSqlInstance.select()
      demoSqlInstance.select("id=123")
      demoSqlInstance.select("id=123","create_time desc")

      selectOne(selectSql?: string): Promise<T | null>

      demoSqlInstance.selectOne("id=123")

      selectBySql(selectSql: string): Promise<T[]>

      demoSqlInstance.selectOne("select * from todo where id='123' order by create_time desc")

      executeSql(sql: string): Promise

      demoSqlInstance.executeSql("select * from todo where id='123' order by create_time desc")

    If you like this tool, please add a star to my Github Repo. Thanks!

    That's all. Enjoy! :)

    Install

    npm i react-native-sqlite-tool

    DownloadsWeekly Downloads

    1

    Version

    1.0.4

    License

    MIT

    Unpacked Size

    27.7 kB

    Total Files

    6

    Last publish

    Collaborators

    • avatar