Browse Source

feat: use structured ids instead of human readable ids

Signed-off-by: mertmit <mertmit99@gmail.com>
feat/export-nest-dir-restructure
mertmit 2 years ago committed by starbirdtech383
parent
commit
d0b25cee21
  1. 19
      packages/nocodb/src/controllers/exportImport/export.ctl.ts
  2. 69
      packages/nocodb/src/services/exportImport/export.svc.ts
  3. 8
      packages/nocodb/src/services/exportImport/import.svc.ts

19
packages/nocodb/src/controllers/exportImport/export.ctl.ts

@ -3,17 +3,28 @@ import ncMetaAclMw from '../../meta/helpers/ncMetaAclMw';
import { exportService } from '../../services'; import { exportService } from '../../services';
import type { Request, Response } from 'express'; import type { Request, Response } from 'express';
export async function exportModel(req: Request, res: Response) { export async function exportBaseSchema(req: Request, res: Response) {
res.json( res.json(
await exportService.exportModel({ modelId: req.params.modelId.split(',') }) await exportService.exportBaseSchema({ baseId: req.params.baseId })
);
}
export async function exportModelData(req: Request, res: Response) {
res.json(
await exportService.exportModelData({ projectId: req.params.projectId, modelId: req.params.modelId, viewId: req.params.viewId })
); );
} }
const router = Router({ mergeParams: true }); const router = Router({ mergeParams: true });
router.get( router.get(
'/api/v1/db/meta/export/:modelId', '/api/v1/db/meta/export/:projectId/:baseId',
ncMetaAclMw(exportModel, 'exportModel') ncMetaAclMw(exportBaseSchema, 'exportBaseSchema')
);
router.get(
'/api/v1/db/meta/export/data/:projectId/:modelId/:viewId?',
ncMetaAclMw(exportModelData, 'exportModelData')
); );
export default router; export default router;

69
packages/nocodb/src/services/exportImport/export.svc.ts

@ -1,13 +1,33 @@
import { NcError } from './../../meta/helpers/catchError'; import { NcError } from './../../meta/helpers/catchError';
import { ViewTypes } from 'nocodb-sdk'; import { ViewTypes } from 'nocodb-sdk';
import { Project, Base, Model } from '../../models'; import { Project, Base, Model } from '../../models';
import { dataService } from '..';
import { getViewAndModelByAliasOrId } from '../dbData/helpers';
export async function exportModel(param: { modelId: string[] }) { /*
const exportData = { {
models: [], "entity": "project",
}; "bases": [
### current scope
{
"entity": "base",
"models": [
{
"entity": "model",
"model": {},
"views": []
}
]
}
### end current scope
]
}
*/
// db id to human readable id async function serializeModels(param: { modelId: string[] }) {
const serializedModels = [];
// db id to structured id
const idMap = new Map<string, string>(); const idMap = new Map<string, string>();
const projects: Project[] = [] const projects: Project[] = []
@ -32,19 +52,19 @@ export async function exportModel(param: { modelId: string[] }) {
const all_models = await base.getModels(); const all_models = await base.getModels();
for (const md of all_models) { for (const md of all_models) {
idMap.set(md.id, `${project.title}::${base.alias || 'default'}::${clearPrefix(md.table_name, project.prefix)}`); idMap.set(md.id, `${project.id}::${base.id}::${md.id}`);
await md.getColumns(); await md.getColumns();
for (const column of md.columns) { for (const column of md.columns) {
idMap.set(column.id, `${idMap.get(md.id)}::${column.column_name || column.title}`); idMap.set(column.id, `${idMap.get(md.id)}::${column.id}`);
} }
} }
modelsMap.set(base.id, all_models); modelsMap.set(base.id, all_models);
} }
idMap.set(project.id, project.title); idMap.set(project.id, project.id);
idMap.set(base.id, `${project.title}::${base.alias || 'default'}`); idMap.set(base.id, `${project.id}::${base.id}`);
idMap.set(model.id, `${idMap.get(base.id)}::${clearPrefix(model.table_name, project.prefix)}`); idMap.set(model.id, `${idMap.get(base.id)}::${model.id}`);
await model.getColumns(); await model.getColumns();
await model.getViews(); await model.getViews();
@ -52,7 +72,7 @@ export async function exportModel(param: { modelId: string[] }) {
for (const column of model.columns) { for (const column of model.columns) {
idMap.set( idMap.set(
column.id, column.id,
`${idMap.get(model.id)}::${column.column_name || column.title}` `${idMap.get(model.id)}::${column.id}`
); );
await column.getColOptions(); await column.getColOptions();
if (column.colOptions) { if (column.colOptions) {
@ -90,7 +110,7 @@ export async function exportModel(param: { modelId: string[] }) {
} }
for (const view of model.views) { for (const view of model.views) {
idMap.set(view.id, `${idMap.get(model.id)}::${view.title}`); idMap.set(view.id, `${idMap.get(model.id)}::${view.id}`);
await view.getColumns(); await view.getColumns();
await view.getFilters(); await view.getFilters();
await view.getSorts(); await view.getSorts();
@ -98,7 +118,7 @@ export async function exportModel(param: { modelId: string[] }) {
const export_filters = [] const export_filters = []
for (const fl of view.filter.children) { for (const fl of view.filter.children) {
const tempFl = { const tempFl = {
id: fl.id, id: `${idMap.get(view.id)}::${fl.id}`,
fk_column_id: idMap.get(fl.fk_column_id), fk_column_id: idMap.get(fl.fk_column_id),
fk_parent_id: fl.fk_parent_id, fk_parent_id: fl.fk_parent_id,
is_group: fl.is_group, is_group: fl.is_group,
@ -165,7 +185,8 @@ export async function exportModel(param: { modelId: string[] }) {
} }
} }
exportData.models.push({ serializedModels.push({
entity: 'model',
model: { model: {
id: idMap.get(model.id), id: idMap.get(model.id),
prefix: project.prefix, prefix: project.prefix,
@ -224,10 +245,26 @@ export async function exportModel(param: { modelId: string[] }) {
}); });
} }
return serializedModels;
}
export async function exportBaseSchema(param: { baseId: string }) {
const base = await Base.get(param.baseId);
if (!base) return NcError.badRequest(`Base not found for id '${param.baseId}'`);
const project = await Project.get(base.project_id);
const models = (await base.getModels()).filter((m) => !m.mm);
const exportedModels = await serializeModels({ modelId: models.map(m => m.id) });
const exportData = { id: `${project.id}::${base.id}`, entity: 'base', models: exportedModels };
return exportData; return exportData;
} }
const clearPrefix = (text: string, prefix?: string) => { const clearPrefix = (text: string, prefix?: string) => {
if (!prefix) return text; if (!prefix || prefix.length === 0) return text;
return text.replace(new RegExp(`^${prefix}_`), ''); return text.replace(new RegExp(`^${prefix}_?`), '');
} }

8
packages/nocodb/src/services/exportImport/import.svc.ts

@ -12,7 +12,7 @@ export async function importModels(param: {
req: any; req: any;
}) { }) {
// human readable id to db id // structured id to db id
const idMap = new Map<string, string>(); const idMap = new Map<string, string>();
const project = await Project.get(param.projectId); const project = await Project.get(param.projectId);
@ -529,3 +529,9 @@ function getParentIdentifier(id: string) {
arr.pop(); arr.pop();
return arr.join('::'); return arr.join('::');
} }
/*
function getEntityIdentifier(id: string) {
const arr = id.split('::');
return arr.pop();
}
*/

Loading…
Cancel
Save