Skip to content
Will Wen Gunn edited this page Jul 13, 2015 · 1 revision

This page help you learning to build your Web App with MinDB. Just calm down and follow up.

Above of all

MinDB is a port of data-operating on JavaScript in browsers or anywhere else, which will help you control the data store and data structure easier.
Evan all the Web Apps need to deal with the data which are created by users or downloaded from servers but native JavaScript dose not have a good solution on data-storing and data-operating.
JavaScript is weak in Data Structures(but there are somethings new in ES2015). So, MinDB copied Redis's API design style and built it with JavaScript, and use [Store Interface] (https://github.com/iwillwen/mindb/wiki/Store-Interfaces) for achieving a reusable data-storing protocol.

Concepts Overview

Key

MinDB is a key-value database management system, just like DOM Storage.

min.set('name', 'iwillwen', (err) => {
  if (err) return console.error(err)

  min.get('name', (err, value) => {
    if (err) return console.error(err)

    console.log(value) //=> iwillwen
  })
})

Each key contains a String by default. Of course, it can be an operational number.

min.incr('user:id:seq', (err, currId) => {
  if (err) return console.error(err)
  
  console.log(currId) //=> 1, 2, 3, ...
})

Click here to read more about the commands of keys.

Hashes

Hash is a type of data structure like Map or Dictionary.

min.hmset('me', {
  name: 'Will Wen Gunn',
  sign: 'iwillwen',
  homepage: 'http://lifemap.in'
}, err => {
  if (err) return console.error(err)

  min.hget('me', 'name', (err, value) => {
    if (err) return console.error(err)

    console.log(value) //=> Will Wen Gunn
  })
})

Use a number to be a sequence for collection elements' index.

// Here we use Promises
var prefix = 'demo:'
var insert = (obj) => {
  return new Promise((resolve, reject) => {
    min.incr(prefix + 'id:seq')
      .then(currId => {
         obj.id = currId
         return min.hmset(`${prefix}:obj:${currId}`, obj)
      })
      .then(_ => resolve(id))
      .catch(reject)
  })
}

var fetch = (id) => {
  return min.hgetall(`${prefix}:obj:${id}`)
}

insert({ foo: 'bar' })
  .then(id => fetch(id))
  .then(obj => console.log(obj))   //=> { foo: 'bar' }
  .catch(err => console.error(err))

More commands here.

Lists

List is a type of data structure like Array in JavaScript. From a very general point of view a List is just a sequence of ordered elements: 10,20,1,2,3 is a list.

var key = 'list'
min.multi()
  .lpush(key, 1) // prepend a element to the list
  .lpush(key, 2)
  .rpush(key, 3) // append a element to the list
  .exec(_ => {
  
  	min.llen(key).then(len => min.lrange(key, 0, len - 1 /* the last index */))
  	  // Or using min.get(key)
  	  .then(list => {
  	    console.log(list) //=> 2, 1, 3
  	    
  	    return min.lindex(2)
  	  })
  	  .then(value => {
  	  	console.log(value) //=> 3
  	  
  	  	return min.ltrim(key, 1, 2)
  	  })
  	  .then(_ => min.get(key))
  	  .then(list => console.log(list)) //=> 1, 3

  })

TBD

Clone this wiki locally