|
|
|
@ -100,10 +100,9 @@ class BaseModelSqlv2 {
|
|
|
|
|
|
|
|
|
|
qb.where(_wherePk(this.model.primaryKeys, id)); |
|
|
|
|
|
|
|
|
|
let data = (await this.extractRawQueryAndExec(qb))?.[0]; |
|
|
|
|
let data = (await this.execAndParse(qb))?.[0]; |
|
|
|
|
|
|
|
|
|
if (data) { |
|
|
|
|
data = this.convertAttachmentType(data); |
|
|
|
|
const proto = await this.getProto(); |
|
|
|
|
data.__proto__ = proto; |
|
|
|
|
} |
|
|
|
@ -162,7 +161,6 @@ class BaseModelSqlv2 {
|
|
|
|
|
let data = await qb.first(); |
|
|
|
|
|
|
|
|
|
if (data) { |
|
|
|
|
data = this.convertAttachmentType(data); |
|
|
|
|
const proto = await this.getProto(); |
|
|
|
|
data.__proto__ = proto; |
|
|
|
|
} |
|
|
|
@ -254,8 +252,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
|
|
|
|
|
if (!ignoreViewFilterAndSort) applyPaginate(qb, rest); |
|
|
|
|
const proto = await this.getProto(); |
|
|
|
|
let data = await this.extractRawQueryAndExec(qb); |
|
|
|
|
data = this.convertAttachmentType(data); |
|
|
|
|
let data = await this.execAndParse(qb); |
|
|
|
|
|
|
|
|
|
return data?.map((d) => { |
|
|
|
|
d.__proto__ = proto; |
|
|
|
@ -367,7 +364,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
qb.groupBy(args.column_name); |
|
|
|
|
if (sorts) await sortV2(sorts, qb, this.dbDriver); |
|
|
|
|
applyPaginate(qb, rest); |
|
|
|
|
return this.convertAttachmentType(await qb); |
|
|
|
|
return await qb; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
async multipleHmList({ colId, ids }, args: { limit?; offset? } = {}) { |
|
|
|
@ -426,8 +423,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
.as('list') |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
let children = await this.extractRawQueryAndExec(childQb); |
|
|
|
|
children = this.convertAttachmentType(children); |
|
|
|
|
let children = await this.execAndParse(childQb, childTable); |
|
|
|
|
const proto = await ( |
|
|
|
|
await Model.getBaseModelSQL({ |
|
|
|
|
id: childTable.id, |
|
|
|
@ -554,8 +550,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
|
|
|
|
|
await childModel.selectObject({ qb }); |
|
|
|
|
|
|
|
|
|
let children = await this.extractRawQueryAndExec(qb); |
|
|
|
|
children = this.convertAttachmentType(children); |
|
|
|
|
let children = await this.execAndParse(qb, childTable); |
|
|
|
|
|
|
|
|
|
const proto = await ( |
|
|
|
|
await Model.getBaseModelSQL({ |
|
|
|
@ -672,11 +667,10 @@ class BaseModelSqlv2 {
|
|
|
|
|
!this.isSqlite |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
let children = await this.extractRawQueryAndExec(finalQb); |
|
|
|
|
let children = await this.execAndParse(finalQb, childTable); |
|
|
|
|
if (this.isMySQL) { |
|
|
|
|
children = children[0]; |
|
|
|
|
} |
|
|
|
|
children = this.convertAttachmentType(children); |
|
|
|
|
const proto = await ( |
|
|
|
|
await Model.getBaseModelSQL({ |
|
|
|
|
id: rtnId, |
|
|
|
@ -741,8 +735,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
qb.limit(+rest?.limit || 25); |
|
|
|
|
qb.offset(+rest?.offset || 0); |
|
|
|
|
|
|
|
|
|
let children = await this.extractRawQueryAndExec(qb); |
|
|
|
|
children = this.convertAttachmentType(children); |
|
|
|
|
let children = await this.execAndParse(qb, childTable); |
|
|
|
|
const proto = await ( |
|
|
|
|
await Model.getBaseModelSQL({ id: rtnId, dbDriver: this.dbDriver }) |
|
|
|
|
).getProto(); |
|
|
|
@ -969,7 +962,6 @@ class BaseModelSqlv2 {
|
|
|
|
|
|
|
|
|
|
const proto = await childModel.getProto(); |
|
|
|
|
let data = await qb; |
|
|
|
|
data = this.convertAttachmentType(data); |
|
|
|
|
return data.map((c) => { |
|
|
|
|
c.__proto__ = proto; |
|
|
|
|
return c; |
|
|
|
@ -1083,8 +1075,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
applyPaginate(qb, rest); |
|
|
|
|
|
|
|
|
|
const proto = await childModel.getProto(); |
|
|
|
|
let data = await this.extractRawQueryAndExec(qb); |
|
|
|
|
data = this.convertAttachmentType(data); |
|
|
|
|
let data = await this.execAndParse(qb, childTable); |
|
|
|
|
|
|
|
|
|
return data.map((c) => { |
|
|
|
|
c.__proto__ = proto; |
|
|
|
@ -1202,8 +1193,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
applyPaginate(qb, rest); |
|
|
|
|
|
|
|
|
|
const proto = await parentModel.getProto(); |
|
|
|
|
let data = await this.extractRawQueryAndExec(qb); |
|
|
|
|
data = this.convertAttachmentType(data); |
|
|
|
|
let data = await this.execAndParse(qb, childTable); |
|
|
|
|
|
|
|
|
|
return data.map((c) => { |
|
|
|
|
c.__proto__ = proto; |
|
|
|
@ -1535,7 +1525,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
query.returning( |
|
|
|
|
`${this.model.primaryKey.column_name} as ${this.model.primaryKey.title}` |
|
|
|
|
); |
|
|
|
|
response = await this.extractRawQueryAndExec(query); |
|
|
|
|
response = await this.execAndParse(query); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const ai = this.model.columns.find((c) => c.ai); |
|
|
|
@ -1545,7 +1535,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
|
|
|
|
|
// handle if autogenerated primary key is used
|
|
|
|
|
if (ag) { |
|
|
|
|
if (!response) await this.extractRawQueryAndExec(query); |
|
|
|
|
if (!response) await this.execAndParse(query); |
|
|
|
|
response = await this.readByPk(data[ag.title]); |
|
|
|
|
} else if ( |
|
|
|
|
!response || |
|
|
|
@ -1555,7 +1545,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
if (response?.length) { |
|
|
|
|
id = response[0]; |
|
|
|
|
} else { |
|
|
|
|
const res = await this.extractRawQueryAndExec(query); |
|
|
|
|
const res = await this.execAndParse(query); |
|
|
|
|
id = res?.id ?? res[0]?.insertId; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -1660,7 +1650,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
.update(updateObj) |
|
|
|
|
.where(await this._wherePk(id)); |
|
|
|
|
|
|
|
|
|
await this.extractRawQueryAndExec(query); |
|
|
|
|
await this.execAndParse(query); |
|
|
|
|
|
|
|
|
|
const response = await this.readByPk(id); |
|
|
|
|
await this.afterUpdate(response, trx, cookie); |
|
|
|
@ -2667,7 +2657,6 @@ class BaseModelSqlv2 {
|
|
|
|
|
const proto = await this.getProto(); |
|
|
|
|
|
|
|
|
|
let data = await groupedQb; |
|
|
|
|
data = this.convertAttachmentType(data); |
|
|
|
|
const result = data?.map((d) => { |
|
|
|
|
d.__proto__ = proto; |
|
|
|
|
return d; |
|
|
|
@ -2770,38 +2759,51 @@ class BaseModelSqlv2 {
|
|
|
|
|
return await qb; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private async extractRawQueryAndExec(qb: Knex.QueryBuilder) { |
|
|
|
|
private async execAndParse( |
|
|
|
|
qb: Knex.QueryBuilder, |
|
|
|
|
childTable?: Model |
|
|
|
|
) { |
|
|
|
|
let query = qb.toQuery(); |
|
|
|
|
if (!this.isPg && !this.isMssql) { |
|
|
|
|
query = unsanitize(qb.toQuery()); |
|
|
|
|
} else { |
|
|
|
|
query = sanitize(query); |
|
|
|
|
} |
|
|
|
|
return this.isPg |
|
|
|
|
? (await this.dbDriver.raw(query))?.rows |
|
|
|
|
: query.slice(0, 6) === 'select' && !this.isMssql |
|
|
|
|
? await this.dbDriver.from( |
|
|
|
|
this.dbDriver.raw(query).wrap('(', ') __nc_alias') |
|
|
|
|
) |
|
|
|
|
: await this.dbDriver.raw(query); |
|
|
|
|
return this.convertAttachmentType( |
|
|
|
|
this.isPg |
|
|
|
|
? (await this.dbDriver.raw(query))?.rows |
|
|
|
|
: query.slice(0, 6) === 'select' && !this.isMssql |
|
|
|
|
? await this.dbDriver.from( |
|
|
|
|
this.dbDriver.raw(query).wrap('(', ') __nc_alias') |
|
|
|
|
) |
|
|
|
|
: await this.dbDriver.raw(query), |
|
|
|
|
childTable |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private _convertAttachmentType(attachmentColumns, d) { |
|
|
|
|
attachmentColumns.forEach((col) => { |
|
|
|
|
if (d[col.title] && typeof d[col.title] === 'string') { |
|
|
|
|
d[col.title] = JSON.parse(d[col.title]); |
|
|
|
|
private _convertAttachmentType( |
|
|
|
|
attachmentColumns: Record<string, any>[], |
|
|
|
|
d: Record<string, any> |
|
|
|
|
) { |
|
|
|
|
try { |
|
|
|
|
if (d) { |
|
|
|
|
attachmentColumns.forEach((col) => { |
|
|
|
|
if (d[col.title] && typeof d[col.title] === 'string') { |
|
|
|
|
d[col.title] = JSON.parse(d[col.title]); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} catch {} |
|
|
|
|
return d; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private convertAttachmentType(data) { |
|
|
|
|
private convertAttachmentType(data: Record<string, any>, childTable?: Model) { |
|
|
|
|
// attachment is stored in text and parse in UI
|
|
|
|
|
// convertAttachmentType is used to convert the response in string to array of object in API response
|
|
|
|
|
if (data) { |
|
|
|
|
const attachmentColumns = this.model.columns.filter( |
|
|
|
|
(c) => c.uidt === UITypes.Attachment |
|
|
|
|
); |
|
|
|
|
const attachmentColumns = ( |
|
|
|
|
childTable ? childTable.columns : this.model.columns |
|
|
|
|
).filter((c) => c.uidt === UITypes.Attachment); |
|
|
|
|
if (attachmentColumns.length) { |
|
|
|
|
if (Array.isArray(data)) { |
|
|
|
|
data = data.map((d) => |
|
|
|
|