Last active
June 19, 2019 23:13
-
-
Save aranajhonny/ee332dff000ff358826f73beb4f23745 to your computer and use it in GitHub Desktop.
temp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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