db-reader
v0.1.4
Published
``` npm i db-reader ```
Downloads
3
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 表