- Node with NestJS
- React with NextJS (next laboratory)
- Create NestJS project using GitHub Copilot from a Scratch.
- Using GHCS to create a Postgres Container.
- Setting up the Nest Config using .env file.
- Connect to Database.
- Create Seeder file to populate the database.
- Create Update and Delete Methods.
- Create a Better description of the products using Azure Open AI.
- Add Swagger Documentation.
- Create README.md file with Developer Docs of the project.
- Export Chat to Developer Docs.
- Create Unit Testing for Services (Optional Homework 🪙)
Requirements
- VS Code version
- Docker for Desktop
- Node Installed (nvm optional)
- GitHub CLI + GitHub Copilot Extension Enabled
- Insomnia or Postman or any Rest Client Installed.
- GitHub CLI
@workspace /new "create a nestjs 9 application with one entity called products, this entity has 4 attributes, id: string, name: string, description: string, image: string, using typeorm 0.3.20 for persistence and Postgres adapter"
- Select this folder as your workspace to create the project.
- Install the dependencies using
npm install
GitHub Command
ghcs "how to run a postgres using docker in my local"
-
Run the command that ghc suggest run pg locally.
-
Copilot will suggest a command to run a postgres container in your local machine like this:
docker run --rm --name postgres -e POSTGRES_PASSWORD=mysecretpassword -p 5432:5432 -d postgres:latest- Use DBeaver or Datagrip to connect to the server and create a database called
copilot
Go to app.module.ts
TypeOrmModule.forRoot({
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'postgres',
password: 'mysecretpassword',
database: 'copilot',
entities: [__dirname + '/**/*.entity{.ts,.js}'],
synchronize: true,
}),
npm run startSometimes copilot don't create the project with the correct dependencies, so you need to install them manually.
npm install @nestjs/typeorm class-validator- Ask for good practices to the github copilot chat in order to use other approach to don't harcode the env-vars in app.module.ts
could you suggest good practices in order to use other approach to don't harcode the env-vars in app.module.ts using @nestjs/config?
- Copilot will suggest to create a
.envfile in the root of the project.
touch .env- Add the following content to the
.envfile. - Use GH Copilot Chat selecting #file:app.module.ts to generate keys that will be used in the
.envfile.
- Replace the keys in
.envfile with the values that we use to run the docker container.
- Check product service ask to copilot how to add support to uuid for generate the id of the product.
could you suggest how to add support to uuid for generate the id of the product?
- Copilot will suggest installing the
uuidpackage.
npm install uuid- Add the following code to the
product.entity.tsfile.
import { v4 as uuidv4 } from 'uuid';- Add modifications to make it work
Ask in copilot chat how to create a seeder file to populate the database with some products using @faker-js/faker and onModuleInit
- Copilot will suggest installing the
@faker-js/fakerpackage.
npm install @faker-js/faker- Create a
product.seeder.service.tsfile in thesrc/seederfolder.
import { faker } from "@faker-js/faker";
import { Injectable, OnModuleInit } from "@nestjs/common";
import { CreateProductDto } from "./dto/create-product.dto";
import { ProductsService } from "./products.service";
@Injectable()
export class ProductsSeeder implements OnModuleInit {
constructor(private readonly productsService: ProductsService) {}
async onModuleInit() {
await this.seedProducts();
}
async seedProducts() {
for (let i = 0; i < 10; i++) {
const product: CreateProductDto = {
name: faker.commerce.productName(),
description: faker.lorem.sentence(),
image: faker.image.urlLoremFlickr({
category: "products",
}),
};
await this.productsService.create(product);
}
}
}- Lets add a condition to only seed the products if the table is empty using Comment Driven Development.
- Use copilot suggestion and use
Ctrl + Enterso see all the suggestion and apply the changes.
const products = await this.productsService.getProducts();
if (products.length > 0) {
return;
}- Add the seeder to the
products.module.tsfile.
@Module({
imports: [TypeOrmModule.forFeature([Product])],
controllers: [ProductsController],
providers: [ProductsService, ProductsSeeder],
})- Run the application and check if the products are being seeded in your db client.
- Using comment driven development add the login for Update and Create Methods
- Using chat create a UpdateProductDto
create a UpdateProductDto that extends from #file:create-product.dto.ts and add id as a attribute
- Test the new methods using Insomnia or Postman
- We are going to use the translate and refactor characteristics of GitHub Copilot to translate a call made in python to NodeJS.
import requests
headers = {
'Content-Type': 'application/json',
'api-key': 'YOUR_API_KEY',
}
params = {
'api-version': '2024-08-01-preview',
}
json_data = {
'messages': [
{
'role': 'system',
'content': 'You are an ai wizard that helps people create product descriptions.',
},
{
'role': 'user',
'content': 'nike',
}
],
'max_tokens': 800,
'temperature': 0.7,
'frequency_penalty': 0,
'presence_penalty': 0,
'top_p': 0.95,
'stop': None,
}
response = requests.post(
'https://clever-dev-openai.openai.azure.com/openai/deployments/chat4o/chat/completions',
params=params,
headers=headers,
json=json_data,
)Using copilot chat reference the openai-call.md to translate in NestJS code using axios to call the Azure Open AI API.
translate this implementation #file:openai-call.md to node (nestjs) using @nestjs/axios
- Ask copilot how to add swagger documentation to the project.
could you suggest how to add swagger documentation to the project?
- Ask copilot how to create a README.md file with the developer docs of the project.
@workspace "create a README.md file with the developer docs of the project"
- Press
Ctrl + Shift + Pand selectChat: Export Chat - This will generate a json file with all the chat history.
- Try to export the chat in a new VS Code windows pressing
Ctrl + Shift + Pand selectChat: Import Chat - This will generate a new chat with all the history, this will help other developers to understand the decisions made during the development.
- Use
Ctrl + iand tell to copilot to create a gitignore file to nestjs project.
# Dependency directories
node_modules/
# Build output
dist/
# Environment variables
.env
# IDE files
.vscode/
.idea/
# Logs
logs/
# Compiled TypeScript files
*.js
# Nest.js specific files
.nest/
- Ask copilot how to add Jest Support to the project.
could you suggest how to add Jest Support to the project?
- Copilot will suggest installing the
@nestjs/jestpackage.
npm install @nestjs/jest- Follow the instructions in the documentation to add Jest support to the project.
