Skip to content

Instantly share code, notes, and snippets.

@aranajhonny
Last active June 19, 2019 23:13
Show Gist options
  • Select an option

  • Save aranajhonny/ee332dff000ff358826f73beb4f23745 to your computer and use it in GitHub Desktop.

Select an option

Save aranajhonny/ee332dff000ff358826f73beb4f23745 to your computer and use it in GitHub Desktop.
temp
export const getOrCreateUser = async (email, authId, name) => {
const users = await dbSql.table("user").where("email", email);
if (users.length === 0) {
try {
const result = await dbSql
.transaction(function(trx) {
const id = genId("usr");
dbSql
.table("user")
.transacting(trx)
.insert({ id, name, email, authId })
.then(function() {
// not support return res.id :(
// return id generated by util
return id;
})
.then(trx.commit)
.catch(trx.rollback);
})
.then(function(id) {
// transaction OK;
return { id };
});
return { id: result.id, authId, email };
} catch (e) {
throw new Error("Failed to create user");
}
} else if (users.length > 1) {
throw new Error("Multiple users with the same email found");
} else {
return users[0];
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment