Reputation: 557
I have been starting with RethinkDB. I have gone through the documentation. I have a simple use case of returning the inserted document when a POST call is made. This is something I have done so far.
router.post('/user', jsonParser, async (req: Request, res: Response) => {
const connection = await conn;
const { name, email, employeeId } = req.body;
// Email is primary key
consumerTable.insert({
name,
email,
employeeId,
}).run(connection, (err: any, result: any) => {
if (err) {
throw err;
} else {
consumerTable.get(email).run(connection, (innerErr: any, userResult: any) => {
if(innerErr) {
throw innerErr;
} else {
res.send({
data: {
...userResult
},
responseCode: 200,
});
}
});
}
});
});
Is there any way to get the inserted object in the insert result itself. I read a lot of documentation, but didn't find anything useful.
Any help would be great. Cheers!
Upvotes: 0
Views: 95
Reputation: 3321
You may use the returnChanges
option of the insert
command as specified in the documentation. It should look like this:
consumerTable.insert({
name,
email,
employeeId,
}, { returnChanges: true }).run(connection, async (err: any, cursor: any) => {
const data = await cursor.toArray();
// .error should be undefined, unless... an error happened!
// not sure about the format though, console.log data to check it really is an array
res.send({ data: data[0].new_val, error: data[0].error });
});
Upvotes: 1