1.0.4 • Published 3 years ago

react-native-sqlite-tool v1.0.4

Weekly downloads
240
License
MIT
Repository
-
Last release
3 years ago

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! :)