Nik
Nik
PPrisma
Created by Nik on 8/9/2024 in #help-and-questions
Query fields on an object
Given my prisma schema:
enum PointType {
Point
}
type Point {
type PointType
coordinates Float[] //manually index as: '2dsphere'
}
type FeatureProperties {
id String
unit String
number String
street String
city String
district String
region String
postcode String
hash String
}
enum FeatureType {
Feature
}
model Feature {
id String @id @default(auto()) @map("_id") @db.ObjectId

type FeatureType
county County @relation(fields: [county_id], references: [id])
county_id String @db.ObjectId
properties FeatureProperties
geometry Point

zip_boundary ZipBoundary @relation(fields: [zip_boundary_id], references: [id])
zip_boundary_id String @db.ObjectId

houses House[]

created_at DateTime @default(now())
updated_at DateTime @updatedAt
@@map("features")
}
enum PointType {
Point
}
type Point {
type PointType
coordinates Float[] //manually index as: '2dsphere'
}
type FeatureProperties {
id String
unit String
number String
street String
city String
district String
region String
postcode String
hash String
}
enum FeatureType {
Feature
}
model Feature {
id String @id @default(auto()) @map("_id") @db.ObjectId

type FeatureType
county County @relation(fields: [county_id], references: [id])
county_id String @db.ObjectId
properties FeatureProperties
geometry Point

zip_boundary ZipBoundary @relation(fields: [zip_boundary_id], references: [id])
zip_boundary_id String @db.ObjectId

houses House[]

created_at DateTime @default(now())
updated_at DateTime @updatedAt
@@map("features")
}
I am looking to make a query on the zip code, quite simple requirement. However, Prisma isn't allowing this to happen: let dominantZipAddresses = await database.feature.findMany({where:{ properties:{postcode: dominantZip} }}); This fails because it expects id, unit, etc. to also be included with the postcode. But for a query that makes no sense. Without defaulting to raw queries, how can I accomplish this?
2 replies
PPrisma
Created by Nik on 5/6/2024 in #help-and-questions
Convert Raw Query back to prisma type
Is there any utility function to convert the result of a raw query back to prisma types?
{
_id: { '$oid': '6587f42a8aad36c44d06e712' },
type: 'Feature',
county: { '$oid': '6587f3b38aad36c44d02686a' },
properties: {
id: '12103003',
unit: '',
number: '3238',
street: 'WOEDEE DR',
city: 'UNASSIGNED',
district: '',
region: '',
postcode: '',
hash: '81c9b3d207ae1932'
},
geometry: { type: 'Point', coordinates: [Array] },
created_at: { $date: '2023-03-23T06:00:46.804Z' },
updated_at: { $date: '2023-03-23T06:00:46.804Z' },
}
{
_id: { '$oid': '6587f42a8aad36c44d06e712' },
type: 'Feature',
county: { '$oid': '6587f3b38aad36c44d02686a' },
properties: {
id: '12103003',
unit: '',
number: '3238',
street: 'WOEDEE DR',
city: 'UNASSIGNED',
district: '',
region: '',
postcode: '',
hash: '81c9b3d207ae1932'
},
geometry: { type: 'Point', coordinates: [Array] },
created_at: { $date: '2023-03-23T06:00:46.804Z' },
updated_at: { $date: '2023-03-23T06:00:46.804Z' },
}
2 replies
PPrisma
Created by Nik on 4/24/2024 in #help-and-questions
How to type $allModels result extension
I have the following simple example of a result extension:
$allModels:{
$toJson:{
compute(data){
return ()=>data
}
}
}
$allModels:{
$toJson:{
compute(data){
return ()=>data
}
}
}
However on all my modelsI can't even call it. It just says type never has no call signatures
2 replies
PPrisma
Created by Nik on 4/17/2024 in #help-and-questions
prisma aggregateRaw, how to parse Date back to javascript date, and other special types,
Unfortunately I've been forced to use aggregateRaw because I run into memory limits that requires the "{ allowDiskUse: true }". How can I either: 1) Add this option to regular queries or 2) Have aggregateRaw parse back to javascript types.
2 replies
PPrisma
Created by Nik on 4/16/2024 in #help-and-questions
Add Model Extension methods
Hey so I'm using Prisma client extensions to add some methods to my instantiated database objects. However I find cases where either some external force strips the methods off the object or I need to. I would like to be able to reattach them. Is there a way to do this? Like run the model through a Prisma.applyExtensions(modelName, data) or maybe extendedPrismaClient.enhance(data)
1 replies
PPrisma
Created by Nik on 4/15/2024 in #help-and-questions
[Typescript] Prisma result extension typing
How can I get proper typing in a prisma result extension for $allModels? For a simple example, I want to return the same type available in a Model.create for any model
1 replies
PPrisma
Created by Nik on 4/15/2024 in #help-and-questions
@unique on optional field
model Player {
id String @id @default(auto()) @map("_id") @db.ObjectId
steam_id String @unique
...
user User? @relation(fields: [user_id], references: [id])
user_id String? @db.ObjectId @unique
...
@@map("players")
}
model Player {
id String @id @default(auto()) @map("_id") @db.ObjectId
steam_id String @unique
...
user User? @relation(fields: [user_id], references: [id])
user_id String? @db.ObjectId @unique
...
@@map("players")
}
and
model User {
id String @id @default(auto()) @map("_id") @db.ObjectId
name String?
email String? @unique
...
player Player?
...
@@map("users")
}
model User {
id String @id @default(auto()) @map("_id") @db.ObjectId
name String?
email String? @unique
...
player Player?
...
@@map("users")
}
As you can see, user_id is an optional field. However the relation REQUIRES it to be marked as unique. In doing so, I get the following error: duplicate key { user_id: null } I imagine this will be an issue for excluded emails too, though I haven't run into it yet
20 replies
PPrisma
Created by Nik on 4/7/2024 in #help-and-questions
Update nested field in composite types
I have a deeply nested structure. It has to be this way. I want to update a single field at the end of the nested tree structure. How do I accomplish this?
3 replies
PPrisma
Created by Nik on 3/31/2024 in #help-and-questions
[ADVANCED] Prisma Extensions - dynamic typing
I'm trying to create a $allModels extension with proper typing and have been struggling. This is what I was able to cobble together:
model: {
$allModels:{
async findOrCreate<T>(
this: T,
query: Prisma.Args<T, 'create'>['data']
):Promise< Prisma.Result<T, undefined, 'findFirstOrThrow'> >{
const ctx = Prisma.getExtensionContext(this);
let record = await (ctx as any).findFirst({where:query});
if(!record)
record = await (ctx as any).create({data:query});
return record;
},
}
}
model: {
$allModels:{
async findOrCreate<T>(
this: T,
query: Prisma.Args<T, 'create'>['data']
):Promise< Prisma.Result<T, undefined, 'findFirstOrThrow'> >{
const ctx = Prisma.getExtensionContext(this);
let record = await (ctx as any).findFirst({where:query});
if(!record)
record = await (ctx as any).create({data:query});
return record;
},
}
}
There are a few issues I want to resolve here: 1) There has to be some better typing for the ctx (or accessing the parent model). 2) I want to have an includes parameter, and return dynamic typing much like a regular .findFirst query. How can I achieve this? The only thing I can think of is that undefined Result generic which i can't seem to figure out what it does. Thanks
3 replies