the local storage service provides json databases and sqlite databases.

Import Module

  1. const Storage = require('ee-core').Storage;

Json Database

connection

  1. const jdb = Storage.JsonDB.connection('demo');
  2. # or
  3. let lowdbOptions = {
  4. driver: 'lowdb'
  5. }
  6. const jdb = Storage.JsonDB.connection('demo', lowdbOptions);

jdb.setItem()

  • introduction: Create a stored key/value pair

    1. jdb.setItem('test_key', {name:'xiaoming'})

    jdb.getItem()

  • obtain the stored key value

    1. jdb.getItem('test_key')

    jdb.db

  • introduction: The instantiated lowdb object

  • note: lowdb documentation: https://www.npmjs.com/package/lowdb

usage

  1. # 添加对象和数据
  2. db.defaults({posts: [], user: {}, count: 0})
  3. .write();
  4. db.get('posts')
  5. .push({id: 1, title: 'lowdb is awesome'})
  6. .write()
  7. db.set('user.name', 'typicode')
  8. .write()
  9. db.update('count', n => n + 1)
  10. .write()
  11. 运行程序会在项目中添加db.json文件,里面存储了添加的数据:
  12. {
  13. "posts": [
  14. {
  15. "id": 1,
  16. "title": "lowdb is awesome"
  17. }
  18. ],
  19. "user": {
  20. "name": "typicode"
  21. },
  22. "count": 1
  23. }
  • you can use any powerful lodash functions, such as.get() and.find(), and can use the following functions in series:

    1. db.get('users')
    2. .find({sex: 'male'})
    3. .value()
  • query

you can directly use the lodash function to query. Note that some operations may cause the original data to be modified. To avoid such misoperations, you need to use. cloneDeep(). Operations are inert and will only be executed after calling. value() or. write().
Check whether users exist

  1. db.has('users')
  2. .value()
  • set users

    1. db.set('users', [])
    2. .write()
  • sort and select

    1. db.get('users')
    2. .filter({sex: 'male'})
    3. .sortBy('age')
    4. .take(5)
    5. .value()
  • obtain a specific field

    1. db.get('users')
    2. .map('name')
    3. .value()
  • number obtained

    1. db.get('users')
    2. .size()
    3. .value()
  • obtain specific information

    1. db.get('users[0].name')
    2. .value()
  • update information

    1. db.get('users')
    2. .find({name: 'Tom'})
    3. .assign({name: 'Tim'})
    4. .write()
  • remove Properties

    1. db.unset('users.name)
    2. .write()
  • deep copy

    1. db.get('users')
    2. .cloneDeep()
    3. .value()
  • use id index

    can be used shortidcreate a unique id index for each record in the database, and then retrieve the operation records by id: ``` const shortid = require(‘shortid’)

const postId = db .get(‘posts’) .push({ id: shortid.generate(), title: ‘low!’ }) .write() .id

const post = db .get(‘posts’) .find({ id: postId }) .value()

  1. <a name="dM3Fx"></a>
  2. ### **Sqlite Database **
  3. <a name="iLfey"></a>
  4. #### **connection**

// sqlite let sqliteOptions = { driver: ‘sqlite’, default: { timeout: 6000, verbose: console.log } } const sdb = Storage.JsonDB.connection(‘sqlite-demo.db’, sqliteOptions);

  1. <a name="snM73"></a>
  2. #### sdb.db

add data

const insert = sdb.db.prepare(INSERT INTO ${table} (name, age) VALUES (@name, @age)); insert.run(data); ```