refactor(nestjs): seed migrations

This commit is contained in:
Ahmed Bouhuolia
2025-04-03 19:57:11 +02:00
parent 8eb23d3a6f
commit 0a2ac4ee56
14 changed files with 33 additions and 17 deletions

View File

@@ -12,6 +12,7 @@ import signupConfirmation from './signup-confirmation';
import signupRestrictions from './signup-restrictions'; import signupRestrictions from './signup-restrictions';
import jwt from './jwt'; import jwt from './jwt';
import mail from './mail'; import mail from './mail';
import loops from './loops';
export const config = [ export const config = [
systemDatabase, systemDatabase,
@@ -27,5 +28,6 @@ export const config = [
signupConfirmation, signupConfirmation,
signupRestrictions, signupRestrictions,
jwt, jwt,
mail mail,
loops
]; ];

View File

@@ -0,0 +1,6 @@
import { registerAs } from '@nestjs/config';
export default registerAs('loops', () => ({
apiKey: process.env.LOOPS_API_KEY,
}));

View File

@@ -9,5 +9,5 @@ export default registerAs('tenantDatabase', () => ({
password: process.env.TENANT_DB_PASSWORD || process.env.DB_PASSWORD, password: process.env.TENANT_DB_PASSWORD || process.env.DB_PASSWORD,
dbNamePrefix: process.env.TENANT_DB_NAME_PERFIX || 'bigcapital_tenant_', dbNamePrefix: process.env.TENANT_DB_NAME_PERFIX || 'bigcapital_tenant_',
migrationsDir: path.join(__dirname, '../../database/migrations'), migrationsDir: path.join(__dirname, '../../database/migrations'),
seedsDir: path.join(__dirname, '../../database/seeds'), seedsDir: path.join(__dirname, '../../database/seeds/core'),
})); }));

View File

@@ -1,5 +1,5 @@
import { TenantSeeder } from '@/libs/migration-seed/TenantSeeder'; import { TenantSeeder } from '@/libs/migration-seed/TenantSeeder';
import AccountsData from '../data/accounts'; import { AccountsData } from '../data/accounts';
export default class SeedAccounts extends TenantSeeder { export default class SeedAccounts extends TenantSeeder {
/** /**
@@ -8,8 +8,8 @@ export default class SeedAccounts extends TenantSeeder {
up(knex) { up(knex) {
const data = AccountsData.map((account) => ({ const data = AccountsData.map((account) => ({
...account, ...account,
name: this.i18n.__(account.name), name: this.i18n.t(account.name),
description: this.i18n(account.description), description: account.description ? this.i18n.t(account.description) : '',
currencyCode: this.tenant.metadata.baseCurrency, currencyCode: this.tenant.metadata.baseCurrency,
seededAt: new Date(), seededAt: new Date(),
})); }));

View File

@@ -25,6 +25,7 @@ export const UnearnedRevenueAccount = {
slug: 'unearned-revenue', slug: 'unearned-revenue',
account_type: 'other-current-liability', account_type: 'other-current-liability',
parent_account_id: null, parent_account_id: null,
description: '',
code: '50005', code: '50005',
active: true, active: true,
index: 1, index: 1,
@@ -36,6 +37,7 @@ export const PrepardExpenses = {
slug: 'prepaid-expenses', slug: 'prepaid-expenses',
account_type: 'other-current-asset', account_type: 'other-current-asset',
parent_account_id: null, parent_account_id: null,
description: '',
code: '100010', code: '100010',
active: true, active: true,
index: 1, index: 1,
@@ -47,6 +49,7 @@ export const StripeClearingAccount = {
slug: 'stripe-clearing', slug: 'stripe-clearing',
account_type: 'other-current-asset', account_type: 'other-current-asset',
parent_account_id: null, parent_account_id: null,
description: '',
code: '100020', code: '100020',
active: true, active: true,
index: 1, index: 1,
@@ -57,6 +60,7 @@ export const DiscountExpenseAccount = {
name: 'Discount', name: 'Discount',
slug: 'discount', slug: 'discount',
account_type: 'other-income', account_type: 'other-income',
description: '',
code: '40008', code: '40008',
active: true, active: true,
index: 1, index: 1,
@@ -67,6 +71,7 @@ export const PurchaseDiscountAccount = {
name: 'Purchase Discount', name: 'Purchase Discount',
slug: 'purchase-discount', slug: 'purchase-discount',
account_type: 'other-expense', account_type: 'other-expense',
description: '',
code: '40009', code: '40009',
active: true, active: true,
index: 1, index: 1,
@@ -77,13 +82,14 @@ export const OtherChargesAccount = {
name: 'Other Charges', name: 'Other Charges',
slug: 'other-charges', slug: 'other-charges',
account_type: 'other-income', account_type: 'other-income',
description: '',
code: '40010', code: '40010',
active: true, active: true,
index: 1, index: 1,
predefined: true, predefined: true,
}; };
export default [ export const AccountsData = [
{ {
name: 'Bank Account', name: 'Bank Account',
slug: 'bank-account', slug: 'bank-account',

View File

@@ -1,6 +1,6 @@
import path from 'path'; import * as path from 'path';
import * as fs from 'fs';
import { sortBy } from 'lodash'; import { sortBy } from 'lodash';
import fs from 'fs';
import { promisify } from 'util'; import { promisify } from 'util';
import { MigrateItem } from './interfaces'; import { MigrateItem } from './interfaces';
import { importWebpackSeedModule } from './Utils'; import { importWebpackSeedModule } from './Utils';
@@ -93,7 +93,7 @@ class FsMigrations {
* @returns {string} * @returns {string}
*/ */
public getMigration(migration: MigrateItem): string { public getMigration(migration: MigrateItem): string {
return importWebpackSeedModule(migration.file); return importWebpackSeedModule(migration.file.replace('.ts', ''));
} }
} }

