javascript - How do i store custom object in prisma schema? - Stack Overflow

I have a model called "Setup"model Setup {id String @id @default(auto()) @map("_id"

I have a model called "Setup"

model Setup {
  id String @id @default(auto()) @map("_id") @db.ObjectId

  userId String? @unique @db.ObjectId
  user   User?   @relation(fields: [userId], references: [id])

  contract String[]
  legal    String[]

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

In this model i want to store an array like

const contractData = {
    id: '729a4839f3dapob44zt2b4b1',
    name: 'Example Name',
    text: 'Example Text'
}

so in my above model "Setup" i want to store the contractData

prisma.setup.create({
    data: {
      userId: '6399bc74426f71f2da6e316c',
      personal: [],
      contract: contractData,
      legal: []
    }
  })

Unfortunately, this not work.

How can i define an Object for contract and store this in my database?

I have a model called "Setup"

model Setup {
  id String @id @default(auto()) @map("_id") @db.ObjectId

  userId String? @unique @db.ObjectId
  user   User?   @relation(fields: [userId], references: [id])

  contract String[]
  legal    String[]

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

In this model i want to store an array like

const contractData = {
    id: '729a4839f3dapob44zt2b4b1',
    name: 'Example Name',
    text: 'Example Text'
}

so in my above model "Setup" i want to store the contractData

prisma.setup.create({
    data: {
      userId: '6399bc74426f71f2da6e316c',
      personal: [],
      contract: contractData,
      legal: []
    }
  })

Unfortunately, this not work.

How can i define an Object for contract and store this in my database?

Share Improve this question asked Dec 16, 2022 at 12:19 user19540948user19540948
Add a ment  | 

3 Answers 3

Reset to default 2

If you want to store a raw JSON, check out this guide: https://www.prisma.io/docs/concepts/ponents/prisma-client/working-with-fields/working-with-json-fields

You will want to use the Json in Prisma in order to be able to store a raw JSON object (or multiple JSON objects as an array.)

model Setup {
  id String @id @default(auto()) @map("_id") @db.ObjectId

  userId String? @unique @db.ObjectId
  user   User?   @relation(fields: [userId], references: [id])

  contract Json[]
  legal    String[]

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

Your Prisma prisma.setup.create query would basically be the exact same. Note that querying for what's in this JSON will be trickier - I would remend creating a new model and then connecting it to the Setup model, but that if that isn't an option you can still perform some limited queries on the contract field.

Docs: https://www.prisma.io/docs/concepts/ponents/prisma-client/working-with-fields/working-with-json-fields#filter-on-a-json-field

this is old,

quick answer is, it will be best to create a new model for your contractData object and then link to the parent using Prisma's relation.

JSON would be extremely difficult to parse, and you can't just manufacture a data type like "object".

Lastly, [] is used to indicate a many relationship when suffixed to another model name, not to be confused with the List you have in Js or Ts

In order to store an object in the contract field, you will need to change the type of the contract field from String[] to Object[].

model Setup {
  id String @id @default(auto()) @map("_id") @db.ObjectId

  userId String? @unique @db.ObjectId
  user   User?   @relation(fields: [userId], references: [id])

  contract Object[]
  legal    String[]

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

store an object in the contract field like

prisma.setup.create({
  data: {
    userId: '6399bc74426f71f2da6e316c',
    personal: [],
    contract: [contractData],
    legal: []
  }
})

contract field is now an array, so you will need to pass the object as an element of the array, like [contractData]

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745157893a4614235.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信