Browse Source

fix: maintain table list order in GQL

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/852/head
Pranav C 3 years ago
parent
commit
72f5c96e69
  1. 40
      packages/nocodb/src/lib/noco/common/BaseApiBuilder.ts
  2. 15
      packages/nocodb/src/lib/noco/gql/GqlApiBuilder.ts
  3. 21
      packages/nocodb/src/lib/noco/rest/RestApiBuilder.ts

40
packages/nocodb/src/lib/noco/common/BaseApiBuilder.ts

@ -2126,8 +2126,11 @@ export default abstract class BaseApiBuilder<T extends Noco>
}
protected async syncRelations(): Promise<boolean> {
const [relations, missingRelations] = await this.getRelationsAndMissingRelations()
if(!missingRelations) return false;
const [
relations,
missingRelations
] = await this.getRelationsAndMissingRelations();
if (!missingRelations) return false;
this.relationsCount = relations.length + missingRelations.length;
@ -2158,7 +2161,7 @@ export default abstract class BaseApiBuilder<T extends Noco>
protected async getRelationsAndMissingRelations(): Promise<[any, any]> {
// Relations in metadata
let relations = await this.xcMeta.metaList(
const relations = await this.xcMeta.metaList(
this.projectId,
this.dbAlias,
'nc_relations',
@ -2177,7 +2180,6 @@ export default abstract class BaseApiBuilder<T extends Noco>
'db_type',
'dr',
'fkn'
]
}
);
@ -2186,13 +2188,15 @@ export default abstract class BaseApiBuilder<T extends Noco>
const dbRelations = (await this.sqlClient.relationListAll())?.data?.list;
// Relations missing in metadata
const missingRelations = dbRelations.filter(dbRelation => {
return relations.every(relation => relation?.fkn !== dbRelation?.cstn)
}).map(relation => {
relation.enabled = true;
relation.fkn = relation?.cstn;
return relation
});
const missingRelations = dbRelations
.filter(dbRelation => {
return relations.every(relation => relation?.fkn !== dbRelation?.cstn);
})
.map(relation => {
relation.enabled = true;
relation.fkn = relation?.cstn;
return relation;
});
return [relations, missingRelations];
}
@ -2929,6 +2933,20 @@ export default abstract class BaseApiBuilder<T extends Noco>
public getMeta(tableName: string): any {
return this.metas?.[tableName];
}
protected async getOrderVal(): Promise<number> {
const order =
(
await this.xcMeta
.knex('nc_models')
.where({
project_id: this.projectId,
db_alias: this.dbAlias
})
.max('order as max')
.first()
)?.max || 0;
return order;
}
}
export { IGNORE_TABLES };

15
packages/nocodb/src/lib/noco/gql/GqlApiBuilder.ts

@ -725,9 +725,15 @@ export class GqlApiBuilder extends BaseApiBuilder<Noco> implements XcMetaMgr {
args?.tableNames,
args?.type
);
let order = await this.getOrderVal();
let tables;
/* Get all relations */
let [relations, missingRelations] = await this.getRelationsAndMissingRelations()
let [
relations,
missingRelations
] = await this.getRelationsAndMissingRelations();
relations = relations.concat(missingRelations);
// set table name alias
@ -761,7 +767,8 @@ export class GqlApiBuilder extends BaseApiBuilder<Noco> implements XcMetaMgr {
tables = args.tableNames.map(({ tn, _tn }) => ({
tn,
_tn,
type: args.type
type: args.type,
order: ++order
}));
tables.push(...relatedTableList.map(t => ({ tn: t })));
@ -777,6 +784,7 @@ export class GqlApiBuilder extends BaseApiBuilder<Noco> implements XcMetaMgr {
this.viewsCount++;
v.type = 'view';
v.tn = v.view_name;
v.order = ++order;
return v;
})
.filter(v => {
@ -839,7 +847,7 @@ export class GqlApiBuilder extends BaseApiBuilder<Noco> implements XcMetaMgr {
}
this.tablesCount = tables.length;
this.syncRelations()
await this.syncRelations();
if (tables.length) {
relations.forEach(rel => (rel.enabled = true));
@ -928,6 +936,7 @@ export class GqlApiBuilder extends BaseApiBuilder<Noco> implements XcMetaMgr {
this.dbAlias,
'nc_models',
{
order: table.order || ++order,
title: table.tn,
type: table.type || 'table',
meta: JSON.stringify(this.metas[table.tn]),

21
packages/nocodb/src/lib/noco/rest/RestApiBuilder.ts

@ -354,21 +354,14 @@ export class RestApiBuilder extends BaseApiBuilder<Noco> {
);
let tables;
const swaggerRefs: { [table: string]: any[] } = {};
let order =
(
await this.xcMeta
.knex('nc_models')
.where({
project_id: this.projectId,
db_alias: this.dbAlias
})
.max('order as max')
.first()
)?.max || 0;
let order = await this.getOrderVal();
/* Get all relations */
let [relations, missingRelations] = await this.getRelationsAndMissingRelations()
let [
relations,
// eslint-disable-next-line prefer-const
missingRelations
] = await this.getRelationsAndMissingRelations();
relations = relations.concat(missingRelations);
// set table name alias
@ -459,7 +452,7 @@ export class RestApiBuilder extends BaseApiBuilder<Noco> {
r._rtn = this.getTableNameAlias(r.rtn);
});
this.syncRelations()
this.syncRelations();
const tableRoutes = tables.map(table => {
return async () => {

Loading…
Cancel
Save