From f4e57293509a090e937006ccc63eaa1732bac9f1 Mon Sep 17 00:00:00 2001 From: Pranav C <61551451+pranavxc@users.noreply.github.com> Date: Mon, 5 Jul 2021 19:03:41 +0530 Subject: [PATCH] feat: upgrade migrations Signed-off-by: Pranav C <61551451+pranavxc@users.noreply.github.com> --- packages/nocodb/src/example/docker.ts | 2 +- .../src/lib/noco/common/BaseApiBuilder.ts | 19 ++++-- .../src/lib/noco/rest/RestApiBuilder.ts | 62 +++++++++++++++---- .../sqlMgr/code/models/xc/BaseModelXcMeta.ts | 2 +- .../code/models/xc/ModelXcMetaFactory.ts | 3 +- .../code/routes/xc-ts/ExpressXcTsRoutes.ts | 2 +- 6 files changed, 69 insertions(+), 21 deletions(-) diff --git a/packages/nocodb/src/example/docker.ts b/packages/nocodb/src/example/docker.ts index 7db6861a48..78e69c6bae 100644 --- a/packages/nocodb/src/example/docker.ts +++ b/packages/nocodb/src/example/docker.ts @@ -1,5 +1,5 @@ import Noco from "../lib/noco/Noco"; - +process.env.NC_VERSION = '0009044'; import express from 'express'; import cors from 'cors'; diff --git a/packages/nocodb/src/lib/noco/common/BaseApiBuilder.ts b/packages/nocodb/src/lib/noco/common/BaseApiBuilder.ts index a4d75a3ada..de8e62e2a9 100644 --- a/packages/nocodb/src/lib/noco/common/BaseApiBuilder.ts +++ b/packages/nocodb/src/lib/noco/common/BaseApiBuilder.ts @@ -668,7 +668,7 @@ export default abstract class BaseApiBuilder implements XcDynami const NC_VERSIONS = [ {name: '0009000', handler: null}, - {name: '0009044', handler: this.xcUpManyToMany} + {name: '0009044', handler: this.ncUpManyToMany} ] if (!await this.xcMeta?.knex?.schema?.hasTable?.('nc_store')) { return; @@ -1660,22 +1660,29 @@ export default abstract class BaseApiBuilder implements XcDynami } - private async xcUpManyToMany(): Promise { - const metas = await this.xcMeta.metaList(this.projectId, this.dbAlias, 'xc_models', { + protected async ncUpManyToMany(): Promise { + const models = await this.xcMeta.metaList(this.projectId, this.dbAlias, 'nc_models', { fields: ['meta'] }); + if (!models.length) { + return + } + const metas = []; // add virtual columns for relations - for (const {meta: metaJson} of metas) { - const meta = JSON.parse(metaJson.meta); + for (const metaObj of models) { + const meta = JSON.parse(metaObj.meta); + metas.push(meta); const ctx = this.generateContextForTable(meta.tn, meta.columns, [], meta.hasMany, meta.belongsTo, meta.type, meta._tn); meta.v = ModelXcMetaFactory.create(this.connectionConfig, {dir: '', ctx, filename: ''}).getVitualColumns(); - await this.xcMeta.metaUpdate(this.projectId, this.dbAlias, 'xc_models', { + ModelXcMetaFactory.create(this.connectionConfig, {}).mapDefaultPrimaryValue(meta.columns); + await this.xcMeta.metaUpdate(this.projectId, this.dbAlias, 'nc_models', { meta: JSON.stringify(meta) }, {title: meta.tn}) } // generate many to many relations an columns await this.getManyToManyRelations(); + return metas; } } diff --git a/packages/nocodb/src/lib/noco/rest/RestApiBuilder.ts b/packages/nocodb/src/lib/noco/rest/RestApiBuilder.ts index dc50ff82dc..c6f1b90cb7 100644 --- a/packages/nocodb/src/lib/noco/rest/RestApiBuilder.ts +++ b/packages/nocodb/src/lib/noco/rest/RestApiBuilder.ts @@ -1,13 +1,3 @@ -// import { -// ExpressXcTsRoutes, -// ExpressXcTsRoutesBt, -// ExpressXcTsRoutesHm, -// ModelXcMetaFactory, -// SwaggerXc, -// SwaggerXcHm, -// SwaggerXcBt -// } from 'nc-help'; - import {RestCtrl} from "./RestCtrl"; import {RestCtrlBelongsTo} from "./RestCtrlBelongsTo"; import {RestCtrlHasMany} from "./RestCtrlHasMany"; @@ -59,7 +49,7 @@ export class RestApiBuilder extends BaseApiBuilder { this.xcMeta = xcMeta; } - public async init():Promise{ + public async init(): Promise { await super.init(); await this.loadRoutes(null); } @@ -1684,6 +1674,56 @@ export class RestApiBuilder extends BaseApiBuilder { }) } + + protected async ncUpManyToMany(): Promise { + const metas = await super.ncUpManyToMany(); + if (!metas) { + return; + } + for (const meta of metas) { + const ctx = this.generateContextForTable(meta.tn, meta.columns, [], meta.hasMany, meta.belongsTo, meta.type, meta._tn); + + /* create routes for table */ + const routes = new ExpressXcTsRoutes({dir: '', ctx, filename: ''}).getObjectWithoutFunctions(); + + /* create nc_routes, add new routes or update order */ + const routesInsertion = routes.map((route, i) => { + return async () => { + if (!await this.xcMeta.metaGet(this.projectId, this.dbAlias, 'nc_routes', { + path: route.path, + tn: meta.tn, + title: meta.tn, + type: route.type + })) { + await this.xcMeta.metaInsert(this.projectId, this.dbAlias, 'nc_routes', { + acl: JSON.stringify(route.acl), + handler: JSON.stringify(route.handler), + order: i, + path: route.path, + tn: meta.tn, + title: meta.tn, + type: route.type, + }) + } else { + await this.xcMeta.metaUpdate(this.projectId, this.dbAlias, 'nc_routes', { + order: i, + }, { + path: route.path, + tn: meta.tn, + title: meta.tn, + type: route.type + }) + } + } + }); + + await NcHelp.executeOperations(routesInsertion, this.connectionConfig.client); + + } + + // add new routes + + } } diff --git a/packages/nocodb/src/lib/sqlMgr/code/models/xc/BaseModelXcMeta.ts b/packages/nocodb/src/lib/sqlMgr/code/models/xc/BaseModelXcMeta.ts index 8aaddee873..ba3ba6e032 100644 --- a/packages/nocodb/src/lib/sqlMgr/code/models/xc/BaseModelXcMeta.ts +++ b/packages/nocodb/src/lib/sqlMgr/code/models/xc/BaseModelXcMeta.ts @@ -33,7 +33,7 @@ abstract class BaseModelXcMeta extends BaseRender { ]; } - protected mapDefaultPrimaryValue(columnsArr: any[]): void { + public mapDefaultPrimaryValue(columnsArr: any[]): void { // pk can be at the end // diff --git a/packages/nocodb/src/lib/sqlMgr/code/models/xc/ModelXcMetaFactory.ts b/packages/nocodb/src/lib/sqlMgr/code/models/xc/ModelXcMetaFactory.ts index 3f0d5cf26f..3e55a2e084 100644 --- a/packages/nocodb/src/lib/sqlMgr/code/models/xc/ModelXcMetaFactory.ts +++ b/packages/nocodb/src/lib/sqlMgr/code/models/xc/ModelXcMetaFactory.ts @@ -3,9 +3,10 @@ import ModelXcMetaSqlite from "./ModelXcMetaSqlite"; import ModelXcMetaPg from "./ModelXcMetaPg"; import ModelXcMetaMssql from "./ModelXcMetaMssql"; import ModelXcMetaOracle from "./ModelXcMetaOracle"; +import BaseModelXcMeta from "./BaseModelXcMeta"; class ModelXcMetaFactory { - public static create(connectionConfig, args) { + public static create(connectionConfig, args):BaseModelXcMeta { if (connectionConfig.client === "mysql2" || connectionConfig.client === "mysql") { return new ModelXcMetaMysql(args) } else if (connectionConfig.client === "sqlite3") { diff --git a/packages/nocodb/src/lib/sqlMgr/code/routes/xc-ts/ExpressXcTsRoutes.ts b/packages/nocodb/src/lib/sqlMgr/code/routes/xc-ts/ExpressXcTsRoutes.ts index a4b201dd41..81e7e7cb51 100644 --- a/packages/nocodb/src/lib/sqlMgr/code/routes/xc-ts/ExpressXcTsRoutes.ts +++ b/packages/nocodb/src/lib/sqlMgr/code/routes/xc-ts/ExpressXcTsRoutes.ts @@ -327,7 +327,7 @@ async function(req, res){ } - getObjectWithoutFunctions() { + getObjectWithoutFunctions() { return this.getObject().map(({functions, ...rest}) => rest) } }