Browse Source

feat: migration init

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/5444/head
Pranav C 2 years ago
parent
commit
37b8b7b2c1
  1. BIN
      packages/nocodb-nest/noco.db
  2. 4046
      packages/nocodb-nest/package-lock.json
  3. 7
      packages/nocodb-nest/package.json
  4. 2
      packages/nocodb-nest/src/auth/auth.service.ts
  5. 29
      packages/nocodb-nest/src/connection/connection.ts
  6. 2
      packages/nocodb-nest/src/db/util/emit.ts
  7. 22
      packages/nocodb-nest/src/meta/meta.service.ts
  8. 6
      packages/nocodb-nest/src/users/users.service.ts

BIN
packages/nocodb-nest/noco.db

Binary file not shown.

4046
packages/nocodb-nest/package-lock.json generated

File diff suppressed because it is too large Load Diff

7
packages/nocodb-nest/package.json

@ -26,10 +26,15 @@
"@nestjs/mapped-types": "*", "@nestjs/mapped-types": "*",
"@nestjs/platform-express": "^9.0.0", "@nestjs/platform-express": "^9.0.0",
"bcryptjs": "^2.4.3", "bcryptjs": "^2.4.3",
"boxen": "^5.0.0",
"colors": "^1.4.0",
"cors": "^2.8.5", "cors": "^2.8.5",
"dayjs": "^1.11.7", "dayjs": "^1.11.7",
"knex": "^2.4.2", "emittery": "^0.7.1",
"knex": "2.2.0",
"mysql2": "^3.2.0",
"nanoid": "^3.1.20", "nanoid": "^3.1.20",
"nc-help": "^0.2.87",
"nocodb-sdk": "^0.106.0-beta.0", "nocodb-sdk": "^0.106.0-beta.0",
"parse-database-url": "^0.3.0", "parse-database-url": "^0.3.0",
"passport-jwt": "^4.0.1", "passport-jwt": "^4.0.1",

2
packages/nocodb-nest/src/auth/auth.service.ts

@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common';
import { OrgUserRoles } from 'nocodb-sdk' import { OrgUserRoles } from 'nocodb-sdk'
import { UsersService } from '../users/users.service'; import { UsersService } from '../users/users.service';
import { promisify } from 'util'; import { promisify } from 'util';
import bcrypt from 'bcryptjs'; import * as bcrypt from 'bcryptjs';
import { JwtService } from '@nestjs/jwt'; import { JwtService } from '@nestjs/jwt';
import { CreateUserDto } from './auth.controller'; import { CreateUserDto } from './auth.controller';

29
packages/nocodb-nest/src/connection/connection.ts

@ -1,26 +1,21 @@
import { Global, Injectable } from '@nestjs/common' import { Global, Injectable, OnModuleInit } from '@nestjs/common';
import * as knex from "knex"; import * as knex from 'knex';
import NcConfigFactory from '../utils/NcConfigFactory';
@Global() @Global()
@Injectable() @Injectable()
export class Connection { export class Connection implements OnModuleInit {
private readonly knex: knex.Knex; private knex: knex.Knex;
private dbConfig: any;
constructor() {
this.knex = knex.default({
client: "mysql2",
connection: {
host: "localhost",
user: "root",
password: "password",
database: "sakila"
}
});
}
get knexInstance(): knex.Knex { get knexInstance(): knex.Knex {
return this.knex; return this.knex;
} }
// init metadb connection
async onModuleInit(): Promise<void> {
this.dbConfig = await NcConfigFactory.make();
this.knex = knex.default({ ...this.dbConfig.meta.db, useNullAsDefault: true });
}
} }

2
packages/nocodb-nest/src/db/util/emit.ts

@ -1,4 +1,4 @@
import Emittery from 'emittery'; import * as Emittery from 'emittery';
let emitSingleton = null; let emitSingleton = null;

22
packages/nocodb-nest/src/meta/meta.service.ts

@ -1,6 +1,6 @@
import { Global, Injectable } from '@nestjs/common' import { Global, Inject, Injectable, OnApplicationBootstrap, OnModuleInit } from '@nestjs/common'
import XcMigrationSource from './migrations/XcMigrationSource' import XcMigrationSource from './migrations/XcMigrationSource';
import XcMigrationSourcev2 from './migrations/XcMigrationSourcev2' import XcMigrationSourcev2 from './migrations/XcMigrationSourcev2';
import { Connection } from '../connection/connection'; import { Connection } from '../connection/connection';
import { customAlphabet } from 'nanoid'; import { customAlphabet } from 'nanoid';
@ -51,7 +51,6 @@ export enum MetaTable {
STORE = 'nc_store', STORE = 'nc_store',
} }
export const orderedMetaTables = [ export const orderedMetaTables = [
MetaTable.MODEL_ROLE_VISIBILITY, MetaTable.MODEL_ROLE_VISIBILITY,
MetaTable.PLUGIN, MetaTable.PLUGIN,
@ -171,13 +170,11 @@ const nanoidv2 = customAlphabet('1234567890abcdefghijklmnopqrstuvwxyz', 14);
@Global() @Global()
@Injectable() @Injectable()
export class MetaService { export class MetaService implements OnApplicationBootstrap {
constructor(private connection: Connection) {}
constructor(private connection: Connection) {
}
public async metaInit(): Promise<boolean> { public async metaInit(): Promise<boolean> {
await this.connection.knexInstance.migrate.latest({ await this.connection.knexInstance.migrate.latest({
migrationSource: new XcMigrationSource(), migrationSource: new XcMigrationSource(),
tableName: 'xc_knex_migrations', tableName: 'xc_knex_migrations',
@ -238,7 +235,7 @@ export class MetaService {
base_id: string, base_id: string,
target: string, target: string,
data: any, data: any,
ignoreIdGeneration?: boolean ignoreIdGeneration?: boolean,
): Promise<any> { ): Promise<any> {
const id = data?.id || this.genNanoid(target); const id = data?.id || this.genNanoid(target);
const insertObj = { const insertObj = {
@ -254,7 +251,6 @@ export class MetaService {
return insertObj; return insertObj;
} }
private genNanoid(target: string) { private genNanoid(target: string) {
let prefix; let prefix;
switch (target) { switch (target) {
@ -347,4 +343,8 @@ export class MetaService {
return `${prefix}${nanoidv2()}`; return `${prefix}${nanoidv2()}`;
} }
async onApplicationBootstrap(): Promise<void> {
await this.metaInit();
}
} }

6
packages/nocodb-nest/src/users/users.service.ts

@ -1,5 +1,5 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { MetaService } from '../meta/meta.service'; import { MetaService, MetaTable } from '../meta/meta.service'
@Injectable() @Injectable()
export class UsersService { export class UsersService {
@ -8,7 +8,7 @@ export class UsersService {
} }
async findOne(email: string) { async findOne(email: string) {
const user = await this.metaService.metaGet(null, null, 'users', { email }); const user = await this.metaService.metaGet(null, null, MetaTable.USERS, { email });
return user; return user;
@ -16,6 +16,6 @@ export class UsersService {
} }
async insert(param: { token_version: string; firstname: any; password: any; salt: any; email_verification_token: any; roles: string; email: string; lastname: any }) { async insert(param: { token_version: string; firstname: any; password: any; salt: any; email_verification_token: any; roles: string; email: string; lastname: any }) {
return this.metaService.metaInsert2(null, null, 'users', param) return this.metaService.metaInsert2(null, null, MetaTable.USERS, param)
} }
} }

Loading…
Cancel
Save