user2502954
user2502954

Reputation:

How to test CRUD operations using Node.js postgres

In my create, read, update, and delete functions, I am returning true if the transaction was successful and false otherwise - except for read, where I return the requested data or null.

This strategy should make testing easy due to simply assessing the return value given various test inputs to a test database.

My problem is that I cannot fully determine whether the transaction was successful until db.query("...") completes and its callback is invoked, but, by this time,the original CRUD function has returned.

What is the common practice?

Upvotes: 0

Views: 1047

Answers (1)

drj
drj

Reputation: 573

Javascript is Asynchronous, and you'll definitely want to understand how that works when using node.

There's a lot of information on the net about this, but here's a SO answer:

Understanding Asynchronous Code in Layman's terms

Generally, best practice in your case is to use promises. Promises are very useful but can also take some time to learn. I personally use the Q promise library

https://github.com/kriskowal/q

I truly hope this helps.

Upvotes: 1

Related Questions