Database API lets developers interact with the database from the document script. It is useful when developing features which require getting and saving data directly to the database.
daffy.db.get_doc
Fetches the document from database based on document type and name. If using filters returns the first document that matches with filters.
// based on name
daffy.db.get_doc('Task', 'TASK00001')
.then(doc => {
console.log(doc)
})
// based on filters
daffy.db.get_doc('Task', null, { status: 'Open' })
.then(doc => {
console.log(doc)
})
daffy.db.get_list
Fetches a list of records with specified document type, fields and filters if any,
daffy.db.get_list('Task', { fields: ['subject', 'description'],
filters: { status: 'Open' } })
.then(records => { console.log(records); })
Fetches one or more field values of a specified document type.
// single value
daffy.db.get_value('Task', 'TASK00002', 'status')
.then(r => { console.log(r.message.status) // Open })
// multiple values
daffy.db.get_value('Task', 'TASK00002', ['status', 'subject'])
.then(r => { let values = r.message;
console.log(values.status, values.subject) })
// using filters
daffy.db.get_value('Task', {status: 'Open'}, 'subject')
.then(r => { let values = r.message;
console.log(values.subject) })
daffy.db.get_single_value
Save and update document fields on the database.
// save a field’s value
daffy.db.set_value('Task', 'TASK00002', 'status', 'Open')
.then(r => { let doc = r.message;
console.log(doc);\ })
// save multiple fields
daffy.db.set_value('Task', 'TASK00002', { status: 'Working', priority: 'Medium' })
.then(r => { let doc = r.message;
console.log(doc); })
daffy.db.insert
Insert a new document object of specified type.
daffy.db.insert({ doctype: 'Task', subject: 'New Task' })
.then(doc => { console.log(doc); })
daffy.db.count
Count the number of records based on document type or filters.
// total number of records
daffy.db.count('Task')
.then(count => { console.log(count) })
// total number of Open Tasks
daffy.db.count('Task', { status: 'Open' })
.then(count => { console.log(count) })
daffy.db.exists
Returns True/False if a record exists
daffy.db.exists('Task', 'TASK00003')
.then(exists => { console.log(exists) })
daffy.db.delete_doc
Delete a specified record based on document type and name.
daffy.db.delete_doc('Task', 'TASK00002')