Use

Install Modli

To get started, you need to have the Modli core installed in your project:

npm install modli --save

Setup The Datasource

The example below shows a simple example of setting up and using Modli with the NeDB adapter.

First, install the adapter:

npm install modli-nedb --save

Then add the adapter, create a model, and use them together as an object to interact with the datasource.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 // Import modli and the NeDB adapter import { model , adapter , use , Joi } from 'modli' ; import nedb from 'modli-nedb' ; // Add an adapter instance adapter . add ({ name : 'modliNeDB' source : nedb , config : { inMemoryOnly : true } }); // Add a data model model . add ({ name : 'modliTest' , version : 1 , schema : { id : Joi . number (). integer (), fname : Joi . string (). min ( 3 ). max ( 30 ), lname : Joi . string (). min ( 3 ). max ( 30 ), email : Joi . string (). email (). min ( 3 ). max ( 254 ). required () } }); // Create an instance of the datasource object with the Model and Adapter const myDataSource = use ( 'modliTest' , 'modliNeDB' );

Now the myDataSource object can be acted upon using the CRUD methods available:

Interact with the Datasource

For example, you could add to the datastore with the following:

1 2 3 4 5 6 7 8 9 10 11 12 myDataSource . create ({ id : 12345 , fname : 'John' , lname : 'Smith' , email : 'jsmith@email.com' }) . then (( result ) => { console . log ( 'Success:' , result ); }) . catch (( err ) => { console . log ( 'Error:' , err ); });

You could then read the created record with the following:

1 2 3 4 5 6 7 myDataSource . read ({ id : 12345 }) . then (( results ) => { console . log ( 'Success:' , results ); }) . catch (( err ) => { console . log ( 'Error:' , err ); });