PR comments.

This commit is contained in:
mike12345567 2024-09-06 16:47:43 +01:00
parent 09824f9a4d
commit 76273ff860
3 changed files with 45 additions and 91 deletions

View File

@ -855,6 +855,21 @@ class InternalBuilder {
return withSchema return withSchema
} }
private buildJsonField(field: string): string {
const parts = field.split(".")
let tableField: string, unaliased: string
if (parts.length > 1) {
const alias = parts.shift()!
unaliased = parts.join(".")
tableField = `${this.quote(alias)}.${this.quote(unaliased)}`
} else {
unaliased = parts.join(".")
tableField = this.quote(unaliased)
}
const separator = this.client === SqlClient.ORACLE ? " VALUE " : ","
return `'${unaliased}'${separator}${tableField}`
}
addJsonRelationships( addJsonRelationships(
query: Knex.QueryBuilder, query: Knex.QueryBuilder,
fromTable: string, fromTable: string,
@ -864,27 +879,6 @@ class InternalBuilder {
const knex = this.knex const knex = this.knex
const { resource, tableAliases: aliases, endpoint } = this.query const { resource, tableAliases: aliases, endpoint } = this.query
const fields = resource?.fields || [] const fields = resource?.fields || []
const jsonField = (field: string) => {
const parts = field.split(".")
let tableField: string, unaliased: string
if (parts.length > 1) {
const alias = parts.shift()!
unaliased = parts.join(".")
tableField = `${this.quote(alias)}.${this.quote(unaliased)}`
} else {
unaliased = parts.join(".")
tableField = this.quote(unaliased)
}
let separator = ","
switch (sqlClient) {
case SqlClient.ORACLE:
separator = " VALUE "
break
case SqlClient.MS_SQL:
separator = ":"
}
return `'${unaliased}'${separator}${tableField}`
}
for (let relationship of relationships) { for (let relationship of relationships) {
const { const {
tableName: toTable, tableName: toTable,
@ -914,7 +908,7 @@ class InternalBuilder {
) )
} }
const fieldList: string = relationshipFields const fieldList: string = relationshipFields
.map(field => jsonField(field)) .map(field => this.buildJsonField(field))
.join(",") .join(",")
// SQL Server uses TOP - which performs a little differently to the normal LIMIT syntax // SQL Server uses TOP - which performs a little differently to the normal LIMIT syntax
// it reduces the result set rather than limiting how much data it filters over // it reduces the result set rather than limiting how much data it filters over
@ -1066,43 +1060,6 @@ class InternalBuilder {
return query return query
} }
addRelationships(
query: Knex.QueryBuilder,
fromTable: string,
relationships: RelationshipsJson[]
): Knex.QueryBuilder {
const tableSets: Record<string, [RelationshipsJson]> = {}
// aggregate into table sets (all the same to tables)
for (let relationship of relationships) {
const keyObj: { toTable: string; throughTable: string | undefined } = {
toTable: relationship.tableName,
throughTable: undefined,
}
if (relationship.through) {
keyObj.throughTable = relationship.through
}
const key = JSON.stringify(keyObj)
if (tableSets[key]) {
tableSets[key].push(relationship)
} else {
tableSets[key] = [relationship]
}
}
for (let [key, relationships] of Object.entries(tableSets)) {
const { toTable, throughTable } = JSON.parse(key)
query = this.addJoin(
query,
{
from: fromTable,
to: toTable,
through: throughTable,
},
relationships
)
}
return query
}
qualifiedKnex(opts?: { alias?: string | boolean }): Knex.QueryBuilder { qualifiedKnex(opts?: { alias?: string | boolean }): Knex.QueryBuilder {
let alias = this.query.tableAliases?.[this.query.endpoint.entityId] let alias = this.query.tableAliases?.[this.query.endpoint.entityId]
if (opts?.alias === false) { if (opts?.alias === false) {
@ -1186,8 +1143,7 @@ class InternalBuilder {
if (!primary) { if (!primary) {
throw new Error("Primary key is required for upsert") throw new Error("Primary key is required for upsert")
} }
const ret = query.insert(parsedBody).onConflict(primary).merge() return query.insert(parsedBody).onConflict(primary).merge()
return ret
} else if ( } else if (
this.client === SqlClient.MS_SQL || this.client === SqlClient.MS_SQL ||
this.client === SqlClient.ORACLE this.client === SqlClient.ORACLE

View File

@ -129,32 +129,29 @@ export function basicProcessing({
: typeof value === "string" : typeof value === "string"
? JSON.parse(value) ? JSON.parse(value)
: undefined : undefined
if (array) { if (array && Array.isArray(array)) {
thisRow[col] = array thisRow[col] = array
// make sure all of them have an _id // make sure all of them have an _id
if (Array.isArray(thisRow[col])) { const sortField = relatedTable.primaryDisplay || relatedTable.primary![0]!
const sortField = thisRow[col] = (thisRow[col] as Row[])
relatedTable.primaryDisplay || relatedTable.primary![0]! .map(relatedRow => {
thisRow[col] = (thisRow[col] as Row[]) relatedRow._id = relatedRow._id
.map(relatedRow => { ? relatedRow._id
relatedRow._id = relatedRow._id : generateIdForRow(relatedRow, relatedTable)
? relatedRow._id return relatedRow
: generateIdForRow(relatedRow, relatedTable) })
return relatedRow .sort((a, b) => {
}) const aField = a?.[sortField],
.sort((a, b) => { bField = b?.[sortField]
const aField = a?.[sortField], if (!aField) {
bField = b?.[sortField] return 1
if (!aField) { } else if (!bField) {
return 1 return -1
} else if (!bField) { }
return -1 return aField.localeCompare
} ? aField.localeCompare(bField)
return aField.localeCompare : aField - bField
? aField.localeCompare(bField) })
: aField - bField
})
}
} }
} }
return thisRow return thisRow

View File

@ -832,12 +832,13 @@ describe.each(
}, },
}) })
expect(res).toHaveLength(1) expect(res).toHaveLength(1)
expect(res[0]).toEqual( expect(res[0]).toEqual({
expect.objectContaining({ id: 2,
id: 2, name: "two",
name: "two", // the use of table.* introduces the possibility of nulls being returned
}) birthday: null,
) number: null,
})
}) })
// this parameter really only impacts SQL queries // this parameter really only impacts SQL queries