Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jsonb location update and infoj #1270

Merged
merged 5 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/location/decorate.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ Update a location from host

async function update() {

this.infoj
.filter(entry => entry.jsonb_key)
.filter(entry => entry.jsonb_field)
.filter(entry => typeof entry.newValue !== 'undefined')
.forEach(entry => {

entry.newValue = {
'jsonb': {
[entry.jsonb_field]: {
[entry.jsonb_key]: entry.newValue
}
}
}
})

this.infoj
.filter(entry => entry.json_field)
.filter(entry => entry.json_key)
Expand Down
27 changes: 17 additions & 10 deletions lib/ui/locations/infoj.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ export default (location, infoj_order) => {
// The default entry type is text.
entry.type = entry.type || 'text'

entryJSONB(entry)

entryObject(entry)

// Skip entry depending on flag and value.
Expand Down Expand Up @@ -241,6 +243,16 @@ function entryQuery(entry) {
}
}

function entryJSONB(entry) {
if (!entry.jsonb_field) return;
if (!entry.jsonb_key) return;
if (entry.value === null) return;
if (typeof entry.value !== 'object') return;
if (!entry.value.jsonb) return;

entry.value = entry.value.jsonb[entry.jsonb_field][entry.jsonb_key]
}

/**
* @function entryObject
* @param {Object} entry
Expand All @@ -265,13 +277,16 @@ function entryObject(entry) {
if (typeof fieldEntry.value !== 'object') return;

// Lookup for json value field entry
if(entry.json_field) {
if (entry.json_field) {

if(!entry.json_key){
if (!entry.json_key) {
console.warn('json_field requires entry.json_key to be specified')
return;
}

// The fieldEntry value may be null or undefined.
if (!fieldEntry.value) return;

entry.value = fieldEntry.value[entry.json_key]
}

Expand All @@ -283,14 +298,6 @@ function entryObject(entry) {
if (entry.objectMergeFromField) {

mapp.utils.merge(entry, fieldEntry.value)

// // Find the entry to merge.
// let fieldEntry = entry.location.infoj.find(_entry => _entry.type === entry.objectMergeFromEntry)

// // Only merge the entry.merge object.
// fieldEntry
// && fieldEntry.merge instanceof Object
// && mapp.utils.merge(entry, fieldEntry.merge)
}
}

Expand Down
16 changes: 15 additions & 1 deletion mod/workspace/templates/location_update.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,22 @@ module.exports = _ => {

// Value is an object and must be stringified.
if (typeof _.body[key] === 'object' && !Array.isArray(_.body[key])) {

_[key] = JSON.stringify(_.body[key])
if (_.body[key]['jsonb']) {

const jsonb = _.body[key]['jsonb']

const jsonb_field = Object.keys(jsonb)[0]

let updateObject = []
Object.keys(jsonb[jsonb_field]).forEach( key => {
let value = typeof jsonb[jsonb_field][key] === 'string' ? `"${jsonb[jsonb_field][key]}"`: jsonb[jsonb_field][key]
updateObject.push(`"${key}":${value}`)
})

return `${jsonb_field} = coalesce(json_field::jsonb,'{}'::jsonb)::jsonb || '{${updateObject.join(',')}}'::jsonb`
}
}

// Value is an array (of strings)
Expand Down
Loading