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

dynasty.table() requires the table to exist #46

Open
chetbox opened this issue Jul 4, 2015 · 4 comments
Open

dynasty.table() requires the table to exist #46

chetbox opened this issue Jul 4, 2015 · 4 comments
Assignees
Milestone

Comments

@chetbox
Copy link

chetbox commented Jul 4, 2015

The following example fails due to a race condition where the table has not yet been created.

dynasty.list()
  .then(function(resp) {
    if (resp.TableNames.indexOf(`my_table`) === -1) {
      dynasty.create('my_table', {key_schema: {hash: ['id', 'string']}});
    }
  });
exports.my_table = dynasty.table('my_table');

Doing any operation on my_table fails with the error Unhandled rejection ResourceNotFoundException: Cannot do operations on a non-existent table even after the table has been successfully created.

Is it possible to allow dynasty.table() to be lazy (i.e. the table does not need to exist when the object is created) or make it a promise somehow?

What I'm trying to achieve is:

  1. Create my_table if necessary
  2. Export dynasty.table('my_table')
@victorquinn
Copy link
Owner

Interesting idea! I'll dig into the code and see if I can do that in a way that wouldn't break things, I suspect I can make this happen.

In the meantime though, you can do it yourself in user space with something like the following:

exports.my_table = dynasty.list()
    .then(function(resp) {
        if (resp.TableNames.indexOf('my_table') === -1) {
            return dynasty
                       .create('my_table', {key_schema: {hash: ['id', 'string']}})
                       .return(dynasty.table('my_table'));
        } else {
            return dynasty.table('my_table');
        }
  });

note, I didn't test this code, so I'd suggest doing so!

That should accomplish what you want -- returning a promise that either resolves to the table or to the table post-creation.

I'll leave this issue open until I can assess whether dynasty.table() can be updated to include this functionality.

@victorquinn victorquinn added this to the 0.2 milestone Jul 6, 2015
@victorquinn victorquinn self-assigned this Jul 6, 2015
@chetbox
Copy link
Author

chetbox commented Jul 6, 2015

Ah, thanks. It's easier than I thought!

Noticed there's a typo in my original code (which I just fixed). USERS should read 'my_table.

@chetbox
Copy link
Author

chetbox commented Jul 6, 2015

Tried it out and it's not ideal because you end up with a promise, not a dynasty.table.

So when requiring this file as db it looks something like this.

var db = require('db');
db.my_table
  .then(function(table) {
    // do operations on table here
  });

Ideally I just want to be able do this:

dynasty.create('my_table', {key_schema: {hash: ['id', 'string']}})
  .find('some_id');

Though this will probably require making .find, .scan etc. being methods on the Promise prototype.

@tshelburne
Copy link

@chetbox Anytime I have a module where the exported value is a "future" value, I always export a callable function - in your case, something like exports.getMyTable = function() {...}. This tends to make things easier to test, and puts the mechanics in place early to allow for configuration down the road.

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

No branches or pull requests

3 participants