背景

  1. 学习前端新框架、新技术。如果需要做一些数据库的操作来增加demo的体验(CURD流程可以让演示的体验根据丝滑)
  2. 最开始的时候一个演示程序我们会调用后台,这样其实有一点弊端,就是增加了开发和维护成本,简单的一个demo不应该劳师动众
  3. 后来我会在demo中使用一些websql,奈何,websql也真的是没前景了。代码写起来也不是特别好
  4. 下面来介绍下今天的主角indexedDB和jsStore

介绍

  1. indexedDB可以给浏览器本地存储的能力,并且容量还比较大。
  2. jsStore只是众多封装的indexedDB库中的一个。可以用一种类似SQL的感觉操作数据

开始

package.json

{
"name": "npm1",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"lint": "eslint scripts/**",
"fix": "eslint scripts/** --fix",
"serve": "webpack serve"
},
"dependencies": {
"jsstore": "^4.4.4",
"lodash": "^4.17.21"
},
"devDependencies": {
"css-loader": "^6.7.1",
"eslint": "^8.23.1",
"eslint-config-google": "^0.14.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.2.1",
"file-loader": "^6.2.0",
"html-webpack-plugin": "^5.5.0",
"prettier": "2.7.1",
"style-loader": "^3.3.1",
"webpack": "^5.74.0",
"webpack-cli": "^4.10.0",
"webpack-dev-server": "^4.11.1"
},
"author": "",
"license": "ISC"
}

webpack配置,添加了devServer配置

//webpack.config.js
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin"); module.exports = {
context: path.resolve(__dirname),
devServer: {
static: {
directory: path.join(__dirname, "dist"),
},
compress: true,
port: 9000,
},
mode: "production",
optimization: {
minimize: false,
},
entry: "./src/main.js",
target: ["web", "es5"],
output: {
clean: true,
filename: "bundle.js",
path: path.resolve(__dirname, "dist"),
},
plugins: [
new HtmlWebpackPlugin({
template: "index.html",
}),
],
module: {
rules: [
{
test: /\.css$/i,
use: ["style-loader", "css-loader"],
},
],
},
};

jsStore链接帮助类,结合webpack+webworker。安装了file-loader

//store-connection.js
import { Connection } from "jsstore";
const getWorkerPath = () => {
// return dev build when env is development
if (process.env.NODE_ENV === "development") {
return require("file-loader?name=scripts/[name].[hash].js!jsstore/dist/jsstore.worker.js");
} else {
// return prod build when env is production return require("file-loader?name=scripts/[name].[hash].js!jsstore/dist/jsstore.worker.min.js");
}
}; const workerPath = getWorkerPath().default;
export const connection = new Connection(new Worker(workerPath));

主逻辑

//main.js
import { connection } from "./store-connection"; async function init() {
var dbName = "JsStore_Demo";
var tblProduct = {
name: "Product",
columns: {
// Here "Id" is name of column
id: { primaryKey: true, autoIncrement: true },
itemName: { notNull: true, dataType: "string" },
price: { notNull: true, dataType: "number" },
quantity: { notNull: true, dataType: "number" },
},
};
var database = {
name: dbName,
tables: [tblProduct],
}; const isDbCreated = await connection.initDb(database);
if (isDbCreated === true) {
console.log("db created");
// here you can prefill database with some data
} else {
console.log("db opened");
} var value = {
itemName: "Blue Jeans",
price: 2000,
quantity: 1000,
}; var insertCount = await connection.insert({
into: "Product",
values: [value],
}); console.log(`${insertCount} rows inserted`); // results will be array of objects
var results = await connection.select({
from: "Product",
}); results.forEach((item) => {
console.log(item);
});
} window.addEventListener("load", function () {
console.log(connection);
init();
});

数据已经存进去了



API插入、查询也没什么问题

总结

  1. 使用indexDB强化自己的demo体验,避免搭建后端环境,增加复杂度
  2. jsStore 的API多了解下,并且涉及的indexedDB的API都是异步的
  3. API没有啥,主要就是打开链接,事务,CRUD。语法参考下官网的例子即可

https://jsstore.net/tutorial/get-started/

最新文章

  1. TFS2013 设置签出独占锁
  2. h5手机页面禁止缩放
  3. Cannot initialize Cluster. Please check your configuration for mapreduce.framework.name and the co
  4. 自定义的 ListBoxItem 自适应ListBox的宽度
  5. Android 获取本地图片
  6. Cisco IOS Software Activation Command Reference
  7. Linux OpenCV读取视频失败,cvCreateFileCapture失败的解决
  8. Java内存区域之程序计数器--《深入理解Java虚拟机》学习笔记及个人理解(一)
  9. SpringBoot的启动流程分析(1)
  10. JS IOS/iPhone的Safari浏览器不兼容Javascript中的Date()问题的解决方法
  11. poj 3352 Road Construction(边双连通分量+缩点)
  12. vue-watch监听路由的变化
  13. NET设计模式 第二部分 行为型模式(17):迭代器模式(Iterator Pattern)
  14. vue中$ref的基本用法
  15. Linux网络编程--sendfile零拷贝高效率发送文件
  16. mysql分页(ajax)
  17. 24、List三个子类的特点
  18. ProtocolBuffer 使用及 一些坑
  19. activemq用户手册
  20. Android 画闹钟

热门文章

  1. Webpack干货系列 | 怎么运用 Webpack 5 处理css/scss/sass、less、stylus样式资源
  2. EFCore高级Saas系统下一个DbContext如何支持多数据库迁移
  3. Android开启OTG功能/USB Host API功能
  4. 常用的函数式接口_Predicate接口和常用的函数式借楼_Predicate_默认方法and
  5. YII behaviors使用
  6. YII服务定位器依赖注入
  7. CMake教程——Leeds_Garden
  8. 【Azure 事件中心】使用Azure AD认证方式创建Event Hub Consume Client + 自定义Event Position
  9. GreatSQL FAQ
  10. Sphere类定义