Skip to content

Commit

Permalink
Merge pull request #1 from levivannoort/amplication
Browse files Browse the repository at this point in the history
chore: add amplication generated service
  • Loading branch information
levivannoort authored Dec 4, 2023
2 parents 38147de + 7e3f4ca commit f182bce
Show file tree
Hide file tree
Showing 107 changed files with 4,899 additions and 0 deletions.
8 changes: 8 additions & 0 deletions server/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.dockerignore
docker-compose.yml
Dockerfile
dist/
node_modules
.env
.gitignore
.prettierignore
8 changes: 8 additions & 0 deletions server/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
BCRYPT_SALT=10
COMPOSE_PROJECT_NAME=amp_clpquu1do0r59ie01scpeycqg
PORT=3000
DB_URL=postgres://admin:admin@localhost:5432/my-db
DB_USER=admin
DB_PASSWORD=admin
DB_PORT=5432
DB_NAME=my-db
5 changes: 5 additions & 0 deletions server/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

/node_modules
/dist
.DS_Store
5 changes: 5 additions & 0 deletions server/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules/
dist/
prisma/migrations/
package-lock.json
coverage/
68 changes: 68 additions & 0 deletions server/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# multi-stage: base (build)
FROM node:18.13.0 AS base

# create directory where the application will be built
WORKDIR /app

# copy over the dependency manifests, both the package.json
# and the package-lock.json are copied over
COPY package*.json ./

# installs packages and their dependencies
RUN npm install

# copy over the prisma schema
COPY prisma/schema.prisma ./prisma/

# generate the prisma client based on the schema
RUN npm run prisma:generate

# copy over the code base
COPY . .

# create the bundle of the application
RUN npm run build

# multi-stage: production (runtime)
FROM node:18.13.0-slim AS production

# create arguments of builds time variables
ARG user=amplication
ARG group=${user}
ARG uid=1001
ARG gid=$uid

# [temporary] work around to be able to run prisma
RUN apt-get update -y && apt-get install -y openssl

# create directory where the application will be executed from
WORKDIR /app

# add the user and group
RUN groupadd --gid ${gid} ${user}
RUN useradd --uid ${uid} --gid ${gid} -m ${user}

# copy over the bundled code from the build stage
COPY --from=base /app/node_modules/ ./node_modules
COPY --from=base /app/package.json ./package.json
COPY --from=base /app/dist ./dist
COPY --from=base /app/prisma ./prisma
COPY --from=base /app/scripts ./scripts
COPY --from=base /app/src ./src
COPY --from=base /app/tsconfig* ./

# change ownership of the workspace directory
RUN chown -R ${uid}:${gid} /app/

# get rid of the development dependencies
RUN npm install --production

# set user to the created non-privileged user
USER ${user}

# expose a specific port on the docker container
ENV PORT=3000
EXPOSE ${PORT}

# start the server using the previously build application
CMD [ "node", "./dist/main.js" ]
64 changes: 64 additions & 0 deletions server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<p align="right">
<a href="https://amplication.com" target="_blank">
<img alt="amplication-logo" height="70" alt="Amplication Logo" src="https://amplication.com/images/amplication-logo-purple.svg"/>
</a>
</p>

# Introduction

