Home > Web Front-end > JS Tutorial > body text

How to implement a local database in javascript? (Detailed analysis)

不言
Release: 2018-09-28 15:57:24
forward
3933 people have browsed it

The content of this article is about how to implement a local database in JavaScript? (Detailed analysis), it has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

The front-end often needs to save some data, and saving here refers to long-term storage. The previous idea was to save the data in the cookie, or save the key in the cookie, and save other data on the server.

These scenes have many uses and are very mature and easy to use. But I still want a kind of data that can be saved locally for a long time, similar to a database or similar to Web SQL.

The new generation of browsers basically support local databases. Just use it directly when you need it. If it doesn't work, you can still use Storage to make do.

What if I need the functionality of a database and there is no compatible storage method? What if I also want to encrypt and store these things locally? What if I want to save a lot of things?

I currently encountered this situation when using React Native. I need to store a lot of data locally. Someone said, wouldn't it be better to use SQLite directly?

Okay, absolutely fine. I am just developing it from a front-end attitude here. What if some students don’t need SQLite, but just need a simple way to store a large amount of data?

There may be many usage scenarios, the bottom layer of the implementation can be replaced at will, and even the implementation method can be written at will. Here I am building an informal, front-end-friendly data repository based on the attitude of the front-end creating the world.

Underlying Storage

The usage scenario here is React Native, so I use RN’s AsyncStorage.

Convert all data to be saved into objects and into strings. The core idea here is serialization. Store all data as strings.

import { AsyncStorage } from 'react-native';

exports.setItem = async (key, value) => {    
let item = JSON.stringify({

       v: value

   });    
   return await AsyncStorage.setItem(key, item);

}

当读取的时候也需要做一次转化,将字符串转成当初存入的数据。

exports.getItem = async (key) => {    
let item = await AsyncStorage.getItem(key);    
if (!item) {        
return null;

   }    
   return JSON.parse(item).v || null;

}
Copy after login

What needs special treatment is the acquisition of the list. RN has an API that returns multiple pieces of data based on multiple keys.

It returns an array object. Array serial number 0 is the key value of data storage, and serial number 1 is the specific string of data storage.

exports.getlist = async (keys) => {    
let list = await AsyncStorage.multiGet(keys);

   list = list.map(item => JSON.parse(item[1]).v || null);    
   return list;

}
Copy after login

Let’s also take out a few other methods used. Nest one more layer here, keeping the same formation as above.

exports.removeItem = async (key) => await AsyncStorage.removeItem(key);

exports.removeItems = async (keys) => await AsyncStorage.multiRemove(keys);

exports.allKeys = async () => await AsyncStorage.getAllKeys();
Copy after login

Underlying Optimization

The above is just a simple implementation. If there are no special requirements, it will be almost the same. However, if you want to go further, you can consider optimization.

For example, optimize the speed of JSON conversion. When we use the JSON object method to convert, there is actually a process of determining the numerical type. If we define the data type in advance. There is no need to judge again when converting.

You can define a model object and pre-define the fields required by this table. You can take a look at how Sequelize is defined. It is very simple to do this according to the relational database method.

//用户对象const users = db.define('t_users', {

   id: {

       type: Sequelize.INTEGER,

       primaryKey: true,

   },    //用户名

   username: {

       type: Sequelize.STRING

   },    //密码

   pwd: {

       type: Sequelize.STRING

   },    //状态

   status: {

       type: Sequelize.INTEGER

   },    //昵称

   nickname: {

       type: Sequelize.STRING

   },    //token

   token: {

       type: Sequelize.STRING

   },

   create_time: {

       type: Sequelize.TIME

   }

}, {

   freezeTableName: true,

   timestamps: false,

});
Copy after login

Implementing storage

Let’s refer to the implementation of relational database here.

First you need to divide the tables and databases. In this way, you can pay less attention to this information when storing data, and focus on data operations.

constructor(tableName = "table", db = "db") {        //检查库,表是否存在

       //初始化索引表

       this.db = db;        
       this.tableName = tableName;        
       this.tableKey = db + "_" + tableName;        
       this.init();

   }
Copy after login

Store them separately inside the current object. When creating the object, you can create different operation methods based on different libraries and tables.

Class is used here, and each table corresponds to a separate object.

Since we are using the storage method provided by RN, the addition and update here are actually the same method.

When adding, a unique id will be created based on the current timestamp, and this id will be used as the key to store it in the database.

So there is no need to store the id separately when using it. However, if you feel that the id is different from what you need, you can also define an id yourself to store as the key value.

//添加和更新

   async add(data = {}) {        
   if (data.constructor !== Object) return;        
   if (!data._id)data._id = uuid();

       await setItem(this.tableKey + "_" + data._id, data);        
       return data;

   }
Copy after login

A separate method of obtaining based on id is provided when obtaining. What is considered here is that it is very simple and convenient to obtain through id. Some data can be read quickly, and there is no need to query line by line.

/**

* 通过id查询

* @param {*} id

*/
async getById(id) {    
if (!id) return {};    
return await getItem(this.tableKey + "_" + id);

}
Copy after login

