SkygearIO / skygear-SDK-JS

Skygear SDK for JavaScript
https://docs.skygear.io/guides/quickstart/js/
Other
23 stars 33 forks source link

[Cloud Code] Provide a Promise-based API for DB queries #197

Open akiroz opened 7 years ago

akiroz commented 7 years ago

Usage:

const skygearCloud = require('skygear/cloud');
skygearCloud.databaseQuery(
  'SELECT id FROM _user WHERE username = $1',
  ['bob']
).then(result => {
   // ...
});

Implementation:

export function databaseQuery(stmt, data = []) {
  return new Promise((rsov, rjct) => {
    skygearCloud.poolConnect((err, client, done) => {
      if(err) rjct(err);
      client.query(stmt, data, (err, result) => {
        done(err);
        if(err) rjct(err);
        rsov(result)
      });
    });
  });
}
cheungpat commented 7 years ago

I think it is a good idea to have a promise interface. I'd suggest not roll out our own implementation though. This is a good package to get started: https://github.com/vitaly-t/pg-promise

akiroz commented 7 years ago

Hmm... should we ship this package in the SDK alongside the existing https://github.com/brianc/node-postgres one? Maybe mark skygearCloud.poolConnect as @deprecated?

cheungpat commented 7 years ago

The pg-promise depends on node-postgres so the answer is yes, we will have pg-promise and node-postgres as dependencies.

As for poolConnect, I think it is okay as is. If everyone uses the promise way to connect DB and no one uses poolConnect, we can decide to deprecate poolConnect at that time.

vitaly-t commented 7 years ago

The pg-promise depends on node-postgres so the answer is yes, we will have pg-promise and node-postgres as dependencies.

pg-promise includes he right version of node-postgres, so do not include the latter as a dependency ;)