简体   繁体   中英

PRISMA: How can i select only first string from string array

I'm looking for solution to taking exact element of String Array in Prisma ORM, in my situation, I want to take only first element of paragraphs array to render short article description on the front.

Thats my article model

model articles {
  id         BigInt   @id @default(autoincrement())
  articleId  Int      @unique
  scrapedAt  DateTime @default(now())
  timeString String
  time       BigInt
  title      String
  url        String
  img        String
  paragraphs String[]
}

Assuming we create articles like the code below

const result = await prisma.articles.create({
  data:{
  articleId: 1,
  timeString: "25082022",
  time:       25082022,
  title:      "First Article title",
  url:        "First Url",
  img:       "Image URL",
  paragraphs: ["First Paragraph for article", "second paragraph"]
  }
})

Then we can query for the paragraphs and pick the first one from the result set

const result = await prisma.articles.findFirst({
  where:{
    id: 1n
  },
  select:{
    paragraphs: true
  }
})
console.log(result.paragraphs[0])

The query returns the result in this form

{ paragraphs: [ 'First Paragrah for article', 'second paragraph' ] }

Then the result of logging result.paragraphs[0] is

Output

First Paragraph for article

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM