From 28234d2aee32d3299e8d91c2d9e5ef392e3bcfde Mon Sep 17 00:00:00 2001 From: Pranav C Balan Date: Sat, 5 Jun 2021 16:24:15 +0530 Subject: [PATCH] fix: hasmany list api correction Signed-off-by: Pranav C Balan --- .../src/lib/dataMapper/lib/sql/BaseModelSql.ts | 12 ++++++------ packages/nocodb/src/lib/noco/rest/RestCtrlHasMany.ts | 2 +- .../sqlMgr/code/routes/xc-ts/ExpressXcTsRoutesHm.ts | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/nocodb/src/lib/dataMapper/lib/sql/BaseModelSql.ts b/packages/nocodb/src/lib/dataMapper/lib/sql/BaseModelSql.ts index 6d6599c1dd..1b51618234 100644 --- a/packages/nocodb/src/lib/dataMapper/lib/sql/BaseModelSql.ts +++ b/packages/nocodb/src/lib/dataMapper/lib/sql/BaseModelSql.ts @@ -1107,13 +1107,14 @@ class BaseModelSql extends BaseModel { async _getChildListInParent({parent, child}, rest = {}, index) { let {fields, where, limit, offset, sort} = this._getChildListArgs(rest, index); const {cn} = this.hasManyRelations.find(({tn}) => tn === child) || {}; + const _cn = this.dbModels[child].columnToAlias?.[cn]; if (fields !== '*' && fields.split(',').indexOf(cn) === -1) { fields += ',' + cn; } - let childs = await this._run(this.dbDriver.union( + const childs = await this._run(this.dbDriver.union( parent.map(p => { const query = this @@ -1122,15 +1123,14 @@ class BaseModelSql extends BaseModel { .xwhere(where, this.dbModels[child].selectQuery('')) .select(this.dbModels[child].selectQuery(fields)) // ...fields.split(',')); - - this._paginateAndSort(query, {sort, limit, offset}, null, true); + this._paginateAndSort(query, {sort, limit, offset}, null,true); return this.isSqlite() ? this.dbDriver.select().from(query) : query; }), !this.isSqlite() )); - let gs = _.groupBy(childs, cn); + let gs = _.groupBy(childs, _cn); parent.forEach(row => { - row[this.dbModels?.[child]?._tn || child] = gs[row[this.pks[0].cn]] || []; + row[this.dbModels?.[child]?._tn || child] = gs[row[this.pks[0]._cn]] || []; }) } @@ -1390,7 +1390,7 @@ class BaseModelSql extends BaseModel { * @returns {Object} query appended with paginate and sort params * @private */ - _paginateAndSort(query, {limit = 20, offset = 0, sort = ''}: XcFilter, table?: string, isUnion?: boolean) { + _paginateAndSort(query, {limit = 20, offset = 0, sort = ''}: XcFilter, table?: string, isUnion?:boolean) { query.offset(offset) .limit(limit); diff --git a/packages/nocodb/src/lib/noco/rest/RestCtrlHasMany.ts b/packages/nocodb/src/lib/noco/rest/RestCtrlHasMany.ts index d2fd271ad5..c9b21e9f9f 100644 --- a/packages/nocodb/src/lib/noco/rest/RestCtrlHasMany.ts +++ b/packages/nocodb/src/lib/noco/rest/RestCtrlHasMany.ts @@ -124,7 +124,7 @@ export class RestCtrlHasMany extends RestBaseCtrl { public async hasManyList(req: Request | any, res): Promise { const data = await req.parentModel.hasManyList({ ...req.query, - childs: req.childModel.tn + childs: req.params.childs // req.childModel.tn } as any); res.xcJson(data); } diff --git a/packages/nocodb/src/lib/sqlMgr/code/routes/xc-ts/ExpressXcTsRoutesHm.ts b/packages/nocodb/src/lib/sqlMgr/code/routes/xc-ts/ExpressXcTsRoutesHm.ts index 0e6b41e8c4..6bfebe8827 100644 --- a/packages/nocodb/src/lib/sqlMgr/code/routes/xc-ts/ExpressXcTsRoutesHm.ts +++ b/packages/nocodb/src/lib/sqlMgr/code/routes/xc-ts/ExpressXcTsRoutesHm.ts @@ -34,7 +34,7 @@ class ExpressXcTsRoutesHm extends BaseRender { const ejsData: any = this.prepare(); return [ { - path: `/api/${this.ctx.routeVersionLetter}/${ejsData._tn}/has/${ejsData._ctn}`, + path: `/api/${this.ctx.routeVersionLetter}/${ejsData._tn}/has/:childs`, // ${ejsData._ctn} type: 'get', handler: ['hasManyList'], acl: {