feat: MongoDB & Prisma [serverless] (#224)

* feat: init prisma.

* feat: update the prisma model.

* feat: write client tests.

* feat: Prisma integration and helper [serverless] (#229)

* refactor: rename the prisma folder to prisma-setup.

* feat: install prisma client.

* feat: add new build and invoke commands for prisma and the modules.

* feat: add prisma helper, update serverless config for testing purposes

* docs: update readme and include a .env.example file.

* feat: zoruka's suggestion on using finally instead of catch.

Co-authored-by: Felipe Mendes <zo.fmendes@gmail.com>

---------

Co-authored-by: Felipe Mendes <zo.fmendes@gmail.com>

* feat: prisma-setup restructure.

---------

Co-authored-by: Felipe Mendes <zo.fmendes@gmail.com>
This commit is contained in:
Shredder 2023-04-27 15:01:21 +03:30 committed by GitHub
parent 8f1263e43d
commit d838f1a449
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 123 additions and 3 deletions

7
serverless/.env.example Normal file
View File

@ -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="mongodb+srv://root:randompassword@cluster0.ab1cd.mongodb.net/mydb?retryWrites=true&w=majority"

View File

@ -36,3 +36,15 @@ To deploy to development environment:
To deploy to production environment:
`yarn sls deploy --stage prd`
### Prisma configuration
In order to use and integrate Prisma, both of the `prisma` and `@prisma/client` packages are needed. The `prisma` package reads the schema and generates a version of Prisma Client that is tailored to our modules.
Run the following commands to install the packages and generate the customized Prisma Client version based on the schema:
```
yarn add prisma @prisma/client
yarn prisma:generate
```

View File

@ -0,0 +1,32 @@
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
async function main() {
// Connect the client
await prisma.$connect();
// Query the total count of tokens
const allTokens = await prisma.tokens.findMany();
console.log('Total tokens:');
console.log(allTokens.length);
// Query the token associated with tokenId 1
const tokenOne = await prisma.tokens.findRaw({
filter: {
tokenId: 1,
},
});
console.log('Token Id One:');
console.log(tokenOne);
}
main()
.then(async () => {
await prisma.$disconnect();
})
.catch(async (e) => {
console.error(e);
await prisma.$disconnect();
process.exit(1);
});

View File

@ -5,7 +5,9 @@
"main": "index.js",
"scripts": {
"build": "yarn tsc",
"test": "echo \"Error: no test specified\" && exit 1"
"invoke:build": "yarn build && serverless invoke local --function submitBuildInfo",
"prisma:generate": "npx prisma generate",
"prisma:pull": "npx prisma db pull"
},
"author": "fleek",
"license": "MIT",
@ -27,8 +29,13 @@
"@middy/core": "^4.2.7",
"@middy/http-json-body-parser": "^4.2.7",
"@middy/http-response-serializer": "^4.2.8",
"@prisma/client": "^4.13.0",
"aws-sdk": "^2.1342.0",
"prisma": "^4.13.0",
"uuid": "^9.0.0",
"@types/node": "^18.15.11",
"ts-node": "^10.9.1",
"typescript": "^5.0.4"
"web3": "^1.9.0"
}
}

View File

@ -0,0 +1,16 @@
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}
model tokens {
id String @id @default(auto()) @map("_id") @db.ObjectId
commit_hash String
github_url String
owner String
tokenId Int
}

View File

@ -31,7 +31,7 @@ custom:
functions:
submitBuildInfo:
handler: src/functions/builds/handler.submitBuildInfo
handler: src/functions/builds/handler.submitBuildInfo # Change `src` to `dist` for deployment
events:
- http:
path: build
@ -49,7 +49,7 @@ functions:
tokenId: true
submitMintInfo:
handler: dist/functions/mints/handler.submitMintInfo
handler: src/functions/mints/handler.submitMintInfo
events:
- http:
path: mint

View File

@ -0,0 +1,16 @@
import { PrismaClient } from '@prisma/client';
export const prisma = new PrismaClient();
export async function initPrisma() {
// Connect the client
await prisma.$connect();
}
initPrisma()
.catch(async (e) => {
console.error(e);
})
.finally(async () => {
await prisma.$disconnect();
});

View File

@ -9,6 +9,12 @@
"noUnusedParameters": true,
"removeComments": true,
"outDir": "dist",
"target": "es2016",
"module": "commonjs",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"baseUrl": ".",
"paths": {
"@functions/*": ["src/functions/*"],

View File

@ -1384,6 +1384,23 @@
"@nodelib/fs.scandir" "2.1.5"
fastq "^1.6.0"
"@prisma/client@^4.13.0":
version "4.13.0"
resolved "https://registry.yarnpkg.com/@prisma/client/-/client-4.13.0.tgz#271d2b9756503ea17bbdb459c7995536cf2a6191"
integrity sha512-YaiiICcRB2hatxsbnfB66uWXjcRw3jsZdlAVxmx0cFcTc/Ad/sKdHCcWSnqyDX47vAewkjRFwiLwrOUjswVvmA==
dependencies:
"@prisma/engines-version" "4.13.0-50.1e7af066ee9cb95cf3a403c78d9aab3e6b04f37a"
"@prisma/engines-version@4.13.0-50.1e7af066ee9cb95cf3a403c78d9aab3e6b04f37a":
version "4.13.0-50.1e7af066ee9cb95cf3a403c78d9aab3e6b04f37a"
resolved "https://registry.yarnpkg.com/@prisma/engines-version/-/engines-version-4.13.0-50.1e7af066ee9cb95cf3a403c78d9aab3e6b04f37a.tgz#ae338908d11685dee50e7683502d75442b955bf9"
integrity sha512-fsQlbkhPJf08JOzKoyoD9atdUijuGBekwoOPZC3YOygXEml1MTtgXVpnUNchQlRSY82OQ6pSGQ9PxUe4arcSLQ==
"@prisma/engines@4.13.0":
version "4.13.0"
resolved "https://registry.yarnpkg.com/@prisma/engines/-/engines-4.13.0.tgz#582a6b90b6efeb0f465984f1fe0e72a4afaaa5ae"
integrity sha512-HrniowHRZXHuGT9XRgoXEaP2gJLXM5RMoItaY2PkjvuZ+iHc0Zjbm/302MB8YsPdWozAPHHn+jpFEcEn71OgPw==
"@serverless/dashboard-plugin@^6.2.3":
version "6.2.3"
resolved "https://registry.npmjs.org/@serverless/dashboard-plugin/-/dashboard-plugin-6.2.3.tgz"
@ -5140,6 +5157,13 @@ pinkie@^2.0.0:
resolved "https://registry.npmjs.org/pinkie/-/pinkie-2.0.4.tgz"
integrity sha512-MnUuEycAemtSaeFSjXKW/aroV7akBbY+Sv+RkyqFjgAe73F+MR0TBWKBRDkmfWq/HiFmdavfZ1G7h4SPZXaCSg==
prisma@^4.13.0:
version "4.13.0"
resolved "https://registry.yarnpkg.com/prisma/-/prisma-4.13.0.tgz#0b83f40acf50cd47d7463a135c4e9b275713e602"
integrity sha512-L9mqjnSmvWIRCYJ9mQkwCtj4+JDYYTdhoyo8hlsHNDXaZLh/b4hR0IoKIBbTKxZuyHQzLopb/+0Rvb69uGV7uA==
dependencies:
"@prisma/engines" "4.13.0"
process-nextick-args@~2.0.0:
version "2.0.1"
resolved "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz"