Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support MongoDB #1290

Merged
merged 1 commit into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .github/workflows/docker-e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ jobs:

steps:
- uses: actions/checkout@v4
- name: Run e2e tests
run: docker compose -f docker-compose.ci.yaml --env-file env-example -p ci up --build --exit-code-from api
- name: Run e2e tests for NestJS with TypeORM
run: docker compose -f docker-compose.relational.ci.yaml --env-file env-example-relational -p ci-relational up --build --exit-code-from api
- name: Run e2e tests for NestJS with Mongoose
run: docker compose -f docker-compose.document.ci.yaml --env-file env-example-document -p ci-document up --build --exit-code-from api
21 changes: 21 additions & 0 deletions .hygen/seeds/create-document/module.ejs.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
to: src/database/seeds/document/<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>/<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>-seed.module.ts
---
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
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';
import { <%= name %>SeedService } from './<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>-seed.service';

@Module({
imports: [
MongooseModule.forFeature([
{
name: <%= name %>SchemaClass.name,
schema: <%= name %>Schema,
},
]),
],
providers: [<%= name %>SeedService],
exports: [<%= name %>SeedService],
})
export class <%= name %>SeedModule {}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
inject: true
to: src/database/seeds/run-seed.ts
to: src/database/seeds/document/run-seed.ts
after: \@nestjs\/core
---
import { <%= name %>SeedService } from './<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>/<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>-seed.service';
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
inject: true
to: src/database/seeds/run-seed.ts
to: src/database/seeds/document/run-seed.ts
before: close
---
await app.get(<%= name %>SeedService).run();
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
inject: true
to: src/database/seeds/seed.module.ts
to: src/database/seeds/document/seed.module.ts
before: \@Module
---
import { <%= name %>SeedModule } from './<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>/<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>-seed.module';
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
inject: true
to: src/database/seeds/seed.module.ts
to: src/database/seeds/document/seed.module.ts
after: imports
---
<%= name %>SeedModule,
24 changes: 24 additions & 0 deletions .hygen/seeds/create-document/service.ejs.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
to: src/database/seeds/document/<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>/<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>-seed.service.ts
---
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { <%= name %>SchemaClass } from 'src/<%= h.inflection.transform(name, ['pluralize', 'underscore', 'dasherize']) %>/infrastructure/persistence/document/entities/<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>.schema';

@Injectable()
export class <%= name %>SeedService {
constructor(
@InjectModel(<%= name %>SchemaClass.name)
private readonly model: Model<<%= name %>SchemaClass>,
) {}

async run() {
const count = await this.model.countDocuments();

if (count === 0) {
const data = new this.model({});
await data.save();
}
}
}
14 changes: 14 additions & 0 deletions .hygen/seeds/create-relational/module.ejs.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
to: src/database/seeds/relational/<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>/<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>-seed.module.ts
---
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { <%= name %>Entity } from 'src/<%= h.inflection.transform(name, ['pluralize', 'underscore', 'dasherize']) %>/infrastructure/persistence/relational/entities/<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>.entity';
import { <%= name %>SeedService } from './<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>-seed.service';

@Module({
imports: [TypeOrmModule.forFeature([<%= name %>Entity])],
providers: [<%= name %>SeedService],
exports: [<%= name %>SeedService],
})
export class <%= name %>SeedModule {}
6 changes: 6 additions & 0 deletions .hygen/seeds/create-relational/run-seed-import.ejs.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
inject: true
to: src/database/seeds/relational/run-seed.ts
after: \@nestjs\/core
---
import { <%= name %>SeedService } from './<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>/<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>-seed.service';
6 changes: 6 additions & 0 deletions .hygen/seeds/create-relational/run-seed-service.ejs.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
inject: true
to: src/database/seeds/relational/run-seed.ts
before: close
---
await app.get(<%= name %>SeedService).run();
6 changes: 6 additions & 0 deletions .hygen/seeds/create-relational/seed-module-import.ejs.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
inject: true
to: src/database/seeds/relational/seed.module.ts
before: \@Module
---
import { <%= name %>SeedModule } from './<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>/<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>-seed.module';
6 changes: 6 additions & 0 deletions .hygen/seeds/create-relational/seed-module.ejs.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
inject: true
to: src/database/seeds/relational/seed.module.ts
after: imports
---
<%= name %>SeedModule,
23 changes: 23 additions & 0 deletions .hygen/seeds/create-relational/service.ejs.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
to: src/database/seeds/relational/<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>/<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>-seed.service.ts
---
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { <%= name %>Entity } from 'src/<%= h.inflection.transform(name, ['pluralize', 'underscore', 'dasherize']) %>/infrastructure/persistence/relational/entities/<%= h.inflection.transform(name, ['underscore', 'dasherize']) %>.entity';
import { Repository } from 'typeorm';

@Injectable()
export class <%= name %>SeedService {
constructor(
@InjectRepository(<%= name %>Entity)
private repository: Repository<<%= name %>Entity>,
) {}

async run() {
const count = await this.repository.count();

if (count === 0) {
await this.repository.save(this.repository.create({}));
}
}
}
14 changes: 0 additions & 14 deletions .hygen/seeds/create/module.ejs.t

