Browse Source

feat: exportImport nestJS

Signed-off-by: mertmit <mertmit99@gmail.com>
feat/export-nest-dir-restructure
mertmit 2 years ago committed by starbirdtech383
parent
commit
2e1f8b2d55
  1. 6
      packages/nocodb-nest/.gitignore
  2. 21
      packages/nocodb-nest/src/controllers/export-import.controller.spec.ts
  3. 51
      packages/nocodb-nest/src/controllers/export-import.controller.ts
  4. 72
      packages/nocodb-nest/src/helpers/exportImportHelpers.ts
  5. 5
      packages/nocodb-nest/src/modules/metas/metas.module.ts
  6. 19
      packages/nocodb-nest/src/services/export-import.service.spec.ts
  7. 1441
      packages/nocodb-nest/src/services/export-import.service.ts

6
packages/nocodb-nest/.gitignore vendored

@ -35,4 +35,10 @@ lerna-debug.log*
!.vscode/extensions.json !.vscode/extensions.json
/noco.db /noco.db
<<<<<<< HEAD
=======
# export
export/**
>>>>>>> 59591edeb (feat: exportImport nestJS)
/docker/main.js /docker/main.js

21
packages/nocodb-nest/src/controllers/export-import.controller.spec.ts

@ -0,0 +1,21 @@
import { Test } from '@nestjs/testing';
import { ExportImportService } from './../services/export-import.service';
import { ExportImportController } from './export-import.controller';
import type { TestingModule } from '@nestjs/testing';
describe('ExportImportController', () => {
let controller: ExportImportController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [ExportImportController],
providers: [ExportImportService],
}).compile();
controller = module.get<ExportImportController>(ExportImportController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});

51
packages/nocodb-nest/src/controllers/export-import.controller.ts

@ -0,0 +1,51 @@
import {
Body,
Controller,
HttpCode,
Param,
Patch,
Post,
Request,
UseGuards,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { GlobalGuard } from '../guards/global/global.guard';
import {
Acl,
ExtractProjectIdMiddleware,
} from '../middlewares/extract-project-id/extract-project-id.middleware';
import { ExportImportService } from './../services/export-import.service';
@Controller()
@UseGuards(ExtractProjectIdMiddleware, GlobalGuard)
export class ExportImportController {
constructor(private readonly exportImportService: ExportImportService) {}
@Post('/api/v1/db/meta/export/:projectId/:baseId')
@HttpCode(200)
@Acl('exportBase')
async exportBase(@Param('baseId') baseId: string, @Body() body: any) {
return await this.exportImportService.exportBase({
baseId,
path: body.path,
});
}
@Post('/api/v1/db/meta/import/:projectId/:baseId')
@HttpCode(200)
@Acl('importBase')
async importBase(
@Request() req,
@Param('projectId') projectId: string,
@Param('baseId') baseId: string,
@Body() body: any,
) {
return await this.exportImportService.importBase({
user: (req as any).user,
projectId: projectId,
baseId: baseId,
src: body.src,
req,
});
}
}

72
packages/nocodb-nest/src/helpers/exportImportHelpers.ts

@ -0,0 +1,72 @@
import type { Base } from '../models';
export async function generateBaseIdMap(
base: Base,
idMap: Map<string, string>,
) {
idMap.set(base.project_id, base.project_id);
idMap.set(base.id, `${base.project_id}::${base.id}`);
const models = await base.getModels();
for (const md of models) {
idMap.set(md.id, `${base.project_id}::${base.id}::${md.id}`);
await md.getColumns();
for (const column of md.columns) {
idMap.set(column.id, `${idMap.get(md.id)}::${column.id}`);
}
}
return models;
}
export function clearPrefix(text: string, prefix?: string) {
if (!prefix || prefix.length === 0) return text;
return text.replace(new RegExp(`^${prefix}_?`), '');
}
export function withoutNull(obj: any) {
const newObj = {};
let found = false;
for (const [key, value] of Object.entries(obj)) {
if (value !== null) {
newObj[key] = value;
found = true;
}
}
if (!found) return null;
return newObj;
}
export function reverseGet(map: Map<string, string>, vl: string) {
for (const [key, value] of map.entries()) {
if (vl === value) {
return key;
}
}
return undefined;
}
export function withoutId(obj: any) {
const { id, ...rest } = obj;
return rest;
}
export function getParentIdentifier(id: string) {
const arr = id.split('::');
arr.pop();
return arr.join('::');
}
export function getEntityIdentifier(id: string) {
const arr = id.split('::');
return arr.pop();
}
export function findWithIdentifier(map: Map<string, any>, id: string) {
for (const key of map.keys()) {
if (getEntityIdentifier(key) === id) {
return map.get(key);
}
}
return undefined;
}

5
packages/nocodb-nest/src/modules/metas/metas.module.ts

@ -1,6 +1,8 @@
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { MulterModule } from '@nestjs/platform-express'; import { MulterModule } from '@nestjs/platform-express';
import multer from 'multer'; import multer from 'multer';
import { ExportImportController } from 'src/controllers/export-import.controller';
import { ExportImportService } from 'src/services/export-import.service';
import { NC_ATTACHMENT_FIELD_SIZE } from '../../constants'; import { NC_ATTACHMENT_FIELD_SIZE } from '../../constants';
import { ApiDocsController } from '../../controllers/api-docs/api-docs.controller'; import { ApiDocsController } from '../../controllers/api-docs/api-docs.controller';
import { ApiTokensController } from '../../controllers/api-tokens.controller'; import { ApiTokensController } from '../../controllers/api-tokens.controller';
@ -82,6 +84,7 @@ import { DatasModule } from '../datas/datas.module';
}, },
}), }),
GlobalModule, GlobalModule,
DatasModule,
], ],
controllers: [ controllers: [
ApiDocsController, ApiDocsController,
@ -91,6 +94,7 @@ import { DatasModule } from '../datas/datas.module';
BasesController, BasesController,
CachesController, CachesController,
ColumnsController, ColumnsController,
ExportImportController,
FiltersController, FiltersController,
FormColumnsController, FormColumnsController,
FormsController, FormsController,
@ -126,6 +130,7 @@ import { DatasModule } from '../datas/datas.module';
BasesService, BasesService,
CachesService, CachesService,
ColumnsService, ColumnsService,
ExportImportService,
FiltersService, FiltersService,
FormColumnsService, FormColumnsService,
FormsService, FormsService,

19
packages/nocodb-nest/src/services/export-import.service.spec.ts

@ -0,0 +1,19 @@
import { Test } from '@nestjs/testing';
import { ExportImportService } from './export-import.service';
import type { TestingModule } from '@nestjs/testing';
describe('ExportImportService', () => {
let service: ExportImportService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [ExportImportService],
}).compile();
service = module.get<ExportImportService>(ExportImportService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});

1441
packages/nocodb-nest/src/services/export-import.service.ts

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save