Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confused and not clear docs about serialize #1710

Open
vitonsky opened this issue Jul 9, 2023 · 1 comment
Open

Confused and not clear docs about serialize #1710

vitonsky opened this issue Jul 9, 2023 · 1 comment
Labels

Comments

@vitonsky
Copy link

vitonsky commented Jul 9, 2023

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:

  • How works this wrapper? Why SQL queries inside callback will executes one by one?
  • Can i wrap callbacks to promises? Is it will still executes one by one?
  • How to wrap to promises to make my code with complex request inside transaction are flatten?

Steps to Reproduce

Version

5.1.6

Node.js Version

v18.16.0

How did you install the library?

npm i sqlite3

@vitonsky vitonsky added the bug label Jul 9, 2023
@johanatan
Copy link

johanatan commented Sep 10, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants