Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding postgres type orm support #39

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions node_modules/.package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 43 additions & 2 deletions src/utils/create-backend-project.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import {
} from './filemanager.js';
import { AppModuleContent } from '../../templates/backend/nestjs/base/app-module.js';
import path from 'path';
import {MongodbDatabaseConfig, MongodbSchema} from '../../templates/backend/nestjs/base/databases.js';
import {MongodbDatabaseConfig, MongodbSchema, TypeOrmDatabaseModule, TypeOrmAbstractDocument, TypeOrmEntity} from '../../templates/backend/nestjs/base/databases.js';
import {
NEST_MONGOOSE_PACKAGE,
NestjsPackageJsonTemplate
NestjsPackageJsonTemplate,
NEST_TYPEORM_PACKAGE
} from '../../templates/backend/nestjs/base/nestjs-package-json.js';
import { ENVIRONMENT_TEMPLATE } from '../../templates/backend/nestjs/base/environment.js';
import { EXPRESSJS_SERVER_TEMPLATE } from '../../templates/backend/expressjs/base/server.js';
Expand All @@ -22,6 +23,7 @@ import {
import { ExpressJsPackageJsonTemplate } from '../../templates/backend/expressjs/base/package-json.js';
import { ExpressJsEnvironmentTemplate } from '../../templates/backend/expressjs/base/config.js';
import ora from 'ora';
import { Postgres_Database_Server } from '../../templates/backend/nestjs/base/docker.js';

/**
* loader
Expand Down Expand Up @@ -67,6 +69,9 @@ export async function createBackendProject(
// update app module file content
writeToFile(`${destinationPath}/src/app.module.ts`, AppModuleContent);

// Create a folder for configs
createFolder(`${destinationPath}/src/common/configs`);

// add environment file
writeToFile(
`${destinationPath}/src/common/configs/environment.ts`,
Expand Down Expand Up @@ -117,6 +122,42 @@ export async function createBackendProject(
break;
}
break;
case 'postgres':
switch(orm) {
case 'typeorm':
// Create the databse module itself
createAndUpdateFile(
`${destinationPath}/src/module/v1/database/database.module.ts`,
TypeOrmDatabaseModule
);
// Create an abstract entity provider for the database
createAndUpdateFile(`${destinationPath}/src/module/v1/database/abstract.entity.ts`, TypeOrmAbstractDocument);

// create schema folder
createAndUpdateFile(`${destinationPath}/src/module/v1/user/entities/user.entity.ts`, TypeOrmEntity);

// Create a docker-compose file to spin up database server
createAndUpdateFile(`${destinationPath}/docker-compose.yml`,Postgres_Database_Server);

// add mongoose dependencies
packageJson.dependencies = {
...packageJson.dependencies,
...NEST_TYPEORM_PACKAGE.dependencies
};

// update environment
environmentInterface += `\nDB: {
HOST: string;\nPORT: number;\nUSER: string;\nPASSWORD: string;\nDATABASE: string\n}`;
environmentContent += `\n DB: {
HOST: process.env.DB_HOST, USER: process.env.DB_USER, DATABASE: process.env.DB_NAME, PASSWORD: process.env.DB_PASSWORD, PORT: process.env.DB_PORT}`;

// update app module
appModules += 'DatabaseModule';
appModuleImports +=
'import { DatabaseModule } from "./module/v1/database/database.module";';
break;

}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/utils/prompts.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export async function promptDatabase() {
type: 'list',
name: 'database',
message: 'select a database',
choices: ['MongoDB']
choices: ['MongoDB', 'Postgres']
}
]);

Expand All @@ -84,7 +84,7 @@ export async function promptOrm(database) {
if (database === 'mongodb') {
ormChoices = ['Mongoose'];
} else {
ormChoices = ['Typeorm'];
ormChoices = ['Typeorm', 'Prisma'];
}

const ans = await inquirer.prompt([
Expand Down
52 changes: 52 additions & 0 deletions templates/backend/nestjs/base/databases.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,56 @@ import { ENVIRONMENT } from 'src/common/configs/environment';
imports: [MongooseModule.forRoot(ENVIRONMENT.DB.URL)],
})
export class DatabaseModule {}
`;

export const TypeOrmDatabaseModule = `
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ENVIRONMENT } from 'src/common/configs/environment';

// At your terminal run: docker-compose up to start your database server
@Module({
imports: [
TypeOrmModule.forRoot({
type: 'postgres',
host: ENVIRONMENT.DB.HOST || 'localhost',
port: 5435,
database: ENVIRONMENT.DB.DATABASE || 'startease',
username: ENVIRONMENT.DB.USER || 'root',
password: ENVIRONMENT.DB.PASSWORD || '123',
autoLoadEntities: true,
entities: [__dirname + '/../**/*.entity.{js,ts}'],
synchronize: true, // when production set to false
})
]
})

export class DatabaseModule {}
`;

export const TypeOrmAbstractDocument = `
import { PrimaryGeneratedColumn } from 'typeorm';

export class AbstractEntity<T> {
@PrimaryGeneratedColumn()
id: number;

constructor(entity: Partial<T>) {
Object.assign(this, entity);
}
}
`;
export const TypeOrmEntity = `
import { Entity, Column } from 'typeorm';
import { AbstractEntity } from '../../database/abstract.entity';

@Entity()
export class User extends AbstractEntity<User> {

@Column('text')
email: string;

@Column('text')
password: string;
}
`;
15 changes: 15 additions & 0 deletions templates/backend/nestjs/base/docker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const Postgres_Database_Server = `version: '0.0.1'
services:
startease-db:
image: postgres:15
ports:
- 5435:5432
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: 123
POSTGRES_DB: startease
networks:
- typeorm-pro
networks:
typeorm-pro:
`;
8 changes: 8 additions & 0 deletions templates/backend/nestjs/base/nestjs-package-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,12 @@ export const NEST_MONGOOSE_PACKAGE = {
"@nestjs/mongoose": "^10.0.1",
"mongoose": "^7.5.2",
}
}

export const NEST_TYPEORM_PACKAGE = {
"dependencies": {
"typeorm": "^0.3.17",
"@nestjs/typeorm": "^10.0.0",
"pg": "^8.11.3",
}
}