nfp_sites/api/page/model.mjs

78 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-09-13 13:33:10 +00:00
import bookshelf from '../bookshelf.mjs'
import Media from '../media/model.mjs'
import Staff from '../staff/model.mjs'
import Article from '../article/model.mjs'
/*
Page model:
{
filename,
filetype,
small_image,
medium_image,
large_image,
*small_url,
*medium_url,
*large_url,
size,
staff_id,
is_deleted,
created_at,
updated_at,
}
*/
const Page = bookshelf.createModel({
tableName: 'pages',
banner() {
return this.belongsTo(Media, 'banner_id')
},
parent() {
return this.belongsTo(Page, 'parent_id')
},
children() {
return this.hasManyFiltered(Page, 'children', 'parent_id')
.query(qb => {
qb.orderBy('name', 'ASC')
})
2019-09-13 13:33:10 +00:00
},
news() {
return this.hasManyFiltered(Article, 'news', 'parent_id')
.query(qb => {
qb.orderBy('id', 'desc')
})
},
media() {
return this.belongsTo(Media, 'media_id')
},
staff() {
return this.belongsTo(Staff, 'staff_id')
},
}, {
getSingle(id, withRelated = [], require = true, ctx = null) {
return this.query(qb => {
qb.where({ id: Number(id) || 0 })
.orWhere({ path: id })
})
.fetch({ require, withRelated, ctx })
},
2019-10-01 03:45:44 +00:00
getTree() {
return this.query(qb => {
qb.where({ parent_id: null })
qb.select(['id', 'name', 'path'])
qb.orderBy('name', 'ASC')
2019-10-01 03:45:44 +00:00
}).fetchAll({ withRelated: ['children'] })
},
2019-09-13 13:33:10 +00:00
})
export default Page