You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
166 lines
3.4 KiB
166 lines
3.4 KiB
3 years ago
|
import PouchDB from 'pouchdb'
|
||
|
import PouchDBfind from 'pouchdb-find'
|
||
|
|
||
|
/**
|
||
|
* Repository
|
||
|
*
|
||
|
* @author Björn Hase, me@herr-hase.wtf
|
||
|
* @license http://opensource.org/licenses/MIT The MIT License
|
||
|
* @link https://gitea.node001.net/HerrHase/super-hog
|
||
|
*
|
||
|
*/
|
||
|
|
||
|
class Repository
|
||
|
{
|
||
|
constructor()
|
||
|
{
|
||
|
PouchDB.plugin(PouchDBfind)
|
||
|
this.db = new PouchDB('./../../storage/database', {
|
||
|
revs_limit: 0
|
||
|
})
|
||
|
|
||
|
// adding index
|
||
|
this.index = [
|
||
|
'type'
|
||
|
]
|
||
|
|
||
|
if (this.index && this.index.lenth > 0) {
|
||
|
this.addIndex(this.index)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* add index
|
||
|
*
|
||
|
* @param {array} fields
|
||
|
*
|
||
|
*/
|
||
|
async addIndex(fields)
|
||
|
{
|
||
|
try {
|
||
|
await this.db.createIndex({
|
||
|
index: {
|
||
|
fields: fields
|
||
|
}
|
||
|
})
|
||
|
} catch (error) {
|
||
|
console.log(error);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
*
|
||
|
* @param {array} data
|
||
|
* @param {function} success
|
||
|
*
|
||
|
*/
|
||
|
create(data)
|
||
|
{
|
||
|
//data._id = uuidv4()
|
||
|
data.type = this.type
|
||
|
|
||
|
// if beforeCreate exists
|
||
|
if (typeof this['beforeCreate'] === 'function') {
|
||
|
data = this.beforeCreate(data)
|
||
|
}
|
||
|
|
||
|
return this.db.post(data)
|
||
|
.then((response) => {
|
||
|
|
||
|
console.log(response)
|
||
|
|
||
|
// if afterCreate exists
|
||
|
if (typeof this['afterCreate'] === 'function') {
|
||
|
this.afterCreate(response)
|
||
|
}
|
||
|
|
||
|
return response
|
||
|
})
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
*
|
||
|
* @param {array} data
|
||
|
* @param {function} success
|
||
|
*
|
||
|
*/
|
||
|
update(data)
|
||
|
{
|
||
|
// if beforeUpdate exists
|
||
|
if (typeof this['beforeUpdate'] === 'function') {
|
||
|
data = this.beforeUpdate(data)
|
||
|
}
|
||
|
|
||
|
return this.db.put(data)
|
||
|
.then((response) => {
|
||
|
|
||
|
// if beforeUpdate exists
|
||
|
if (typeof this['afterUpdate'] === 'function') {
|
||
|
this.afterUpdate(response)
|
||
|
}
|
||
|
|
||
|
return response
|
||
|
})
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
*
|
||
|
* @param {string} id
|
||
|
* @param {function} success
|
||
|
*
|
||
|
*/
|
||
|
remove(id, success)
|
||
|
{
|
||
|
this.db.get(id).then((documents) => {
|
||
|
|
||
|
// if beforeUpdate exists
|
||
|
if (typeof this['beforeRemove'] === 'function') {
|
||
|
this.beforeRemove(documents)
|
||
|
}
|
||
|
|
||
|
this.db.remove(documents, {}, success)
|
||
|
|
||
|
// if beforeUpdate exists
|
||
|
if (typeof this['afterRemove'] === 'function') {
|
||
|
this.afterRemove(documents)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* find documents
|
||
|
*
|
||
|
* @param {object} fields
|
||
|
* @param {object} query
|
||
|
* @param {function} success
|
||
|
*/
|
||
|
find(query)
|
||
|
{
|
||
|
return this.db.find(query).then((documents) => {
|
||
|
return documents.docs
|
||
|
})
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* find documents
|
||
|
*
|
||
|
* @param {object} fields
|
||
|
* @param {object} query
|
||
|
* @param {function} success
|
||
|
*/
|
||
|
findOne(query)
|
||
|
{
|
||
|
return this.db.find(query).then((documents) => {
|
||
|
if (documents.docs.length === 0) {
|
||
|
return null
|
||
|
} else {
|
||
|
return documents.docs[0]
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export default Repository
|