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

Create if not exists #189

Open
rafoli opened this issue Sep 20, 2016 · 3 comments
Open

Create if not exists #189

rafoli opened this issue Sep 20, 2016 · 3 comments

Comments

@rafoli
Copy link

rafoli commented Sep 20, 2016

Hi,

I have a simple table:

const Query = vogels.define('Query', {
    hashKey: 'queryId',

    schema: {
        queryId: vogels.types.uuid(),
        name: Joi.string().required()
    },

    tableName: 'query'
});

How to avoid duplicated queries that contain the same 'name'?

I've tried the following code:

function saveQuery(query, cb) {
    var params = {};
    params.ConditionExpression = '#n <> :x';
    params.ExpressionAttributeNames = { '#n': 'name' };
    params.ExpressionAttributeValues = { ':x': query.name };


    queryModel.Query.create(query, params, function(err, res) {
        if (err) {
            console.log(err);
            cb(err);
        } else {
            cb(null, res);
        }
    });
}

But, duplicated queries have been created.
Is there a way to do it using 'create'?

Thanks

@set-killer
Copy link

set-killer commented Sep 20, 2016

DynamoDB is not an SQL server. Please read some articles about the best approaches with DynamoDB.

Option 1: Set the name as HashKey
Option 2: Create Global Primary Key with hash name - the name of the query.
Option 3: Use the scan method ->

Query.scan().where("name").eq(yourQueryName).loadAll().exec()

Using the scan method will be vary ineffective on large tables. It will consume more read capacity.

@rafoli
Copy link
Author

rafoli commented Sep 20, 2016

Thanks,

I'll try the global primary key approach, 'name' was just an example, a have more fields to guarantee the uniqueness.

One more question, about attribute -> 'params' in:

Query.create(query, params, ...

What is it used for? The docs only say...use expressions api to do conditional writes That is why I tried to use 'params' to avoid duplicated rows.

@set-killer
Copy link

Oh, just saw the pointed out example in the documentation with conditional create.
It could be a regression.

Just find out that there are no tests for this situation.

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

2 participants