API References


Database

constructor

new Vasern(props)

Arguments

  • props.schemas (Array<Object>): a list of Document schema in any order.
  • props.version (int): version of Document, 1 by default.

Example

We already created TodoSchema and UserModel above. Let’s create a Vasern instance

import Vasern from 'vasern';

const VasernDB = new Vasern({
    schemas: [UserModel, TodoSchema],
    version: 1
})

collect

Select a collection in order to perform retrieval or modify records

Vasern.collect( collectionName: string )
=> Collection

Arguments

  • collectionName(string): name of collection to be selected.

Example

var peopleCollection = VasernDB.collect('People')

removeAllCollections

Remove all records in all collections

VasernDB.removeAllCollection()
=> void

Argument

N/A

Example

VasernDB.removeAllCollection()

Collection

insert

Insert a new record into selected collection

Collection.insert( record )
=> ResultObject

Argument

  • record(Object | Array<Object>): A record or a list of record to be inserted

Example

VasernDB.collect("people").insert({
    firstName: "John",
    lastName: "Citizen"
}).asPromise()
.then(rs => {
    console.log(rs);
})

update

Modify an existing record from a collection

Collection.update(id, changes)
=> ResultObject

Argument

  • id: existing record primary key
  • changes(Object): an object contains property and value changes

Example

VasernDB.collect("people").update(recordID, {
    firstName: "Lisa"
}).asPromise()
.then(rs => {
    // Insert completed
})
.catch(err => {
    // Handle error
})

remove

Remove an existing record from a selected collection

Collection.remove(id)
=> ResultObject

Argument

  • id<string>: primary key of existing record to be remove

Example

VasernDB.collect("people").remove(`auto_generated_id_1`)
.asPromise()
.then(rs => {
    // Insert completed
})
.catch(err => {
    // Handle error
})

removeAllRecords

Use with caution!

Remove all records from a seleted collection.

Collection.removeAllRecords()
=> ResultObject

Arguments

N/A

Example

VasernDB.collect("people").removeAllRecords()
.asPromise()
.then(rs => {
    // Insert completed
})
.catch(err => {
    // Handle error
})

Query Records

get

Retrieve a record from a collection using primary key

Collection.get(id)

Arguments

  • id<string>: an existing record’s primary key

Example

VasernDB.collect("People").get(`auto_generated_id_1`)
.asPromise()
.then(rs => {
    let item = rs.first(); // item found
})
.catch(err => {
    // Handle error
})

filter

Filter records from a collection that match with query

Collection.filter(query)
=> QueryBuilder

Arguments

  • query<Object>: query filter

Example

VasernDB.collect("People").filter({ firstName: "Peter", yearOfBirth: 1987 })
.asPromise()
.then(rs => {
    let items = rs.items; // matched records
})
.catch(err => {
    // Handle error
})

paging

Note: not work with `limit`

Record filter pagination, return records within a given size and page

QueryBuilder.paging(max, page)
=> QueryBuilder

Arguments

  • max<number>: maxinum number of returned records
  • max<page>: current page, start with 0

Example

VasernDB.collect("People").filter({ firstName: "Peter", yearOfBirth: 1987 })
.paging(10, 2) // return maxinum 10 records, skip the first 20 records
.asPromise()
.then(rs => {
    let items = rs.items; // matched records
})
.catch(err => {
    // Handle error
})

limit

Note: not work with `paging`

Filter records with limit number of returned records

QueryBuilder.limit(max)
=> QueryBuilder

Arguments

  • max<number>: maxinum number of returned records

Example

VasernDB.collect("People").filter({ firstName: "Peter", yearOfBirth: 1987 })
.limit(14) // return maxinum 14 records
.asPromise()
.then(rs => {
    let items = rs.items; // matched records
})
.catch(err => {
    // Handle error
})

sort

Sort result of a record filter

QueryBuilder.sort(propertyName1, propertyName2, ... )

Arguments

  • propertyName<string> name of the property will be used for sorting

Example

VasernDB.collect("People").filter({ firstName: "Peter", yearOfBirth: 1987 })
.sort('yearOfBirth') // return maxinum 14 records
.asPromise()
.then(rs => {
    let items = rs.items; // matched records
})
.catch(err => {
    // Handle error
})

Result Object

asProxy

Only available in JavaScript enviroment that support proxy (i.e not available on React Native for Android)

Return result with a proxy object. Once the request is completed, the proxy result will also be updated


asPromise

Return result with promise object


subscribe

Subscribe a callback handler for any changes

ResultObject.subscribe(callback)

Argument

  • callback<Function> callback handler whenever ResultObject receive a change event

Data types

Data types used to define Vasern data schema.

import { types } from 'vasern';

primary

types.primary(55)

string

types.string(55)

boolean

types.boolean()

number

types.number()

ref

types.ref('Users')