Skip to content

Commit

Permalink
feat: add database sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavodalves committed Oct 12, 2023
1 parent f9a0052 commit 371241d
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 5 deletions.
7 changes: 7 additions & 0 deletions payment/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Environment variables declared in this file are automatically made available to Prisma.
# See the documentation for more detail: https://pris.ly/d/prisma-schema#accessing-environment-variables-from-the-schema

# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB.
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings

DATABASE_URL="file:./dev.db"
2 changes: 2 additions & 0 deletions payment/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
"eslint-plugin-n": "^15.0.0",
"eslint-plugin-promise": "^6.0.0",
"jest": "^29.7.0",
"prisma": "^5.4.2",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"typescript": "*"
},
"dependencies": {
"@prisma/client": "^5.4.2",
"amqplib": "^0.10.3",
"cors": "^2.8.5",
"crypto": "^1.0.1",
Expand Down
Binary file added payment/prisma/dev.db
Binary file not shown.
Binary file added payment/prisma/dev.db-journal
Binary file not shown.
7 changes: 7 additions & 0 deletions payment/prisma/migrations/20231012230624_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- CreateTable
CREATE TABLE "transactions" (
"id" TEXT NOT NULL PRIMARY KEY,
"price" REAL NOT NULL,
"status" INTEGER NOT NULL,
"orderId" TEXT NOT NULL
);
3 changes: 3 additions & 0 deletions payment/prisma/migrations/migration_lock.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "sqlite"
20 changes: 20 additions & 0 deletions payment/prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

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

datasource db {
provider = "sqlite"
url = env("DATABASE_URL")
}

model Transaction {
id String @id
price Float
status Int
orderId String
@@map("transactions")
}
18 changes: 14 additions & 4 deletions payment/src/infra/repositories/transaction.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import Transaction from '../../domain/entities/Transaction';
import { PrismaClient } from '@prisma/client';
import { TransactionRepository } from '../../domain/repositories/Transaction';
import Transaction from '../../domain/entities/Transaction';

export default class FakeTransactionRepository implements TransactionRepository {
private readonly data: Transaction[] = [];
export default class PrismaTransactionRepository implements TransactionRepository {
constructor(
private readonly prismaClient: PrismaClient
) {}

async save(transaction: Transaction): Promise<void> {
this.data.push(transaction);
await this.prismaClient.transaction.create({
data: {
id: transaction.id.getValue(),
orderId: transaction.orderId.getValue(),
price: transaction.price,
status: transaction.status
}
});
}
}
6 changes: 5 additions & 1 deletion payment/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import QueueController from './infra/queue/QueueController';
import RabbitMQQueueAdapter from './infra/queue/RabbitMQQueueAdapter';
import FakePaymentGateway from './infra/gateways/payment/fake';
import FakeTransactionRepository from './infra/repositories/transaction';
import { PrismaClient } from '@prisma/client';
import PrismaTransactionRepository from './infra/repositories/transaction';

async function main () {
const queue = new RabbitMQQueueAdapter();
await queue.connect();
const paymentGateway = new FakePaymentGateway();
const transactionRepository = new FakeTransactionRepository();

const prismaClient = new PrismaClient({ log: ['query']});
const transactionRepository = new PrismaTransactionRepository(prismaClient);
const eventManager = new DomainEventManagerAdapter(queue);

const processPayment = new ProcessPayment(
Expand Down
24 changes: 24 additions & 0 deletions payment/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,23 @@
"@nodelib/fs.scandir" "2.1.5"
fastq "^1.6.0"

"@prisma/client@^5.4.2":
version "5.4.2"
resolved "https://registry.yarnpkg.com/@prisma/client/-/client-5.4.2.tgz#786f9c1d8f06d955933004ac638d14da4bf14025"
integrity sha512-2xsPaz4EaMKj1WS9iW6MlPhmbqtBsXAOeVttSePp8vTFTtvzh2hZbDgswwBdSCgPzmmwF+tLB259QzggvCmJqA==
dependencies:
"@prisma/engines-version" "5.4.1-2.ac9d7041ed77bcc8a8dbd2ab6616b39013829574"

"@prisma/engines-version@5.4.1-2.ac9d7041ed77bcc8a8dbd2ab6616b39013829574":
version "5.4.1-2.ac9d7041ed77bcc8a8dbd2ab6616b39013829574"
resolved "https://registry.yarnpkg.com/@prisma/engines-version/-/engines-version-5.4.1-2.ac9d7041ed77bcc8a8dbd2ab6616b39013829574.tgz#ff14f2926890edee47e8f1d08df7b4f392ee34bf"
integrity sha512-wvupDL4AA1vf4TQNANg7kR7y98ITqPsk6aacfBxZKtrJKRIsWjURHkZCGcQliHdqCiW/hGreO6d6ZuSv9MhdAA==

"@prisma/engines@5.4.2":
version "5.4.2"
resolved "https://registry.yarnpkg.com/@prisma/engines/-/engines-5.4.2.tgz#ba2b7faeb227c76e423e88f962afe6a031319f3f"
integrity sha512-fqeucJ3LH0e1eyFdT0zRx+oETLancu5+n4lhiYECyEz6H2RDskPJHJYHkVc0LhkU4Uv7fuEnppKU3nVKNzMh8g==

"@sinclair/typebox@^0.27.8":
version "0.27.8"
resolved "https://registry.yarnpkg.com/@sinclair/typebox/-/typebox-0.27.8.tgz#6667fac16c436b5434a387a34dedb013198f6e6e"
Expand Down Expand Up @@ -3525,6 +3542,13 @@ pretty-format@^29.0.0, pretty-format@^29.7.0:
ansi-styles "^5.0.0"
react-is "^18.0.0"

prisma@^5.4.2:
version "5.4.2"
resolved "https://registry.yarnpkg.com/prisma/-/prisma-5.4.2.tgz#7eac9276439ec7073ec697c6c0dfa259d96e955e"
integrity sha512-GDMZwZy7mysB2oXU+angQqJ90iaPFdD0rHaZNkn+dio5NRkGLmMqmXs31//tg/qXT3iB0cTQwnGGQNuirhSTZg==
dependencies:
"@prisma/engines" "5.4.2"

prompts@^2.0.1:
version "2.4.2"
resolved "https://registry.yarnpkg.com/prompts/-/prompts-2.4.2.tgz#7b57e73b3a48029ad10ebd44f74b01722a4cb069"
Expand Down

0 comments on commit 371241d

Please sign in to comment.