diff --git a/server/postgres/src/storage.ts b/server/postgres/src/storage.ts index 8c8a063577..5cb5eeceb1 100644 --- a/server/postgres/src/storage.ts +++ b/server/postgres/src/storage.ts @@ -753,12 +753,14 @@ abstract class PostgresAdapterBase implements DbAdapter { if (!isDataField(key)) return `"${key}"` const arr = key.split('.').filter((p) => p) let tKey = '' + let isNestedField = false for (let i = 0; i < arr.length; i++) { const element = arr[i] if (element === '$lookup') { tKey += arr[++i] + '_lookup' } else if (this.hierarchy.isMixin(element as Ref>)) { + isNestedField = true tKey += `${element}` if (i !== arr.length - 1) { tKey += "'->'" @@ -773,7 +775,7 @@ abstract class PostgresAdapterBase implements DbAdapter { tKey = this.checkMixinKey(tKey, _class, isDataArray) } - return isDataArray ? `data->'${tKey}'` : `data#>>'{${tKey}}'` + return isDataArray || isNestedField ? `data->'${tKey}'` : `data#>>'{${tKey}}'` } private checkMixinKey(key: string, _class: Ref>, isDataArray: boolean): string { @@ -1046,7 +1048,9 @@ abstract class PostgresAdapterBase implements DbAdapter { const vals = part .map((doc) => { const d = convertDoc(doc, this.workspaceId.name) - return `('${d._id}', '${d.workspaceId}', '${d._class}', '${d.createdBy ?? d.modifiedBy}', '${d.modifiedBy}', ${d.modifiedOn}, ${d.createdOn ?? d.modifiedOn}, '${d.space}', '${d.attachedTo ?? '[NULL]'}', '${escapeBackticks(JSON.stringify(d.data))}')` + return `('${d._id}', '${d.workspaceId}', '${d._class}', '${d.createdBy ?? d.modifiedBy}', '${d.modifiedBy}', ${d.modifiedOn}, ${d.createdOn ?? d.modifiedOn}, '${d.space}', ${ + d.attachedTo != null ? `'${d.attachedTo}'` : 'NULL' + }, '${escapeBackticks(JSON.stringify(d.data))}')` }) .join(', ') await client.query( @@ -1133,7 +1137,9 @@ abstract class PostgresAdapterBase implements DbAdapter { const vals = part .map((doc) => { const d = convertDoc(doc, this.workspaceId.name) - return `('${d._id}', '${d.workspaceId}', '${d._class}', '${d.createdBy ?? d.modifiedBy}', '${d.modifiedBy}', ${d.modifiedOn}, ${d.createdOn ?? d.modifiedOn}, '${d.space}', '${d.attachedTo ?? '[NULL]'}', '${escapeBackticks(JSON.stringify(d.data))}')` + return `('${d._id}', '${d.workspaceId}', '${d._class}', '${d.createdBy ?? d.modifiedBy}', '${d.modifiedBy}', ${d.modifiedOn}, ${d.createdOn ?? d.modifiedOn}, '${d.space}', ${ + d.attachedTo != null ? `'${d.attachedTo}'` : 'NULL' + }, '${escapeBackticks(JSON.stringify(d.data))}')` }) .join(', ') await client.query( diff --git a/server/postgres/src/utils.ts b/server/postgres/src/utils.ts index 904bfc344c..58091da343 100644 --- a/server/postgres/src/utils.ts +++ b/server/postgres/src/utils.ts @@ -290,7 +290,7 @@ export function translateDomain (domain: string): string { export function parseDocWithProjection (doc: DBDoc, projection: Projection | undefined): T { const { workspaceId, data, ...rest } = doc for (const key in rest) { - if ((rest as any)[key] === 'NULL') { + if ((rest as any)[key] === 'NULL' || (rest as any)[key] === null) { if (key === 'attachedTo') { // eslint-disable-next-line @typescript-eslint/no-dynamic-delete delete rest[key] @@ -321,7 +321,7 @@ export function parseDocWithProjection (doc: DBDoc, projection: P export function parseDoc (doc: DBDoc): T { const { workspaceId, data, ...rest } = doc for (const key in rest) { - if ((rest as any)[key] === 'NULL') { + if ((rest as any)[key] === 'NULL' || (rest as any)[key] === null) { if (key === 'attachedTo') { // eslint-disable-next-line @typescript-eslint/no-dynamic-delete delete rest[key]