ice-db
Version:
In-memory DB
191 lines (178 loc) • 3.99 kB
JavaScript
const {
Object
} = require('./object');
class Collection {
/**
*Creates an instance of Collection.
* @param {*} {
* name
* }
* @memberof Collection
*/
constructor({
name
}) {
this.name = name;
this.data = []
}
/**
*Checks if an id of an object is defined
*
* @param {Object} object object to check
* @throws error if id is undefined
* @memberof Collection
*/
_checkIdDefined(object) {
if (!object.id) {
throw {
code: 400,
message: 'ID is undefined',
object
}
}
}
/**
*Checks if a rev of an object is defined
*
* @param {Object} object object to check
* @throws error if rev is undefined
* @memberof Collection
*/
_checkRevDefined(object) {
if (!object.rev) {
throw {
code: 400,
message: 'Rev is undefined',
object
}
}
}
/**
*Checks if rev is valid
*
* @param {*} objectOne An object which is given by a user
* @param {*} objectTwo An object with a valid rev
* @memberof Collection
* @throws error if rev is not valid
*/
_checkRevValid(objectOne, objectTwo) {
if (!(objectOne.rev === objectTwo.rev)) {
throw {
code: 409,
message: `Conflict: revision number ${object.rev} is not latest`
}
}
}
/**
*Clears all data in this collection
*
* @memberof Collection
*/
drop() {
this.data = []
delete this
}
/**
*Inserts a new object ino collection
*
* @param {*} object object to create
* @returns {Object} created object
* @memberof Collection
*/
insert(object) {
let newObject = new Object(object)
this.data.push(newObject)
return newObject
}
/**
*Gets an object from the colection by a given id
*
* @param {String} id id of an object
* @returns {Object} an objet with a given id
* @memberof Collection
*/
get(id) {
return this.data[this._getIndexInData(id)]
}
/**
*Gets all objects in a collection
*
* @returns {[Object]} all objects in a collection
* @memberof Collection
*/
list() {
return this.data
}
/**
*Returns an index in data array by a given object id
*
* @param {String} id id of an object
* @returns {Number} an index
* @throws error if id is not defined
* @throws error if index is not found
* @memberof Collection
*/
_getIndexInData(id) {
if (!id) {
throw {
code: 400,
message: `Id is not defined`
}
}
let index
this.data.forEach((obj, i) => {
if (obj.id === id) {
index = i
}
})
if (typeof index === 'undefined') {
throw {
code: 404,
message: `Object with id "${id}" is not found`
}
}
return index
}
/**
*Deletes an object from the collection without any checks
*
* @param {Object} object an object to delete
* @memberof Collection
*/
_forceDelete(object) {
let index = this._getIndexInData(object.id)
this.data.splice(index, 1)
}
/**
* Deletes an object from the collection
* @param {Object} object an object to delete
* @memberof Collection
*/
delete(object) {
// first check whether a given object is actually an object of the collection
this._checkIdDefined(object)
this._checkRevDefined(object)
// retreive an object which should be deleted
let oldObject = this.get(object.id)
// check if revision number is the valid one
this._checkRevValid(object, oldObject)
// delete an object
this._forceDelete(oldObject);
}
/**
* Updates a given object
* @param {Object} object with updated params
* @returns {Object} an updated object
*/
update(object) {
// deletes object (with all checks)
this.delete(object)
// increments rev number according to rules
object._incrementRev()
this.insert(object)
return object
}
}
module.exports = {
Collection
}