This service was generated with Amplication. The server-side of the generated project. This component provides the different backend services - i.e., REST API, GraphQL API, authentication, authorization, logging, data validation and the connection to the database. Additional information about the server component and the architecture around it, can be found on the [documentation](https://docs.amplication.com/guides/getting-started) site.

# Getting started

## Step 1: Configuration

Configuration for the server component can be provided through the use of environment variables. These can be passed to the application via the use of the `.env` file in the base directory of the generated service. Below a table can be found which show the different variables that can be passed - these are the variables which exist by default, through the use of plugins additional integrations could require additional values. These values are provided default values after generation, change them to the desired values.

| Variable | Description | Value |
| -------------------- | -------------------------------------------- | ------------------------------------------------------------------- |
| BCRYPT_SALT | the string used for hashing | [random-string] |
| COMPOSE_PROJECT_NAME | the identifier of the service plus prefix | amp_[service-identifier] |
| PORT | the port on which to run the server | 3000 |
| DB_URL | the connection url for the database | [db-provider]://[username]:[password]@localhost:[db-port]/[db-name] |
| DB_PORT | the port used by the database instance | [db-provider-port] |
| DB_USER | the username used to connect to the database | [username] |
| DB_PASSWORD | the password used to connect to the database | [password] |
| DB_NAME | the name of the database | [service-name] / [project-name] |
| JWT_SECRET_KEY | the secret used to sign the json-web token | [secret] |
| JWT_EXPIRATION | the expiration time for the json-web token | 2d |

> **Note**
> Amplication generates default values and stores them under the .env file. It is advised to use some form of secrets manager/vault solution when using in production.
## Step 2.1: Scripts - pre-requisites

After configuration of the server the next step would be to run the application. Before running the server side of the component, make sure that the different pre-requisites are met - i.e., node.js [^16.x], npm, docker. After the setup of the pre-requisites the server component can be started.

```sh
# installation of the dependencies
$ npm install

# generate the prisma client
$ npm run prisma:generate
```

## Step 2.2: Scripts - local development

```sh
# start the database where the server component will connect to
$ npm run docker:dev

# initialize the database
$ npm run db:init

# start the server component
$ npm run start
```
By default, your app comes with one user with the username "admin" and password "admin".

## Step 2.2: Scripts - container based development

```shell
# start the server component as a docker container
$ npm run compose:up
```
13 changes: 13 additions & 0 deletions server/docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: "3"
services:
db:
image: postgres:12
ports:
- ${DB_PORT}:5432
environment:
POSTGRES_USER: ${DB_USER}
POSTGRES_PASSWORD: ${DB_PASSWORD}
volumes:
- postgres:/var/lib/postgresql/data
volumes:
postgres: ~
47 changes: 47 additions & 0 deletions server/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
version: "3"
services:
server:
build:
context: .
args:
NPM_LOG_LEVEL: notice
ports:
- ${PORT}:3000
environment:
BCRYPT_SALT: ${BCRYPT_SALT}
DB_URL: postgres://${DB_USER}:${DB_PASSWORD}@db:5432/${DB_NAME}
depends_on:
- migrate
restart: on-failure
migrate:
build:
context: .
args:
NPM_LOG_LEVEL: notice
command: npm run db:init
working_dir: /app/server
environment:
BCRYPT_SALT: ${BCRYPT_SALT}
DB_URL: postgres://${DB_USER}:${DB_PASSWORD}@db:5432/${DB_NAME}
depends_on:
db:
condition: service_healthy
db:
image: postgres:12
ports:
- ${DB_PORT}:5432
environment:
POSTGRES_USER: ${DB_USER}
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_DB: ${DB_NAME}
volumes:
- postgres:/var/lib/postgresql/data
healthcheck:
test:
- CMD-SHELL
- pg_isready -d $${POSTGRES_DB} -U $${POSTGRES_USER}
timeout: 45s
interval: 10s
retries: 10
volumes:
postgres: ~
6 changes: 6 additions & 0 deletions server/nest-cli.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"sourceRoot": "src",
"compilerOptions": {
"assets": ["swagger"]
}
}
73 changes: 73 additions & 0 deletions server/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"name": "@bookstore/server",
"private": true,
"scripts": {
"start": "nest start",
"start:watch": "nest start --watch",
"start:debug": "nest start --debug --watch",
"build": "nest build",
"test": "jest",
"seed": "ts-node scripts/seed.ts",
"db:migrate-save": "prisma migrate dev",
"db:migrate-up": "prisma migrate deploy",
"db:clean": "prisma migrate reset",
"db:init": "run-s \"db:migrate-save -- --name 'initial version'\" db:migrate-up seed",
"prisma:generate": "prisma generate",
"docker:dev": "docker-compose -f docker-compose.dev.yml up -d",
"package:container": "docker build .",
"compose:up": "docker-compose up -d",
"compose:down": "docker-compose down --volumes"
},
"dependencies": {
"@apollo/server": "^4.9.4",
"@nestjs/apollo": "12.0.9",
"@nestjs/common": "10.2.7",
"@nestjs/config": "3.1.1",
"@nestjs/core": "10.2.7",
"@nestjs/graphql": "12.0.9",
"@nestjs/jwt": "^10.1.1",
"@nestjs/passport": "^10.0.2",
"@nestjs/platform-express": "10.2.7",
"@nestjs/serve-static": "4.0.0",
"@nestjs/swagger": "7.1.13",
"@prisma/client": "^5.4.2",
"@types/bcrypt": "5.0.0",
"bcrypt": "5.1.1",
"class-transformer": "0.5.1",
"class-validator": "0.14.0",
"dotenv": "16.3.1",
"graphql": "^16.8.1",
"graphql-type-json": "0.3.2",
"npm-run-all": "4.1.5",
"passport": "0.6.0",
"passport-http": "0.3.0",
"passport-jwt": "4.0.1",
"reflect-metadata": "0.1.13",
"ts-node": "10.9.1",
"type-fest": "2.19.0",
"validator": "13.11.0"
},
"devDependencies": {
"@nestjs/cli": "^10.1.18",
"@nestjs/testing": "^10.2.7",
"@types/express": "^4.17.19",
"@types/graphql-type-json": "0.3.3",
"@types/jest": "^29.5.5",
"@types/normalize-path": "3.0.0",
"@types/passport-http": "0.3.9",
"@types/passport-jwt": "3.0.10",
"@types/supertest": "^2.0.14",
"@types/validator": "^13.11.2",
"jest": "^29.7.0",
"jest-mock-extended": "^3.0.5",
"prisma": "^5.4.2",
"supertest": "^6.3.3",
"ts-jest": "^29.1.1",
"typescript": "^5.2.2"
},
"jest": {
"preset": "ts-jest",
"testEnvironment": "node",
"modulePathIgnorePatterns": ["<rootDir>/dist/"]
}
}
23 changes: 23 additions & 0 deletions server/prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
datasource db {
provider = "postgresql"
url = env("DB_URL")
}

generator client {
provider = "prisma-client-js"
}

model Author {
books Book[]
id Int @id @default(autoincrement())
name String
}

model Book {
author Author @relation(fields: [authorId], references: [id])
authorId Int
genre String
id Int @id @default(autoincrement())
published DateTime
title String
}
7 changes: 7 additions & 0 deletions server/scripts/customSeed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { PrismaClient } from "@prisma/client";

export async function customSeed() {
const client = new PrismaClient();

client.$disconnect();
}
25 changes: 25 additions & 0 deletions server/scripts/seed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as dotenv from "dotenv";
import { PrismaClient } from "@prisma/client";
import { customSeed } from "./customSeed";

if (require.main === module) {
dotenv.config();

const { BCRYPT_SALT } = process.env;

if (!BCRYPT_SALT) {
throw new Error("BCRYPT_SALT environment variable must be defined");
}
}

async function seed() {
console.info("Seeding database...");

const client = new PrismaClient();
void client.$disconnect();

console.info("Seeding database with custom seed...");
customSeed();

console.info("Seeded database successfully");
}
Loading

0 comments on commit f182bce

Please sign in to comment.