Skip to content
Draft
Show file tree
Hide file tree
Changes from 3 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
Binary file modified .yarn/install-state.gz
Binary file not shown.
1 change: 1 addition & 0 deletions common/activityLog/activityLog.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export const keysContent = {
// Node
nodeDefUuid: 'nodeDefUuid',
recordUuid: 'recordUuid',
nodeIId: 'nodeIId',
// User
groupUuid: 'groupUuid',
// Analysis
Expand Down
4 changes: 2 additions & 2 deletions common/model/db/tables/dataNodeDef/dataColProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const { nodeDefType } = NodeDef
const colValueProcessor = 'colValueProcessor'

const _extractCategoryItem = ({ survey, node }) => {
let item = NodeRefData.getCategoryItem(node)
const item = NodeRefData.getCategoryItem(node)
if (item) return item

const itemUuid = Node.getCategoryItemUuid(node)
Expand Down Expand Up @@ -105,7 +105,7 @@ const props = {
},
},
[nodeDefType.entity]: {
[colValueProcessor]: () => () => Node.getUuid,
[colValueProcessor]: () => () => Node.getIId,
Comment thread
SteRiccio marked this conversation as resolved.
Outdated
},
[nodeDefType.file]: {
[colValueProcessor]: ({ nodeDefCol }) => {
Expand Down
67 changes: 48 additions & 19 deletions common/model/db/tables/dataNodeDef/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ const columnSet = {
id: Table.columnSetCommon.id,
dateCreated: Table.columnSetCommon.dateCreated,
dateModified: Table.columnSetCommon.dateModified,
uuid: Table.columnSetCommon.uuid,
iId: Table.columnSetCommon.iId,
parentUuid: 'parent_uuid',
parentInternalId: 'p_i_id',
ancestorUuid: 'ancestor_uuid',
ancestorIId: 'a_i_id',
recordUuid: 'record_uuid',
recordCycle: 'record_cycle',
recordStep: 'record_step',
Expand All @@ -32,15 +34,15 @@ const rootDefColumnNames = [
]

const commonColumnNamesAndTypes = [
`${columnSet.id} bigint NOT NULL GENERATED ALWAYS AS IDENTITY`,
`${columnSet.dateCreated} TIMESTAMP NOT NULL DEFAULT (now() AT TIME ZONE 'UTC')`,
`${columnSet.dateModified} TIMESTAMP NOT NULL DEFAULT (now() AT TIME ZONE 'UTC')`,
`${columnSet.uuid} uuid NOT NULL`,
`${columnSet.parentUuid} uuid NULL`,
`${columnSet.id} bigint NOT NULL GENERATED ALWAYS AS IDENTITY`,
`${columnSet.recordUuid} uuid NOT NULL`,
`${columnSet.iId} int NOT NULL`,
`${columnSet.parentInternalId} int NULL`,
`${columnSet.dateCreated} TIMESTAMP NOT NULL DEFAULT (now() AT TIME ZONE 'UTC')`,
`${columnSet.dateModified} TIMESTAMP NOT NULL DEFAULT (now() AT TIME ZONE 'UTC')`,
]

const rootDefColumnNamesAndTypes = [
`${columnSet.recordUuid} uuid NOT NULL`,
`${columnSet.recordCycle} varchar(2) NOT NULL`,
`${columnSet.recordStep} varchar(63) NOT NULL`,
`${columnSet.recordOwnerUuid} uuid NOT NULL`,
Expand Down Expand Up @@ -71,6 +73,10 @@ export default class TableDataNodeDef extends TableSurveyRdb {
return this.getColumn(columnSet.id)
}

get columnIId() {
return this.getColumn(columnSet.iId)
}

get columnUuid() {
return this.getColumn(columnSet.uuid)
}
Expand All @@ -79,6 +85,10 @@ export default class TableDataNodeDef extends TableSurveyRdb {
return this.getColumn(columnSet.parentUuid)
}

get columnParentInternalId() {
return this.getColumn(columnSet.parentInternalId)
}

get columnRecordUuid() {
return this.getColumn(columnSet.recordUuid)
}
Expand Down Expand Up @@ -121,7 +131,13 @@ export default class TableDataNodeDef extends TableSurveyRdb {
getColumnNames = ({ includeAnalysis = true } = {}) => {
const { nodeDef } = this
const nodeDefsForColumns = this.getNodeDefsForColumns({ includeAnalysis })
const names = [columnSet.uuid, columnSet.parentUuid, columnSet.dateCreated, columnSet.dateModified]
const names = [
columnSet.recordUuid,
columnSet.iId,
columnSet.parentInternalId,
columnSet.dateCreated,
columnSet.dateModified,
]
if (NodeDef.isRoot(nodeDef)) {
names.push(...rootDefColumnNames)
}
Expand All @@ -135,16 +151,24 @@ export default class TableDataNodeDef extends TableSurveyRdb {
if (NodeDef.isRoot(this.nodeDef)) {
columnsAndType.push(...rootDefColumnNamesAndTypes)
}
this.columnNodeDefs.forEach((nodeDefColumn) => {
columnsAndType.push(...nodeDefColumn.names.map((name, i) => `${name} ${nodeDefColumn.types[i]}`))
})
for (const nodeDefColumn of this.columnNodeDefs) {
for (let i = 0; i < nodeDefColumn.names.length; i++) {
const name = nodeDefColumn.names[i]
const type = nodeDefColumn.types[i]
columnsAndType.push(`${name} ${type}`)
}
}
return columnsAndType
}

_getConstraintFk(tableReferenced, column) {
_getConstraintFk(tableReferenced, ...columns) {
const referencedColumnNames = [columnSet.recordUuid]
if (columns.length > 1) {
referencedColumnNames.push(columnSet.iId)
}
return `CONSTRAINT ${this.name}_${tableReferenced.name}_fk
FOREIGN KEY (${column})
REFERENCES ${tableReferenced.nameQualified} (${columnSet.uuid})
FOREIGN KEY (${columns.join(', ')})
REFERENCES ${tableReferenced.nameQualified} (${referencedColumnNames.join(', ')})
ON DELETE CASCADE`
}

Expand All @@ -160,19 +184,24 @@ export default class TableDataNodeDef extends TableSurveyRdb {
return null
}
const ancestorMultipleEntity = Survey.getNodeDefAncestorMultipleEntity(this.nodeDef)(this.survey)
return this._getConstraintFk(new TableDataNodeDef(this.survey, ancestorMultipleEntity), columnSet.parentUuid)
return this._getConstraintFk(
new TableDataNodeDef(this.survey, ancestorMultipleEntity),
columnSet.recordUuid,
columnSet.parentInternalId
)
}

getConstraintUuidUnique() {
return `CONSTRAINT ${NodeDef.getName(this.nodeDef)}_uuid_unique_ix1 UNIQUE (${columnSet.uuid})`
getConstraintIIdUnique() {
return `CONSTRAINT ${NodeDef.getName(this.nodeDef)}_i_id_unique_ix1 UNIQUE (${columnSet.recordUuid}, ${columnSet.iId})`
}

getRowValuesByColumnName = ({ nodeRow, nodeDefColumns }) => {
const { survey, nodeDef } = this
const valuesByColumnName = TableDataNodeDefRowUtils.getValuesByColumnName({ survey, nodeRow, nodeDefColumns })
const result = {
[columnSet.uuid]: nodeRow[columnSet.uuid],
[columnSet.parentUuid]: nodeRow[columnSet.ancestorUuid],
[columnSet.iId]: nodeRow[columnSet.iId],
[columnSet.recordUuid]: nodeRow[columnSet.recordUuid],
[columnSet.parentInternalId]: nodeRow[columnSet.ancestorIId],
[columnSet.dateCreated]: nodeRow[columnSet.dateCreated],
[columnSet.dateModified]: nodeRow[columnSet.dateModified],
}
Expand Down
5 changes: 2 additions & 3 deletions common/model/db/tables/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ const columnSetCommon = {
dateCreated: 'date_created',
dateModified: 'date_modified',
id: 'id',
iId: 'i_id',
props: 'props',
propsDraft: 'props_draft',
uuid: 'uuid',
}

/**
* A database table object.
*
* @typedef {object} module:arena.Table
* @property {string} schema - The schema it belongs to.
* @property {string} name - The table name.
Expand All @@ -22,10 +22,9 @@ const columnSetCommon = {
export default class Table {
/**
* Create an instance of a Table.
*
* @param {!string} schema - The schema.
* @param {!string} name - The table name.
* @param {{[key: string]: string}} [columnSet={}] - The table column set.
* @param {{[key: string]: string}} [columnSet] - The table column set.
*/
constructor(schema, name, columnSet = {}) {
if (new.target === Table) {
Expand Down
6 changes: 6 additions & 0 deletions core/objectUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ export const keys = {
draft: 'draft',
extra: 'extra',
id: 'id',
iId: 'iId',
index: 'index',
name: 'name',
nodeDefUuid: 'nodeDefUuid',
pIId: 'pIId',
parentUuid: 'parentUuid',
props: 'props',
propsDraft: 'propsDraft',
Expand All @@ -31,6 +33,7 @@ export const keysProps = {

// ====== READ
export const getId = R.prop(keys.id)
export const getIId = R.prop(keys.iId)
export const getUuid = R.propOr(null, keys.uuid)

export const getProps = R.propOr({}, keys.props)
Expand All @@ -40,6 +43,7 @@ export const isKeyTrue = (key) => (obj) => !!R.propOr(false, key)(obj)
export const isPropTrue = (prop) => (obj) => !!getProp(prop)(obj)

export const getParentUuid = R.propOr(null, keys.parentUuid)
export const getParentInternalId = R.propOr(null, keys.pIId)

export const getLabels = getProp(keysProps.labels, {})
export const getLabel = (lang, defaultTo = null) => R.pipe(getLabels, R.propOr(defaultTo, lang))
Expand Down Expand Up @@ -132,6 +136,8 @@ export const toIndexedObj = (array, propNameOrExtractor) =>

export const toUuidIndexedObj = R.partialRight(toIndexedObj, [keys.uuid])

export const toIIdIndexedObj = R.partialRight(toIndexedObj, [keys.iId])

export const groupByProps =
(...propNamesOrExtractors) =>
(items) =>
Expand Down
6 changes: 3 additions & 3 deletions core/record/_record/recordNodesUpdater.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,17 +453,17 @@ const deleteNodesInEntityByNodeDefUuid =
async (record) => {
const updateResult = new RecordUpdateResult({ record })

const nodeUuidsToDelete = []
const nodeIIdsToDelete = []
for (const nodeDefUuid of nodeDefUuids) {
const children = RecordReader.getNodeChildrenByDefUuid(entity, nodeDefUuid)(record)
nodeUuidsToDelete.push(...children.map(Node.getUuid))
nodeIIdsToDelete.push(...children.map(Node.getIId))
}

const nodesDeleteUpdateResult = await deleteNodes({
user,
survey,
record,
nodeUuids: nodeUuidsToDelete,
nodeUuids: nodeIIdsToDelete,
Comment thread
SteRiccio marked this conversation as resolved.
Outdated
categoryItemProvider,
taxonProvider,
sideEffect,
Expand Down
10 changes: 5 additions & 5 deletions core/record/_record/recordReader.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { keys } from './recordKeys'

const {
getChildren: getNodeChildren,
getNodeByUuid,
getNodeByInternalId,
getNodesByDefUuid,
getParent: getParentNode,
getRoot: getRootNode,
Expand All @@ -27,7 +27,7 @@ const {
export const getNodes = R.propOr({}, keys.nodes)
export const getNodesArray = (record) => Object.values(getNodes(record))

export { getNodeChildren, getNodeByUuid, getNodesByDefUuid, getRootNode, getParentNode }
export { getNodeChildren, getNodeByInternalId, getNodesByDefUuid, getRootNode, getParentNode }

export const findNodeChildren = (parentNode, childDefUuid) => (record) => {
try {
Expand Down Expand Up @@ -201,10 +201,10 @@ export const getParentCodeAttribute = (_survey, parentNode, nodeDef) => (record)
export const visitAncestorCodeAttributes =
({ survey, parentNode, nodeDef, visitor }) =>
(record) => {
const visitedNodeUuids = new Set() // avoid cycles
const visitedNodeIIds = new Set() // avoid cycles
let currentParentCodeAttribute = Records.getParentCodeAttribute({ parentNode, nodeDef })(record)
while (currentParentCodeAttribute && !visitedNodeUuids.has(Node.getUuid(currentParentCodeAttribute))) {
visitedNodeUuids.add(Node.getUuid(currentParentCodeAttribute))
while (currentParentCodeAttribute && !visitedNodeIIds.has(Node.getIId(currentParentCodeAttribute))) {
visitedNodeIIds.add(Node.getIId(currentParentCodeAttribute))
visitor(currentParentCodeAttribute)
const parentCodeAttributeNodeDef = SurveyNodeDefs.getNodeDefByUuid(
Node.getNodeDefUuid(currentParentCodeAttribute)
Expand Down
12 changes: 5 additions & 7 deletions core/record/_record/recordUpdater.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ import * as RecordReader from './recordReader'
* Updates the record nodes with the ones passed as parameter.
* Existing nodes will be replaced by the ones in the specified parameter.
* Nodes marked as "deleted" will be removed from the record.
*
* @param {object} nodes - The nodes to be added or updated.
* @param {boolean} [removeFlags = false] - True if flags like "deleted" or "created" must be removed from the nodes, false otherwise.
* @param {boolean} [removeFlags] - True if flags like "deleted" or "created" must be removed from the nodes, false otherwise.
* @returns {object} - The updated record.
*/
export const mergeNodes =
Expand Down Expand Up @@ -64,11 +63,10 @@ export const assocNode =
/**
* Adds new nodes to the record.
* Nodes shouldn't have been added previously to the record, so in this casa there is no need to check for duplicates.
*
* @param {!object} params - The parameters.
* @param {!object} [params.nodes] - The nodes to be added.
* @param {boolean} [params.updateNodesIndex = true] - True if the nodes must be added to the index (slower), false otherwise (faster).
* @param {boolean} [params.sideEffect = true] - True if the passed record object can be modified by calling this function (faster), false otherwise.
* @param {boolean} [params.updateNodesIndex] - True if the nodes must be added to the index (slower), false otherwise (faster).
* @param {boolean} [params.sideEffect] - True if the passed record object can be modified by calling this function (faster), false otherwise.
* @returns {object} - The updated record.
*/
export const assocNodes =
Expand All @@ -93,7 +91,7 @@ export const assocDateModified = (dateModified) => (record) => {
export const deleteNode =
(node, { sideEffect = false } = {}) =>
(record) => {
const nodeUuid = Node.getUuid(node)
const { record: recordUpdated } = Records.deleteNode(nodeUuid, { sideEffect })(record)
const nodeIId = Node.getIId(node)
const { record: recordUpdated } = Records.deleteNode(nodeIId, { sideEffect })(record)
return recordUpdated
}
Loading
Loading