Node.js - Prisma support
Introduction
Prisma is an open source next-generation framework - see https://www.prisma.io/docs fore more information.
Objects
Prisma framework particularly supports access to both SQL and MongoDB databases. For the SQL analysis, the entities are created from the prisma configuration file, and the entity operations linked to these entities are also created then. For the MongoDB analysis, a connection and the collections are created from the prisma configuration file:
Node.js Prisma configuration
object when .prisma file is found in project.Node.js entity
object when SQL database is used and a model keyword is found inside .prisma file.Node.js entity operation
object when SQL database is used and a Prisma method is defined within a .ts/.tsx file.Node.js MongoDB connection
orNode.js unknown MongoDB connection
object when MongoDB framework is used.Node.js MongoDB collection
object when MongoDB framework is used and a model keyword is found inside .prisma file.
Icon | Description |
---|---|
![]() |
Node.js Prisma configuration |
![]() |
Node.js entity |
![]() |
Node.js entity operation |
![]() |
Node.js MongoDB connection |
![]() |
Node.js unknown MongoDB connection |
![]() |
Node.js MongoDB collection |
Analysis of prisma configuration file
If the .prisma configuration file is missing from the analyzed source code, this extension will not be able to create the objects above.
Supported persistence SQL databases
Supported operations
Entity Operation | Supported APIs |
---|---|
Add |
|
Update |
|
Remove |
|
Select |
|
The ’entity’ above is Node.js entity defined in .prisma file.
Supported links
Link Type | Caller type | Callee type |
---|---|---|
callLink |
|
|
useInsertLink | Node.js entity operation: Add | SQL table |
useUpdateLink | Node.js entity operation: Update | SQL table |
useDeleteLink | Node.js entity operation: Remove | SQL table |
useSelectLink | Node.js entity operation: Select | SQL table |
Example
Taking the following codes of prisma and typescript files:
datasource db {
provider = "postgresql"
url = "postgresql://doe:password@localhost:5432/mydb?schema=sample"
}
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
title String
content String?
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId Int
}
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
async function main1() {
await prisma.user.create({
})
await prisma.user.create({
})
const allUsers = await prisma.user.findMany({
})
}
In this example, two ‘Node.js Entity’ and two ‘Node.js Entity Operation’ objects are created and two ‘call’ links between the function ‘main1’ and these entity operations are added. The SQL Analyzer can then link these entity operations with the corresponding table. In the present case, this extension creates a ‘useInsert’ and a ‘useSelect’ links to the missing table ‘user’:
Supported persistence MongoDB database
Supported links
Link Type | Source and destination of link | Supported APIs |
---|---|---|
useInsertLink | Between TypeScript Function/Method/Module and Node.js MongoDB collection |
|
useUpdateLink | Between TypeScript Function/Method/Module and Node.js MongoDB collection |
|
useDeleteLink | Between TypeScript Function/Method/Module and Node.js MongoDB collection |
|
useSelectLink | Between TypeScript Function/Method/Module and Node.js MongoDB collection |
|
The ’entity’ above is Node.js entity defined in .prisma file.
Example
Taking the following codes of prisma and typescript files:
datasource db {
provider = "mongodb"
url = "mongodb+srv://test:test@cluster0.mongodb.net/database2"
}
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
title String
content String?
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId Int
}
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
async function main2() {
await prisma.post.create({
})
await prisma.post.create({
})
const allPosts = await prisma.post.findMany({
})
}
In this example, a ‘Node.js MongoDB connection’ and two ‘Node.js MongoDB collection’ objects are created. This extension creates a ‘useInsert’ and a ‘useSelect’ links from the function ‘main2’ to the collection ‘Post’: