🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] ## 概述 一个带有 SQL 语法的 IndexedDB 包装器。 ## demo ``` // 创建数据库 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', where: { id: 5 } }); alert(results.length + 'record found'); // 更新数据 var rowsUpdated = await connection.update({ in: 'Product', where: { itemName: { like: '%black%' } }, set: { quantity: 2000 } }); alert(rowsUpdated + ' rows updated'); // 删除数据 var rowsDeleted = await connection.remove({ from: 'Product', where: { id: 10 } }); alert(rowsDeleted + ' record deleted'); ```