Browse Source

fix : #543 - update swagger onTableDelete

Swagger needs update onRelationDelete (hm,mm,bt). This is just onTableDelete

Signed-off-by: Naveen MR <oof1lab@gmail.com>
pull/559/head
Naveen MR 3 years ago
parent
commit
3c65f0fd78
  1. 67
      packages/nocodb/src/lib/noco/rest/RestApiBuilder.ts

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

@ -314,8 +314,8 @@ export class RestApiBuilder extends BaseApiBuilder<Noco> {
} }
} }
tables = args.tableNames.map(({tn,_tn}) => ({tn, type: args.type,_tn})); tables = args.tableNames.map(({tn, _tn}) => ({tn, type: args.type, _tn}));
tables.push(...relatedTableList.map(t => ({tn:t}))) 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));
@ -564,8 +564,6 @@ export class RestApiBuilder extends BaseApiBuilder<Noco> {
await NcHelp.executeOperations(relationRoutes, this.connectionConfig.client); await NcHelp.executeOperations(relationRoutes, this.connectionConfig.client);
const swaggerDoc = { const swaggerDoc = {
tags: [], tags: [],
paths: {}, paths: {},
@ -1628,54 +1626,75 @@ export class RestApiBuilder extends BaseApiBuilder<Noco> {
addApis?: any, addApis?: any,
deleteTags?: string[] deleteTags?: string[]
}) { }) {
this.log(`swaggerUpdate :`,); this.log(`swaggerUpdate :`,);
if (!args.deleteApis && !args.addApis && !args.deleteTags) { if (!args.deleteApis && !args.addApis && !args.deleteTags) {
return; return;
} }
/* load swagger JSON */
const swaggerFilePath = path.join(this.config.toolDir, 'nc', this.projectId, this.getDbAlias(), 'swagger'); const swaggerFilePath = path.join(this.config.toolDir, 'nc', this.projectId, this.getDbAlias(), 'swagger');
const swaggerDoc = JSON.parse(fs.readFileSync(path.join(swaggerFilePath, 'swagger.json'), 'utf8')); const swaggerJson = JSON.parse(fs.readFileSync(path.join(swaggerFilePath, 'swagger.json'), 'utf8'));
/* remove tags, paths and keys */
if (args.deleteApis) { if (args.deleteApis) {
this.log(`swaggerUpdate : deleting swagger apis`); this.log(`swaggerUpdate : deleting swagger apis`);
const {tags, paths, definitions} = args.deleteApis; const {tags, paths, definitions} = args.deleteApis;
if (tags) { if (tags) {
swaggerDoc.tags = swaggerDoc.tags.filter(({name}) => tags.find(t => t.name === name)) swaggerJson.tags = swaggerJson.tags.filter(({name}) => tags.find(t => t.name === name))
} }
if (paths) { if (paths) {
Object.keys(paths).forEach(path => delete swaggerDoc.tags[path]); Object.keys(paths).forEach(path => delete swaggerJson.tags[path]);
} }
if (definitions) { if (definitions) {
Object.keys(definitions).forEach(def => delete swaggerDoc.definitions[def]); Object.keys(definitions).forEach(def => delete swaggerJson.definitions[def]);
}
}
if (args.deleteTags) {
this.log(`swaggerUpdate : deleting swagger tags : %o`, args.deleteTags);
for (const tag of args.deleteTags) {
swaggerDoc.tags = swaggerDoc.tags.filter(t => t.name !== tag);
Object.keys(swaggerDoc.paths).forEach(p => {
if (swaggerDoc.paths?.[p]?.[Object.keys(swaggerDoc.paths[p])[0]]?.tags?.includes(tag)) {
delete swaggerDoc.paths[p];
}
})
} }
} }
/* add tags, paths and defnitions */
if (args.addApis) { if (args.addApis) {
this.log(`swaggerUpdate : adding swagger apis`); this.log(`swaggerUpdate : adding swagger apis`);
const {tags, paths, definitions} = args.addApis; const {tags, paths, definitions} = args.addApis;
if (tags) { if (tags) {
swaggerDoc.tags.push(...tags); swaggerJson.tags.push(...tags);
} }
if (paths) { if (paths) {
Object.assign(swaggerDoc.paths, paths); Object.assign(swaggerJson.paths, paths);
} }
if (definitions) { if (definitions) {
Object.assign(swaggerDoc.definitions, definitions); Object.assign(swaggerJson.definitions, definitions);
} }
} }
/* remove tags, paths & defnitions */
if (args.deleteTags) {
this.log(`swaggerUpdate : deleting swagger tags : %o`, args.deleteTags);
for (const tag of args.deleteTags) {
fs.writeFileSync(path.join(swaggerFilePath, 'swagger.json'), JSON.stringify(swaggerDoc)); swaggerJson.tags = swaggerJson.tags.filter(t => t.name !== tag);
Object.keys(swaggerJson.paths).forEach(p => {
if (swaggerJson.paths?.[p]?.[Object.keys(swaggerJson.paths[p])[0]]?.tags?.includes(tag)) {
delete swaggerJson.paths[p];
}
})
if (swaggerJson.definitions) {
if(swaggerJson.definitions[tag]) {
delete swaggerJson.definitions[tag];
}
if(swaggerJson.definitions[`${tag}Nested`]) {
delete swaggerJson.definitions[`${tag}Nested`];
}
}
}
}
fs.writeFileSync(path.join(swaggerFilePath, 'swagger.json'), JSON.stringify(swaggerJson));
} }
@ -1800,7 +1819,7 @@ export class RestApiBuilder extends BaseApiBuilder<Noco> {
protected async getManyToManyRelations(args = {}): Promise<Set<any>> { protected async getManyToManyRelations(args = {}): Promise<Set<any>> {
const metas:Set<any> = await super.getManyToManyRelations(args); const metas: Set<any> = await super.getManyToManyRelations(args);
for (const metaObj of metas) { for (const metaObj of metas) {

Loading…
Cancel
Save