Write Schema, Setup Vasern
Schemas are required to help Documents define its properties data type and validate records.
The following information will help you to start writing schema for Documents,
and create Vasern instance.
Write schema
Note: Vasern has its own id implementation and will be automatically ignore custom id in schema. Custom id will be supported in later version
There are a quick method and a detailed method to write a schema. Use the detailed method if you need extra functions for your Document.
Quick method
// Note: no "id" in "props", it will be automatically ignored otherwise
var TodoSchema = {
name: "Todos",
props: {
name: "string",
note: "?string",
completed: "boolean"
},
assignTo: "#Users"
};
Detailed method
class UserModel {
name = "Users"
// Note: no "id" in "props", it will be automatically ignored otherwise
props = {
fname: "string",
lname: "string",
email: "?string"
}
getRandomUser() {
// Get a random index within a range of 0 to length of records;
var userIndex = Math.floor(Math.random() * this.length());
// Document method that return a record
// using index or record's id
return this.object(userIndex);
}
}
Create Vasern instance
new Vasern(props)
Props (Object) include
- props.schemas (Array<Object>): a list of
Documentschema in any order. - props.version (int): version of
Document,1by 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
})
Access Document
Documents are now initiated, and accessible through VasernDB using its name
// Either
var todoList = VasernDB.Todos.data();
// or
const { Todos, Users } = VasernDB;
var userList = Users.data();
What's next?
Learn about basic CRUD operations or see examples