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> { protected async syncRelations(): Promise<boolean> {
const [relations, missingRelations] = await this.getRelationsAndMissingRelations() const [
if(!missingRelations) return false; relations,
missingRelations
] = await this.getRelationsAndMissingRelations();
if (!missingRelations) return false;
this.relationsCount = relations.length + missingRelations.length; this.relationsCount = relations.length + missingRelations.length;
@ -2158,7 +2161,7 @@ export default abstract class BaseApiBuilder<T extends Noco>
protected async getRelationsAndMissingRelations(): Promise<[any, any]> { protected async getRelationsAndMissingRelations(): Promise<[any, any]> {
// Relations in metadata // Relations in metadata
let relations = await this.xcMeta.metaList( const relations = await this.xcMeta.metaList(
this.projectId, this.projectId,
this.dbAlias, this.dbAlias,
'nc_relations', 'nc_relations',
@ -2177,7 +2180,6 @@ export default abstract class BaseApiBuilder<T extends Noco>
'db_type', 'db_type',
'dr', 'dr',
'fkn' 'fkn'
] ]
} }
); );
@ -2186,13 +2188,15 @@ export default abstract class BaseApiBuilder<T extends Noco>
const dbRelations = (await this.sqlClient.relationListAll())?.data?.list; const dbRelations = (await this.sqlClient.relationListAll())?.data?.list;
// Relations missing in metadata // Relations missing in metadata
const missingRelations = dbRelations.filter(dbRelation => { const missingRelations = dbRelations
return relations.every(relation => relation?.fkn !== dbRelation?.cstn) .filter(dbRelation => {
}).map(relation => { return relations.every(relation => relation?.fkn !== dbRelation?.cstn);
relation.enabled = true; })
relation.fkn = relation?.cstn; .map(relation => {
return relation relation.enabled = true;
}); relation.fkn = relation?.cstn;
return relation;
});
return [relations, missingRelations]; return [relations, missingRelations];
} }
@ -2929,6 +2933,20 @@ export default abstract class BaseApiBuilder<T extends Noco>
public getMeta(tableName: string): any { public getMeta(tableName: string): any {
return this.metas?.[tableName]; 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 }; 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?.tableNames,
args?.type args?.type
); );
let order = await this.getOrderVal();
let tables; let tables;
/* Get all relations */ /* Get all relations */
let [relations, missingRelations] = await this.getRelationsAndMissingRelations() let [
relations,
missingRelations
] = await this.getRelationsAndMissingRelations();
relations = relations.concat(missingRelations); relations = relations.concat(missingRelations);
// set table name alias // set table name alias
@ -761,7 +767,8 @@ export class GqlApiBuilder extends BaseApiBuilder<Noco> implements XcMetaMgr {
tables = args.tableNames.map(({ tn, _tn }) => ({ tables = args.tableNames.map(({ tn, _tn }) => ({
tn, tn,
_tn, _tn,
type: args.type type: args.type,
order: ++order
})); }));
tables.push(...relatedTableList.map(t => ({ tn: t }))); tables.push(...relatedTableList.map(t => ({ tn: t })));
@ -777,6 +784,7 @@ export class GqlApiBuilder extends BaseApiBuilder<Noco> implements XcMetaMgr {
this.viewsCount++; this.viewsCount++;
v.type = 'view'; v.type = 'view';
v.tn = v.view_name; v.tn = v.view_name;
v.order = ++order;
return v; return v;
}) })
.filter(v => { .filter(v => {
@ -839,7 +847,7 @@ export class GqlApiBuilder extends BaseApiBuilder<Noco> implements XcMetaMgr {
} }
this.tablesCount = tables.length; this.tablesCount = tables.length;
this.syncRelations() await this.syncRelations();
if (tables.length) { if (tables.length) {
relations.forEach(rel => (rel.enabled = true)); relations.forEach(rel => (rel.enabled = true));
@ -928,6 +936,7 @@ export class GqlApiBuilder extends BaseApiBuilder<Noco> implements XcMetaMgr {
this.dbAlias, this.dbAlias,
'nc_models', 'nc_models',
{ {
order: table.order || ++order,
title: table.tn, title: table.tn,
type: table.type || 'table', type: table.type || 'table',
meta: JSON.stringify(this.metas[table.tn]), 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; let tables;
const swaggerRefs: { [table: string]: any[] } = {}; const swaggerRefs: { [table: string]: any[] } = {};
let order = await this.getOrderVal();
let order =
(
await this.xcMeta
.knex('nc_models')
.where({
project_id: this.projectId,
db_alias: this.dbAlias
})
.max('order as max')
.first()
)?.max || 0;
/* Get all relations */ /* 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); relations = relations.concat(missingRelations);
// set table name alias // set table name alias
@ -459,7 +452,7 @@ export class RestApiBuilder extends BaseApiBuilder<Noco> {
r._rtn = this.getTableNameAlias(r.rtn); r._rtn = this.getTableNameAlias(r.rtn);
}); });
this.syncRelations() this.syncRelations();
const tableRoutes = tables.map(table => { const tableRoutes = tables.map(table => {
return async () => { return async () => {

Loading…
Cancel
Save