nfp_sites/api/page/model.mjs

67 lines
1.1 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')
},
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 })
},
})
export default Page