This file was deleted.

23 changes: 0 additions & 23 deletions .hygen/seeds/create/service.ejs.t

This file was deleted.

10 changes: 5 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ COPY . /usr/src/app
RUN cp -a /tmp/app/node_modules /usr/src/app
COPY ./wait-for-it.sh /opt/wait-for-it.sh
RUN chmod +x /opt/wait-for-it.sh
COPY ./startup.dev.sh /opt/startup.dev.sh
RUN chmod +x /opt/startup.dev.sh
COPY ./startup.relational.dev.sh /opt/startup.relational.dev.sh
RUN chmod +x /opt/startup.relational.dev.sh
RUN sed -i 's/\r//g' /opt/wait-for-it.sh
RUN sed -i 's/\r//g' /opt/startup.dev.sh
RUN sed -i 's/\r//g' /opt/startup.relational.dev.sh

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

CMD ["/opt/startup.dev.sh"]
CMD ["/opt/startup.relational.dev.sh"]
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
web: npm run start:prod
release: echo '' > .env && npm run migration:run && npm run seed:run
release: echo '' > .env && npm run migration:run && npm run seed:run:relational
119 changes: 2 additions & 117 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,139 +20,24 @@ Frontend (React, Next.js): <https://github.com/brocoders/extensive-react-boilerp
## Table of Contents <!-- omit in toc -->

- [Features](#features)
- [Quick run](#quick-run)
- [Comfortable development](#comfortable-development)
- [Links](#links)
- [Automatic update of dependencies](#automatic-update-of-dependencies)
- [Database utils](#database-utils)
- [Tests](#tests)
- [Tests in Docker](#tests-in-docker)
- [Test benchmarking](#test-benchmarking)
- [Contributors](#contributors)

## Features

- [x] Database ([typeorm](https://www.npmjs.com/package/typeorm)).
- [x] Database. Support [TypeORM](https://www.npmjs.com/package/typeorm) and [Mongoose](https://www.npmjs.com/package/mongoose).
- [x] Seeding.
- [x] Config Service ([@nestjs/config](https://www.npmjs.com/package/@nestjs/config)).
- [x] Mailing ([nodemailer](https://www.npmjs.com/package/nodemailer)).
- [x] Sign in and sign up via email.
- [x] Social sign in (Apple, Facebook, Google, Twitter).
- [x] Admin and User roles.
- [x] I18N ([nestjs-i18n](https://www.npmjs.com/package/nestjs-i18n)).
- [x] Internationalization/Translations (I18N) ([nestjs-i18n](https://www.npmjs.com/package/nestjs-i18n)).
- [x] File uploads. Support local and Amazon S3 drivers.
- [x] Swagger.
- [x] E2E and units tests.
- [x] Docker.
- [x] CI (Github Actions).

## Quick run

```bash
git clone --depth 1 https://github.com/brocoders/nestjs-boilerplate.git my-app
cd my-app/
cp env-example .env
docker compose up -d
```

For check status run

```bash
docker compose logs
```

## Comfortable development

```bash
git clone --depth 1 https://github.com/brocoders/nestjs-boilerplate.git my-app
cd my-app/
cp env-example .env
```

Change `DATABASE_HOST=postgres` to `DATABASE_HOST=localhost`

Change `MAIL_HOST=maildev` to `MAIL_HOST=localhost`

Run additional container:

```bash
docker compose up -d postgres adminer maildev
```

```bash
npm install

npm run migration:run

npm run seed:run

npm run start:dev
```

## Links

- Swagger: <http://localhost:3000/docs>
- Adminer (client for DB): <http://localhost:8080>
- Maildev: <http://localhost:1080>

## Automatic update of dependencies

If you want to automatically update dependencies, you can connect [Renovate](https://github.com/marketplace/renovate) for your project.

## Database utils

Generate migration

```bash
npm run migration:generate -- src/database/migrations/CreateNameTable
```

Run migration

```bash
npm run migration:run
```

Revert migration

```bash
npm run migration:revert
```

Drop all tables in database

```bash
npm run schema:drop
```

Run seed

```bash
npm run seed:run
```

## Tests

```bash
# unit tests
npm run test

# e2e tests
npm run test:e2e
```

## Tests in Docker

```bash
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
```

## Test benchmarking

```bash
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
```

## Contributors

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
Expand Down
28 changes: 28 additions & 0 deletions docker-compose.document.ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
services:
mongo:
image: mongo:7.0.3
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: ${DATABASE_USERNAME}
MONGO_INITDB_ROOT_PASSWORD: ${DATABASE_PASSWORD}
expose:
- 27017

maildev:
build:
context: .
dockerfile: maildev.Dockerfile
expose:
- 1080
- 1025

# Uncomment to use redis
# redis:
# image: redis:7-alpine
# expose:
# - 6379

api:
build:
context: .
dockerfile: document.e2e.Dockerfile
Loading