TryGhost / node-sqlite3

SQLite3 bindings for Node.js
BSD 3-Clause "New" or "Revised" License
6.23k stars 817 forks source link

Confused and not clear docs about `serialize` #1710

Open vitonsky opened 1 year ago

vitonsky commented 1 year ago

Issue Summary

I need to execute few SQL queries and wrap it in transaction, to ensure data consistency. I see DB have method serialize and i've read the docs https://github.com/tryghost/node-sqlite3/wiki/Control-Flow

It is absolutely not clear how this method works technically. Some questions i have after read:

Steps to Reproduce

Version

5.1.6

Node.js Version

v18.16.0

How did you install the library?

npm i sqlite3

johanatan commented 1 year ago

For promises, I created prun using p/deferred from the promesa library in clojurescript:

(defn- prun [db sql & [args]]
  (let [p (p/deferred)]
    #_(js/console.debug (format "running sql: %s with args: %s" sql args))
    (.run db sql (if args (clj->js args) #js [])
          (fn [err]
            (if err
              (p/reject! p (js->clj err))
              (p/resolve! p (this-as t {:last-id (.-lastID t)
                                        :changes (.-changes t)})))))
      p))

You could do similarly in plain JavaScript.