Skip to content

Commit

Permalink
chore: remove database folder of gitignore
Browse files Browse the repository at this point in the history
  • Loading branch information
andre-ols committed Dec 31, 2023
1 parent fd68620 commit 40462be
Show file tree
Hide file tree
Showing 16 changed files with 152 additions and 11 deletions.
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ services:
context: .
target: development
dockerfile: Dockerfile
restart: always
volumes:
- './src/:/usr/app/src/'
- './assets/:/usr/app/assets/'
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"typescript": "~5.1"
},
"scripts": {
"dev": "yarn --env-file=.env nodemon",
"dev": "yarn --env-file=.env nodemon --legacy-watch",
"docker:up": "docker compose --env-file .env up -d --build --force-recreate app_dev",
"docker:migrate": "docker compose --env-file .env exec app_dev yarn prisma migrate reset",
"start": "node build/src/main.js",
Expand Down
11 changes: 10 additions & 1 deletion prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ import {
} from '@/modules/beer/application/event/created-beer'
import { FindCategoryHandlerImpl } from '@/modules/beer/application/query/find-category'
import { CreateBeerSqlRepository } from '@/modules/beer/infra/repository/sql/create-beer'
import { mongoService } from '@/modules/core/database/nosql/mongo-service'
import { BeerModel } from '@/modules/core/database/nosql/schema'
import { env } from '@/modules/core/env'
import { eventBus } from '@/modules/core/event-bus'
import { PrismaClient } from '@prisma/client'
import { randomUUID } from 'crypto'
const prisma = new PrismaClient()

console.log('Seeding...')

const eventHandler = new CreatedBeerHandlerImpl()
const eventHandler = new CreatedBeerHandlerImpl(BeerModel)

eventBus.subscribe(CreatedBeerEvent, (event) => eventHandler.execute(event))

Expand Down Expand Up @@ -84,8 +87,14 @@ async function saveBeer() {
}

async function main() {
await mongoService.connect(env.MONGO_URL)
await mongoService.dropCollection('beers')
await saveBeer()
console.log('Seeding finished!')

setTimeout(() => {
process.exit(0)
}, 1000)
}

main()
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import 'module-alias/register'

import { beerRouter } from './modules/beer/router'
import { app } from './modules/core/api/http/express'
import { connect } from './modules/core/db/nosql/connection'
import { mongoService } from './modules/core/database/nosql/mongo-service'
import { env } from './modules/core/env'
import { orderRouter } from './modules/order/router'

connect()
mongoService.connect(env.MONGO_URL)

app.use('/beer', beerRouter)

Expand Down
7 changes: 5 additions & 2 deletions src/modules/beer/application/event/created-beer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { BeerModel } from '@/modules/core/db/nosql/schema'
import { BeerModel } from '@/modules/core/database/nosql/schema'
import { BaseEvent } from '@/modules/core/event-bus'

type Data = {
id: string
name: string
description: string
price: number
imageUrl: string
abv: number
ibu: number
Expand Down Expand Up @@ -33,6 +34,8 @@ export interface CreatedBeerHandler {
}

export class CreatedBeerHandlerImpl implements CreatedBeerHandler {
constructor(private readonly beerModel: typeof BeerModel) {}

count = 0
async execute(event: CreatedBeerEvent): Promise<void> {
const { data } = event
Expand All @@ -41,7 +44,7 @@ export class CreatedBeerHandlerImpl implements CreatedBeerHandler {

console.log('CreatedBeerHandlerImpl', ++this.count)

const beer = new BeerModel(data)
const beer = new this.beerModel(data)

await beer.save()
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/beer/application/query/find-beer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BeerModel } from '@/modules/core/db/nosql/schema'
import { BeerModel } from '@/modules/core/database/nosql/schema'
import { NotFoundError } from '@/modules/core/errors/not-found'

export class FindBeerQuery {
Expand Down
2 changes: 1 addition & 1 deletion src/modules/beer/application/query/list-beer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BeerModel } from '@/modules/core/db/nosql/schema'
import { BeerModel } from '@/modules/core/database/nosql/schema'
import { Pagination } from '../../../core/querying/pagination'

export class ListBeerQuery {
Expand Down
1 change: 1 addition & 0 deletions src/modules/beer/domain/aggregate/beer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export class Beer {
id: this.id,
name: this.name,
description: this.description,
price: this.price,
imageUrl: this.imageUrl,
abv: this.abv.getValue(),
ibu: this.ibu.getValue(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ describe('ListBeerControllerImpl', () => {
page: 1,
limit: 10,
},
body: undefined,
params: undefined,
}

const response = {
Expand Down Expand Up @@ -89,6 +91,8 @@ describe('ListBeerControllerImpl', () => {
query: {
page: -10,
},
body: undefined,
params: undefined,
}

const result = await sut.execute(request)
Expand Down
2 changes: 1 addition & 1 deletion src/modules/beer/router.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Request, Response, Router } from 'express'
import { BeerModel } from '../core/db/nosql/schema'
import { BeerModel } from '../core/database/nosql/schema'
import { FindBeerHandlerImpl } from './application/query/find-beer'
import { ListBeerHandlerImpl } from './application/query/list-beer'
import { FindBeerControllerImpl } from './presentation/controller/find-beer'
Expand Down
Empty file.
28 changes: 28 additions & 0 deletions src/modules/core/database/nosql/mongo-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import mongoose, { Collection } from 'mongoose'

export class MongoService {
client: mongoose.Mongoose

async connect(uri: string): Promise<void> {
this.client = await mongoose.connect(uri, {})
}

async disconnect(): Promise<void> {
await this.client.disconnect()
}

async isConnected() {
const state = this.client.connection.readyState
return state === 1
}

getCollection(name: string): Collection {
return this.client.connection.collection(name)
}

dropCollection(name: string): Promise<void> {
return this.client.connection.dropCollection(name)
}
}

export const mongoService = new MongoService()
17 changes: 17 additions & 0 deletions src/modules/core/database/nosql/protocols/no-sql-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export interface NoSqlService {
connect(): Promise<void>
disconnect(): Promise<void>
isConnected(): Promise<boolean>
getCollection(name: string): Promise<Collection>
}

export interface Collection {
insertOne(data: any): Promise<any>
insertMany(data: any[]): Promise<any>
findOne(data: any): Promise<any>
findMany(data: any): Promise<any>
updateOne(data: any): Promise<any>
updateMany(data: any): Promise<any>
deleteOne(data: any): Promise<any>
deleteMany(data: any): Promise<any>
}
76 changes: 76 additions & 0 deletions src/modules/core/database/nosql/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import mongoose, { Schema } from 'mongoose'

const beerSchema = new Schema({
id: {
type: String,
unique: true,
required: true,
},
name: {
type: String,
required: true,
},
description: {
type: String,
required: true,
},
imageUrl: {
type: String,
required: true,
},
price: {
type: Number,
required: true,
},
abv: {
type: Number,
required: true,
},
ibu: {
type: Number,
required: true,
},
ebc: {
type: Number,
required: true,
},
category: {
type: {
id: {
type: String,
required: true,
},
name: {
type: String,
required: true,
},
createdAt: {
type: Date,
required: true,
},
updatedAt: {
type: Date,
required: true,
},
},
required: true,
},
foodPairing: {
type: [String],
required: true,
},
brewersTips: {
type: String,
required: true,
},
createdAt: {
type: Date,
required: true,
},
updatedAt: {
type: Date,
required: true,
},
})

export const BeerModel = mongoose.model('Beer', beerSchema)
3 changes: 3 additions & 0 deletions src/modules/core/database/sql/prisma-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { PrismaClient } from '@prisma/client'

export const prismaClient = new PrismaClient()
2 changes: 1 addition & 1 deletion src/modules/order/router.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Request, Router } from 'express'
import { FindBeerSqlRepository } from '../beer/infra/repository/sql/find-beer'
import { prismaClient } from '../core/db/sql/prisma-client'
import { prismaClient } from '../core/database/sql/prisma-client'
import { CreateOrderHandlerImpl } from './application/command/create-order'
import { CreateOrderSqlRepository } from './infra/repository/sql/create-order'
import { CreateOrderControllerImpl } from './presentation/controller/create-order'
Expand Down

0 comments on commit 40462be

Please sign in to comment.