README
xvi-lowdb-express
Exposes a simple API for lowdb package using express server
Example
//Include package
const Database = require('xvi-lowdb-express');
//instanciate DB
var db = new Database({
dbFolder: '', //folder which contains the json files for each models db
enableApi: true, //if true, will try to add routes to the server in order to access the stored data as an API endpoint
apiPath: '/api/', //base route of the api on the express server
existingServer: false, //one can pass an existing express server instance. if set to false, a new express server will be set
port: 3000 //port used for the express server, if no existingServer provided
});
//test function
async function test() {
try {
//Adding entries to the "person" model, stored into person.json in the dbFolder
// if the entry does not exist, it will be created
// will add the properties _created, _updated, _changed which are dates used to track the evolution of the record
// if the entry already exists, will check if a change has been done (using md5 hash of the item)
// if a change has been done, will update the record, its new hash and the _changed property (date created using moment package)
// if no change has been done, it will update the _updated date property (date created using moment package)
await db.update('person', {
_id: 'some-hash-or-other-id-1', // required field to insert a record
name: 'Benoit',
job: 'Engineer'
});
await db.update('person', {
_id: 'some-hash-or-other-id-2',
name: 'Eric',
job: 'Engineer'
});
//Information about DB, same as lowdb Package
db._('person'); //access the lowdb object for model person
db._('person').get('person').values(); // => returns all persons
//API
// http://127.0.0.1:3000/api/person?format=json => will return all persons as json
// http://127.0.0.1:3000/api/person?format=csv => will return all persons as csv
} catch (err) {
console.log(err);
}
}
test();