Skip to content

feat: add @Unique decorator, support specifying multiple unique indexes on the same attribute - #15342

Merged
ephys merged 12 commits into
mainfrom
ephys/attribute-table-decorators
Nov 27, 2022
Merged

ephys merged 12 commits into
mainfrom
ephys/attribute-table-decorators

Conversation

@ephys

@ephys ephys commented Nov 27, 2022 •

Copy link
Copy Markdown
Member

Pull Request Checklist

  • Have you added new tests to prevent regressions?
  • If a documentation update is necessary, have you opened a PR to the documentation repository?
  • Did you update the typescript typings accordingly (if applicable)?
  • Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Does the name of your PR follow our conventions?

Description Of Change

Closes #8148

This PR adds the @Unique attribute decorator, and extends the "unique" model attribute option to support using the same attribute in multiple composite uniques.

This is now possible:

const User = current.define('User', {
  firstName: {
    type: DataTypes.STRING,
    // this creates 3 uniques:
    // - one only on firstName
    // - one on firstName & lastName
    // - one on firstName & country
    // (obv you wouldn't want to make names unique but this is just a demo)
    unique: [true, 'firstName-lastName', 'firstName-country'],
  },
  lastName: {
    type: DataTypes.STRING,
    unique: 'firstName-lastName',
  },
  country: {
    type: DataTypes.STRING,
    unique: 'firstName-country',
  },
});

Or with decorators:

class User extends Model {
  @Attribute(DataTypes.STRING)
  @Unique
  @Unique('firstName-lastName')
  @Unique('firstName-country')
  // can also be an array:
  // @Unique([true, 'firstName-lastName', 'firstName-country'])
  declare firstName: string;

  @Attribute(DataTypes.STRING)
  @Unique('firstName-lastName')
  declare lastName: string;

  @Attribute(DataTypes.STRING)
  @Unique('firstName-country')
  declare country: string;
}

@ephys

ephys commented Nov 27, 2022

Copy link
Copy Markdown
Member Author

Don't pay attention to the commit history, I forgot to checkout main before working on this so it includes that commits for @Attribute & @Table. Final code is fine 😅

@ephys ephys mentioned this pull request Nov 27, 2022
13 of 15 tasks
Comment thread test/integration/model/create.test.js Resolved
Comment thread test/unit/model/define.test.js Resolved
WikiRik
WikiRik previously approved these changes Nov 27, 2022

@WikiRik WikiRik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Only comment is non-blocking

fzn0x
fzn0x previously approved these changes Nov 27, 2022
@ephys
ephys dismissed stale reviews from fzn0x and WikiRik via 6d6b74f November 27, 2022 20:41
@WikiRik WikiRik changed the title feat: Add @Unique decorator, support specifying multiple unique indexes on the same attribute feat: add @Unique decorator, support specifying multiple unique indexes on the same attribute Nov 27, 2022
@ephys
ephys merged commit 43bca57 into main Nov 27, 2022
@ephys
ephys deleted the ephys/attribute-table-decorators branch November 27, 2022 21:43
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

Successfully merging this pull request may close these issues.

Many composite unique keys on same field

3 participants