Add ability to retrieve specific page versions and nodes with ?version=XX param
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -11,6 +11,28 @@ class VersionedModel extends Model {
|
||||
}
|
||||
}
|
||||
|
||||
__is_version_instantation = false
|
||||
__version_base = undefined
|
||||
__version_data = undefined
|
||||
|
||||
is_a_version() {
|
||||
return this.__is_version_instantation
|
||||
}
|
||||
|
||||
version_base() {
|
||||
return this.__version_base
|
||||
}
|
||||
|
||||
raw_version_data() {
|
||||
return this.__version_data
|
||||
}
|
||||
|
||||
__initialize_version(base, data = {}) {
|
||||
this.__is_version_instantation = true
|
||||
this.__version_base = base
|
||||
this.__version_data = data
|
||||
}
|
||||
|
||||
async version_save(message = undefined, user_id = undefined) {
|
||||
await this.new_version(message, user_id)
|
||||
return this.save()
|
||||
@@ -44,6 +66,43 @@ class VersionedModel extends Model {
|
||||
delete data.version_archive
|
||||
return data
|
||||
}
|
||||
|
||||
async get_version_data(version_num = undefined) {
|
||||
if ( typeof version_num === 'undefined' ) {
|
||||
return await this.cast_to_version_data()
|
||||
} else {
|
||||
return this.version_archive.find(data => Number(data.version_num) === Number(version_num))
|
||||
}
|
||||
}
|
||||
|
||||
has_version(version_num = undefined) {
|
||||
return !!this.version_archive.find(data => {
|
||||
return Number(data.version_num) === Number(version_num)
|
||||
})
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
const data = {...this}
|
||||
delete data.__version_data
|
||||
delete data.__version_base
|
||||
delete data.__is_version_instantation
|
||||
delete data.version_archive
|
||||
|
||||
if ( this.is_a_version() ) {
|
||||
data.version_message = this.__version_data.version_message
|
||||
data.version_user_id = this.__version_data.version_user_id
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
async as_version(version_num = undefined) {
|
||||
const data = await this.get_version_data(version_num)
|
||||
console.log('as version data', data, version_num)
|
||||
const inst = new this.constructor(data)
|
||||
inst.__initialize_version(this, data)
|
||||
return inst
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = exports = VersionedModel
|
||||
|
||||
@@ -115,8 +115,35 @@ class Page extends VersionedModel {
|
||||
|
||||
get nodes() {
|
||||
const Node = this.models.get("api:Node")
|
||||
return this.has_many(Node, "NodeIds", "UUID")
|
||||
const node_promise = this.has_many(Node, "NodeIds", "UUID")
|
||||
if ( this.is_a_version() ) {
|
||||
// return the nodes for this version!
|
||||
return (async () => {
|
||||
const nodes = await node_promise
|
||||
const version_data = this.raw_version_data()
|
||||
const return_nodes = []
|
||||
if ( version_data?.node_version_nums ) {
|
||||
const node_x_version_num = {}
|
||||
for ( const item of version_data.node_version_nums ) {
|
||||
node_x_version_num[item.NodeId] = item.version_num
|
||||
}
|
||||
|
||||
for ( const node of nodes ) {
|
||||
if ( node_x_version_num[node.UUID] ) {
|
||||
return_nodes.push(await node.as_version(node_x_version_num[node.UUID]))
|
||||
} else {
|
||||
return_nodes.push(node)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return return_nodes
|
||||
})()
|
||||
} else {
|
||||
return node_promise
|
||||
}
|
||||
}
|
||||
|
||||
get childPages() {
|
||||
const Page = this.models.get("api:Page")
|
||||
return this.has_many(Page, "ChildPageIds", "UUID")
|
||||
|
||||
Reference in New Issue
Block a user