@on-time/core
Version:
core business logic for OT API
63 lines (55 loc) • 1.4 kB
JavaScript
// const Mapper = require('@on-time/util/mapper');
class Repository {
constructor(model){
this.model = model;
this.create = this.create.bind(this);
this.getAll = this.getAll.bind(this);
this.getById = this.getById.bind(this);
this.update = this.update.bind(this);
this.delete = this.delete.bind(this);
}
/**
* Creates a new record in database
* @param data
* @returns {Promise<void>}
*/
async create(data){
const d = await this.model.create(data);
return d;
}
/**
* Gets all records matching query
* @param query
* @returns {Promise<*>}
*/
async getAll(query){
const data = await this.model.find(query);
return data;
}
/**
* Gets the record matching the ID
* @param id
* @returns {Promise<void>}
*/
async getById(id){
return this.model.findById(id);
}
/**
* Removes the record with a matching ID
* @param id
* @returns {Promise<void>}
*/
async delete(id){
return this.model.findByIdAndDelete(id);
}
/**
* Updates a record with a matching ID.
* @param id
* @param data
* @returns {Promise<void>}
*/
async update(id, data){
return this.model.findByIdAndUpdate(id, data, { new: true });
}
}
module.exports = Repository;