model

Installation
npm install @janiscommerce/model
Client injection
The client injection is useful when you have a dedicated database per client.
Using the public setter client
, the client will be stored in the controller
instance.
All the controllers and models getted using that controller will have the client injected.
Database Dispatcher
The Model
uses Database Dispatcher for getting the correct DBDriver for a Model
.
The DBDriver will perform all the queries to the database.
Configure Database connection with databaseKey
If you have the connection settings you should add a databaseKey
getter in you Model
.
class MyModel extends Model {
get databaseKey() {
return 'core';
}
}
Database Dispatcher will try to use one of the following settings
- Using Settings, with settings in file
/path/to/root/.janiscommercerc.json
:
{
"database": {
"core": {
"host": "http://my-host-name.org",
"type": "mysql",
}
}
}
- Using ENV variables:
DB_CORE_HOST = "http://my-host-name.org";
DB_CORE_DATABASE = "db-name";
DB_CORE_USER = "user";
DB_CORE_PASSWORD = "foo123456";
Database connection configurated with client injected
When your Model
is a Client Model, and the database connection settings are in the client injected, you don't need to configurate the databaseKey
.
You can add settings for the fields in the connection, the fields are the following.
For settings the package use Settings.
Field | Default value | Description |
---|
clients.fields.read.type | dbReadType | The type for DB Read (mylsq, mongodb) |
clients.fields.read.host | dbReadHost | The host for DB Read |
clients.fields.read.protocol | dbReadProtocol | The database protocol for DB Read |
clients.fields.read.database | dbReadDatabase | The database name for DB Read |
clients.fields.read.user | dbReadUser | The database username for DB Read |
clients.fields.read.password | dbReadPassword | The database password for DB Read |
clients.fields.read.port | dbReadPort | The database port for DB Read |
clients.fields.write.type | dbWriteType | The type for DB Write (mylsq, mongodb) |
clients.fields.write.host | dbWriteHost | The host for DB Write |
clients.fields.write.protocol | dbWriteProtocol | The database protocol for DB Write |
clients.fields.write.database | dbWriteDatabase | The database name for DB Write |
clients.fields.write.user | dbWriteUser | The database username for DB Write |
clients.fields.write.password | dbWritePassword | The database password for DB Write |
clients.fields.write.port | dbWritePort | The database port for DB Write |
Example of settings:
{
"clients": {
"fields": {
"read": {
"type": "dbReadType",
"host": "dbReadHost",
"protocol": "dbReadProtocol",
"database": "dbReadDatabase",
"user": "dbReadUser",
"password": "dbReadPassword",
"port": "dbReadPort"
},
"write": {
"type": "dbWriteType",
"host": "dbWriteHost",
"protocol": "dbWriteProtocol",
"database": "dbWriteDatabase",
"user": "dbWriteUser",
"password": "dbWritePassword",
"port": "dbWritePort"
}
}
}
}
API
const items = await myModel.get(params)
- Returns items from database
Params is an optional Object with filters, order, paginator.
const items = await myModel.get({ filters: { status: 'active' } });
myModel.getPaged(params, callback)
- Returns items from database using pages, the default limit is 500 items per page.
await myModel.getPaged({ filters: { status: 'active' } }, (items, page, limit) => {
});
myModel.getTotals()
- After performing a
get()
sometimes you need data of totals. This method returns an object with that information.
Result object structure:
pages: The total pages for the filters applied
page: The current page
limit: The limit applied in get
total: The total number of items in DB for the applied filters
await myModel.get({ filters: { status: 'active' } });
const totals = await myModel.getTotals();
myModel.insert(item)
- Insert an item in DB. This method is only for insert, will not update perform an update.
await myModel.insert({ foo: 'bar' });
const items = await myModel.get({ filters: { foo: 'bar' }});
myModel.save(item)
- Insert/update an item in DB. This method will perfrom an upsert.
await myModel.save({ foo: 'bar' });
const items = await myModel.get({ filters: { foo: 'bar' }});
myModel.update(values, filter)
- Update items that match with the
filter
.
await myModel.update({ updated: 1 }, { status: 5 });
myModel.remove(item)
await myModel.remove({ foo: 'bar' });
const items = await myModel.get({ filters: { foo: 'bar' }});
myModel.multiInsert(items)
- Perform a bulk insert of items in DB. This action will insert elements, and will not update elements.
await myModel.multiInsert([{ foo: 1 }, { foo: 2 }]);
const items = await myModel.get();
myModel.multiSave(items)
- Perform a bulk save of items in DB. This action will insert/update (upsert) elements.
await myModel.multiSave([{ foo: 1 }, { foo: 2 }]);
const items = await myModel.get();
myModel.multiRemove(filter)
- Perform a bulk remove of items in DB.
await myModel.multiRemove({ status: 2 });
const items = await myModel.get({ filters: { status: 2 }});