|
|
|
@ -41,6 +41,7 @@ import { customValidators } from './customValidators';
|
|
|
|
|
import { NcError } from '../../../../meta/helpers/catchError'; |
|
|
|
|
import { customAlphabet } from 'nanoid'; |
|
|
|
|
import DOMPurify from 'isomorphic-dompurify'; |
|
|
|
|
import { sanitize, unsanitize } from './helpers/sanitize'; |
|
|
|
|
|
|
|
|
|
const GROUP_COL = '__nc_group_id'; |
|
|
|
|
|
|
|
|
@ -310,11 +311,11 @@ class BaseModelSqlv2 {
|
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
qb.count(this.model.primaryKey?.column_name || '*', { |
|
|
|
|
qb.count(sanitize(this.model.primaryKey?.column_name) || '*', { |
|
|
|
|
as: 'count' |
|
|
|
|
}).first(); |
|
|
|
|
|
|
|
|
|
return ((await qb) as any).count; |
|
|
|
|
const res = (await this.dbDriver.raw(unsanitize(qb.toQuery()))) as any; |
|
|
|
|
return (this.isPg ? res.rows[0] : res[0][0] ?? res[0]).count; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
async groupBy( |
|
|
|
@ -911,7 +912,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
|
|
|
|
|
const proto = await childModel.getProto(); |
|
|
|
|
|
|
|
|
|
return (await qb).map(c => { |
|
|
|
|
return (await this.extractRawQueryAndExec(qb)).map(c => { |
|
|
|
|
c.__proto__ = proto; |
|
|
|
|
return c; |
|
|
|
|
}); |
|
|
|
@ -997,8 +998,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
applyPaginate(qb, args); |
|
|
|
|
|
|
|
|
|
const proto = await parentModel.getProto(); |
|
|
|
|
|
|
|
|
|
return (await qb).map(c => { |
|
|
|
|
return (await this.extractRawQueryAndExec(qb)).map(c => { |
|
|
|
|
c.__proto__ = proto; |
|
|
|
|
return c; |
|
|
|
|
}); |
|
|
|
@ -1246,7 +1246,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
await populatePk(this.model, data); |
|
|
|
|
|
|
|
|
|
// todo: filter based on view
|
|
|
|
|
const insertObj = await this.model.mapAliasToColumn(data, sanitize); |
|
|
|
|
const insertObj = await this.model.mapAliasToColumn(data); |
|
|
|
|
|
|
|
|
|
await this.validate(insertObj); |
|
|
|
|
|
|
|
|
@ -1262,12 +1262,11 @@ class BaseModelSqlv2 {
|
|
|
|
|
// const driver = trx ? trx : this.dbDriver;
|
|
|
|
|
|
|
|
|
|
const query = this.dbDriver(this.tnPath).insert(insertObj); |
|
|
|
|
|
|
|
|
|
if (this.isPg || this.isMssql) { |
|
|
|
|
query.returning( |
|
|
|
|
`${this.model.primaryKey.column_name} as ${this.model.primaryKey.title}` |
|
|
|
|
); |
|
|
|
|
response = await query; |
|
|
|
|
response = await this.extractRawQueryAndExec(query); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const ai = this.model.columns.find(c => c.ai); |
|
|
|
@ -1279,11 +1278,19 @@ class BaseModelSqlv2 {
|
|
|
|
|
if (response?.length) { |
|
|
|
|
id = response[0]; |
|
|
|
|
} else { |
|
|
|
|
id = (await query)[0]; |
|
|
|
|
const res = await this.extractRawQueryAndExec(query); |
|
|
|
|
id = res?.id ?? res[0]?.insertId; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (ai) { |
|
|
|
|
// response = await this.readByPk(id)
|
|
|
|
|
if (this.isSqlite) { |
|
|
|
|
// sqlite doesnt return id after insert
|
|
|
|
|
id = ( |
|
|
|
|
await this.dbDriver(this.tnPath) |
|
|
|
|
.select(ai.column_name) |
|
|
|
|
.max(ai.column_name, { as: 'id' }) |
|
|
|
|
)[0].id; |
|
|
|
|
} |
|
|
|
|
response = await this.readByPk(id); |
|
|
|
|
} else { |
|
|
|
|
response = data; |
|
|
|
@ -1330,14 +1337,11 @@ class BaseModelSqlv2 {
|
|
|
|
|
|
|
|
|
|
await this.beforeUpdate(data, trx, cookie); |
|
|
|
|
|
|
|
|
|
// const driver = trx ? trx : this.dbDriver;
|
|
|
|
|
//
|
|
|
|
|
// this.validate(data);
|
|
|
|
|
// await this._run(
|
|
|
|
|
await this.dbDriver(this.tnPath) |
|
|
|
|
const query = this.dbDriver(this.tnPath) |
|
|
|
|
.update(updateObj) |
|
|
|
|
.where(await this._wherePk(id)); |
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
await this.extractRawQueryAndExec(query); |
|
|
|
|
|
|
|
|
|
const response = await this.readByPk(id); |
|
|
|
|
await this.afterUpdate(response, trx, cookie); |
|
|
|
@ -2033,11 +2037,19 @@ class BaseModelSqlv2 {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private async extractRawQueryAndExec(qb: QueryBuilder) { |
|
|
|
|
let query = qb.toQuery(); |
|
|
|
|
if (!this.isPg && !this.isMssql) { |
|
|
|
|
query = unsanitize(qb.toQuery()); |
|
|
|
|
} else { |
|
|
|
|
query = sanitize(query); |
|
|
|
|
} |
|
|
|
|
return this.isPg |
|
|
|
|
? qb |
|
|
|
|
: await this.dbDriver.from( |
|
|
|
|
this.dbDriver.raw(qb.toString()).wrap('(', ') __nc_alias') |
|
|
|
|
); |
|
|
|
|
? (await this.dbDriver.raw(query))?.rows |
|
|
|
|
: query.slice(0, 6) === 'select' |
|
|
|
|
? await this.dbDriver.from( |
|
|
|
|
this.dbDriver.raw(query).wrap('(', ') __nc_alias') |
|
|
|
|
) |
|
|
|
|
: await this.dbDriver.raw(query); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -2172,10 +2184,6 @@ function getCompositePk(primaryKeys: Column[], row) {
|
|
|
|
|
return primaryKeys.map(c => row[c.title]).join('___'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export function sanitize(v) { |
|
|
|
|
return v?.replace(/([^\\]|^)([?])/g, '$1\\$2'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export { BaseModelSqlv2 }; |
|
|
|
|
/** |
|
|
|
|
* @copyright Copyright (c) 2021, Xgene Cloud Ltd |
|
|
|
|