Lndb

LNDB is a flexible persistent storage database encapsulated by a local file system and supports Node and Electron.

Making: github.com/yansenlei/l…

Using the file system as the base storage and providing a flexible plugin mechanism for upper-level reads and writes means you can customize the read and write functionality to your own needs. See the plugin mechanism for details.

The installation

$ npm install lndb
Copy the code

use

const LNDB = require('lndb')
const db = new LNDB('your/path')
// Initial type
const pg = db.init('page')
// Write data
pg.set('key', {hello: 'lndb! '})

// Read the type information
pg.get('key')

// Delete the cache for the specified key
pg.remove('key')

// Clear all caches under type
pg.clear()
Copy the code

Read and write information

  1. By default, regardless of the type of data saved, pass.get(key)All obtained is a file structure, which is in order to freely obtain file information. As in the example aboveObjectType data is stored by defaultdata.jsonIn:
    ├ ─ garbage /page/ ├ ─ data.jsonCopy the code

    When you go through.get(key)The obtained data looks like this:

    {
      "data.json": {
        "path": "pro/__lndb__/page/key/data.json"
      },
      "data": {
        "hello": "lndb!"}}Copy the code

    If you are usingBuilt-in plug-insWithin theunzipPlugin like this to store files, the file tree looks like this:

    __lndb__ / page / └ ─ ─ key └ ─ ─ files ├ ─ ─ index. The HTML └ ─ ─ the about the HTMLCopy the code

    When you go through.get(key)The obtained data looks like this:

    {
      "files": {
        "path": "pro/__lndb__/page/key/files/"."child": {
          "index.html": {
            "path": "pro/__lndb__/page/key/files/index.html"
          },
          "about.html": {
            "path": "pro/__lndb__/page/key/files/about.html"}}}}Copy the code
  2. If the data is stored as in the example aboveObjectType that loads data when read by defaultget(key).dataObtain data information that can be used if more flexible manipulation is requiredlodashThe plug-in.
  3. Since we are using file names, we name the pair of symbols (\ /) is sensitive, although the program uses it by default__lndb__To replace the sensitive symbols and then rename the file, but I hope you pay attention to avoid the sensitive symbols when using to ensure the normal operation of the program.

Plug-in mechanism

Maybe the default read/write operation does not meet your requirements. It is worth mentioning that the core of Lndb is flexible extension of read/write functions. You can introduce standard plug-ins to replace the existing read/write functions:

const demoPlugin = require('demo-plugin')
// add plugin
db.use('demo-plugin', demoPlugin, { "options": true})

const pg = db.init('page')

pg.set('key', { hello: 'lndb! '}, {
  name: 'demo-plugin'.options: {}})Copy the code
  • .use(name, plugin, options)
    • nameA unique name
    • pluginPlug-in object
    • optionsThe plug-in parameters
  • .set/.get(k, v, {name, options})
    • nameA unique name
    • optionsPlug-in parameters, replacedb.use()When the prefabrication parameters

The plugin specification

Now you can write your own reads to suit your needs, but you must follow the necessary specifications. The plug-in template looks like this:

module.exports = {
  install(Ln, params = {}, options = {}){
    // Your custom operations

    // Call file fetch
    Ln.fls.get(id)
    // Call file write
    Ln.fls.set(params.id, params.value)
    // Get the current data saving path
    Ln.fls.datapath

    return true}}Copy the code

Currently the plugin allows you to do custom operations on set() and get(). Inside the plug-in is an object. The function inside the object is called install and takes three parameters:

  • install(Ln, params, options)
    • LnCurrent read and write context
    • paramsRead/write parameters{id, value}
    • optionsThe configuration information passed in for the plug-in

Built-in plug-ins

Built-in plug-ins are already in the system plug-in list by default and can be used directly in.get().set()

lodash

Read the extension and use the LoDash plugin to flexibly read and write. The plugin references the lowDB usage, which can be very flexible to handle individual files. Refer to the LoDash documentation for details

const pg = db.init('page')

const _ = pg.get('key'['lodash'])
_.setState({hello: 'lndb! '}).write()
_.getState() // { hello: 'lndb! '}
_.has("hello").value() // true
_.update("hello", n => n.toUpperCase()).write() // update -> { hello: 'LNDB!! '}
Copy the code

lodash plugin API

  • _.setState()
    • Writes data to the instance
  • _.getState()
    • Gets data for the current instance
  • _.update()
    • Update the data
  • _.read()
    • Read data from disk to instance, return instance
  • _.write()
    • All write operations need to be called.write()Then write the data to the disk
unzip

Write the extension, unzip the zip file to the specified directory when writing it, which means you can use the.get() method to get the decompressed path and use the file inside

pg.set('key', value, {
  name: 'unzip'
})

pg.get('key')
Copy the code
  • value <Buffer> | <Base64>

License

At present there is still need to optimize the place, welcome the boss to correct. If you have any comments or would like to contribute to this project, please submit Issues or PR.

MIT – yansenlei