Hi! Is there any way to get the `data` and `count...
# orm-help
j
Hi! Is there any way to get the
data
and
count
in one function call? TypeORM has a function for this called
.findManyAndCount()
and I was wondering if Prisma had something similar. I found
.count()
but this is results in two calls: one for the data, and one for the count. This can get quite complex with relations and joins, adding a lot of code and overhead. Would be great if there is a way to combine them 🙂
r
@Julian 👋 Currently there’s no such method, but I would suggest creating a feature request here.
j
Alright! Will do, thanks. Is there any other recommended way of doing this?
r
Two calls for now as you mentioned or a raw query.
j
Okay good to know, thanks!
👍 1
I have another question; when performing a create or update function and giving it a data object, is there any way to automatically drop fields in the object that do not belong there? For example, I send
password
to the API, but this does not exist in the data model. Instead, I convert it to a
hashedPassword
. If I don't delete the password field afterwards, Prisma gives an error.
Copy code
const data: any = {
	...userIn,
};
delete data.password;

data.hashedPassword = await this._authService.hashPassword(
	userIn.password
);
const user = this._prismaService.user.create({ data: data });
Compared to TypeORM I could just insert the
userIn
object directly, which has the password field, and it would automatically drop it as it is not relevant for the database:
Copy code
user = await queryRunner.manager.create(User, { ...userIn })
Am I doing it the correct way for Prisma or is there a better way?
r
Due to Prisma’s strict value checking, this is not possible. You need to send the exact fields required in the data.
j
Alright I will look at making this a little prettier then. This is an edge case so in most cases the data coming in perfectly fits the requirements of the data model
💯 1
@ryan I've submitted the feature request 🙂 https://github.com/prisma/prisma/issues/7550
r
Ohh. I see that you need this for pagination info, then we already have a request for that here 🙂
j
Ah I tried searching for it yesterday but couldn't find anything. My bad