Skip to content

1. Quickstart

Hieu Nguyen (Jack) edited this page Feb 1, 2019 · 2 revisions

1. Setup a ClientDB instance

var stores = [{
    name: "Users",
    // version: 1, need to increase by 1 when keys or collections need to be updated
    // name: "__default_clientDB",
    // onerror: (err) => {}, // error if cannot open a database instance
    // onsucess: () => {}, // callback when database is successfully opened
    keys: {
        // _id: true, // automatically added, will throw error if you add "_id"
        firstName: false,
        lastName: false,
        username: true
    }
}]
var myDB = new ClientDB({ stores });

2. Working with data

Insert record

// Insert
myDB.collect("Users").insert({
    _id: 'your_own_id', // add your own id 
    firstName: 'Peter',
    lastName: 'Griffin',
    username: 'peter_grif' // unique field, will throw error if found duplicates
})
.then(({ items, changes}) => {
    
    // items => Array(1) => [{ added record }]
    // changes => { inserted: 1, removed: 0, updated: 0, unchage: 0 }
})
.catch(error => {

    // error => { message }
})

Retrieve records

myDB.collect("Users").filter({ 
    firstName: 'Peter'
})
.then(({ items, changes}) => {
    
    // items => Array(1) => [{ added record }]
    // changes => { inserted: 1, removed: 0, updated: 0, unchage: 0 }
})
.catch(error => {

    // error => { message }
})

Update records

// Update firstName from Peter to Bryan

myDB.collect("Users").update("your_own_id",  firstName: 'Bryan' })
.then(({ items, changes}) => {
    
    // items => Array(1) => [{ added record }]
    // changes => { inserted: 1, removed: 0, updated: 0, unchage: 0 }
})
.catch(error => {

    // error => { message }
})

Delete records

// remove receive an id or an array of ids
myDB.collect("Users").remove("your_own_id")
.then(({ items, changes}) => {
    
    // items => Array(1) => [{ added record }]
    // changes => { inserted: 1, removed: 0, updated: 0, unchage: 0 }
})
.catch(error => {

    // error => { message }
})
Clone this wiki locally