Browse Source

fix: hasmany list api correction

Signed-off-by: Pranav C Balan <pranavxc@gmail.com>
pull/341/head
Pranav C Balan 3 years ago committed by Pranav C
parent
commit
28234d2aee
  1. 12
      packages/nocodb/src/lib/dataMapper/lib/sql/BaseModelSql.ts
  2. 2
      packages/nocodb/src/lib/noco/rest/RestCtrlHasMany.ts
  3. 2
      packages/nocodb/src/lib/sqlMgr/code/routes/xc-ts/ExpressXcTsRoutesHm.ts

12
packages/nocodb/src/lib/dataMapper/lib/sql/BaseModelSql.ts

@ -1107,13 +1107,14 @@ class BaseModelSql extends BaseModel {
async _getChildListInParent({parent, child}, rest = {}, index) { async _getChildListInParent({parent, child}, rest = {}, index) {
let {fields, where, limit, offset, sort} = this._getChildListArgs(rest, index); let {fields, where, limit, offset, sort} = this._getChildListArgs(rest, index);
const {cn} = this.hasManyRelations.find(({tn}) => tn === child) || {}; const {cn} = this.hasManyRelations.find(({tn}) => tn === child) || {};
const _cn = this.dbModels[child].columnToAlias?.[cn];
if (fields !== '*' && fields.split(',').indexOf(cn) === -1) { if (fields !== '*' && fields.split(',').indexOf(cn) === -1) {
fields += ',' + cn; fields += ',' + cn;
} }
let childs = await this._run(this.dbDriver.union( const childs = await this._run(this.dbDriver.union(
parent.map(p => { parent.map(p => {
const query = const query =
this this
@ -1122,15 +1123,14 @@ class BaseModelSql extends BaseModel {
.xwhere(where, this.dbModels[child].selectQuery('')) .xwhere(where, this.dbModels[child].selectQuery(''))
.select(this.dbModels[child].selectQuery(fields)) // ...fields.split(',')); .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; return this.isSqlite() ? this.dbDriver.select().from(query) : query;
}), !this.isSqlite() }), !this.isSqlite()
)); ));
let gs = _.groupBy(childs, cn); let gs = _.groupBy(childs, _cn);
parent.forEach(row => { 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 * @returns {Object} query appended with paginate and sort params
* @private * @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) query.offset(offset)
.limit(limit); .limit(limit);

2
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<void> { public async hasManyList(req: Request | any, res): Promise<void> {
const data = await req.parentModel.hasManyList({ const data = await req.parentModel.hasManyList({
...req.query, ...req.query,
childs: req.childModel.tn childs: req.params.childs // req.childModel.tn
} as any); } as any);
res.xcJson(data); res.xcJson(data);
} }

2
packages/nocodb/src/lib/sqlMgr/code/routes/xc-ts/ExpressXcTsRoutesHm.ts

@ -34,7 +34,7 @@ class ExpressXcTsRoutesHm extends BaseRender {
const ejsData: any = this.prepare(); const ejsData: any = this.prepare();
return [ return [
{ {
path: `/api/${this.ctx.routeVersionLetter}/${ejsData._tn}/has/${ejsData._ctn}`, path: `/api/${this.ctx.routeVersionLetter}/${ejsData._tn}/has/:childs`, // ${ejsData._ctn}
type: 'get', type: 'get',
handler: ['hasManyList'], handler: ['hasManyList'],
acl: { acl: {

Loading…
Cancel
Save