Compared with querying based on id, fuzzy query is indeed very slow. If it is not really needed, it is better not to use this fuzzy query.

A custom query method is provided here, which can determine whether this row of data is needed based on the returned object.

You can also add the top parameter to limit the number returned. Using this parameter can also improve performance when there is a lot of data.

Query by filtering method

* @param {*} fn

*/async get(fn, top = 0) {    
let keys = await allKeys();    
if (keys.length == 0) return [];    
if (top > 0 && keys.length > top) keys.length = top;    
const listkey = keys.filter(item => item.indexOf(this.tableKey + "_") === 0);    
if (listkey.length == 0) return [];    
let list = await getlist(listkey);

   list = list.filter(item => fn(item));    
   return list;

}
Copy after login

Finally, add the deletion and clearing methods, and a simple deletion library is completed.

/**

* 删除

* @param {*} id

*/
async delete(id) {    
if (!id) return {};

   await removeItem(this.tableKey + "_" + id);

}/**

* 清空表

*/
async clear() {    
let keys = await allKeys();    
const listkey = keys.filter(item => item.indexOf(this.tableKey + "_") === 0);    
if (listkey.length == 0) return;

   removeItems(listkey);

}
Copy after login

When using it, you only need to create the object and then call it where needed. It is simple and convenient to use, and after optimization, it can even be used as client-side Redis.

//初始化数据库
let db=new JSDB();
//添加一个自定义数据
db.add({name:"test",key:"abc"});
//根据id获取数据
db.getById("1223467890");
//根据条件查询数据
db.get(d=>d.name==="test");
//删除对应的数据
db.delete("1223467890");
//情况所有数据
db.clear()
Copy after login

Optimize creation

The first thing to optimize is the creation of objects. Each object creation actually consumes a lot of money. Wouldn't it be great if this consumption could be reduced?

Here we draw on the concept of database pool and implement an object pool method. There is no direct return after the object is created, it has to go through the pool operation.

将对象放入池内,并在页面销毁的时候重置为一个空对象。下次请求创建的时候就不必再创建新的了。直接赋值表、库的名称就可以使用了。内存毫无变化,并且有点想笑。

优化查询

每次查询都需要去读 Stroage 还是很麻烦的,尤其这个操作是异步操作,是需要发消息到 Native 端的。

我们可以将上次读取的数据先存在一个变量中,如果下次还需要使用这行数据,就完全不需要再去读取了。这样就可以很简单的提供读取速度。

//按照使用情况保存数据到缓存
async getById(id) {    
if (!id) return {};

   id = this.tableKey + "_" + id;    //如果有缓存

   if (this.cacheList.has(id)) {        
   let tmp = this.cacheList.get(id);        //如果过量了

       if (this.cacheKeyList.length > 20) {            
       this.cacheKeyList.push(id);            
       let k = this.cacheKeyList.shift();            
       this.cacheList.delete(k);

       }        
       return tmp;

   }    
   this.cacheKeyList.push(id);    
   this.cacheList.set(tmp);    
   return await getItem(id);

}
Copy after login

这个方式还可以继续优化。将变量中保存的数据限制数量,防止数量太多超过了 App 的限制。

还可以将这个保存的时限做一个逻辑判断,常使用的数据放在里面,不常用的就找机会删除。

使用这种方式也可以优化变量中数据的有效性,减少变量占用内存的大小。

不过实现的方式尽量不要使用定时器的形式,可以考虑使用触发式的。在条件满足的时候再触发删除动作。

优化写入

上面提到读取的时候需要放入变量来提高读取速度。我们顺便想到写入的速度是不是也可以提高啊?

我们将要存的数据放在临时的变量里,如果超过我们设置的时间或者数据长度超过我们设置的数量就触发一次保存操作。

这里要注意,保存数据的变量和存入时候使用的变量要交替使用,防止出现丢数据的情况。

比如:存的时候使用变量1,在写到数据库之前,将要存的对象改成变量2,然后读取变量1的数据并存入数据库中。这就是双缓存写入。

当然还是要判断一次 App 退出事件的,如果 App 退出了,请一定要把变量中的内容保存一次,不然你的心血就全丢了。

注意写入要和上面的读取结合起来,不然就会发现写入的数据会有一个很大的延迟。

//添加和更新
async add(data = {}) {    
if (data.constructor !== Object) return;

   data._id = uuid();    
   const key = this.tableKey + "_" + data._id    
   this.writeList.set(key, data);    
   this.write();    //this.read();

   return data;

}
//写入的实现,now立马写入
write(now = false) {    
if (now || this.writeList.size > 20) {        
let tmp = this.writeList;        
this.writeList = this.backWriteList;        
this.backWriteList = tmp;        
this.backWriteList.forEach((v, k) => {

           setItem(k, v);

       })

   }

}
Copy after login

好了,一个简单的数据库就完成了。想要使用的可以先在 npm 上搜索 react-native-jsdb 这个库。我已经将第一部分实现放在了 npm 上,后续的优化也会慢慢地开源出来。

The above is the detailed content of How to implement a local database in javascript? (Detailed analysis). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!