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. 30
      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.router = express.Router();
this.projectRouter = 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 *******************/ /******************* prints : start *******************/
// this.sumTable = new Table({ // this.sumTable = new Table({
// head: ['#DBs', '#Tables', // head: ['#DBs', '#Tables',
@ -169,6 +143,32 @@ export default class Noco {
server?: http.Server, server?: http.Server,
_app?: express.Express _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 // @ts-ignore
const { const {
progressCallback, progressCallback,
@ -278,6 +278,7 @@ export default class Noco {
instance: getInstance, instance: getInstance,
}); });
Tele.emit('evt_app_started', await User.count()); Tele.emit('evt_app_started', await User.count());
console.log(`App started successfully.\nVisit -> ${Noco.dashboardUrl}`);
weAreHiring(); weAreHiring();
return this.router; 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); await this._readProjectJson(projJsonFilePath);
this.emit('Migrator for project initalised successfully'); this.emit('Migrator for project initalised successfully');
} else if (NcConfigFactory.hasDbUrl()) { } else if (NcConfigFactory.hasDbUrl()) {
this.project = NcConfigFactory.make(); this.project = await NcConfigFactory.make();
} else { } else {
args.type = args.type || 'sqlite'; args.type = args.type || 'sqlite';

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

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

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

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

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

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

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

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

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

@ -21,22 +21,18 @@ server.set('view engine', 'ejs');
process.env[`DEBUG`] = 'xc*'; process.env[`DEBUG`] = 'xc*';
(async () => { (async () => {
const httpServer = server.listen(process.env.PORT || 8080, () => { const httpServer = server.listen(process.env.PORT || 8080, async () => {
console.log(`App started successfully.\nVisit -> ${Noco.dashboardUrl}`); server.use(await Noco.init({}, httpServer, server));
});
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'))) { if (!(await User.getByEmail('user@nocodb.com'))) {
const response = await axios.post( const response = await axios.post(
`http://localhost:${process.env.PORT || 8080}/api/v1/auth/user/signup`, `http://localhost:${process.env.PORT || 8080}/api/v1/auth/user/signup`,
{ {
email: 'user@nocodb.com', email: 'user@nocodb.com',
password: 'Password123.', password: 'Password123.',
} }
); );
console.log(response.data); console.log(response.data);
} }
});
})().catch((e) => console.log(e)); })().catch((e) => console.log(e));

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

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

Loading…
Cancel
Save