shiftcode / dynamo-easy

DynamoDB client for NodeJS and browser with a fluent api to build requests. We take care of the type mapping between JS and DynamoDB, customizable trough typescript decorators.
https://shiftcode.github.io/dynamo-easy/
MIT License
206 stars 27 forks source link

#91 default value provider #268

Closed simonmumenthaler closed 4 years ago

simonmumenthaler commented 4 years ago

solves #244 enables '#91 auto generate timestamps' on put (not on update)

coveralls commented 4 years ago

Coverage Status

Coverage decreased (-0.01%) to 95.597% when pulling ed47cc35f2a4144ec6726085d3f7f1168d8dd9a1 on #91-default-value-provider into 3ffded4f059bfb2cf28b7740979d29a6b9b3d371 on master.

benheymink commented 4 years ago

We've just ran into https://github.com/shiftcode/dynamo-easy/issues/244 (we wanted to store a 'lastUpdated' on write to DB) - is this PR still active?

simonmumenthaler commented 4 years ago

@benheymink this PR is still active - but it does not yet satisfy the lastUpdated feature but a defaultValueProvider which is only applied on PUT.

We made the prerelease 6.0.0-pr91.1 with this defaultValueProvider feature.

avnerenv0 commented 4 years ago

hey just wanted to say thanks for the hard work We are also waiting for this PR (will start to use the prerelease for now).

We managed to get lastUpdated like this:

  @Property({
    mapper: {
      fromDb: dateToStringMapper.fromDb,
      toDb: (existingUpdateAt) => {
        // When creating an entity, defaultValueProvider will be called, and then toDb will also be called,
        // so the returned instance and the saved one will have a millisecond difference in 'updatedAt'
        // This is a big mess in the tests (returned and created objects are not equal)
        // so on isFirstSave we will not use a new date value
        const dateToSave = existingUpdateAt.isFirstSave ? existingUpdateAt : new Date();
        existingUpdateAt.isFirstSave = false;
        return dateToStringMapper.toDb(dateToSave);
      }
    },
    defaultValueProvider: () => {
      const now = new Date();
      now.isFirstSave = true;
      return now;
    }
  })
  updatedAt?: Date;
github-actions[bot] commented 4 years ago

:tada: This PR is included in version 7.0.0-next.1 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

github-actions[bot] commented 4 years ago

:tada: This PR is included in version 7.0.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: