any ideas on how to model this in prisma?

creating 2 tables and referencing them seems like a good approach but i am kinda confused
17 Replies
Neto
Neto•2y ago
is already normalized or its kind of generic data?
Lopen
Lopen•2y ago
generic and 100% random
Neto
Neto•2y ago
you can set a more kv type of data or you have to go nosql
Lopen
Lopen•2y ago
this is not an option whats kv?
Neto
Neto•2y ago
key-value like
{
listeners: {
data: [{label: 'Total', value: 1908022'}]
}
}
{
listeners: {
data: [{label: 'Total', value: 1908022'}]
}
}
Lopen
Lopen•2y ago
🤔 what will be the data type? array?
Neto
Neto•2y ago
yea data: Array<Record<string, unknown>> if you know the value will always be a number you can switch from unknown to number or whatever
Lopen
Lopen•2y ago
still lost how will structure the prisma model
Neto
Neto•2y ago
model Something {
id String @id @default(cuid())
name String
kvs KV[]
}

model KV {
id String @id @default(cuid())
key String
value Float

somethingId String
something Something @relation(fields: [somethingId], references: [id])
}
model Something {
id String @id @default(cuid())
name String
kvs KV[]
}

model KV {
id String @id @default(cuid())
key String
value Float

somethingId String
something Something @relation(fields: [somethingId], references: [id])
}
would shape for something like
[
{
id: 'some-cuid',
name: 'Listenership',
kvs: [
{
id: 'some-cuid',
key: 'Total',
value: 1908022
}
]
}
]
[
{
id: 'some-cuid',
name: 'Listenership',
kvs: [
{
id: 'some-cuid',
key: 'Total',
value: 1908022
}
]
}
]
the kvs key is name you want
Lopen
Lopen•2y ago
thanks so something like this is possible
[
{
id: 'some-cuid',
name: 'Listenership',
kvs: [
{
id: 'some-cuid',
key: 'Total',
value: 1908022
},
{
id: '2',
key: 'Main',
value: 323445
}
]
}
]
[
{
id: 'some-cuid',
name: 'Listenership',
kvs: [
{
id: 'some-cuid',
key: 'Total',
value: 1908022
},
{
id: '2',
key: 'Main',
value: 323445
}
]
}
]
and like this
[
{
id: 'some-cuid',
name: 'Listenership',
kvs: [
{
id: 'some-cuid',
key: 'Total',
value: 1908022
},
{
id: '2',
key: 'Main',
value: 323445
}
]
},
{
id: 'some-2',
name: 'AgeGroups:',
kvs: [
{
id: 'some-cuid',
key: 'Total',
value: 1908022
},
{
id: '2',
key: 'Main',
value: 323445
}
]
}
]
[
{
id: 'some-cuid',
name: 'Listenership',
kvs: [
{
id: 'some-cuid',
key: 'Total',
value: 1908022
},
{
id: '2',
key: 'Main',
value: 323445
}
]
},
{
id: 'some-2',
name: 'AgeGroups:',
kvs: [
{
id: 'some-cuid',
key: 'Total',
value: 1908022
},
{
id: '2',
key: 'Main',
value: 323445
}
]
}
]
Neto
Neto•2y ago
ye i just know this snippet of the data
Lopen
Lopen•2y ago
??
Neto
Neto•2y ago
i dont know the complete data to talk about a better structure
Lopen
Lopen•2y ago
the data is complex thats just a faction i got so to create the row i will use connectOrCreate ?
Neto
Neto•2y ago
yes you can use a transaction or create with creating the kvs in the same object
Neto
Neto•2y ago
Prisma
Relation queries (Concepts)
Prisma Client provides convenient queries for working with relations, such as a fluent API, nested writes (transactions), nested reads and relation filters.
Lopen
Lopen•2y ago
thanks
Want results from more Discord servers?
Add your server