Skip to content

Instantly share code, notes, and snippets.

View hzburki's full-sized avatar
👋
Hi! Any questions?

Haseeb Burki hzburki

👋
Hi! Any questions?
View GitHub Profile
@hzburki
hzburki / user.controller.ts
Created July 14, 2019 15:16
User controller - NestJS SequelizeJS
import { Controller, Get, Post, Body } from '@nestjs/common';
import { UserService } from './user.service';
import { User } from './user.entity';
@Controller('user')
export class UserController {
constructor(private readonly userService: UserService) {}
@Get()
public async getUsers(): Promise<User[]> {
@hzburki
hzburki / user.service.ts
Created July 14, 2019 15:14
Contains code with database interactions - NestJS SequelizeJS
import { Injectable, Inject } from '@nestjs/common';
import { USER_REPOSITORY } from '../utils/constants';
import { User } from './user.entity';
import { CreateUserDto } from './dto/index';
@Injectable()
export class UserService {
constructor(
@Inject(USER_REPOSITORY) private readonly userRepository: typeof User,
) {}
@hzburki
hzburki / user.module.ts
Created July 14, 2019 15:12
User module - NestJS SequelizeJS
import { Module } from '@nestjs/common';
import { UserService } from './user.service';
import { UserController } from './user.controller';
import { DatabaseModule } from '../database/database.module';
import { userProviders } from './user.providers';
@Module({
imports: [DatabaseModule],
controllers: [UserController],
providers: [UserService, ...userProviders],
@hzburki
hzburki / createUserDto.ts
Created July 14, 2019 15:11
Data transfer object for creating users - NestJS SequelizeJS blog
export class CreateUserDto {
readonly name: string;
readonly age: number;
readonly email: string;
}
@hzburki
hzburki / user.provider.ts
Created July 14, 2019 15:08
Exporting user entity model so it can be injected into NestJS services - NestJS SequelizeJS
import { User } from './user.entity';
import { USER_REPOSITORY } from '../utils/constants';
export const userProviders = [
{
provide: USER_REPOSITORY,
useValue: User,
},
];
@hzburki
hzburki / user.entity.js
Created July 14, 2019 15:06
User entity model - NestJS SequelizeJS Blog
import {
Table,
Column,
Model,
DataType,
CreatedAt,
UpdatedAt,
DeletedAt,
} from 'sequelize-typescript';
import { IDefineOptions } from 'sequelize-typescript/lib/interfaces/IDefineOptions';
@hzburki
hzburki / database.module.ts
Created July 14, 2019 14:59
Database Module for NestJS Sequelize Blog
import { Module } from '@nestjs/common';
import { databaseProviders } from './database.providers';
@Module({
providers: [...databaseProviders],
exports: [...databaseProviders],
})
export class DatabaseModule {}
@hzburki
hzburki / database.providers.ts
Last active July 14, 2019 14:58
For providing database connection details to Sequelize with NestJs
import { Sequelize } from 'sequelize-typescript';
/**
* SEQUELIZE variable is stored in a file named
* 'constants' so it can be easily reused anywhere
* without being subject to human error.
*/
import { SEQUELIZE } from '../utils/constants';
import { User } from '../user/user.entity';
@hzburki
hzburki / dbConn.js
Created April 16, 2019 08:55
Re-usable database connections in serverless blog project
const Sequelize = require('sequelize')
const sequelize = new Sequelize(
process.env.DB_NAME,
process.env.DB_USERNAME,
process.env.DB_PASSWORD,
{
host: process.env.DB_HOST,
port: process.env.DB_PORT,
dialect: process.env.DB_DIALECT,
@hzburki
hzburki / app.js
Last active April 16, 2019 08:36
app.js for Serverless Node Project - Serverless Blog
const express = require('express')
const bodyParser = require('body-parser')
const serverless = require('serverless-http')
const app = express()
/* Body Parser */
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))