|
|
@ -101,7 +101,7 @@ class BaseModelSqlv2 { |
|
|
|
|
|
|
|
|
|
|
|
qb.where(_wherePk(this.model.primaryKeys, id)); |
|
|
|
qb.where(_wherePk(this.model.primaryKeys, id)); |
|
|
|
|
|
|
|
|
|
|
|
let data = (await this.execAndParse(qb))?.[0]; |
|
|
|
const data = (await this.execAndParse(qb))?.[0]; |
|
|
|
|
|
|
|
|
|
|
|
if (data) { |
|
|
|
if (data) { |
|
|
|
const proto = await this.getProto(); |
|
|
|
const proto = await this.getProto(); |
|
|
@ -159,7 +159,7 @@ class BaseModelSqlv2 { |
|
|
|
qb.orderBy(this.model.primaryKey.column_name); |
|
|
|
qb.orderBy(this.model.primaryKey.column_name); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let data = await qb.first(); |
|
|
|
const data = await qb.first(); |
|
|
|
|
|
|
|
|
|
|
|
if (data) { |
|
|
|
if (data) { |
|
|
|
const proto = await this.getProto(); |
|
|
|
const proto = await this.getProto(); |
|
|
@ -253,7 +253,7 @@ class BaseModelSqlv2 { |
|
|
|
|
|
|
|
|
|
|
|
if (!ignoreViewFilterAndSort) applyPaginate(qb, rest); |
|
|
|
if (!ignoreViewFilterAndSort) applyPaginate(qb, rest); |
|
|
|
const proto = await this.getProto(); |
|
|
|
const proto = await this.getProto(); |
|
|
|
let data = await this.execAndParse(qb); |
|
|
|
const data = await this.execAndParse(qb); |
|
|
|
|
|
|
|
|
|
|
|
return data?.map((d) => { |
|
|
|
return data?.map((d) => { |
|
|
|
d.__proto__ = proto; |
|
|
|
d.__proto__ = proto; |
|
|
@ -321,7 +321,8 @@ class BaseModelSqlv2 { |
|
|
|
as: 'count', |
|
|
|
as: 'count', |
|
|
|
}).first(); |
|
|
|
}).first(); |
|
|
|
const res = (await this.dbDriver.raw(unsanitize(qb.toQuery()))) as any; |
|
|
|
const res = (await this.dbDriver.raw(unsanitize(qb.toQuery()))) as any; |
|
|
|
return ((this.isPg || this.isSnowflake) ? res.rows[0] : res[0][0] ?? res[0]).count; |
|
|
|
return (this.isPg || this.isSnowflake ? res.rows[0] : res[0][0] ?? res[0]) |
|
|
|
|
|
|
|
.count; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// todo: add support for sortArrJson and filterArrJson
|
|
|
|
// todo: add support for sortArrJson and filterArrJson
|
|
|
@ -424,7 +425,7 @@ class BaseModelSqlv2 { |
|
|
|
.as('list') |
|
|
|
.as('list') |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
let children = await this.execAndParse(childQb, childTable); |
|
|
|
const children = await this.execAndParse(childQb, childTable); |
|
|
|
const proto = await ( |
|
|
|
const proto = await ( |
|
|
|
await Model.getBaseModelSQL({ |
|
|
|
await Model.getBaseModelSQL({ |
|
|
|
id: childTable.id, |
|
|
|
id: childTable.id, |
|
|
@ -551,7 +552,7 @@ class BaseModelSqlv2 { |
|
|
|
|
|
|
|
|
|
|
|
await childModel.selectObject({ qb }); |
|
|
|
await childModel.selectObject({ qb }); |
|
|
|
|
|
|
|
|
|
|
|
let children = await this.execAndParse(qb, childTable); |
|
|
|
const children = await this.execAndParse(qb, childTable); |
|
|
|
|
|
|
|
|
|
|
|
const proto = await ( |
|
|
|
const proto = await ( |
|
|
|
await Model.getBaseModelSQL({ |
|
|
|
await Model.getBaseModelSQL({ |
|
|
@ -736,7 +737,7 @@ class BaseModelSqlv2 { |
|
|
|
qb.limit(+rest?.limit || 25); |
|
|
|
qb.limit(+rest?.limit || 25); |
|
|
|
qb.offset(+rest?.offset || 0); |
|
|
|
qb.offset(+rest?.offset || 0); |
|
|
|
|
|
|
|
|
|
|
|
let children = await this.execAndParse(qb, childTable); |
|
|
|
const children = await this.execAndParse(qb, childTable); |
|
|
|
const proto = await ( |
|
|
|
const proto = await ( |
|
|
|
await Model.getBaseModelSQL({ id: rtnId, dbDriver: this.dbDriver }) |
|
|
|
await Model.getBaseModelSQL({ id: rtnId, dbDriver: this.dbDriver }) |
|
|
|
).getProto(); |
|
|
|
).getProto(); |
|
|
@ -962,7 +963,7 @@ class BaseModelSqlv2 { |
|
|
|
applyPaginate(qb, rest); |
|
|
|
applyPaginate(qb, rest); |
|
|
|
|
|
|
|
|
|
|
|
const proto = await childModel.getProto(); |
|
|
|
const proto = await childModel.getProto(); |
|
|
|
let data = await qb; |
|
|
|
const data = await qb; |
|
|
|
return data.map((c) => { |
|
|
|
return data.map((c) => { |
|
|
|
c.__proto__ = proto; |
|
|
|
c.__proto__ = proto; |
|
|
|
return c; |
|
|
|
return c; |
|
|
@ -1076,7 +1077,7 @@ class BaseModelSqlv2 { |
|
|
|
applyPaginate(qb, rest); |
|
|
|
applyPaginate(qb, rest); |
|
|
|
|
|
|
|
|
|
|
|
const proto = await childModel.getProto(); |
|
|
|
const proto = await childModel.getProto(); |
|
|
|
let data = await this.execAndParse(qb, childTable); |
|
|
|
const data = await this.execAndParse(qb, childTable); |
|
|
|
|
|
|
|
|
|
|
|
return data.map((c) => { |
|
|
|
return data.map((c) => { |
|
|
|
c.__proto__ = proto; |
|
|
|
c.__proto__ = proto; |
|
|
@ -1194,7 +1195,7 @@ class BaseModelSqlv2 { |
|
|
|
applyPaginate(qb, rest); |
|
|
|
applyPaginate(qb, rest); |
|
|
|
|
|
|
|
|
|
|
|
const proto = await parentModel.getProto(); |
|
|
|
const proto = await parentModel.getProto(); |
|
|
|
let data = await this.execAndParse(qb, childTable); |
|
|
|
const data = await this.execAndParse(qb, childTable); |
|
|
|
|
|
|
|
|
|
|
|
return data.map((c) => { |
|
|
|
return data.map((c) => { |
|
|
|
c.__proto__ = proto; |
|
|
|
c.__proto__ = proto; |
|
|
@ -1486,7 +1487,9 @@ class BaseModelSqlv2 { |
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
break; |
|
|
|
default: { |
|
|
|
default: { |
|
|
|
qb.select({ [column.column_name]: barcodeValueColumn.column_name }); |
|
|
|
qb.select({ |
|
|
|
|
|
|
|
[column.column_name]: barcodeValueColumn.column_name, |
|
|
|
|
|
|
|
}); |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -1591,10 +1594,11 @@ class BaseModelSqlv2 { |
|
|
|
.max(ai.column_name, { as: 'id' }) |
|
|
|
.max(ai.column_name, { as: 'id' }) |
|
|
|
)[0].id; |
|
|
|
)[0].id; |
|
|
|
} else if (this.isSnowflake) { |
|
|
|
} else if (this.isSnowflake) { |
|
|
|
id = (( |
|
|
|
id = ( |
|
|
|
await this.dbDriver(this.tnPath) |
|
|
|
(await this.dbDriver(this.tnPath).max(ai.column_name, { |
|
|
|
.max(ai.column_name, { as: 'id' }) |
|
|
|
as: 'id', |
|
|
|
) as any)[0].id; |
|
|
|
})) as any |
|
|
|
|
|
|
|
)[0].id; |
|
|
|
} |
|
|
|
} |
|
|
|
response = await this.readByPk(id); |
|
|
|
response = await this.readByPk(id); |
|
|
|
} else { |
|
|
|
} else { |
|
|
@ -1710,7 +1714,11 @@ class BaseModelSqlv2 { |
|
|
|
if (this.isMssql && schema) { |
|
|
|
if (this.isMssql && schema) { |
|
|
|
return this.dbDriver.raw('??.??', [schema, tb.table_name]); |
|
|
|
return this.dbDriver.raw('??.??', [schema, tb.table_name]); |
|
|
|
} else if (this.isSnowflake) { |
|
|
|
} else if (this.isSnowflake) { |
|
|
|
return [this.dbDriver.client.config.connection.database, this.dbDriver.client.config.connection.schema, tb.table_name].join('.'); |
|
|
|
return [ |
|
|
|
|
|
|
|
this.dbDriver.client.config.connection.database, |
|
|
|
|
|
|
|
this.dbDriver.client.config.connection.schema, |
|
|
|
|
|
|
|
tb.table_name, |
|
|
|
|
|
|
|
].join('.'); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
return tb.table_name; |
|
|
|
return tb.table_name; |
|
|
|
} |
|
|
|
} |
|
|
@ -1850,10 +1858,11 @@ class BaseModelSqlv2 { |
|
|
|
.max(ai.column_name, { as: 'id' }) |
|
|
|
.max(ai.column_name, { as: 'id' }) |
|
|
|
)[0].id; |
|
|
|
)[0].id; |
|
|
|
} else if (this.isSnowflake) { |
|
|
|
} else if (this.isSnowflake) { |
|
|
|
id = (( |
|
|
|
id = ( |
|
|
|
await this.dbDriver(this.tnPath) |
|
|
|
(await this.dbDriver(this.tnPath).max(ai.column_name, { |
|
|
|
.max(ai.column_name, { as: 'id' }) |
|
|
|
as: 'id', |
|
|
|
) as any).rows[0].id; |
|
|
|
})) as any |
|
|
|
|
|
|
|
).rows[0].id; |
|
|
|
} |
|
|
|
} |
|
|
|
response = await this.readByPk(id); |
|
|
|
response = await this.readByPk(id); |
|
|
|
} else { |
|
|
|
} else { |
|
|
@ -2043,9 +2052,7 @@ class BaseModelSqlv2 { |
|
|
|
const res = []; |
|
|
|
const res = []; |
|
|
|
for (const d of deleteIds) { |
|
|
|
for (const d of deleteIds) { |
|
|
|
if (Object.keys(d).length) { |
|
|
|
if (Object.keys(d).length) { |
|
|
|
const response = await transaction(this.tnPath) |
|
|
|
const response = await transaction(this.tnPath).del().where(d); |
|
|
|
.del() |
|
|
|
|
|
|
|
.where(d); |
|
|
|
|
|
|
|
res.push(response); |
|
|
|
res.push(response); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -2417,20 +2424,19 @@ class BaseModelSqlv2 { |
|
|
|
|
|
|
|
|
|
|
|
if (this.isSnowflake) { |
|
|
|
if (this.isSnowflake) { |
|
|
|
const parentPK = this.dbDriver(parentTn) |
|
|
|
const parentPK = this.dbDriver(parentTn) |
|
|
|
.select(parentColumn.column_name) |
|
|
|
.select(parentColumn.column_name) |
|
|
|
.where(_wherePk(parentTable.primaryKeys, childId)) |
|
|
|
.where(_wherePk(parentTable.primaryKeys, childId)) |
|
|
|
.first(); |
|
|
|
.first(); |
|
|
|
|
|
|
|
|
|
|
|
const childPK = this.dbDriver(childTn) |
|
|
|
const childPK = this.dbDriver(childTn) |
|
|
|
.select(childColumn.column_name) |
|
|
|
.select(childColumn.column_name) |
|
|
|
.where(_wherePk(childTable.primaryKeys, rowId)) |
|
|
|
.where(_wherePk(childTable.primaryKeys, rowId)) |
|
|
|
.first(); |
|
|
|
.first(); |
|
|
|
|
|
|
|
|
|
|
|
await this.dbDriver.raw(`INSERT INTO ?? (??, ??) SELECT (${parentPK.toQuery()}), (${childPK.toQuery()})`, [ |
|
|
|
await this.dbDriver.raw( |
|
|
|
vTn, |
|
|
|
`INSERT INTO ?? (??, ??) SELECT (${parentPK.toQuery()}), (${childPK.toQuery()})`, |
|
|
|
vParentCol.column_name, |
|
|
|
[vTn, vParentCol.column_name, vChildCol.column_name] |
|
|
|
vChildCol.column_name, |
|
|
|
); |
|
|
|
]) |
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
await this.dbDriver(vTn).insert({ |
|
|
|
await this.dbDriver(vTn).insert({ |
|
|
|
[vParentCol.column_name]: this.dbDriver(parentTn) |
|
|
|
[vParentCol.column_name]: this.dbDriver(parentTn) |
|
|
@ -2731,7 +2737,7 @@ class BaseModelSqlv2 { |
|
|
|
|
|
|
|
|
|
|
|
const proto = await this.getProto(); |
|
|
|
const proto = await this.getProto(); |
|
|
|
|
|
|
|
|
|
|
|
let data = await groupedQb; |
|
|
|
const data = await groupedQb; |
|
|
|
const result = data?.map((d) => { |
|
|
|
const result = data?.map((d) => { |
|
|
|
d.__proto__ = proto; |
|
|
|
d.__proto__ = proto; |
|
|
|
return d; |
|
|
|
return d; |
|
|
@ -2834,10 +2840,7 @@ class BaseModelSqlv2 { |
|
|
|
return await qb; |
|
|
|
return await qb; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private async execAndParse( |
|
|
|
private async execAndParse(qb: Knex.QueryBuilder, childTable?: Model) { |
|
|
|
qb: Knex.QueryBuilder, |
|
|
|
|
|
|
|
childTable?: Model |
|
|
|
|
|
|
|
) { |
|
|
|
|
|
|
|
let query = qb.toQuery(); |
|
|
|
let query = qb.toQuery(); |
|
|
|
if (!this.isPg && !this.isMssql && !this.isSnowflake) { |
|
|
|
if (!this.isPg && !this.isMssql && !this.isSnowflake) { |
|
|
|
query = unsanitize(qb.toQuery()); |
|
|
|
query = unsanitize(qb.toQuery()); |
|
|
|