diff --git a/.install-scripts/index.ts b/.install-scripts/index.ts index 23c90f136..857db4f35 100644 --- a/.install-scripts/index.ts +++ b/.install-scripts/index.ts @@ -4,10 +4,22 @@ import removeGoogleAuth from './scripts/remove-auth-google'; import removeAppleAuth from './scripts/remove-auth-apple'; import removeTwitterAuth from './scripts/remove-auth-twitter'; import removeInstallScripts from './scripts/remove-install-scripts'; +import removePostgreSql from './scripts/remove-postgresql'; +import removeMongoDb from './scripts/remove-mongodb'; (async () => { const response = await prompts( [ + { + type: 'select', + name: 'database', + message: 'Which database do you want to use?', + choices: [ + { title: 'PostgreSQL and MongoDB', value: 'pg-mongo' }, + { title: 'PostgreSQL', value: 'pg' }, + { title: 'MongoDB', value: 'mongo' }, + ], + }, { type: 'confirm', name: 'isAuthFacebook', @@ -40,6 +52,14 @@ import removeInstallScripts from './scripts/remove-install-scripts'; }, ); + if (response.database === 'mongo') { + removePostgreSql(); + } + + if (response.database === 'pg') { + removeMongoDb(); + } + if (!response.isAuthFacebook) { removeFacebookAuth(); } diff --git a/.install-scripts/scripts/remove-mongodb.ts b/.install-scripts/scripts/remove-mongodb.ts new file mode 100644 index 000000000..cbe925ceb --- /dev/null +++ b/.install-scripts/scripts/remove-mongodb.ts @@ -0,0 +1,267 @@ +import replace from '../helpers/replace'; +import path from 'path'; +import fs from 'fs'; + +const removePostgreSql = async () => { + const filesToRemove = [ + path.join( + process.cwd(), + 'src', + 'files', + 'infrastructure', + 'persistence', + 'document', + ), + path.join( + process.cwd(), + 'src', + 'session', + 'infrastructure', + 'persistence', + 'document', + ), + path.join( + process.cwd(), + 'src', + 'users', + 'infrastructure', + 'persistence', + 'document', + ), + path.join(process.cwd(), 'src', 'database', 'mongoose-config.service.ts'), + path.join(process.cwd(), 'src', 'database', 'seeds', 'document'), + path.join( + process.cwd(), + 'src', + 'roles', + 'infrastructure', + 'persistence', + 'document', + ), + path.join( + process.cwd(), + 'src', + 'statuses', + 'infrastructure', + 'persistence', + 'document', + ), + path.join(process.cwd(), 'env-example-document'), + path.join(process.cwd(), 'docker-compose.document.ci.yaml'), + path.join(process.cwd(), 'docker-compose.document.yaml'), + path.join(process.cwd(), 'startup.document.ci.sh'), + path.join(process.cwd(), 'startup.document.dev.sh'), + path.join(process.cwd(), 'document.Dockerfile'), + path.join(process.cwd(), 'document.e2e.Dockerfile'), + path.join(process.cwd(), '.hygen', 'seeds', 'create-document'), + path.join(process.cwd(), 'src', 'utils', 'document-entity-helper.ts'), + ]; + + replace({ + path: path.join(process.cwd(), 'src', 'app.module.ts'), + actions: [ + { + find: /\/\/ .*\/\/ <\/database-block>/gs, + replace: `const infrastructureDatabaseModule = TypeOrmModule.forRootAsync({ + useClass: TypeOrmConfigService, + dataSourceFactory: async (options: DataSourceOptions) => { + return new DataSource(options).initialize(); + }, +});`, + }, + { + find: /\s*import \{ MongooseModule \} from .*/g, + replace: '', + }, + { + find: /\s*import \{ MongooseConfigService \} from .*/g, + replace: '', + }, + { + find: /\s*import \{ DatabaseConfig \} from .*/g, + replace: '', + }, + ], + }); + replace({ + path: path.join(process.cwd(), 'src', 'files', 'files.module.ts'), + actions: [ + { + find: /\/\/ .*\/\/ <\/database-block>/gs, + replace: `const infrastructurePersistenceModule = RelationalFilePersistenceModule;`, + }, + { + find: /\s*import \{ DocumentFilePersistenceModule \} from .*/g, + replace: '', + }, + { + find: /\s*import \{ DatabaseConfig \} from .*/g, + replace: '', + }, + { + find: /\s*import databaseConfig from .*/g, + replace: '', + }, + ], + }); + replace({ + path: path.join( + process.cwd(), + 'src', + 'files', + 'infrastructure', + 'uploader', + 'local', + 'files.module.ts', + ), + actions: [ + { + find: /\/\/ .*\/\/ <\/database-block>/gs, + replace: `const infrastructurePersistenceModule = RelationalFilePersistenceModule;`, + }, + { + find: /\s*import \{ DocumentFilePersistenceModule \} from .*/g, + replace: '', + }, + { + find: /\s*import \{ DatabaseConfig \} from .*/g, + replace: '', + }, + { + find: /\s*import databaseConfig from .*/g, + replace: '', + }, + ], + }); + replace({ + path: path.join( + process.cwd(), + 'src', + 'files', + 'infrastructure', + 'uploader', + 's3', + 'files.module.ts', + ), + actions: [ + { + find: /\/\/ .*\/\/ <\/database-block>/gs, + replace: `const infrastructurePersistenceModule = RelationalFilePersistenceModule;`, + }, + { + find: /\s*import \{ DocumentFilePersistenceModule \} from .*/g, + replace: '', + }, + { + find: /\s*import \{ DatabaseConfig \} from .*/g, + replace: '', + }, + { + find: /\s*import databaseConfig from .*/g, + replace: '', + }, + ], + }); + replace({ + path: path.join( + process.cwd(), + 'src', + 'files', + 'infrastructure', + 'uploader', + 's3-presigned', + 'files.module.ts', + ), + actions: [ + { + find: /\/\/ .*\/\/ <\/database-block>/gs, + replace: `const infrastructurePersistenceModule = RelationalFilePersistenceModule;`, + }, + { + find: /\s*import \{ DocumentFilePersistenceModule \} from .*/g, + replace: '', + }, + { + find: /\s*import \{ DatabaseConfig \} from .*/g, + replace: '', + }, + { + find: /\s*import databaseConfig from .*/g, + replace: '', + }, + ], + }); + replace({ + path: path.join(process.cwd(), 'src', 'session', 'session.module.ts'), + actions: [ + { + find: /\/\/ .*\/\/ <\/database-block>/gs, + replace: `const infrastructurePersistenceModule = RelationalSessionPersistenceModule;`, + }, + { + find: /\s*import \{ DocumentSessionPersistenceModule \} from .*/g, + replace: '', + }, + { + find: /\s*import \{ DatabaseConfig \} from .*/g, + replace: '', + }, + { + find: /\s*import databaseConfig from .*/g, + replace: '', + }, + ], + }); + + replace({ + path: path.join(process.cwd(), 'src', 'users', 'users.module.ts'), + actions: [ + { + find: /\/\/ .*\/\/ <\/database-block>/gs, + replace: `const infrastructurePersistenceModule = RelationalUserPersistenceModule;`, + }, + { + find: /\s*import \{ DocumentUserPersistenceModule \} from .*/g, + replace: '', + }, + { + find: /\s*import \{ DatabaseConfig \} from .*/g, + replace: '', + }, + { + find: /\s*import databaseConfig from .*/g, + replace: '', + }, + ], + }); + replace({ + path: path.join(process.cwd(), 'package.json'), + actions: [ + { + find: /\s*\"@nestjs\/mongoose\":.*/g, + replace: '', + }, + { + find: /\s*\"mongoose\":.*/g, + replace: '', + }, + { + find: /\s*\"seed:run:document\":.*/g, + replace: '', + }, + { + find: /\s*\"seed:create:document\":.*/g, + replace: '', + }, + ], + }); + + filesToRemove.map((file) => { + fs.rmSync(file, { + recursive: true, + force: true, + }); + }); +}; + +export default removePostgreSql; diff --git a/.install-scripts/scripts/remove-postgresql.ts b/.install-scripts/scripts/remove-postgresql.ts new file mode 100644 index 000000000..75a41a264 --- /dev/null +++ b/.install-scripts/scripts/remove-postgresql.ts @@ -0,0 +1,297 @@ +import replace from '../helpers/replace'; +import path from 'path'; +import fs from 'fs'; + +const removePostgreSql = async () => { + const filesToRemove = [ + path.join( + process.cwd(), + 'src', + 'files', + 'infrastructure', + 'persistence', + 'relational', + ), + path.join( + process.cwd(), + 'src', + 'session', + 'infrastructure', + 'persistence', + 'relational', + ), + path.join( + process.cwd(), + 'src', + 'users', + 'infrastructure', + 'persistence', + 'relational', + ), + path.join(process.cwd(), 'src', 'database', 'migrations'), + path.join(process.cwd(), 'src', 'database', 'data-source.ts'), + path.join(process.cwd(), 'src', 'database', 'typeorm-config.service.ts'), + path.join(process.cwd(), 'src', 'database', 'seeds', 'relational'), + path.join( + process.cwd(), + 'src', + 'roles', + 'infrastructure', + 'persistence', + 'relational', + ), + path.join( + process.cwd(), + 'src', + 'statuses', + 'infrastructure', + 'persistence', + 'relational', + ), + path.join(process.cwd(), 'env-example-relational'), + path.join(process.cwd(), 'docker-compose.relational.ci.yaml'), + path.join(process.cwd(), 'docker-compose.yaml'), + path.join(process.cwd(), 'startup.relational.ci.sh'), + path.join(process.cwd(), 'startup.relational.dev.sh'), + path.join(process.cwd(), 'Dockerfile'), + path.join(process.cwd(), 'relational.e2e.Dockerfile'), + path.join(process.cwd(), '.hygen', 'seeds', 'create-relational'), + path.join(process.cwd(), 'src', 'utils', 'relational-entity-helper.ts'), + ]; + + replace({ + path: path.join(process.cwd(), 'src', 'app.module.ts'), + actions: [ + { + find: /\/\/ .*\/\/ <\/database-block>/gs, + replace: `const infrastructureDatabaseModule = MongooseModule.forRootAsync({ + useClass: MongooseConfigService, +});`, + }, + { + find: /\s*import \{ TypeOrmModule \} from .*/g, + replace: '', + }, + { + find: /\s*import \{ TypeOrmConfigService \} from .*/g, + replace: '', + }, + { + find: /\s*import \{ DataSource, DataSourceOptions \} from .*/g, + replace: '', + }, + { + find: /\s*import \{ DatabaseConfig \} from .*/g, + replace: '', + }, + ], + }); + replace({ + path: path.join(process.cwd(), 'src', 'files', 'files.module.ts'), + actions: [ + { + find: /\/\/ .*\/\/ <\/database-block>/gs, + replace: `const infrastructurePersistenceModule = DocumentFilePersistenceModule;`, + }, + { + find: /\s*import \{ RelationalFilePersistenceModule \} from .*/g, + replace: '', + }, + { + find: /\s*import \{ DatabaseConfig \} from .*/g, + replace: '', + }, + { + find: /\s*import databaseConfig from .*/g, + replace: '', + }, + ], + }); + replace({ + path: path.join( + process.cwd(), + 'src', + 'files', + 'infrastructure', + 'uploader', + 'local', + 'files.module.ts', + ), + actions: [ + { + find: /\/\/ .*\/\/ <\/database-block>/gs, + replace: `const infrastructurePersistenceModule = DocumentFilePersistenceModule;`, + }, + { + find: /\s*import \{ RelationalFilePersistenceModule \} from .*/g, + replace: '', + }, + { + find: /\s*import \{ DatabaseConfig \} from .*/g, + replace: '', + }, + { + find: /\s*import databaseConfig from .*/g, + replace: '', + }, + ], + }); + replace({ + path: path.join( + process.cwd(), + 'src', + 'files', + 'infrastructure', + 'uploader', + 's3', + 'files.module.ts', + ), + actions: [ + { + find: /\/\/ .*\/\/ <\/database-block>/gs, + replace: `const infrastructurePersistenceModule = DocumentFilePersistenceModule;`, + }, + { + find: /\s*import \{ RelationalFilePersistenceModule \} from .*/g, + replace: '', + }, + { + find: /\s*import \{ DatabaseConfig \} from .*/g, + replace: '', + }, + { + find: /\s*import databaseConfig from .*/g, + replace: '', + }, + ], + }); + replace({ + path: path.join( + process.cwd(), + 'src', + 'files', + 'infrastructure', + 'uploader', + 's3-presigned', + 'files.module.ts', + ), + actions: [ + { + find: /\/\/ .*\/\/ <\/database-block>/gs, + replace: `const infrastructurePersistenceModule = DocumentFilePersistenceModule;`, + }, + { + find: /\s*import \{ RelationalFilePersistenceModule \} from .*/g, + replace: '', + }, + { + find: /\s*import \{ DatabaseConfig \} from .*/g, + replace: '', + }, + { + find: /\s*import databaseConfig from .*/g, + replace: '', + }, + ], + }); + replace({ + path: path.join(process.cwd(), 'src', 'session', 'session.module.ts'), + actions: [ + { + find: /\/\/ .*\/\/ <\/database-block>/gs, + replace: `const infrastructurePersistenceModule = DocumentSessionPersistenceModule;`, + }, + { + find: /\s*import \{ RelationalSessionPersistenceModule \} from .*/g, + replace: '', + }, + { + find: /\s*import \{ DatabaseConfig \} from .*/g, + replace: '', + }, + { + find: /\s*import databaseConfig from .*/g, + replace: '', + }, + ], + }); + replace({ + path: path.join(process.cwd(), 'src', 'users', 'users.module.ts'), + actions: [ + { + find: /\/\/ .*\/\/ <\/database-block>/gs, + replace: `const infrastructurePersistenceModule = DocumentUserPersistenceModule;`, + }, + { + find: /\s*import \{ RelationalUserPersistenceModule \} from .*/g, + replace: '', + }, + { + find: /\s*import \{ DatabaseConfig \} from .*/g, + replace: '', + }, + { + find: /\s*import databaseConfig from .*/g, + replace: '', + }, + ], + }); + replace({ + path: path.join(process.cwd(), 'package.json'), + actions: [ + { + find: /\s*\"@nestjs\/typeorm\":.*/g, + replace: '', + }, + { + find: /,\s*\"typeorm\":.*/g, + replace: '', + }, + { + find: /\s*\"typeorm\":.*/g, + replace: '', + }, + { + find: /\s*\"migration:generate\":.*/g, + replace: '', + }, + { + find: /\s*\"migration:create\":.*/g, + replace: '', + }, + { + find: /\s*\"migration:run\":.*/g, + replace: '', + }, + { + find: /\s*\"migration:revert\":.*/g, + replace: '', + }, + { + find: /\s*\"seed:create:relational\":.*/g, + replace: '', + }, + { + find: /\s*\"seed:run:relational\":.*/g, + replace: '', + }, + { + find: /\s*\"schema:drop\":.*/g, + replace: '', + }, + { + find: /\s*\"pg\":.*/g, + replace: '', + }, + ], + }); + + filesToRemove.map((file) => { + fs.rmSync(file, { + recursive: true, + force: true, + }); + }); +}; + +export default removePostgreSql; diff --git a/src/app.module.ts b/src/app.module.ts index 8372eea57..8a03601a9 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -31,6 +31,20 @@ import { MongooseModule } from '@nestjs/mongoose'; import { MongooseConfigService } from './database/mongoose-config.service'; import { DatabaseConfig } from './database/config/database-config.type'; +// +const infrastructureDatabaseModule = (databaseConfig() as DatabaseConfig) + .isDocumentDatabase + ? MongooseModule.forRootAsync({ + useClass: MongooseConfigService, + }) + : TypeOrmModule.forRootAsync({ + useClass: TypeOrmConfigService, + dataSourceFactory: async (options: DataSourceOptions) => { + return new DataSource(options).initialize(); + }, + }); +// + @Module({ imports: [ ConfigModule.forRoot({ @@ -48,16 +62,7 @@ import { DatabaseConfig } from './database/config/database-config.type'; ], envFilePath: ['.env'], }), - (databaseConfig() as DatabaseConfig).isDocumentDatabase - ? MongooseModule.forRootAsync({ - useClass: MongooseConfigService, - }) - : TypeOrmModule.forRootAsync({ - useClass: TypeOrmConfigService, - dataSourceFactory: async (options: DataSourceOptions) => { - return new DataSource(options).initialize(); - }, - }), + infrastructureDatabaseModule, I18nModule.forRootAsync({ useFactory: (configService: ConfigService) => ({ fallbackLanguage: configService.getOrThrow('app.fallbackLanguage', { diff --git a/src/files/files.module.ts b/src/files/files.module.ts index daab7f64e..16fcbcf06 100644 --- a/src/files/files.module.ts +++ b/src/files/files.module.ts @@ -11,10 +11,12 @@ import { FilesS3PresignedModule } from './infrastructure/uploader/s3-presigned/f import { DatabaseConfig } from '../database/config/database-config.type'; import databaseConfig from '../database/config/database.config'; +// const infrastructurePersistenceModule = (databaseConfig() as DatabaseConfig) .isDocumentDatabase ? DocumentFilePersistenceModule : RelationalFilePersistenceModule; +// const infrastructureUploaderModule = (fileConfig() as FileConfig).driver === FileDriver.LOCAL diff --git a/src/files/infrastructure/uploader/local/files.module.ts b/src/files/infrastructure/uploader/local/files.module.ts index 31f7798b7..c94cdab5e 100644 --- a/src/files/infrastructure/uploader/local/files.module.ts +++ b/src/files/infrastructure/uploader/local/files.module.ts @@ -17,10 +17,12 @@ import { AllConfigType } from '../../../../config/config.type'; import { DatabaseConfig } from '../../../../database/config/database-config.type'; import databaseConfig from '../../../../database/config/database.config'; +// const infrastructurePersistenceModule = (databaseConfig() as DatabaseConfig) .isDocumentDatabase ? DocumentFilePersistenceModule : RelationalFilePersistenceModule; +// @Module({ imports: [ diff --git a/src/files/infrastructure/uploader/s3-presigned/files.module.ts b/src/files/infrastructure/uploader/s3-presigned/files.module.ts index 1d40a224d..a9b003b5a 100644 --- a/src/files/infrastructure/uploader/s3-presigned/files.module.ts +++ b/src/files/infrastructure/uploader/s3-presigned/files.module.ts @@ -18,10 +18,12 @@ import { AllConfigType } from '../../../../config/config.type'; import { DatabaseConfig } from '../../../../database/config/database-config.type'; import databaseConfig from '../../../../database/config/database.config'; +// const infrastructurePersistenceModule = (databaseConfig() as DatabaseConfig) .isDocumentDatabase ? DocumentFilePersistenceModule : RelationalFilePersistenceModule; +// @Module({ imports: [ diff --git a/src/files/infrastructure/uploader/s3/files.module.ts b/src/files/infrastructure/uploader/s3/files.module.ts index c6e4e9315..3afbe0c47 100644 --- a/src/files/infrastructure/uploader/s3/files.module.ts +++ b/src/files/infrastructure/uploader/s3/files.module.ts @@ -18,10 +18,12 @@ import { AllConfigType } from '../../../../config/config.type'; import { DatabaseConfig } from '../../../../database/config/database-config.type'; import databaseConfig from '../../../../database/config/database.config'; +// const infrastructurePersistenceModule = (databaseConfig() as DatabaseConfig) .isDocumentDatabase ? DocumentFilePersistenceModule : RelationalFilePersistenceModule; +// @Module({ imports: [ diff --git a/src/session/session.module.ts b/src/session/session.module.ts index 94b59d465..e379fb37a 100644 --- a/src/session/session.module.ts +++ b/src/session/session.module.ts @@ -6,10 +6,12 @@ import { SessionService } from './session.service'; import { DatabaseConfig } from '../database/config/database-config.type'; import databaseConfig from '../database/config/database.config'; +// const infrastructurePersistenceModule = (databaseConfig() as DatabaseConfig) .isDocumentDatabase ? DocumentSessionPersistenceModule : RelationalSessionPersistenceModule; +// @Module({ imports: [infrastructurePersistenceModule], diff --git a/src/users/infrastructure/persistence/user.repository.ts b/src/users/infrastructure/persistence/user.repository.ts index 10ef8ef40..641eba4ba 100644 --- a/src/users/infrastructure/persistence/user.repository.ts +++ b/src/users/infrastructure/persistence/user.repository.ts @@ -1,4 +1,4 @@ -import { DeepPartial } from 'typeorm'; +import { DeepPartial } from '../../../utils/types/deep-partial.type'; import { EntityCondition } from '../../../utils/types/entity-condition.type'; import { NullableType } from '../../../utils/types/nullable.type'; import { IPaginationOptions } from '../../../utils/types/pagination-options'; diff --git a/src/users/users.module.ts b/src/users/users.module.ts index 7c27f60cd..5fac1841e 100644 --- a/src/users/users.module.ts +++ b/src/users/users.module.ts @@ -9,10 +9,12 @@ import { DatabaseConfig } from '../database/config/database-config.type'; import databaseConfig from '../database/config/database.config'; import { FilesModule } from '../files/files.module'; +// const infrastructurePersistenceModule = (databaseConfig() as DatabaseConfig) .isDocumentDatabase ? DocumentUserPersistenceModule : RelationalUserPersistenceModule; +// @Module({ imports: [infrastructurePersistenceModule, FilesModule], diff --git a/src/users/users.service.ts b/src/users/users.service.ts index ea902de59..b96f4fc06 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -9,13 +9,13 @@ import { FilterUserDto, SortUserDto } from './dto/query-user.dto'; import { UserRepository } from './infrastructure/persistence/user.repository'; import { User } from './domain/user'; import bcrypt from 'bcryptjs'; -import { DeepPartial } from 'typeorm'; import { AuthProvidersEnum } from '../auth/auth-providers.enum'; import { FilesService } from '../files/files.service'; import { RoleEnum } from '../roles/roles.enum'; import { StatusEnum } from '../statuses/statuses.enum'; import { EntityCondition } from '../utils/types/entity-condition.type'; import { IPaginationOptions } from '../utils/types/pagination-options'; +import { DeepPartial } from '../utils/types/deep-partial.type'; @Injectable() export class UsersService {