feat: tags on page fetch + pageLinks table schema

This commit is contained in:
Nick 2019-09-02 18:32:33 -04:00
parent efab00fa0c
commit ab463fcae1
5 changed files with 102 additions and 40 deletions

View File

@ -77,11 +77,11 @@
label label
color='teal lighten-5' color='teal lighten-5'
v-for='(tag, idx) in tags' v-for='(tag, idx) in tags'
:href='`/t/` + tag.slug' :href='`/t/` + tag.tag'
:key='tag.slug' :key='tag.tag'
) )
v-icon(color='teal', left, small) mdi-label v-icon(color='teal', left, small) mdi-label
span.teal--text.text--darken-2 {{tag.text}} span.teal--text.text--darken-2 {{tag.title}}
v-card.mb-5 v-card.mb-5
.pa-5 .pa-5

View File

@ -0,0 +1,13 @@
exports.up = knex => {
return knex.schema
.createTable('pageLinks', table => {
table.increments('id').primary()
table.integer('sourcePageId').unsigned().references('id').inTable('pages').onDelete('CASCADE')
table.integer('targetPageId').unsigned().references('id').inTable('pages').onDelete('CASCADE')
})
}
exports.down = knex => {
return knex.schema
.dropTableIfExists('pageLinks')
}

View File

@ -0,0 +1,19 @@
/* global WIKI */
exports.up = knex => {
const dbCompat = {
charset: (WIKI.config.db.type === `mysql` || WIKI.config.db.type === `mariadb`)
}
return knex.schema
.createTable('pageLinks', table => {
if (dbCompat.charset) { table.charset('utf8mb4') }
table.increments('id').primary()
table.integer('sourcePageId').unsigned().references('id').inTable('pages').onDelete('CASCADE')
table.integer('targetPageId').unsigned().references('id').inTable('pages').onDelete('CASCADE')
})
}
exports.down = knex => {
return knex.schema
.dropTableIfExists('pageLinks')
}

View File

@ -63,6 +63,18 @@ module.exports = class Page extends Model {
to: 'tags.id' to: 'tags.id'
} }
}, },
links: {
relation: Model.ManyToManyRelation,
modelClass: Page,
join: {
from: 'pages.id',
through: {
from: 'pageLinks.sourcePageId',
to: 'pageLinks.targetPageId'
},
to: 'pages.id'
}
},
author: { author: {
relation: Model.BelongsToOneRelation, relation: Model.BelongsToOneRelation,
modelClass: require('./users'), modelClass: require('./users'),
@ -120,6 +132,12 @@ module.exports = class Page extends Model {
publishEndDate: 'string', publishEndDate: 'string',
publishStartDate: 'string', publishStartDate: 'string',
render: 'string', render: 'string',
tags: [
{
tag: 'string',
title: 'string'
}
],
title: 'string', title: 'string',
toc: 'string', toc: 'string',
updatedAt: 'string' updatedAt: 'string'
@ -349,42 +367,53 @@ module.exports = class Page extends Model {
static async getPageFromDb(opts) { static async getPageFromDb(opts) {
const queryModeID = _.isNumber(opts) const queryModeID = _.isNumber(opts)
return WIKI.models.pages.query() try {
.column([ return WIKI.models.pages.query()
'pages.*', .column([
{ 'pages.*',
authorName: 'author.name', {
authorEmail: 'author.email', authorName: 'author.name',
creatorName: 'creator.name', authorEmail: 'author.email',
creatorEmail: 'creator.email' creatorName: 'creator.name',
} creatorEmail: 'creator.email'
]) }
.joinRelation('author') ])
.joinRelation('creator') .joinRelation('author')
.where(queryModeID ? { .joinRelation('creator')
'pages.id': opts .eagerAlgorithm(Model.JoinEagerAlgorithm)
} : { .eager('tags(selectTags)', {
'pages.path': opts.path, selectTags: builder => {
'pages.localeCode': opts.locale builder.select('tag', 'title')
}) }
// .andWhere(builder => { })
// if (queryModeID) return .where(queryModeID ? {
// builder.where({ 'pages.id': opts
// 'pages.isPublished': true } : {
// }).orWhere({ 'pages.path': opts.path,
// 'pages.isPublished': false, 'pages.localeCode': opts.locale
// 'pages.authorId': opts.userId })
// }) // .andWhere(builder => {
// }) // if (queryModeID) return
// .andWhere(builder => { // builder.where({
// if (queryModeID) return // 'pages.isPublished': true
// if (opts.isPrivate) { // }).orWhere({
// builder.where({ 'pages.isPrivate': true, 'pages.privateNS': opts.privateNS }) // 'pages.isPublished': false,
// } else { // 'pages.authorId': opts.userId
// builder.where({ 'pages.isPrivate': false }) // })
// } // })
// }) // .andWhere(builder => {
.first() // if (queryModeID) return
// if (opts.isPrivate) {
// builder.where({ 'pages.isPrivate': true, 'pages.privateNS': opts.privateNS })
// } else {
// builder.where({ 'pages.isPrivate': false })
// }
// })
.first()
} catch (err) {
WIKI.logger.warn(err)
throw err
}
} }
static async savePageToCache(page) { static async savePageToCache(page) {
@ -402,6 +431,7 @@ module.exports = class Page extends Model {
publishEndDate: page.publishEndDate, publishEndDate: page.publishEndDate,
publishStartDate: page.publishStartDate, publishStartDate: page.publishStartDate,
render: page.render, render: page.render,
tags: page.tags.map(t => _.pick(t, ['tag', 'title'])),
title: page.title, title: page.title,
toc: _.isString(page.toc) ? page.toc : JSON.stringify(page.toc), toc: _.isString(page.toc) ? page.toc : JSON.stringify(page.toc),
updatedAt: page.updatedAt updatedAt: page.updatedAt

View File

@ -13,7 +13,7 @@ block body
path=page.path path=page.path
title=page.title title=page.title
description=page.description description=page.description
tags=page.tags :tags=page.tags
created-at=page.createdAt created-at=page.createdAt
updated-at=page.updatedAt updated-at=page.updatedAt
author-name=page.authorName author-name=page.authorName