26
loading...
This website collects cookies to deliver better user experience
provider
.datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}
prisma.schema
file allows us to specify how we want Prisma to connect to our database. We need to tell it what kind of provider we would like to use - in this case mongodb
- and a url
to connect to - this is pointing to an environment variable as we want to keep it secret. We will use a MongoDB connection string as the DATABASE_URL
which can be found in the /prisma/.env
file.generator client {
provider = "prisma-client-js"
previewFeatures = ["mongoDb"]
}
previewFeatures
key.{
"_id": { "$oid": "60d599cb001ef98000f2cad2" },
"email": "[email protected]",
"name": "Somsubhra",
}
model User {
id String @id @default(dbgenerated()) @map("_id") @db.ObjectId
email String @unique
name String?
posts Post[]
}
model Post {
id String @id @default(dbgenerated()) @map("_id")
published Boolean @default(false)
title String
user User? @relation(fields: [userId], references: [id])
userId String?
}
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const UserSchema = new Schema({
name: String,
email: {
type: String,
unique: true,
},
});
module.exports = User = mongoose.model("user", UserSchema);
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const PostSchema = new Schema({
title: String,
user: {
type: mongoose.Types.ObjectId,
ref: "user",
},
published: {
type: Boolean
default: false,
},
});
module.exports = Post = mongoose.model("post", PostSchema);
import { ObjectId } from "bson";
const id = new ObjectId();
where
property but in mongoose it has findById
method.const user = await prisma.user.findUnique({
where: {
id: ObjectId('5eb9354624286a04e42401d8'),
},
})
const result = await User.findById('5eb9354624286a04e42401d8')
select()
. This increases the time complexity and also slows down the process.const user = await prisma.user.findUnique({
where: {
id: ObjectId('5eb9354624286a04e42401d8'),
},
select: {
name: true,
},
})
const user = await User.findById('5eb9354624286a04e42401d8').select(['name', 'email'])
include
property but in Mongoose we would have to use the populate()
method.const posts = await prisma.user.findUnique({
where: {
id: ObjectId('5eb9354624286a04e42401d8'),
},
include: {
post: true,
},
})
const userWithPosts = await User.findById(id).populate('posts')
where
property whereas in Mongoose we use the find()
.const posts = await prisma.post.findMany({
where: {
title: {
contains: 'Hello World',
},
},
})
const post = await Post.find({
title: 'Hello World',
})
const posts = await prisma.user.findMany({
where: {
posts: {
some: {
title: {
contains: 'Hello',
},
},
},
},
})
const page = prisma.post.findMany({
before: {
id: ObjectId('6eb93546f4w5486a04e42401d8'),
},
last: 20,
})
const cc = prisma.post.findMany({
skip: 200,
first: 20,
})
const posts = await Post.find({
skip: 5,
limit: 10,
})
const user = await prisma.user.create({
data: {
email: '[email protected]',
},
})
const user = await User.create({
name: 'John',
email: '[email protected]',
})
$set
operator.const user = await prisma.user.update({
data: {
name: 'John',
},
where: {
id: ObjectId('5eb9354624286a04e42401d8'),
},
})
const updatedUser = await User.findOneAndUpdate(
{ _id: id },
{
$set: {
name: 'John',
},
}
)
const user = await prisma.user.delete({
where: {
id: ObjectId('5eb9354624286a04e42401d8'),
},
})
await User.findByIdAndDelete(id)
const users = await prisma.user.deleteMany({
where: {
id: {
in: [1, 2, 6, 34],
},
},
})
await User.deleteMany({ _id: { $in: [1, 2, 6, 34] } })
schema.prisma
file which makes the file cluttered and hard to debug and read.@@id
and auto-increment are not currently supported.