The project is based on model objects that manage and handle object-type queries. There is a base object called
DataModel that loads the models defined with Sequelize or Sequelize-Auto, providing a simple interface to interact
with them. On the other hand, the Action class defines the actions that need to be performed in the database to
achieve the desired impact.
As you progress through the project, more details will be provided about its functionality and best practices for its
use.
Now that you have both objects, you can begin working with them. Below are the usages of each method for the Data Model:
-
The DEFAULT
property allows you to modify the default values for order
, limit
, and start
(offset). It
defaults to "timeCreated"
, 30
, 0
, respectively.
Actions.DEFAULT = { order: 'timeCreated', limit: 30, start: 0 };
-
The OPERATORS
property returns a list of supported operators as keys in the object for queries.
console.log(actions.OPERATORS);
-
The op
property allows the use of other Sequelize operators for custom queries.
-
The processFilters
function allows you to build filters for querying via Sequelize.
const where = {
id: [10, 30, 40, 50],
date: {
between: ['2020-01-01', '2020-12-31'],
},
and: {
name: 'pedro',
quantity: {
gt: 10,
},
or: { status: 1 },
},
};
const filter = actions.processFilters(DataModel.models.Modelo, where);
-
The list
function allows you to list a query. For example:
const params = {
order: 'id',
asc: 'ASC',
limit: 10,
start: 10,
attributes: ['id', 'name', 'quantity'],
where: {
id: [10, 30, 40, 50],
date: {
between: ['2020-01-01', '2020-12-31'],
},
and: {
name: 'pedro',
quantity: {
gt: 10,
},
or: { status: 1 },
},
},
};
const results = await actions.list(model, params, target);
Where model
is the models contained in DataModel.models
, target
is the location where the error occurred, and
params
is distributed as follows:
limit
: limit of elements, defaults to 30.start
: offset of the elements to retrieve, defaults to 0.order
: field to order by.asc
: boolean to indicate whether it is ascending or descending.where
: where format as in the previous example.attributes
: indicates the attributes to display as an array of strings.
-
The data
function retrieves information for a single record from the database using an id. For example:
const params = {
id: 2,
};
const data = await actions.data(model, params, target);
Where model
is the models contained in DataModel.models
, target
is the location where the error occurred, and
params
is the id of the record.
-
The remove
function aims to delete a record by id.
const params = {
id: 2,
};
const remove = await actions.remove(params, model, target);
Where model
is the models contained in DataModel.models
, target
is the location where the error occurred, and
params
is the id of the record.
-
The publish
function allows you to pass the attributes to update or create the record. If the id is included in
the parameters, it updates the record; otherwise, it creates a new one. Example:
const create = {
name: 'Tomas',
phone: '+xxxxxxxxxxxx',
};
const update = {
id: 1,
name: 'Pedro',
};
const publish = await actions.publish(model, create, target);
const publish = await actions.publish(model, update, target);
-
The bulkSave
function allows for an upsert of a massive quantity.
const params = [
{
name: 'Maria',
},
{
id: 2,
name: 'tomas',
},
];
const saveAll = await actions.bulkSave(model, params, target);
We want to thank all the individuals who have contributed to this project and the resources we have used.