mirror of https://github.com/nocodb/nocodb
mertmit
2 years ago
committed by
starbirdtech383
7 changed files with 1615 additions and 0 deletions
@ -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(); |
||||
}); |
||||
}); |
@ -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, |
||||
}); |
||||
} |
||||
} |
@ -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; |
||||
} |
@ -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(); |
||||
}); |
||||
}); |
Loading…
Reference in new issue