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•3y ago
is already normalized or its kind of generic data?
Lopen
LopenOP•3y ago
generic and 100% random
Neto
Neto•3y ago
you can set a more kv type of data or you have to go nosql
Lopen
LopenOP•3y ago
this is not an option whats kv?
Neto
Neto•3y ago
key-value like
{
listeners: {
data: [{label: 'Total', value: 1908022'}]
}
}
{
listeners: {
data: [{label: 'Total', value: 1908022'}]
}
}
Lopen
LopenOP•3y ago
🤔 what will be the data type? array?
Neto
Neto•3y 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
LopenOP•3y ago
still lost how will structure the prisma model
Neto
Neto•3y 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
LopenOP•3y 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•3y ago
ye i just know this snippet of the data
Lopen
LopenOP•3y ago
??
Neto
Neto•3y ago
i dont know the complete data to talk about a better structure
Lopen
LopenOP•3y ago
the data is complex thats just a faction i got so to create the row i will use connectOrCreate ?
Neto
Neto•3y ago
yes you can use a transaction or create with creating the kvs in the same object
Neto
Neto•3y 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
LopenOP•3y ago
thanks
Want results from more Discord servers?
Add your server