Skip to content

Commit cf7e53f

Browse files
committed
feat(database): support MongoDB
1 parent 065231e commit cf7e53f

File tree

125 files changed

+2583
-662
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

125 files changed

+2583
-662
lines changed

.github/workflows/docker-e2e.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@ jobs:
1212

1313
steps:
1414
- uses: actions/checkout@v4
15-
- name: Run e2e tests
16-
run: docker compose -f docker-compose.ci.yaml --env-file env-example -p ci up --build --exit-code-from api
15+
- name: Run e2e tests for NestJS with TypeORM
16+
run: docker compose -f docker-compose.relational.ci.yaml --env-file env-example-relational -p ci-relational up --build --exit-code-from api
17+
- name: Run e2e tests for NestJS with Mongoose
18+
run: docker compose -f docker-compose.document.ci.yaml --env-file env-example-document -p ci-document up --build --exit-code-from api
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
to: src/database/seeds/document/<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>/<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>-seed.module.ts
3+
---
4+
import { Module } from '@nestjs/common';
5+
import { MongooseModule } from '@nestjs/mongoose';
6+
import { <%= name %>Schema, <%= name %>SchemaClass } from 'src/<%= h.inflection.transform(name, ['pluralize', 'underscore', 'dasherize']) %>/infrastructure/persistence/document/entities/<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>.schema';
7+
import { <%= name %>SeedService } from './<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>-seed.service';
8+
9+
@Module({
10+
imports: [
11+
MongooseModule.forFeature([
12+
{
13+
name: <%= name %>SchemaClass.name,
14+
schema: <%= name %>Schema,
15+
},
16+
]),
17+
],
18+
providers: [<%= name %>SeedService],
19+
exports: [<%= name %>SeedService],
20+
})
21+
export class <%= name %>SeedModule {}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
inject: true
3-
to: src/database/seeds/run-seed.ts
3+
to: src/database/seeds/document/run-seed.ts
44
after: \@nestjs\/core
55
---
66
import { <%= name %>SeedService } from './<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>/<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>-seed.service';
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
inject: true
3-
to: src/database/seeds/run-seed.ts
3+
to: src/database/seeds/document/run-seed.ts
44
before: close
55
---
66
await app.get(<%= name %>SeedService).run();
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
inject: true
3-
to: src/database/seeds/seed.module.ts
3+
to: src/database/seeds/document/seed.module.ts
44
before: \@Module
55
---
66
import { <%= name %>SeedModule } from './<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>/<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>-seed.module';
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
inject: true
3-
to: src/database/seeds/seed.module.ts
3+
to: src/database/seeds/document/seed.module.ts
44
after: imports
55
---
66
<%= name %>SeedModule,
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
to: src/database/seeds/document/<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>/<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>-seed.service.ts
3+
---
4+
import { Injectable } from '@nestjs/common';
5+
import { InjectModel } from '@nestjs/mongoose';
6+
import { Model } from 'mongoose';
7+
import { <%= name %>SchemaClass } from 'src/<%= h.inflection.transform(name, ['pluralize', 'underscore', 'dasherize']) %>/infrastructure/persistence/document/entities/<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>.schema';
8+
9+
@Injectable()
10+
export class <%= name %>SeedService {
11+
constructor(
12+
@InjectModel(<%= name %>SchemaClass.name)
13+
private readonly model: Model<<%= name %>SchemaClass>,
14+
) {}
15+
16+
async run() {
17+
const count = await this.model.countDocuments();
18+
19+
if (count === 0) {
20+
const data = new this.model({});
21+
await data.save();
22+
}
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
to: src/database/seeds/relational/<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>/<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>-seed.module.ts
3+
---
4+
import { Module } from '@nestjs/common';
5+
import { TypeOrmModule } from '@nestjs/typeorm';
6+
import { <%= name %>Entity } from 'src/<%= h.inflection.transform(name, ['pluralize', 'underscore', 'dasherize']) %>/infrastructure/persistence/relational/entities/<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>.entity';
7+
import { <%= name %>SeedService } from './<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>-seed.service';
8+
9+
@Module({
10+
imports: [TypeOrmModule.forFeature([<%= name %>Entity])],
11+
providers: [<%= name %>SeedService],
12+
exports: [<%= name %>SeedService],
13+
})
14+
export class <%= name %>SeedModule {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
inject: true
3+
to: src/database/seeds/relational/run-seed.ts
4+
after: \@nestjs\/core
5+
---
6+
import { <%= name %>SeedService } from './<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>/<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>-seed.service';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
inject: true
3+
to: src/database/seeds/relational/run-seed.ts
4+
before: close
5+
---
6+
await app.get(<%= name %>SeedService).run();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
inject: true
3+
to: src/database/seeds/relational/seed.module.ts
4+
before: \@Module
5+
---
6+
import { <%= name %>SeedModule } from './<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>/<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>-seed.module';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
inject: true
3+
to: src/database/seeds/relational/seed.module.ts
4+
after: imports
5+
---
6+
<%= name %>SeedModule,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
to: src/database/seeds/relational/<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>/<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>-seed.service.ts
3+
---
4+
import { Injectable } from '@nestjs/common';
5+
import { InjectRepository } from '@nestjs/typeorm';
6+
import { <%= name %>Entity } from 'src/<%= h.inflection.transform(name, ['pluralize', 'underscore', 'dasherize']) %>/infrastructure/persistence/relational/entities/<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>.entity';
7+
import { Repository } from 'typeorm';
8+
9+
@Injectable()
10+
export class <%= name %>SeedService {
11+
constructor(
12+
@InjectRepository(<%= name %>Entity)
13+
private repository: Repository<<%= name %>Entity>,
14+
) {}
15+
16+
async run() {
17+
const count = await this.repository.count();
18+
19+
if (count === 0) {
20+
await this.repository.save(this.repository.create({}));
21+
}
22+
}
23+
}

.hygen/seeds/create/module.ejs.t

-14
This file was deleted.

.hygen/seeds/create/service.ejs.t

-23
This file was deleted.

Dockerfile

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ COPY . /usr/src/app
1010
RUN cp -a /tmp/app/node_modules /usr/src/app
1111
COPY ./wait-for-it.sh /opt/wait-for-it.sh
1212
RUN chmod +x /opt/wait-for-it.sh
13-
COPY ./startup.dev.sh /opt/startup.dev.sh
14-
RUN chmod +x /opt/startup.dev.sh
13+
COPY ./startup.relational.dev.sh /opt/startup.relational.dev.sh
14+
RUN chmod +x /opt/startup.relational.dev.sh
1515
RUN sed -i 's/\r//g' /opt/wait-for-it.sh
16-
RUN sed -i 's/\r//g' /opt/startup.dev.sh
16+
RUN sed -i 's/\r//g' /opt/startup.relational.dev.sh
1717

1818
WORKDIR /usr/src/app
19-
RUN if [ ! -f .env ]; then cp env-example .env; fi
19+
RUN if [ ! -f .env ]; then cp env-example-relational .env; fi
2020
RUN npm run build
2121

22-
CMD ["/opt/startup.dev.sh"]
22+
CMD ["/opt/startup.relational.dev.sh"]

Procfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
web: npm run start:prod
2-
release: echo '' > .env && npm run migration:run && npm run seed:run
2+
release: echo '' > .env && npm run migration:run && npm run seed:run:relational

README.md

+2-117
Original file line numberDiff line numberDiff line change
@@ -20,139 +20,24 @@ Frontend (React, Next.js): <https://github.com/brocoders/extensive-react-boilerp
2020
## Table of Contents <!-- omit in toc -->
2121

2222
- [Features](#features)
23-
- [Quick run](#quick-run)
24-
- [Comfortable development](#comfortable-development)
25-
- [Links](#links)
26-
- [Automatic update of dependencies](#automatic-update-of-dependencies)
27-
- [Database utils](#database-utils)
28-
- [Tests](#tests)
29-
- [Tests in Docker](#tests-in-docker)
30-
- [Test benchmarking](#test-benchmarking)
3123
- [Contributors](#contributors)
3224

3325
## Features
3426

35-
- [x] Database ([typeorm](https://www.npmjs.com/package/typeorm)).
27+
- [x] Database. Support [TypeORM](https://www.npmjs.com/package/typeorm) and [Mongoose](https://www.npmjs.com/package/mongoose).
3628
- [x] Seeding.
3729
- [x] Config Service ([@nestjs/config](https://www.npmjs.com/package/@nestjs/config)).
3830
- [x] Mailing ([nodemailer](https://www.npmjs.com/package/nodemailer)).
3931
- [x] Sign in and sign up via email.
4032
- [x] Social sign in (Apple, Facebook, Google, Twitter).
4133
- [x] Admin and User roles.
42-
- [x] I18N ([nestjs-i18n](https://www.npmjs.com/package/nestjs-i18n)).
34+
- [x] Internationalization/Translations (I18N) ([nestjs-i18n](https://www.npmjs.com/package/nestjs-i18n)).
4335
- [x] File uploads. Support local and Amazon S3 drivers.
4436
- [x] Swagger.
4537
- [x] E2E and units tests.
4638
- [x] Docker.
4739
- [x] CI (Github Actions).
4840

49-
## Quick run
50-
51-
```bash
52-
git clone --depth 1 https://github.com/brocoders/nestjs-boilerplate.git my-app
53-
cd my-app/
54-
cp env-example .env
55-
docker compose up -d
56-
```
57-
58-
For check status run
59-
60-
```bash
61-
docker compose logs
62-
```
63-
64-
## Comfortable development
65-
66-
```bash
67-
git clone --depth 1 https://github.com/brocoders/nestjs-boilerplate.git my-app
68-
cd my-app/
69-
cp env-example .env
70-
```
71-
72-
Change `DATABASE_HOST=postgres` to `DATABASE_HOST=localhost`
73-
74-
Change `MAIL_HOST=maildev` to `MAIL_HOST=localhost`
75-
76-
Run additional container:
77-
78-
```bash
79-
docker compose up -d postgres adminer maildev
80-
```
81-
82-
```bash
83-
npm install
84-
85-
npm run migration:run
86-
87-
npm run seed:run
88-
89-
npm run start:dev
90-
```
91-
92-
## Links
93-
94-
- Swagger: <http://localhost:3000/docs>
95-
- Adminer (client for DB): <http://localhost:8080>
96-
- Maildev: <http://localhost:1080>
97-
98-
## Automatic update of dependencies
99-
100-
If you want to automatically update dependencies, you can connect [Renovate](https://github.com/marketplace/renovate) for your project.
101-
102-
## Database utils
103-
104-
Generate migration
105-
106-
```bash
107-
npm run migration:generate -- src/database/migrations/CreateNameTable
108-
```
109-
110-
Run migration
111-
112-
```bash
113-
npm run migration:run
114-
```
115-
116-
Revert migration
117-
118-
```bash
119-
npm run migration:revert
120-
```
121-
122-
Drop all tables in database
123-
124-
```bash
125-
npm run schema:drop
126-
```
127-
128-
Run seed
129-
130-
```bash
131-
npm run seed:run
132-
```
133-
134-
## Tests
135-
136-
```bash
137-
# unit tests
138-
npm run test
139-
140-
# e2e tests
141-
npm run test:e2e
142-
```
143-
144-
## Tests in Docker
145-
146-
```bash
147-
docker compose -f docker-compose.ci.yaml --env-file env-example -p ci up --build --exit-code-from api && docker compose -p ci rm -svf
148-
```
149-
150-
## Test benchmarking
151-
152-
```bash
153-
docker run --rm jordi/ab -n 100 -c 100 -T application/json -H "Authorization: Bearer USER_TOKEN" -v 2 http://<server_ip>:3000/api/v1/users
154-
```
155-
15641
## Contributors
15742

15843
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->

docker-compose.document.ci.yaml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
services:
2+
mongo:
3+
image: mongo:7.0.3
4+
restart: always
5+
environment:
6+
MONGO_INITDB_ROOT_USERNAME: ${DATABASE_USERNAME}
7+
MONGO_INITDB_ROOT_PASSWORD: ${DATABASE_PASSWORD}
8+
expose:
9+
- 27017
10+
11+
maildev:
12+
build:
13+
context: .
14+
dockerfile: maildev.Dockerfile
15+
expose:
16+
- 1080
17+
- 1025
18+
19+
# Uncomment to use redis
20+
# redis:
21+
# image: redis:7-alpine
22+
# expose:
23+
# - 6379
24+
25+
api:
26+
build:
27+
context: .
28+
dockerfile: document.e2e.Dockerfile

0 commit comments

Comments
 (0)