PR comments.
This commit is contained in:
parent
09824f9a4d
commit
76273ff860
|
@ -855,16 +855,7 @@ class InternalBuilder {
|
||||||
return withSchema
|
return withSchema
|
||||||
}
|
}
|
||||||
|
|
||||||
addJsonRelationships(
|
private buildJsonField(field: string): string {
|
||||||
query: Knex.QueryBuilder,
|
|
||||||
fromTable: string,
|
|
||||||
relationships: RelationshipsJson[]
|
|
||||||
): Knex.QueryBuilder {
|
|
||||||
const sqlClient = this.client
|
|
||||||
const knex = this.knex
|
|
||||||
const { resource, tableAliases: aliases, endpoint } = this.query
|
|
||||||
const fields = resource?.fields || []
|
|
||||||
const jsonField = (field: string) => {
|
|
||||||
const parts = field.split(".")
|
const parts = field.split(".")
|
||||||
let tableField: string, unaliased: string
|
let tableField: string, unaliased: string
|
||||||
if (parts.length > 1) {
|
if (parts.length > 1) {
|
||||||
|
@ -875,16 +866,19 @@ class InternalBuilder {
|
||||||
unaliased = parts.join(".")
|
unaliased = parts.join(".")
|
||||||
tableField = this.quote(unaliased)
|
tableField = this.quote(unaliased)
|
||||||
}
|
}
|
||||||
let separator = ","
|
const separator = this.client === SqlClient.ORACLE ? " VALUE " : ","
|
||||||
switch (sqlClient) {
|
|
||||||
case SqlClient.ORACLE:
|
|
||||||
separator = " VALUE "
|
|
||||||
break
|
|
||||||
case SqlClient.MS_SQL:
|
|
||||||
separator = ":"
|
|
||||||
}
|
|
||||||
return `'${unaliased}'${separator}${tableField}`
|
return `'${unaliased}'${separator}${tableField}`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
addJsonRelationships(
|
||||||
|
query: Knex.QueryBuilder,
|
||||||
|
fromTable: string,
|
||||||
|
relationships: RelationshipsJson[]
|
||||||
|
): Knex.QueryBuilder {
|
||||||
|
const sqlClient = this.client
|
||||||
|
const knex = this.knex
|
||||||
|
const { resource, tableAliases: aliases, endpoint } = this.query
|
||||||
|
const fields = resource?.fields || []
|
||||||
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
|
||||||
|
|
|
@ -129,12 +129,10 @@ 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 =
|
|
||||||
relatedTable.primaryDisplay || relatedTable.primary![0]!
|
|
||||||
thisRow[col] = (thisRow[col] as Row[])
|
thisRow[col] = (thisRow[col] as Row[])
|
||||||
.map(relatedRow => {
|
.map(relatedRow => {
|
||||||
relatedRow._id = relatedRow._id
|
relatedRow._id = relatedRow._id
|
||||||
|
@ -156,7 +154,6 @@ export function basicProcessing({
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return thisRow
|
return thisRow
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue