UNPKG

paradigm-taxonomies

Version:
190 lines (123 loc) 2.99 kB
const r = require('structure-driver') const RootModel = require('structure-root-model') /** * 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)) } create(pkg = {}, options = {}) { return new Promise( async (resolve, reject) => { try { const doc = await RootModel.prototype.create.call(this, pkg) resolve(doc) } catch(e) { this.logger.error(e) reject(e) } }) } deleteById(id) { return new Promise( async (resolve, reject) => { try { await this.updateById(id, { status: 'deleted' }) resolve() } catch(e) { this.logger.error(e) reject(e) } }) } getAll(ids = [], options = {}) { const applicationId = this.applicationId const organizationId = this.organizationId return new Promise( async (resolve, reject) => { try { let query = r .table(this.table) if(ids.length > 0) { query = query .getAll(r.args(ids)) } query = query .getAll(applicationId, {index: 'applicationId'}) .filter( (doc) => { return doc('status').eq('active') }) const res = await query.run() resolve(res) } catch(e) { this.logger.error(e) reject(e) } }) } getBySlug(slug, options = {}) { const applicationId = this.applicationId const organizationId = this.organizationId return new Promise( async (resolve, reject) => { try { const doc = await r .table(this.table) .getAll([slug, applicationId], {index: 'link_slug_applicationId'}) if(doc.length > 0) { return resolve(doc[0]) } resolve(false) } catch(e) { reject(e) } }) } matchBySlug(slug, options = {}) { const applicationId = this.applicationId const organizationId = this.organizationId return new Promise( async (resolve, reject) => { try { const docs = await r .table(this.table) .getAll(applicationId, {index: 'applicationId'}) .filter(function(doc) { return doc('slug').match(slug) }) resolve(docs) } catch(e) { reject(e) } }) } updateById(id, pkg) { return new Promise( async (resolve, reject) => { try { const res = await RootModel.prototype.updateById.call(this, id, pkg) resolve(res) } catch(e) { logger.error(e) reject(e) } }) } } module.exports = TaxonomyModel