I am new to FeathersJS and I am trying to do the following:
My app has two entities named Company
and User
. When someone registers a new company the name, email and a password for the owner are informed.
Then, after creating the record for the company in my companies table I need:
1) To retrieve the id
of the new company;
2) Create an user with this id
ad companyId
, the name of the owner, the email of the owner and the given password.
Of course this should be done in a after insert hook
for the companies service, but I am very confused about how to invoke the users service to perform this insert.
This is my companies.model.js file:
module.exports = function (app) {
const sequelizeClient = app.get('sequelizeClient');
const companies = sequelizeClient.define('companies', {
company: {
type: DataTypes.STRING,
allowNull: false,
size: 100
},
cpfcnpj: {
type: DataTypes.STRING,
allowNull: false,
size: 25
},
owner: {
type: DataTypes.STRING,
allowNull: false,
size: 100
},
cpf: {
type: DataTypes.STRING,
allowNull: false,
size: 25
},
email: {
type: DataTypes.STRING,
allowNull: false,
size: 100
},
addr1: {
type: DataTypes.STRING,
allowNull: false,
size: 100
},
addr2: {
type: DataTypes.STRING,
allowNull: false,
size: 100
},
city: {
type: DataTypes.STRING,
allowNull: false,
size: 100
},
state: {
type: DataTypes.STRING,
allowNull: false,
size: 2
},
zip: {
type: DataTypes.STRING,
allowNull: false,
size: 10
},
phone: {
type: DataTypes.STRING,
allowNull: false,
size: 50
}
}, {
hooks: {
beforeCount(options) {
options.raw = true;
}
}
});
companies.associate = function (models) {
};
return companies;
};
and this is my users.model.js:
module.exports = function (app) {
const sequelizeClient = app.get('sequelizeClient');
const users = sequelizeClient.define('users', {
companyID: {
type: DataTypes.INTEGER,
allowNull false,
size: 11
},
name: {
type: DataTypes.STRING,
allowNull: false,
size: 100
},
email: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
size: 100
},
password: {
type: DataTypes.STRING,
allowNull: false
},
type: {
type: DataTypes.STRING,
allowNull: false,
default: 'CL',
size: 2
}
}, {
hooks: {
beforeCount(options) {
options.raw = true;
}
}
});
users.associate = function (models) {
};
return users;
};
I understand that in my file companies.hooks.js I should have something like
module.exports = {
before: {
all: [],
...
},
after: {
...
create: [ insertUser() ],
...
},
error: {
...
}
};
but apart from that I don't really know how should I write my insertUser()
function or where to put it. As I told you, I'm new with FeathersJS, this is my first day.
EDITED:
The first part of the question is answered. I used some console.log()
s here and there and found out the object context
passed to the hook has an object named result
inside it when you are in an after
hook, which is the case. Using this object now have the company id, the name and email of the owner.