Is there a way to customize userCreate function to populate additional field?

In my usecase I need to copy user.id value to another column userId or id_email upon user creation
2 Replies
daveycodez
daveycodez2mo ago
This should be possible with API hooks from the docs
ilya
ilyaOP2mo ago
Will run the built in userCreate first and then add an additional field in a second call or will it enrich the payload of the initial user create call? Anyone looking for a solution, here is how it's done
user: {
modelName: 'UsersView',
fields: {
name: 'firstName',
email: 'email',
image: 'avatarUrl',
},
additionalFields: {
userId: {
type: 'string',
},
subscriptionStatus: {
type: 'string',
},
primaryProvider: {
type: 'string',
},
},
},
databaseHooks: {
user: {
create: {
before: async (user) => {
const userId = generateId(24);
return {
data: {
...user,
id: userId,
userId, // id alias
subscriptionStatus: SubscriptionStatus.FREE,
primaryProvider: IdentityProvider.email,
},
};
},
},
},
},
user: {
modelName: 'UsersView',
fields: {
name: 'firstName',
email: 'email',
image: 'avatarUrl',
},
additionalFields: {
userId: {
type: 'string',
},
subscriptionStatus: {
type: 'string',
},
primaryProvider: {
type: 'string',
},
},
},
databaseHooks: {
user: {
create: {
before: async (user) => {
const userId = generateId(24);
return {
data: {
...user,
id: userId,
userId, // id alias
subscriptionStatus: SubscriptionStatus.FREE,
primaryProvider: IdentityProvider.email,
},
};
},
},
},
},

Did you find this page helpful?