Browse Source

fix: asynchronous NcConfigFactory

Signed-off-by: mertmit <mertmit99@gmail.com>
pull/5031/head
mertmit 2 years ago
parent
commit
28e7e7b0c8
  1. 53
      packages/nocodb/src/lib/Noco.ts
  2. 2
      packages/nocodb/src/lib/db/sql-migrator/lib/KnexMigrator.ts
  3. 6
      packages/nocodb/src/lib/meta/NcMetaMgr.ts
  4. 45
      packages/nocodb/src/lib/utils/NcConfigFactory.ts
  5. 2
      packages/nocodb/src/lib/v1-legacy/nc.try.ts
  6. 6
      packages/nocodb/src/run/docker.ts
  7. 5
      packages/nocodb/src/run/dockerRunMysql.ts
  8. 5
      packages/nocodb/src/run/dockerRunPG.ts
  9. 5
      packages/nocodb/src/run/dockerRunPG_CyQuick.ts
  10. 8
      packages/nocodb/src/run/testDocker.ts
  11. 2
      packages/nocodb/src/run/try.ts

53
packages/nocodb/src/lib/Noco.ts

@ -114,32 +114,6 @@ export default class Noco {
this.router = express.Router();
this.projectRouter = express.Router();
/* prepare config */
Noco.config = this.config = NcConfigFactory.make();
/******************* setup : start *******************/
this.env = '_noco'; //process.env['NODE_ENV'] || this.config.workingEnv || 'dev';
this.config.workingEnv = this.env;
this.config.type = 'docker';
if (!this.config.toolDir) {
this.config.toolDir = process.cwd();
}
// this.ncToolApi = new NcToolGui(this.config);
// if (server) {
// server.set('view engine', 'ejs');
// }
const NcMetaImpl = process.env.EE ? NcMetaImplEE : NcMetaImplCE;
// const NcMetaMgr = process.env.EE ? NcMetaMgrEE : NcMetaMgrCE;
Noco._ncMeta = new NcMetaImpl(this, this.config);
// this.metaMgr = new NcMetaMgr(this, this.config, Noco._ncMeta);
// this.metaMgrv2 = new NcMetaMgrv2(this, this.config, Noco._ncMeta);
/******************* setup : end *******************/
/******************* prints : start *******************/
// this.sumTable = new Table({
// head: ['#DBs', '#Tables',
@ -169,6 +143,32 @@ export default class Noco {
server?: http.Server,
_app?: express.Express
) {
/* prepare config */
Noco.config = this.config = await NcConfigFactory.make();
/******************* setup : start *******************/
this.env = '_noco'; //process.env['NODE_ENV'] || this.config.workingEnv || 'dev';
this.config.workingEnv = this.env;
this.config.type = 'docker';
if (!this.config.toolDir) {
this.config.toolDir = process.cwd();
}
// this.ncToolApi = new NcToolGui(this.config);
// if (server) {
// server.set('view engine', 'ejs');
// }
const NcMetaImpl = process.env.EE ? NcMetaImplEE : NcMetaImplCE;
// const NcMetaMgr = process.env.EE ? NcMetaMgrEE : NcMetaMgrCE;
Noco._ncMeta = new NcMetaImpl(this, this.config);
// this.metaMgr = new NcMetaMgr(this, this.config, Noco._ncMeta);
// this.metaMgrv2 = new NcMetaMgrv2(this, this.config, Noco._ncMeta);
/******************* setup : end *******************/
// @ts-ignore
const {
progressCallback,
@ -278,6 +278,7 @@ export default class Noco {
instance: getInstance,
});
Tele.emit('evt_app_started', await User.count());
console.log(`App started successfully.\nVisit -> ${Noco.dashboardUrl}`);
weAreHiring();
return this.router;
}

2
packages/nocodb/src/lib/db/sql-migrator/lib/KnexMigrator.ts

@ -309,7 +309,7 @@ export default class KnexMigrator extends SqlMigrator {
await this._readProjectJson(projJsonFilePath);
this.emit('Migrator for project initalised successfully');
} else if (NcConfigFactory.hasDbUrl()) {
this.project = NcConfigFactory.make();
this.project = await NcConfigFactory.make();
} else {
args.type = args.type || 'sqlite';

6
packages/nocodb/src/lib/meta/NcMetaMgr.ts

@ -522,7 +522,7 @@ export default class NcMetaMgr {
if (projectConfig?.prefix) {
const metaProjConfig =
NcConfigFactory.makeProjectConfigFromConnection(
await NcConfigFactory.makeProjectConfigFromConnection(
this.config?.meta?.db,
args.args.projectType
);
@ -1605,7 +1605,7 @@ export default class NcMetaMgr {
break;
case 'projectCreateByOneClick':
{
const config = NcConfigFactory.makeProjectConfigFromUrl(
const config = await NcConfigFactory.makeProjectConfigFromUrl(
process.env.NC_DB,
args.args.projectType
);
@ -1638,7 +1638,7 @@ export default class NcMetaMgr {
break;
case 'projectCreateByWebWithXCDB': {
await this.checkIsUserAllowedToCreateProject(req);
const config = NcConfigFactory.makeProjectConfigFromConnection(
const config = await NcConfigFactory.makeProjectConfigFromConnection(
this.config?.meta?.db,
args.args.projectType
);

45
packages/nocodb/src/lib/utils/NcConfigFactory.ts

@ -1,6 +1,7 @@
import fs from 'fs';
import parseDbUrl from 'parse-database-url';
import { URL } from 'url';
import { promisify } from 'util';
import {
AuthConfig,
@ -18,6 +19,8 @@ const {
animals,
} = require('unique-names-generator');
const readFileAsync = promisify(fs.readFile);
const driverClientMapping = {
mysql: 'mysql2',
mariadb: 'mysql2',
@ -89,8 +92,8 @@ const knownQueryParams = [
];
export default class NcConfigFactory implements NcConfig {
public static make(): NcConfig {
this.jdbcToXcUrl();
public static async make(): Promise<NcConfig> {
await this.jdbcToXcUrl();
const ncConfig = new NcConfigFactory();
@ -115,7 +118,7 @@ export default class NcConfigFactory implements NcConfig {
}
if (process.env.NC_DB) {
ncConfig.meta.db = this.metaUrlToDbConfig(process.env.NC_DB);
ncConfig.meta.db = await this.metaUrlToDbConfig(process.env.NC_DB);
} else if (process.env.NC_DB_JSON) {
ncConfig.meta.db = JSON.parse(process.env.NC_DB_JSON);
} else if (process.env.NC_DB_JSON_FILE) {
@ -125,7 +128,7 @@ export default class NcConfigFactory implements NcConfig {
throw new Error(`NC_DB_JSON_FILE not found: ${filePath}`);
}
const fileContent = fs.readFileSync(filePath, { encoding: 'utf8' });
const fileContent = await readFileAsync(filePath, { encoding: 'utf8' });
ncConfig.meta.db = JSON.parse(fileContent);
}
@ -291,7 +294,7 @@ export default class NcConfigFactory implements NcConfig {
.replace(/[ -]/g, '_');
}
static metaUrlToDbConfig(urlString) {
static async metaUrlToDbConfig(urlString) {
const url = new URL(urlString);
let dbConfig;
@ -378,29 +381,29 @@ export default class NcConfigFactory implements NcConfig {
typeof dbConfig?.connection?.ssl === 'object'
) {
if (dbConfig.connection.ssl.caFilePath && !dbConfig.connection.ssl.ca) {
dbConfig.connection.ssl.ca = fs
.readFileSync(dbConfig.connection.ssl.caFilePath)
.toString();
dbConfig.connection.ssl.ca = await readFileAsync(
dbConfig.connection.ssl.caFilePath
).toString();
}
if (dbConfig.connection.ssl.keyFilePath && !dbConfig.connection.ssl.key) {
dbConfig.connection.ssl.key = fs
.readFileSync(dbConfig.connection.ssl.keyFilePath)
.toString();
dbConfig.connection.ssl.key = await readFileAsync(
dbConfig.connection.ssl.keyFilePath
).toString();
}
if (
dbConfig.connection.ssl.certFilePath &&
!dbConfig.connection.ssl.cert
) {
dbConfig.connection.ssl.cert = fs
.readFileSync(dbConfig.connection.ssl.certFilePath)
.toString();
dbConfig.connection.ssl.cert = await readFileAsync(
dbConfig.connection.ssl.certFilePath
).toString();
}
}
return dbConfig;
}
public static makeProjectConfigFromUrl(url, type?: string): NcConfig {
public static async makeProjectConfigFromUrl(url, type?: string): Promise<NcConfig> {
const config = new NcConfigFactory();
const dbConfig = this.urlToDbConfig(url, '', config, type);
// config.envs[process.env.NODE_ENV || 'dev'].db.push(dbConfig);
@ -430,7 +433,7 @@ export default class NcConfigFactory implements NcConfig {
}
if (process.env.NC_DB) {
config.meta.db = this.metaUrlToDbConfig(process.env.NC_DB);
config.meta.db = await this.metaUrlToDbConfig(process.env.NC_DB);
}
if (process.env.NC_TRY) {
@ -482,10 +485,10 @@ export default class NcConfigFactory implements NcConfig {
return config;
}
public static makeProjectConfigFromConnection(
public static async makeProjectConfigFromConnection(
dbConnectionConfig: any,
type?: string
): NcConfig {
): Promise<NcConfig> {
const config = new NcConfigFactory();
let dbConfig = dbConnectionConfig;
@ -545,7 +548,7 @@ export default class NcConfigFactory implements NcConfig {
}
if (process.env.NC_DB) {
config.meta.db = this.metaUrlToDbConfig(process.env.NC_DB);
config.meta.db = await this.metaUrlToDbConfig(process.env.NC_DB);
}
if (process.env.NC_TRY) {
@ -637,9 +640,9 @@ export default class NcConfigFactory implements NcConfig {
this.envs = { _noco: { db: [] } };
}
public static jdbcToXcUrl() {
public static async jdbcToXcUrl() {
if (process.env.NC_DATABASE_URL_FILE || process.env.DATABASE_URL_FILE) {
const database_url = fs.readFileSync(
const database_url = await readFileAsync(
process.env.NC_DATABASE_URL_FILE || process.env.DATABASE_URL_FILE,
'utf-8'
);

2
packages/nocodb/src/lib/v1-legacy/nc.try.ts

@ -19,7 +19,7 @@ export default async function (dbUrl): Promise<void> {
server.use(
await app.init({
async afterMetaMigrationInit(): Promise<void> {
const config = NcConfigFactory.makeProjectConfigFromUrl(dbUrl);
const config = await NcConfigFactory.makeProjectConfigFromUrl(dbUrl);
await app.ncMeta.projectCreate(
'Dvdrental (Sample SQLite Database)',
config,

6
packages/nocodb/src/run/docker.ts

@ -18,8 +18,8 @@ server.set('view engine', 'ejs');
process.env[`DEBUG`] = 'xc*';
(async () => {
const httpServer = server.listen(process.env.PORT || 8080, () => {
console.log(`App started successfully.\nVisit -> ${Noco.dashboardUrl}`);
});
const httpServer = server.listen(process.env.PORT || 8080, async () => {
server.use(await Noco.init({}, httpServer, server));
});
})().catch((e) => console.log(e));

5
packages/nocodb/src/run/dockerRunMysql.ts

@ -28,8 +28,7 @@ process.env[`NC_DB`] = `mysql2://localhost:3306?u=root&p=password&d=${metaDb}`;
// process.env[`DEBUG`] = 'xc*';
(async () => {
const httpServer = server.listen(process.env.PORT || 8080, () => {
console.log(`App started successfully.\nVisit -> ${Noco.dashboardUrl}`);
});
const httpServer = server.listen(process.env.PORT || 8080, async () => {
server.use(await Noco.init({}, httpServer, server));
});
})().catch((e) => console.log(e));

5
packages/nocodb/src/run/dockerRunPG.ts

@ -28,8 +28,7 @@ process.env[`NC_DB`] = `pg://localhost:5432?u=postgres&p=password&d=${metaDb}`;
// process.env[`DEBUG`] = 'xc*';
(async () => {
const httpServer = server.listen(process.env.PORT || 8080, () => {
console.log(`App started successfully.\nVisit -> ${Noco.dashboardUrl}`);
});
const httpServer = server.listen(process.env.PORT || 8080, async () => {
server.use(await Noco.init({}, httpServer, server));
});
})().catch((e) => console.log(e));

5
packages/nocodb/src/run/dockerRunPG_CyQuick.ts

@ -21,8 +21,7 @@ process.env[
//process.env[`DEBUG`] = 'xc*';
(async () => {
const httpServer = server.listen(process.env.PORT || 8080, () => {
console.log(`App started successfully.\nVisit -> ${Noco.dashboardUrl}`);
});
const httpServer = server.listen(process.env.PORT || 8080, async () => {
server.use(await Noco.init({}, httpServer, server));
});
})().catch((e) => console.log(e));

8
packages/nocodb/src/run/testDocker.ts

@ -21,14 +21,9 @@ server.set('view engine', 'ejs');
process.env[`DEBUG`] = 'xc*';
(async () => {
const httpServer = server.listen(process.env.PORT || 8080, () => {
console.log(`App started successfully.\nVisit -> ${Noco.dashboardUrl}`);
});
const httpServer = server.listen(process.env.PORT || 8080, async () => {
server.use(await Noco.init({}, httpServer, server));
// Wait for 0.5 seconds for the server to start
await new Promise((resolve) => setTimeout(resolve, 500));
if (!(await User.getByEmail('user@nocodb.com'))) {
const response = await axios.post(
`http://localhost:${process.env.PORT || 8080}/api/v1/auth/user/signup`,
@ -39,4 +34,5 @@ process.env[`DEBUG`] = 'xc*';
);
console.log(response.data);
}
});
})().catch((e) => console.log(e));

2
packages/nocodb/src/run/try.ts

@ -20,7 +20,7 @@ process.env.NC_DB = url;
await app.init({
async afterMetaMigrationInit(): Promise<void> {
if (!(await app.ncMeta.projectList())?.length) {
const config = NcConfigFactory.makeProjectConfigFromUrl(url);
const config = await NcConfigFactory.makeProjectConfigFromUrl(url);
const project = await app.ncMeta.projectCreate(
config.title,
config,

Loading…
Cancel
Save