Is there any way to atomize prisma Models, to not have too much code in the schema.prisma - prisma

I want to know if is there some way to keep each prisma Model in a single file and then export each Model to the schema.prisma file
export model User {
id Int #id #unique #default(autoincrement())
name String #db.VarChar(255)
last_name String #db.VarChar(255)
dateOf_birth DateTime #db.Date()
}
and then
import {User} from 'User.prisma'
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}
model User
so I can keep each model or table in individual file.

It's not possible natively to split the schema.prisma file into multiple files.
You could use the Aurora tool to split the schema files.

Related

Prisma, how to preserve foreign data?

It's been a few months since I started prisma and I'm still confused.
In a normal database, foreign key data also exists in table data. However, according to the prisma document, in prisma, the data does not exist at the database level.
So where is it stored? It seems that the things I do "connect:id:1" are stored in the Prisma client. If I delete the prisma dependency and install it again with npm install, will all these relational data be deleted too?? How can I make it as safe as possible????
And it seems too dangerous when I migrate later. what am I misunderstanding?
ADDED
const user = await prisma.user.create({
data: {
email: 'vlad#prisma.io',
posts: {
connect: [{ id: 8 }, { id: 9 }, { id: 10 }],
},
},
include: {
posts: true, // Include all posts in the returned object
},
})
in this case, id 8, id 9, id 10, Where are all these stored? Is there any way to check other than prisma studio or select query? I don't know where it is physically stored. It's not even in the planet scale database.
// In the workbench, the foriegn key is actually saved and can be exported. I don't know how it's not at the database level, but where it is referenced and stored.
Considering this Schema:
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model User {
id Int #id #default(autoincrement())
name String
email String #unique
posts Post[]
}
model Post {
id Int #id #default(autoincrement())
title String
published Boolean #default(true)
author User #relation(fields: [authorId], references: [id])
authorId Int
}
There is a one-to-many relationship between User and Posts.
according to the prisma document, in prisma, the data does not exist
at the database level.
Only the relation fields do not exist at the database level, so in this case posts in User model and author in Post model would not exist at database level. But the foreign key exists at the database level, so in this case authorId is actually stored in the database.
Based on the create query you have shared:
in this case, id 8, id 9, id 10, Where are all these stored?
The connect statement in create query is essentially linking the records.
So to elaborate Posts with id 8,9,10 would have the authorId value of the new user record which is created.
So the data is stored in database, you can always check which posts are created by a specific author. You just need to query all the posts which has authorId set to the id which you are querying for.

How to fix "createMany does not exists..." in prisma?

I'm planning to create a seeder for my projects table. I'm using createMany to insert multiple data in just a query (see code below). But the problem is, it does not recognize createMany and throws and error after running a jest test.
Another thing that is confusing me, there was no typescript error in my code. And I can create also single data using create function.
I already been to prisma documentation, but I can't determine what was wrong in my code. Could someone help me figure it out. (comments would also help).
error TS2339: Property 'createMany' does not exist on type 'ProviderDelegate<RejectOnNotFound | RejectPerOperation | undefined>'.
schema.prisma
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "sqlite"
url = "file:./dev.db"
}
model Provider {
id Int #id #default(autoincrement())
user_id Int
name String
space_key String
api_key String
projects Project[]
created_at DateTime #default(now())
updated_at DateTime #updatedAt
##unique([user_id, api_key])
}
my usage
import { PrismaClient } from '#prisma/client'
const prisma = new PrismaClient()
...
await prisma.provider.createMany({
data: [
{
user_id: 1,
name: 'Nicole Sal',
space_key: 'nic_spa',
api_key: 'nic_api',
created_at: new Date(),
updated_at: new Date()
},
// ... more data here (same at above)
]
})
Ahh I see. just found this. createMany is not supported for SQLite.
createMany is not supported on SQLite unfortunately: #10710
Documented here: https://www.prisma.io/docs/reference/api-reference/prisma-client-reference#remarks-10
https://github.com/prisma/prisma/issues/11507#issuecomment-1025587202

How to map Grails domain object to a specific mongodb table name

I've setup a simple grails app looking at a mongodb.
My domain object looks like this:
class GoogleSearch {
String _id;
String id;
String query;
String site;
Object results;
Date date;
static mapping = {
table 'google_searches'
}
static constraints = {
}
}
However when I run the grails app up, it keeps reading/writing to a table named "googleSearch"
Does anyone know how I can override this default naming? Is it a gorm/mongodb thing?
Cheers
Basics of MongoDB. There is no concept of table. It is always collections. :)
Refer mapping as collection 'google_searches'.
For more details you can refer Grails MongoDB plugin.

How can I change the default JPA ID field in the Play Framework?

I'm currently creating some POJOs to connect to an existing database called User. The database already has a userid that I would like to use. However the framework creates an id field that it appends to the end of the table. How can I specify that the framework should use the pre-existing userid field and not create a new one?
You can use #AttributeOverride:
#Entity
#AttributeOverride(name = "id", column = #Column(name = "userid"))
public class User extends Model { ... }
To define your own primary key, have your models extend GenericModel instead of model and annotate your primary key with #Id. The model class enhances the generic model with an autogenerated id.
class User extends GenericModel{
#Id
Long userid;
}

ADO.NET Entity : getting data from 3 tables

I have following table structure:
Table: Plant
PlantID: Primary Key
PlantName: String
Table: Party
PartyID: Primary Key
PartyName: String
PlantID: link to Plant table
Table: Customer
PartyID: Primary Key, link to Party
CustomerCode: String
I'd like to have Customer entity object with following fields:
PartyID: Primary Key
CustomerCode: String
PartyName: String
PlantName: String
I am having trouble with PlantName field (which is brought from Plant table
I connected Customer to Party and Party to Plant with associations
However I can not connect Customer to Plant with association ( because it does not have one)
I can not add Plant table to mapping, when I do that - I am getting following error:
Error 3024: Problem in Mapping Fragment starting at line 352: Must specify mapping for all key properties (CustomerSet.PartyID) of the EntitySet CustomerSet
Removing Plant association works.
Any hints or directions very appreciated.
You can get these fields by using the reference path on the Entity Object.
To get the PartyName, use this syntax: Customer.Party.PartyName
To get the PlantName, use this syntax: Customer.Party.Plant.PlantName
You can extend the Customer entity by using the public partial class:
public partial class Customer
{
public string PartyName
{
get { return Party.PartyName; }
set { Party.PartyName = value; }
}
public string PlantName
{
get { return Party.Plant.PlantName; }
set { Party.Plant.PlantName = value; }
}
}
After some research, I came across this thread on MSDN that says you can create a read-only entity, which is enough of a downside to not use it alone, but it gets worse. You will also lose the ability to update all of the models dynamically based on the schema of the database.