JsStore是IndexedDB的包装器。它提供了简单的SQL像api,这是容易学习和使用的。
IndexedDb查询可以在web worker内部执行,JsStore通过提供一个单独的worker文件来保持这种功能。
因此查询可以以两种方式执行——在web worker内部或在web worker之外。建议使用web worker,因为它在后台线程中运行脚本。
在之前几篇我们使用的Indexed是相关函数api,实现某些功能难度较大,但使用JsStore之后就会比较方便了。另外在JsSotre中,对于数据库模型更新更为便捷;也就是说如果初期定义表字段类型,或需要新增字段等,可以通过alter进行更新。
一、数据库模型更新
数据库模式可以通过:
- 递增数据库对象中的版本
- 在表的alter字段中定义要更改的内容。
这里我们使用JsStore基础语法中定义的数据记录表,来演示数据库模型更新范例,如对上章节不了解,可以点击以下链接进行查看:IndexedDB的包装器JsStore - 基础语法_觉醒法师的博客-CSDN博客
1.1 创建数据库连接
代码如下:
import { Connection } from "jsstore";
import workerInjector from "jsstore/dist/worker_injector";
let connection = new Connection();
connection.addPlugin(workerInjector);
export default connection;
1.2 定义和初始化数据库模型
代码如下:
import connection from './index.js';
import { DATA_TYPE } from "jsstore";
const getDatabase = () => {
//用户表
const UserTable = {
name: "Users",
columns: {
id: { primaryKey: true, autoIncrement: true },
username: { notNull: true, dataType: DATA_TYPE.String },
password: { notNull: true, dataType: DATA_TYPE.String },
role: { notNull: true, dataType: DATA_TYPE.Number },
token: { notNull: false, dataType: DATA_TYPE.String },
createtime: { notNull: true, dataType: DATA_TYPE.DateTime },
updatetime: { notNull: true, dataType: DATA_TYPE.DateTime },
}
}
//岗位管理
const PostTable = {
name: "Post",
columns: {
id: { primaryKey: true, autoIncrement: true },
name: { notNull: true, dataType: DATA_TYPE.String },
remark: { notNull: false, dataType: DATA_TYPE.String },
createtime: { notNull: true, dataType: DATA_TYPE.DateTime },
updatetime: { notNull: true, dataType: DATA_TYPE.DateTime },
}
}
const dataBase = {
name: "demo_manage",
tables: [UserTable, PostTable],
version: 1
};
return dataBase;
}
export const initJsStore = async () => {
const dataBase = await getDatabase();
return await connection.initDb(dataBase);
}
1.3 更新模型参数
名称 | 描述 |
modify | 修改对应字段属性或数据类型 |
add | 添加新的字段 |
drop | 删除指定字段 |
IndexedDb是一种浏览器数据库技术,这意味着如果你在你的web应用程序中做了一些更改,任何使用你的web应用程序的人都应该得到最新的更改,包括数据库的更改。
当indexedb初始化时,db版本大于当前db版本时,浏览器决定更改db模式。
此时,我们可以通过alter进行数据库模型的更新迭代了。
1.4 更新字段属性和数据类型
用户表Users中角色,我们需要改成非必填,并且数据类型改成String;另外当前版本为1,要使其生效则需要将version改成2;代码如下:
//用户表
const UserTable = {
name: "Users",
columns: {
id: { primaryKey: true, autoIncrement: true },
username: { notNull: true, dataType: DATA_TYPE.String },
password: { notNull: true, dataType: DATA_TYPE.String },
role: { notNull: true, dataType: DATA_TYPE.Number },
token: { notNull: false, dataType: DATA_TYPE.String },
createtime: { notNull: true, dataType: DATA_TYPE.DateTime },
updatetime: { notNull: true, dataType: DATA_TYPE.DateTime },
},
alter: {
2: {
modify: {
role: { notNull: false, dataType: DATA_TYPE.String }
}
}
}
}
const dataBase = {
name: "demo_manage",
tables: [UserTable],
version: 2
};
1.5 新增字段
用户表Users中,我们需要记录用户登录的时间,由于上一示例版本为2,此时版本需要修改为3,代码如下:
//用户表
const UserTable = {
name: "Users",
columns: {
id: { primaryKey: true, autoIncrement: true },
username: { notNull: true, dataType: DATA_TYPE.String },
password: { notNull: true, dataType: DATA_TYPE.String },
role: { notNull: true, dataType: DATA_TYPE.Number },
token: { notNull: false, dataType: DATA_TYPE.String },
createtime: { notNull: true, dataType: DATA_TYPE.DateTime },
updatetime: { notNull: true, dataType: DATA_TYPE.DateTime },
},
alter: {
2: {
modify: {
role: { notNull: false, dataType: DATA_TYPE.String }
}
},
3: {
add: {
logintime: { notNull: false, dataType: DATA_TYPE.DateTime }
}
}
}
}
const dataBase = {
name: "demo_manage",
tables: [UserTable],
version: 3
};
代码执行后如下图,数据表中增加了logintime字段。
1.6 删除字段
用户表Users中,如果不需要更新时间,将版本号4中,添加删除updatetime字段,代码如下:
//用户表
const UserTable = {
name: "Users",
columns: {
id: { primaryKey: true, autoIncrement: true },
username: { notNull: true, dataType: DATA_TYPE.String },
password: { notNull: true, dataType: DATA_TYPE.String },
role: { notNull: true, dataType: DATA_TYPE.Number },
token: { notNull: false, dataType: DATA_TYPE.String },
createtime: { notNull: true, dataType: DATA_TYPE.DateTime },
updatetime: { notNull: true, dataType: DATA_TYPE.DateTime },
},
alter: {
2: {
modify: {
role: { notNull: false, dataType: DATA_TYPE.String }
}
},
3: {
add: {
logintime: { notNull: false, dataType: DATA_TYPE.DateTime }
}
},
4: {
drop: {
updatetime: { }
}
}
}
}
const dataBase = {
name: "demo_manage",
tables: [UserTable],
version: 4
};
代码执行后如下图,updatetime字段不存在了。
二、数据库记录表删除
dropDb用于从浏览器存储中删除当前数据库。代码如下:
connection.dropDb().then(function() {
console.log('Db deleted successfully');
}).catch(function(error) {
console.log(error);
});;
三、关闭数据库连接
终止关闭连接并释放所有内容。代码如下:
await connection.terminate();
console.log("connection terminated");
但是要重新启动连接时,必须再次创建连接,重新执行new Connection()。