paradigm-core
Version:
Paradigm Core Modules
135 lines (95 loc) • 2.48 kB
JavaScript
const {r, RootModel} = require('structure-core')
/**
* TaxonomyModel Class
*
* @public
* @class TaxonomyModel
*/
class TaxonomyModel extends RootModel {
/**
* TaxonomyModel constructor
*
* @public
* @constructor
* @param {Object} options - Options
*/
constructor(options = {}) {
super(Object.assign({}, {
table: 'taxonomies',
relations: {}
}, options))
}
async create(pkg = {}, options = {}) {
pkg.organizationId = this.organizationId
pkg.applicationId = this.applicationId
pkg.status = 'active'
const res = await RootModel.prototype.create.call(this, pkg)
return res
}
async deleteById(id) {
await this.updateById(id, {
status: 'deleted'
})
}
/**
* Get all taxonomies, with optional pagination
*
* @public
* @param {Object} ids - optional ids to fetch
*/
async getAll(ids = [], options = {}) {
const orderBy = options.orderBy || 'updatedAt'
const orderDir = options.orderDir || 'desc'
const pagination = options.pagination || false
const page = options.page
const limit = options.limit
try {
let query = r.table(this.table)
if(ids.length > 0) {
query = query.getAll(r.args(ids))
} else {
query = query.getAll(this.applicationId, {index: 'applicationId'})
}
if(orderDir === 'asc') {
query = query.orderBy(r.asc(orderBy))
} else {
query = query.orderBy(r.desc(orderBy))
}
let res
if (pagination) {
res = await this.paginate(query, {page, limit})
} else {
res = await query.run()
}
return res
} catch(e) {
this.logger.error(e)
throw e
}
}
async getBySlug(slug, options = {}) {
const applicationId = this.applicationId
const doc = await r
.table(this.table)
.getAll([slug, applicationId], {index: 'link_slug_applicationId'})
if(doc.length > 0) {
return doc[0]
}
return false
}
async matchBySlug(slug, options = {}) {
const applicationId = this.applicationId
const docs = await r
.table(this.table)
.getAll(applicationId, {index: 'applicationId'})
.filter(function(doc) {
return doc('slug').match(slug)
})
return (docs)
}
async updateById(id, pkg) {
const res = await RootModel.prototype.updateById.call(this, id, pkg)
return res
}
}
module.exports = TaxonomyModel