Backend with TypeORM, GraphQL and PostgreSQL
Base NestJS
We use Nestjs/TypeORM
In this project, I've been trying not to use Pure SQL
to make the most of TypeORM.
I've used postgresQL for backend database, The default database taht will be used is named 'postgres' You have to have postgresql Database server before getting started. You can use Docker postgresQL to have server easily
packages: graphql, apollo-server-express and @nestjs/graphql, graphqlUpload ...
We use GraphQL in a Code First approach (our code will create the GraphQL Schemas).
You can see playground for documentation.
I have used apollographql as playground. but if you want to use default playground, you can do like below.
// app.modules.js
GraphQLModule.forRootAsync <
ApolloDriverConfig >
{
...
useFactory: (configService: ConfigService) => ({
...
playground: true,
...
}),
...
};
Some of the GraphQL queries are protected by a NestJS Guard (GraphqlPassportAuthGuard
) and requires you to be authenticated (and some also requires to have the Admin role).
You can solve them with Sending JWT token in Http Header
with the Authorization
.
# Http Header
{
"Authorization": "Bearer TOKEN"
}
- getMe (must be authenticated)
- All the other generated by generator (must be authenticated and must be admin)
MIT
To make most of GraphQL's advantage, We created its own api, such as GetMany or GetOne. We tried to make it as comfortable as possible, but if you find any mistakes or improvements, please point them out or promote them.
You can see detail in declare.module.ts
, processWhere.ts
and custom.input.ts
// query
query($input:GetManyInput) {
getManyPlaces(input:$input){
data{
id
logitude
count
}
}
}
// variables
{
input: {
pagination: {
size: 10,
page: 0, // Started from 0
},
order: { id: 'DESC' },
dataType: 'data', //all or count or data - default: all
where: {
id: 3,
},
},
};
You can see detail of operators in where below or in code.
There is CRUD Generator in NestJS. In this repository, It has its own generator. You can use like below.
$ yarn g
Before you start, make sure you have a recent version of NodeJS environment >=14.0 with NPM 6 or Yarn.
The first thing you will need is to install NestJS CLI.
$ yarn -g @nestjs/cli
And do install the dependencies
$ yarn install # or npm install
for development
$ yarn dev # or npm run dev
for production
$ yarn build # or npm run build
$ yarn start # or npm run start
or run with docker following below
Download docker from Official website
Open terminal and navigate to project directory and run the following command.
# Only for prduction
$ docker-compose --env-file ./.production.env up
If you want to use docker, you have to set DB_HOST in .production.env to be postgres
.
The default set is postgres
You can just create postgresql by below code, sync with .development.env
$ docker run -p 5432:5432 --name postgres -e POSTGRES_PASSWORD=1q2w3e4r -d postgres
- Divide usefactory
- Redis
- ElasticSearch
- Caching
- Graphql Subscription