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 jwt from './jwt';
import mail from './mail';
import loops from './loops';
export const config = [
systemDatabase,
@@ -27,5 +28,6 @@ export const config = [
signupConfirmation,
signupRestrictions,
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,
dbNamePrefix: process.env.TENANT_DB_NAME_PERFIX || 'bigcapital_tenant_',
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 AccountsData from '../data/accounts';
import { AccountsData } from '../data/accounts';
export default class SeedAccounts extends TenantSeeder {
/**
@@ -8,8 +8,8 @@ export default class SeedAccounts extends TenantSeeder {
up(knex) {
const data = AccountsData.map((account) => ({
...account,
name: this.i18n.__(account.name),
description: this.i18n(account.description),
name: this.i18n.t(account.name),
description: account.description ? this.i18n.t(account.description) : '',
currencyCode: this.tenant.metadata.baseCurrency,
seededAt: new Date(),
}));

View File

@@ -25,6 +25,7 @@ export const UnearnedRevenueAccount = {
slug: 'unearned-revenue',
account_type: 'other-current-liability',
parent_account_id: null,
description: '',
code: '50005',
active: true,
index: 1,
@@ -36,6 +37,7 @@ export const PrepardExpenses = {
slug: 'prepaid-expenses',
account_type: 'other-current-asset',
parent_account_id: null,
description: '',
code: '100010',
active: true,
index: 1,
@@ -47,6 +49,7 @@ export const StripeClearingAccount = {
slug: 'stripe-clearing',
account_type: 'other-current-asset',
parent_account_id: null,
description: '',
code: '100020',
active: true,
index: 1,
@@ -57,6 +60,7 @@ export const DiscountExpenseAccount = {
name: 'Discount',
slug: 'discount',
account_type: 'other-income',
description: '',
code: '40008',
active: true,
index: 1,
@@ -67,6 +71,7 @@ export const PurchaseDiscountAccount = {
name: 'Purchase Discount',
slug: 'purchase-discount',
account_type: 'other-expense',
description: '',
code: '40009',
active: true,
index: 1,
@@ -77,13 +82,14 @@ export const OtherChargesAccount = {
name: 'Other Charges',
slug: 'other-charges',
account_type: 'other-income',
description: '',
code: '40010',
active: true,
index: 1,
predefined: true,
};
export default [
export const AccountsData = [
{
name: '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 fs from 'fs';
import { promisify } from 'util';
import { MigrateItem } from './interfaces';
import { importWebpackSeedModule } from './Utils';
@@ -93,7 +93,7 @@ class FsMigrations {
* @returns {string}
*/
public getMigration(migration: MigrateItem): string {
return importWebpackSeedModule(migration.file);
return importWebpackSeedModule(migration.file.replace('.ts', ''));
}
}

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,5 +1,5 @@
// @ts-nocheck
import fs from 'fs';
import * as fs from 'fs';
/**
* Detarmines the module type of the given file path.
@@ -38,5 +38,5 @@ export async function importFile(filepath: string): any {
* @returns
*/
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.createDatabase();
await this.tenantsManager.migrateTenant();
await this.tenantsManager.seedTenant()

View File

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

View File

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