clarkie / dynogels

DynamoDB data mapper for node.js. Originally forked from https://github.com/ryanfitz/vogels
Other
490 stars 110 forks source link

[Question] How to only update if email dosn't exist in table #122

Closed sp90 closed 6 years ago

sp90 commented 6 years ago

Hey,

Im searching for a solution to a problem:

How to only update an item, but only if the email dosn't exist in the whole table?

cdhowie commented 6 years ago

I would need to see your schema as well as some example data to be able to answer this question.

sp90 commented 6 years ago

This is my model:

// Users V2
exports.UsersV2 = dynogels.define('Users', {
  // Settings
  tableName: 'Users-v2',
  hashKey: 'user_id',
  timestamps : true,

  // Schema
  schema: {
    user_id: dynogels.types.uuid(),

    // items of interest
    org_id: Joi.string(),
    email: Joi.string().email(),
    phone_number: Joi.string(),

    // Rest
    code: Joi.string(),
    department_id: Joi.string(),
    internal_user_name: Joi.string(),
    job_title: Joi.string(),
    lang: Joi.string(),
    password: Joi.string(),
    linkedin_id: Joi.string(),
    image_key: Joi.string(),
    user_name: Joi.string(),
    user_role: Joi.string(),
  },

  // Index
  indexes: [{
    hashKey: 'org_id',
    rangeKey: 'email',
    name: 'email-index',
    type: 'global'
  }, {
    hashKey: 'org_id',
    rangeKey: 'phone_number',
    name: 'phone_number-index',
    type: 'global'
  }, {
    hashKey: 'email',
    name: 'just-email-index',
    type: 'global'
  }]
});

And then i try to update the email, but only if it dosn't exist in the table column called email

This is how i try to do it:

UsersV2.update({
  user_id: 'SOME_ID',
  email: 'example@example.com'
}, { 
  expected: { 
    email: { 
      Exists: false 
    } 
  } 
}, function(err, resultData) {
  // Result
});
jkav77 commented 6 years ago

I think what you need is to use the expected parameter. There is an example in the readme under Updating.

On December 18, 2017 at 00:51:16, Simon Dragsbæk Petersen (notifications@github.com(mailto:notifications@github.com)) wrote:

This is my model:

// Users V2 exports.UsersV2 = dynogels.define('Users', { // Settings tableName: 'Users-v2', hashKey: 'user_id', timestamps : true, // Schema schema: { user_id: dynogels.types.uuid(), // items of interest org_id: Joi.string(), email: Joi.string().email(), phone_number: Joi.string(), // Rest code: Joi.string(), department_id: Joi.string(), internal_user_name: Joi.string(), job_title: Joi.string(), lang: Joi.string(), password: Joi.string(), linkedin_id: Joi.string(), image_key: Joi.string(), user_name: Joi.string(), user_role: Joi.string(), }, // Index indexes: [{ hashKey: 'org_id', rangeKey: 'email', name: 'email-index', type: 'global' }, { hashKey: 'org_id', rangeKey: 'phone_number', name: 'phone_number-index', type: 'global' }, { hashKey: 'email', name: 'just-email-index', type: 'global' }] });

And then i try to update the email, but only if it dosn't exist in the table column called email

This is how i try to do it:

UsersV2.update({ user_id: 'SOME_ID', email: 'example@example.com' }, { expected: { email: { Exists: false } } }, function(err, resultData) { // Result });

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub(https://github.com/clarkie/dynogels/issues/122#issuecomment-352362357), or mute the thread(https://github.com/notifications/unsubscribe-auth/AOKj9_2h_B7U9wBV9-3l7DUEl8BZ8WfJks5tBieEgaJpZM4RDaSL).

sp90 commented 6 years ago

@dangerginger in the given example thats what im doing, but it dosn't work as expected :/

cdhowie commented 6 years ago

@sp90 It sounds like you want to update the user with ID SOME_ID to have the given email address, only if that email address isn't present in any other record? DynamoDB cannot do that. The expected attribute applies only to the record being updated.

jkav77 commented 6 years ago

Oh yeah there is no way to enforce uniqueness in dynamodb that I know of except for the hash key without a range key. Your options as I see them are:

But anyways this is a dynamodb limitation and nothing we can really fix with dynogels.