From 5d60a6caad2338163f5bb5ac1f273513928d8fb0 Mon Sep 17 00:00:00 2001 From: Ian Campbell <8953846+iancamp@users.noreply.github.com> Date: Fri, 16 Dec 2022 12:35:22 -0500 Subject: [PATCH] Ensure transaction finishes before returning results (#4) --- lib/adapter.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/adapter.js b/lib/adapter.js index 0267fa5..61f1262 100644 --- a/lib/adapter.js +++ b/lib/adapter.js @@ -257,6 +257,7 @@ const Adapter = { create (connectionName, tableName, record, cb) { let cxn = this.connections.get(connectionName) let pk = this.getPrimaryKey(cxn, tableName) + let newRecord; return cxn.knex.transaction(txn => { return txn.insert(Util.castRecord(record)) @@ -265,11 +266,13 @@ const Adapter = { return txn.select().from(tableName).where('rowid', rowid) }) .then(([ created ]) => { - let record = Util.castSqlValues(created, cxn.collections[tableName]) - _.isFunction(cb) && cb(null, record) - return record + newRecord = Util.castSqlValues(created, cxn.collections[tableName]) + return newRecord }) .catch(AdapterError.wrap(cb)) + }).then(() => { + _.isFunction(cb) && cb(null, newRecord) + return newRecord; }) },