Basic CRUD operations
CRUD shorts for create (insert), read (get), update, delete (remove) - basic database operations. The following contents will explain how to use CRUD on Vasern.
Note: Use perform
for multiple operations
Let's use VasernDB
in Todo use case
example created in Write Schema, Setup Vasern.
const { Todos } = VasernDB;
Insert method
Inserting new records into Document
Todos.insert(newRecords, save = true)
Arguments
- newRecord (Object | Array<Object>): new record/records data
- save (boolean): allow Document to write record to disk right away. If set to
false
, Document won't write record to disk right away and add to commited transaction list.
Return
- Array<Object>: a list of created records (indicate executing insert process)
Example
var item1 = Todos.insert({
name: "Setup database for React Native",
note: "Using Vasern",
completed: false
})[0];
Get method
Document use Queryable object to work with records. Which use lodash
to provide other queries such as include
, find
, filter
, exclude
, etc.
Todos.get(lookupQuery)
Arguments
- lookupQuery ( string, Object )
- (string): match record id with
string
value - (Object): match record properties and values with
Object
properties and values
- (string): match record id with
Return
- Object: object that match with lookupQuery
- undefined if not found
Example
var todoItem = Todos.get({ name: "Setup database for React Native" });
Update method
Update existing record with new valuse
Todos.update(lookupQuery, newValues, save = true);
Arguments
- lookupQuery ( string | Object )
- (string): record id
- (Object): record object that contains
id
- save (boolean): allow Document to write record to disk right away. If set to
false
, Document won't write record to disk right away and add to commited transaction list.
Return
- Object: indicates record is found, and execute update process
- false if item not found
Example
var item1 = Todos.get({ name: "Setup database for React Native" });
Todos.update(item1.id, { completed: true });
Remove method
Remove an existing record from Document's records
Todos.remove(lookupQuery, save = true);
Arguments
- lookupQuery ( string | Object )
- (string): record id
- (Object): record object that contains
id
- save (boolean): allow Document to write record to disk right away. If set to
false
, Document won't write record to disk right away and add to commited transaction list.
Return
- boolean: indicate record is found, and execute remove process
Example
var item1 = Todos.get({ name: "Setup database for React Native" });
Todos.remove(item1);
Perform multiple operations
This method commits multiple operations and write all at once. Which optimize for performance.
Note: Don't use Todos.insert
, Todos.remove
or Todos.update
in callback block as it will
create a separate write process.
function callback(db: { insert, update, remove, get }) {
// Execute operations
}
Todos.perform(callback);
Arguments
- callback ( Function ): ({ insert, update, remove, get }): execution callback block.
- insert is a version of Insert method with
save = false
- get is a version of Get method
- remove is a version of Remove method with
save = false
- update is a version of Update method with
save = false
- insert is a version of Insert method with
Return
- No returning value
Example
Todos.perform(function(db) {
// Remove all completed items
// and update incompleted item to completed
Todos.data().forEach(function(item) {
if (item.completed) {
db.remove(item)
} else {
db.update(item, { completed: true })
}
})
})
What's next?
Learn about Queries or see examples