View File

@@ -1,6 +1,6 @@
// @ts-nocheck // @ts-nocheck
import { differenceWith } from 'lodash'; import { differenceWith } from 'lodash';
import path from 'path'; import * as path from 'path';
import { FsMigrations } from './FsMigrations'; import { FsMigrations } from './FsMigrations';
import { import {
getTable, getTable,

View File

@@ -1,6 +1,6 @@
// @ts-nocheck // @ts-nocheck
import { Knex } from 'knex'; import { Knex } from 'knex';
import Bluebird from 'bluebird'; import * as Bluebird from 'bluebird';
import { getTable, getTableName, getLockTableName } from './TableUtils'; import { getTable, getTableName, getLockTableName } from './TableUtils';
import getMergedConfig from './SeederConfig'; import getMergedConfig from './SeederConfig';
import { import {

View File

@@ -3,7 +3,7 @@ import { DEFAULT_LOAD_EXTENSIONS, FsMigrations } from './FsMigrations';
const CONFIG_DEFAULT = Object.freeze({ const CONFIG_DEFAULT = Object.freeze({
extension: 'js', extension: 'js',
loadExtensions: DEFAULT_LOAD_EXTENSIONS, loadExtensions: DEFAULT_LOAD_EXTENSIONS,
tableName: 'knex_migrations', tableName: 'bigcapital_seeds',
schemaName: null, schemaName: null,
directory: './migrations', directory: './migrations',
disableTransactions: false, disableTransactions: false,

View File

@@ -1,9 +1,10 @@
// @ts-nocheck // @ts-nocheck
import { I18nService } from 'nestjs-i18n';
import { Seeder } from './Seeder'; import { Seeder } from './Seeder';
export class TenantSeeder extends Seeder { export class TenantSeeder extends Seeder {
public knex: any; public knex: any;
public i18n: i18nAPI; public i18n: I18nService;
public models: any; public models: any;
public tenant: any; public tenant: any;

View File

@@ -1,5 +1,5 @@
// @ts-nocheck // @ts-nocheck
import fs from 'fs'; import * as fs from 'fs';
/** /**
* Detarmines the module type of the given file path. * Detarmines the module type of the given file path.
@@ -38,5 +38,5 @@ export async function importFile(filepath: string): any {
* @returns * @returns
*/ */
export async function importWebpackSeedModule(moduleName: string): any { export async function importWebpackSeedModule(moduleName: string): any {
return import(`@/database/seeds/core/${moduleName}`); return import(`../../database/seeds/core/${moduleName}`);
} }

View File

@@ -46,6 +46,7 @@ export class BuildOrganizationService {
await this.tenantsManager.dropDatabaseIfExists(); await this.tenantsManager.dropDatabaseIfExists();
await this.tenantsManager.createDatabase(); await this.tenantsManager.createDatabase();
await this.tenantsManager.migrateTenant(); await this.tenantsManager.migrateTenant();
await this.tenantsManager.seedTenant() await this.tenantsManager.seedTenant()

View File

@@ -104,7 +104,7 @@ export class TenantDBManager {
* @return {Promise<void>} * @return {Promise<void>}
*/ */
public async seed(): Promise<void> { public async seed(): Promise<void> {
await this.systemKnex.migrate.latest({ await this.tenantKnex().migrate.latest({
...tenantSeedConfig(tenant), ...tenantSeedConfig(tenant),
disableMigrationsListValidation: true, disableMigrationsListValidation: true,
}); });

View File

@@ -120,7 +120,7 @@ export class TenantsManagerService {
* @returns * @returns
*/ */
public async getSeedMigrationContext() { public async getSeedMigrationContext() {
const tenant = await this.tenancyContext.getTenant(); const tenant = await this.tenancyContext.getTenant(true);
return { return {
knex: this.tenantKnex(), knex: this.tenantKnex(),