# db-reader **Repository Path**: wanghaibo921231/db-reader ## Basic Information - **Project Name**: db-reader - **Description**: indexDb工具 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 0 - **Created**: 2022-01-02 - **Last Updated**: 2022-03-06 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # DB-READER ## indexDB 操作库 ### 目录 #### 1. 安装 #### 2. 快速使用 ### 安装 ``` npm i db-reader ``` ### 快速使用 #### 创建模型文件`models/user.ts;`以及`models/goods.ts` user.ts ``` import { Model } from "db-reader"; const columns: Column[] = [ { name: "id", unique: true, increment: true, }, { name: "userName", comment: "用户名", }, { name: "password", comment: "密码", }, ]; export default class User extends Model { _table: string = "user"; columns: Column[] = columns; static instance: User = null; constructor() { super(); if (User.instance) { return User.instance; } User.instance = this; } } ``` goods.ts ``` import { Model } from "db-reader"; const columns: Column[] = [ { name: "id", unique: true, increment: true, }, { name: "userName", unique: true, comment: "用户名", }, { name: "password", comment: "密码", }, ]; export default class Goods extends Model { _table: string = "goods"; columns: Column[] = columns; static instance: Goods = null; constructor() { super(); if (Goods.instance) { return Goods.instance; } Goods.instance = this; } } ``` #### 创建`main.ts` ``` import {DB} from 'db-reader' import UserModel from "./models/user"; import GoodsModel from "./models/goods"; const db = new DB('test',1,[new UserModel(),new GoodsModel()]) db.connect(()=>{ const user = new UserModel(); const data = []; for (let i = 0; i < 100; i++) { data.push({ userName: "fff", password: "aaa", }); } user.insertAll(data).then((res) => { console.log(res); user.findList().then((res: object[]) => { console.log(res.length,'@@@'); }); }); }) ``` #### 运行程序 打开调试工具可以看到 Application(应用)的 IndexDB 里有刚才的数据库 test 打开之后看到了 user 和 goods 表