Browse Source

fix: meta sync malfunction

re #434

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/458/head
Pranav C 3 years ago
parent
commit
cbf69718c6
  1. 2
      packages/nocodb/src/lib/noco/NcProjectBuilderEE.ts
  2. 49
      packages/nocodb/src/lib/noco/common/BaseApiBuilder.ts
  3. 53
      packages/nocodb/src/lib/noco/gql/GqlApiBuilder.ts
  4. 5
      packages/nocodb/src/lib/noco/meta/NcMetaMgr.ts
  5. 20
      packages/nocodb/src/lib/noco/rest/RestApiBuilder.ts

2
packages/nocodb/src/lib/noco/NcProjectBuilderEE.ts

@ -36,7 +36,7 @@ export default class NcProjectBuilderEE extends NcProjectBuilder {
break; break;
case 'tableMetaCreate': case 'tableMetaCreate':
// await curBuilder.onTableCreate(data.req.args.tn) // await curBuilder.onTableCreate(data.req.args.tn)
await curBuilder.xcTablesPopulate({tableNames: data.req.args.tableNames.map(tn => ({tn}))}); await curBuilder.xcTablesPopulate({tableNames: data.req.args.tableNames.map(tn => ({tn})), type:'table'});
break; break;
case 'viewMetaCreate': case 'viewMetaCreate':

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

@ -710,7 +710,6 @@ export default abstract class BaseApiBuilder<T extends Noco> implements XcDynami
this.baseLog(`onTableUpdate : Generating model instance for '%s' table`, tn) this.baseLog(`onTableUpdate : Generating model instance for '%s' table`, tn)
await NcHelp.executeOperations(aclOper, this.connectionConfig.client); await NcHelp.executeOperations(aclOper, this.connectionConfig.client);
@ -910,11 +909,17 @@ export default abstract class BaseApiBuilder<T extends Noco> implements XcDynami
childMeta.manyToMany = childMeta.manyToMany.filter(mm => !(mm.tn === parent && mm.rtn === child || mm.tn === child && mm.rtn === parent)) childMeta.manyToMany = childMeta.manyToMany.filter(mm => !(mm.tn === parent && mm.rtn === child || mm.tn === child && mm.rtn === parent))
// filter lookup and relation virtual columns // filter lookup and relation virtual columns
parentMeta.v = parentMeta.v.filter(({mm, ...rest}) => (!mm || !(mm.tn === parent && mm.rtn === child || mm.tn === child && mm.rtn === parent)) parentMeta.v = parentMeta.v.filter(({
mm,
...rest
}) => (!mm || !(mm.tn === parent && mm.rtn === child || mm.tn === child && mm.rtn === parent))
// check for lookup // check for lookup
&& !(rest.lk && rest.lk.type === 'mm' && (rest.lk.tn === parent && rest.lk.rtn === child || rest.lk.tn === child && rest.lk.rtn === parent)) && !(rest.lk && rest.lk.type === 'mm' && (rest.lk.tn === parent && rest.lk.rtn === child || rest.lk.tn === child && rest.lk.rtn === parent))
) )
childMeta.v = childMeta.v.filter(({mm, ...rest}) => (!mm || !(mm.tn === parent && mm.rtn === child || mm.tn === child && mm.rtn === parent)) childMeta.v = childMeta.v.filter(({
mm,
...rest
}) => (!mm || !(mm.tn === parent && mm.rtn === child || mm.tn === child && mm.rtn === parent))
// check for lookup // check for lookup
&& !(rest.lk && rest.lk.type === 'mm' && (rest.lk.tn === parent && rest.lk.rtn === child || rest.lk.tn === child && rest.lk.rtn === parent)) && !(rest.lk && rest.lk.type === 'mm' && (rest.lk.tn === parent && rest.lk.rtn === child || rest.lk.tn === child && rest.lk.rtn === parent))
) )
@ -952,17 +957,17 @@ export default abstract class BaseApiBuilder<T extends Noco> implements XcDynami
protected initDbDriver(): void { protected initDbDriver(): void {
this.dbDriver = NcConnectionMgr.get({ this.dbDriver = NcConnectionMgr.get({
dbAlias:this.dbAlias, dbAlias: this.dbAlias,
env:this.config.env, env: this.config.env,
config:this.config, config: this.config,
projectId:this.projectId projectId: this.projectId
}); });
this.sqlClient = NcConnectionMgr.getSqlClient({ this.sqlClient = NcConnectionMgr.getSqlClient({
dbAlias:this.dbAlias, dbAlias: this.dbAlias,
env:this.config.env, env: this.config.env,
config:this.config, config: this.config,
projectId:this.projectId projectId: this.projectId
}) })
// if (!this.dbDriver) { // if (!this.dbDriver) {
// if(this.projectBuilder?.prefix){ // if(this.projectBuilder?.prefix){
@ -1092,10 +1097,16 @@ export default abstract class BaseApiBuilder<T extends Noco> implements XcDynami
return ctx; return ctx;
} }
protected getTableNameAlias(tn: string) { protected getTableNameAlias(tableName: string) {
let tn = tableName;
if (this.metas?.[tn]?._tn) { if (this.metas?.[tn]?._tn) {
return this.metas?.[tn]?._tn; return this.metas?.[tn]?._tn;
} }
if (this.projectBuilder?.prefix) {
tn = tn.replace(this.projectBuilder?.prefix, '')
}
const modifiedTableName = tn?.replace(/^(?=\d+)/, 'ISN___') const modifiedTableName = tn?.replace(/^(?=\d+)/, 'ISN___')
return this.getInflectedName(modifiedTableName, this.connectionConfig?.meta?.inflection?.tn); return this.getInflectedName(modifiedTableName, this.connectionConfig?.meta?.inflection?.tn);
} }
@ -1104,8 +1115,8 @@ export default abstract class BaseApiBuilder<T extends Noco> implements XcDynami
this.baseLog(`generateContextForHasMany : '%s' => '%s'`, ctx.tn, tnc); this.baseLog(`generateContextForHasMany : '%s' => '%s'`, ctx.tn, tnc);
return { return {
...ctx, ...ctx,
_tn: this.metas[ctx.tn]._tn, _tn: this.metas[ctx.tn]?._tn,
_ctn: this.metas[tnc]._tn, _ctn: this.metas[tnc]?._tn,
ctn: tnc, ctn: tnc,
project_id: this.projectId project_id: this.projectId
}; };
@ -1426,10 +1437,10 @@ export default abstract class BaseApiBuilder<T extends Noco> implements XcDynami
// todo: ignore duplicate m2m relations // todo: ignore duplicate m2m relations
// todo: optimize, just compare associative table(Vtn) // todo: optimize, just compare associative table(Vtn)
...meta.manyToMany.filter((v, i) => !meta.v.some(v1 => v1.mm ...meta.manyToMany.filter((v, i) => !meta.v.some(v1 => v1.mm
&& ( && (
v1.mm.tn === v.tn && v.rtn === v1.mm.rtn v1.mm.tn === v.tn && v.rtn === v1.mm.rtn
|| v1.mm.rtn === v.tn && v.tn === v1.mm.rtn || v1.mm.rtn === v.tn && v.tn === v1.mm.rtn
) && v.vtn === v1.mm.vtn) ) && v.vtn === v1.mm.vtn)
// ignore duplicate // ignore duplicate
&& !meta.manyToMany.some((v1, i1) => i1 !== i && v1.tn === v.tn && v.rtn === v1.rtn && v.vtn === v1.vtn) && !meta.manyToMany.some((v1, i1) => i1 !== i && v1.tn === v.tn && v.rtn === v1.rtn && v.vtn === v1.vtn)
).map(mm => { ).map(mm => {

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

@ -238,7 +238,13 @@ export class GqlApiBuilder extends BaseApiBuilder<Noco> implements XcMetaMgr {
// todo: load procedure and functions // todo: load procedure and functions
await this.loadXcAcl(); await this.loadXcAcl();
const {metaArr, enabledModels, tableAndViewArr, functionArr, procedureArr} = await this.readXcModelsAndGroupByType(); const {
metaArr,
enabledModels,
tableAndViewArr,
functionArr,
procedureArr
} = await this.readXcModelsAndGroupByType();
const procedureResolver = new GqlProcedureResolver(this, functionArr, procedureArr, this.procedureOrFunctionAcls); const procedureResolver = new GqlProcedureResolver(this, functionArr, procedureArr, this.procedureOrFunctionAcls);
@ -286,12 +292,18 @@ export class GqlApiBuilder extends BaseApiBuilder<Noco> implements XcMetaMgr {
}; };
this.schemas[meta.title] = meta.schema; this.schemas[meta.title] = meta.schema;
this.policies[meta.title] = resolversArr.filter(({title}) => title === meta.title).reduce((aclObj, {acl, resolver}) => { this.policies[meta.title] = resolversArr.filter(({title}) => title === meta.title).reduce((aclObj, {
acl,
resolver
}) => {
aclObj[resolver] = JSON.parse(acl); aclObj[resolver] = JSON.parse(acl);
return aclObj; return aclObj;
}, {}); }, {});
const functions = resolversArr.filter(({title}) => title === meta.title).reduce((fnObj, {functions, resolver}) => { const functions = resolversArr.filter(({title}) => title === meta.title).reduce((fnObj, {
functions,
resolver
}) => {
fnObj[resolver] = JSON.parse(functions); fnObj[resolver] = JSON.parse(functions);
return fnObj; return fnObj;
}, {}); }, {});
@ -536,20 +548,32 @@ export class GqlApiBuilder extends BaseApiBuilder<Noco> implements XcMetaMgr {
if (args?.tableNames?.length) { if (args?.tableNames?.length) {
const relatedTableList = []
// extract tables which have relation with the tables in list
for (const r of relations) {
if (args.tableNames.some(t => t.tn === r.tn)) {
if (!relatedTableList.includes(r.rtn)) {
relatedTableList.push(r.rtn)
await this.onTableDelete(r.rtn)
}
} else if (args.tableNames.some(t => t.tn === r.rtn)) {
if (!relatedTableList.includes(r.tn)) {
relatedTableList.push(r.tn)
await this.onTableDelete(r.tn)
}
}
}
tables = args.tableNames.map(({tn, _tn}) => ({ tables = args.tableNames.map(({tn, _tn}) => ({
tn, tn,
_tn, _tn,
type: args.type type: args.type
})); }));
tables.push(...relatedTableList.map(t => ({tn: t})))
} else { } else {
tables = (await this.sqlClient.tableList())?.data?.list?.filter(({tn}) => !IGNORE_TABLES.includes(tn)); tables = (await this.sqlClient.tableList())?.data?.list?.filter(({tn}) => !IGNORE_TABLES.includes(tn));
/* filter based on prefix */
if (this.projectBuilder?.prefix) {
tables = tables.filter(t => t.tn.startsWith(this.projectBuilder?.prefix))
}
this.tablesCount = tables.length;
// enable extra // enable extra
// tables.push(...(await this.sqlClient.viewList())?.data?.list?.map(v => { // tables.push(...(await this.sqlClient.viewList())?.data?.list?.map(v => {
// this.viewsCount++; // this.viewsCount++;
@ -607,6 +631,17 @@ export class GqlApiBuilder extends BaseApiBuilder<Noco> implements XcMetaMgr {
} }
/* filter based on prefix */
if (this.projectBuilder?.prefix) {
tables = tables.filter(t => {
t._tn = t._tn || t.tn.replace(this.projectBuilder?.prefix, '')
return t.tn.startsWith(this.projectBuilder?.prefix)
})
}
this.tablesCount = tables.length;
if (tables.length) { if (tables.length) {
relations.forEach(rel => rel.enabled = true); relations.forEach(rel => rel.enabled = true);

5
packages/nocodb/src/lib/noco/meta/NcMetaMgr.ts

@ -2964,6 +2964,11 @@ export default class NcMetaMgr {
tables = (await sqlClient.tableList())?.data?.list?.map(table => { tables = (await sqlClient.tableList())?.data?.list?.map(table => {
return tables.find(mod => mod.title === table.tn) ?? {title: table.tn, alias: table.tn}; return tables.find(mod => mod.title === table.tn) ?? {title: table.tn, alias: table.tn};
}); });
const config = this.projectConfigs[this.getProjectId(args)]
tables = config?.prefix ? tables.filter(t => {
t.alias = t.title.replace(config?.prefix, '')
return t.title.startsWith(config?.prefix)
}) : tables;
} }
const result = tables.reduce((obj, table) => { const result = tables.reduce((obj, table) => {

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

@ -314,16 +314,11 @@ export class RestApiBuilder extends BaseApiBuilder<Noco> {
} }
} }
tables = args.tableNames.map(({tn}) => ({tn, type: args.type})); tables = args.tableNames.map(({tn,_tn}) => ({tn, type: args.type,_tn}));
tables.push(...relatedTableList.map(t => ({tn:t})))
} else { } else {
tables = (await this.sqlClient.tableList())?.data?.list?.filter(({tn}) => !IGNORE_TABLES.includes(tn)); tables = (await this.sqlClient.tableList())?.data?.list?.filter(({tn}) => !IGNORE_TABLES.includes(tn));
/* filter based on prefix */
if (this.projectBuilder?.prefix) {
tables = tables.filter(t => t?.tn?.startsWith(this.projectBuilder?.prefix))
}
this.tablesCount = tables.length;
// enable extra // enable extra
/* tables.push(...(await this.sqlClient.viewList())?.data?.list?.map(v => { /* tables.push(...(await this.sqlClient.viewList())?.data?.list?.map(v => {
@ -343,6 +338,17 @@ export class RestApiBuilder extends BaseApiBuilder<Noco> {
await this.populteProcedureAndFunctionRoutes(); await this.populteProcedureAndFunctionRoutes();
} }
/* filter based on prefix */
if (this.projectBuilder?.prefix) {
tables = tables.filter(t => {
t._tn = t._tn || t.tn.replace(this.projectBuilder?.prefix, '')
return t?.tn?.startsWith(this.projectBuilder?.prefix)
})
}
this.tablesCount = tables.length;
const relationRoutes: Array<() => Promise<void>> = []; const relationRoutes: Array<() => Promise<void>> = [];
relations.forEach(r => { relations.forEach(r => {

Loading…
Cancel
Save