Hi, is there an easy/best-practice to convert this...
# orm-help
j
Hi, is there an easy/best-practice to convert this data structure to a create query where
password
is removed and replaced with
hashedPassword
, a nested
profile
is created and inside profile an
address
is created?
Copy code
{
  "email": "<mailto:test@test.nl|test@test.nl>",
  "password": "abcd",
  "profile": {
    "firstName": "Test",
    "lastName": "T",
    "address": {
      "street": "A",
      "zipcode": "B",
      "city": "C",
      "state": "D",
      "country": "E"
    }
  }
}
Because it seems like a bit of a pain to manually map every field to an object that can be used for a
create
query function
r
You would need to traverse the fields and convert those as the data will not be uniform everywhere.
So a helper function that does this would need to be created.
🙌 1
j
I can transform data from one DTO to another, but its mostly the nested value format that seems difficult here, because the profile needs to be inside
Copy code
profile: {
  create: { ... }
}
and then the same has to be done inside of that for the address, correct?
💯 1
Or can I put them in without the
create
r
No
create
would be required.
So possibly a recursive function that builds an object like this with
create
.
j
I think I'm kind of managing to do it, ish. But its a bit of a puzzle
Copy code
const userInData: any = { ...userIn };
delete userInData.password;
delete userInData.profile;

console.log(userIn.password);

const hashedPassword = await this._authService.hashPassword(
	userIn.password
);

let data: Prisma.UserCreateInput = {
	...userInData,
	hashedPassword: hashedPassword,
};

if (userIn.profile) {
	const profileData: any = { ...userIn.profile };
	delete profileData.address;
	data.profile = {
		create: { ...profileData },
	};
}

console.log(data);

const user = this._prismaService.user.create({ data: data });
results in this:
Copy code
{
   email: '<mailto:test@test.nl|test@test.nl>',
   hashedPassword: '$2b$10$DaabkBHuBxpwG59eOMAePeU7BmpsJ56tnwmtmgp1GS/8Y7HMQf.zS',
   profile: {
     create: {
       firstName: 'Test',
       lastName: 'T',
       phoneNumber: undefined,
       dateOfBirth: undefined
     }
   }
}
And then it needs another level for the address