Node.js - Prisma support

Introduction

Prisma is an open source next-generation framework - see https://www.prisma.io/docsexternal link for 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:

  • ‘Prisma Configuration’ object when .prisma file is found in project.
  • ‘NodeJS Entity’ object when SQL database is used and a model keyword is found inside .prisma file.
  • ‘NodeJS Entity Operation’ object when SQL database is used and a Prisma method is defined within a .ts/.tsx file.
  • ‘NodeJS MongoDB database’ or ‘NodeJS Unknown MongoDB database’ object when MongoDB framework is used.
  • ‘NodeJS MongoDB collection’ object when MongoDB framework is used and a model keyword is found inside .prisma file.
IconDescription
Prisma Configuration
NodeJS Entity
NodeJS Entity Operation
NodeJS MongoDB database
NodeJS Unknown MongoDB database
NodeJS MongoDB collection
NodeJS Unknown MongoDB collection

If a version of com.castsoftware.nodejs < 2.13 is used, the MongoDB database is replaced with a MongoDB connection object and the icon for the collection is wrong.

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 OperationSupported APIs
Add
  • prisma.entity.create
  • prisma.entity.createMany
Update
  • prisma.entity.update
  • prisma.entity.updateMany
  • prisma.entity.upsert
Remove
  • prisma.entity.delete
  • prisma.entity.deleteMany
Select
  • prisma.entity.findMany
  • prisma.entity.findFirst
  • prisma.entity.findFirstOrThrow
  • prisma.entity.findUnique
  • prisma.entity.findUniqueOrThrow
  • prisma.entity.count

The ’entity’ above is ‘NodeJS Entity’ defined in .prisma file.

Link TypeCaller typeCallee type
callLink
  • TypeScript Function
  • TypeScript Method
  • TypeScript Module
  • NodeJS Entity Operation: Add
  • NodeJS Entity Operation: Update
  • NodeJS Entity Operation: Remove
  • NodeJS Entity Operation: Select
useInsertLinkNodeJS Entity Operation: AddSQL table
useUpdateLinkNodeJS Entity Operation: UpdateSQL table
useDeleteLinkNodeJS Entity Operation: RemoveSQL table
useSelectLinkNodeJS Entity Operation: SelectSQL 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 or Missing tables and procedures for Node.js 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

Link TypeSource and destination of linkSupported APIs
useInsertLinkBetween TypeScript Function/Method/Module and NodeJS MongoDB collection
  • prisma.entity.create
  • prisma.entity.createMany
useUpdateLinkBetween TypeScript Function/Method/Module and NodeJS MongoDB collection
  • prisma.entity.update
  • prisma.entity.updateMany
  • prisma.entity.upsert
useDeleteLinkBetween TypeScript Function/Method/Module and NodeJS MongoDB collection
  • prisma.entity.delete
  • prisma.entity.deleteMany
useSelectLinkBetween TypeScript Function/Method/Module and NodeJS MongoDB collection
  • prisma.entity.findMany
  • prisma.entity.findFirst
  • prisma.entity.findFirstOrThrow
  • prisma.entity.findUnique
  • prisma.entity.findUniqueOrThrow
  • prisma.entity.count

The ’entity’ above is ‘NodeJS 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 ‘NodeJS MongoDB connection’ and two ‘NodeJS MongoDB collection’ objects are created. This extension creates a ‘useInsert’ and a ‘useSelect’ links from the function ‘main2’ to the collection ‘Post’: