diff --git a/packages/nocodb-sdk/src/lib/Api.ts b/packages/nocodb-sdk/src/lib/Api.ts index b1486b3930..9086fd0ea2 100644 --- a/packages/nocodb-sdk/src/lib/Api.ts +++ b/packages/nocodb-sdk/src/lib/Api.ts @@ -2010,6 +2010,46 @@ export class Api< format: 'json', ...params, }), + + /** + * No description + * + * @tags Base + * @name MetaDiffSync + * @request POST:/api/v1/db/meta/projects/{projectId}/meta-diff/{baseId} + * @response `200` `any` OK + */ + metaDiffSync: ( + projectId: string, + baseId: string, + params: RequestParams = {} + ) => + this.request({ + path: `/api/v1/db/meta/projects/${projectId}/meta-diff/${baseId}`, + method: 'POST', + format: 'json', + ...params, + }), + + /** + * No description + * + * @tags Base + * @name MetaDiffGet + * @request GET:/api/v1/db/meta/projects/{projectId}/meta-diff/{baseId} + * @response `200` `any` OK + */ + metaDiffGet: ( + projectId: string, + baseId: string, + params: RequestParams = {} + ) => + this.request({ + path: `/api/v1/db/meta/projects/${projectId}/meta-diff/${baseId}`, + method: 'GET', + format: 'json', + ...params, + }), }; dbTable = { /** diff --git a/packages/nocodb/src/lib/meta/api/baseApis.ts b/packages/nocodb/src/lib/meta/api/baseApis.ts index b173f98402..7b015135b7 100644 --- a/packages/nocodb/src/lib/meta/api/baseApis.ts +++ b/packages/nocodb/src/lib/meta/api/baseApis.ts @@ -1,8 +1,8 @@ import { Request, Response } from 'express'; import Project from '../../models/Project'; -import { ModelTypes, ProjectListType, UITypes } from 'nocodb-sdk'; +import { BaseListType, ModelTypes, ProjectListType, UITypes } from 'nocodb-sdk'; import { PagedResponseImpl } from '../helpers/PagedResponse'; -import syncMigration from '../helpers/syncMigration'; +import { syncBaseMigration } from '../helpers/syncMigration'; import { IGNORE_TABLES } from '../../utils/common/BaseApiBuilder'; import Column from '../../models/Column'; import Model from '../../models/Model'; @@ -39,18 +39,19 @@ export async function baseUpdate( export async function baseList( req: Request, - res: Response, + res: Response, next ) { try { - const projects = await Project.list(req.query); + const bases = await Base.list({ projectId: req.params.projectId }); res // todo: pagination - .json( - new PagedResponseImpl(projects, { - count: projects.length, - limit: projects.length, - }) + .json({ + bases: new PagedResponseImpl(bases, { + count: bases.length, + limit: bases.length, + }) + } ); } catch (e) { console.log(e); @@ -71,9 +72,14 @@ export async function baseDelete( async function baseCreate(req: Request, res) { // type | base | projectId const baseBody = req.body; - const base = await Base.createBase(baseBody); - const project = await base.getProject(); - await syncMigration(project); + const project = await Project.getWithInfo(req.params.projectId); + const base = await Base.createBase({ + ...baseBody, + type: baseBody.config?.client, + projectId: project.id, + }); + + await syncBaseMigration(project, base); const info = await populateMeta(base, project); @@ -120,12 +126,14 @@ async function populateMeta(base: Base, project: Project): Promise { return t; }); - /* filter based on prefix */ + // TODO add base prefix + /* filter based on prefix if (project?.prefix) { tables = tables.filter((t) => { return t?.tn?.startsWith(project?.prefix); }); } + */ info.tablesCount = tables.length; diff --git a/packages/nocodb/src/lib/meta/helpers/syncMigration.ts b/packages/nocodb/src/lib/meta/helpers/syncMigration.ts index 3e106c009c..50dff87b29 100644 --- a/packages/nocodb/src/lib/meta/helpers/syncMigration.ts +++ b/packages/nocodb/src/lib/meta/helpers/syncMigration.ts @@ -1,5 +1,6 @@ import Migrator from '../../db/sql-migrator/lib/KnexMigratorv2'; import Project from '../../models/Project'; +import Base from '../../models/Base'; export default async function syncMigration(project: Project): Promise { for (const base of await project.getBases()) { @@ -19,3 +20,20 @@ export default async function syncMigration(project: Project): Promise { } } } + +export async function syncBaseMigration(project: Project, base: Base): Promise { + try { + /* create sql-migrator */ + const migrator = new Migrator(project); + + await migrator.init(base); + + /* sql-migrator : sync & up */ + await migrator.sync(base); + + await migrator.migrationsUp({ base }); + } catch (e) { + console.log(e); + // throw e; + } +}