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

Make a field unique within a collection without it being the Primary Key #855

Open
leggetter opened this issue Feb 20, 2023 · 7 comments
Open
Labels
enhancement New feature or request

Comments

@leggetter
Copy link

Is your feature request related to a problem? Please describe.

Given the following:

import {
  Field,
  PrimaryKey,
  TigrisCollection,
  TigrisDataTypes,
} from "@tigrisdata/core";

@TigrisCollection("users")
class User {
  @PrimaryKey(TigrisDataTypes.INT64, { order: 1, autoGenerate: true })
  id?: string;

  @Field()
  username!: string;

  @Field(TigrisDataTypes.DATE_TIME, { timestamp: "createdAt" })
  createdAt?: Date;
}

export default User;

I would like to be able to force the username field also to be unique.

Although username can be changed, it should still be globally unique within the application that I'm building. I believe this is also a reasonable requirement in applications where a user has a human-readable, globally unique identifier.

This is potentially similar to something like UNIQUE in MySQL.

CREATE TABLE table_name(
    ...,
    column_name data_type UNIQUE,
    ...
);

Describe the solution you'd like

Something like:

import {
  Field,
  PrimaryKey,
  TigrisCollection,
  TigrisDataTypes,
} from "@tigrisdata/core";

@TigrisCollection("users")
class User {
  @PrimaryKey(TigrisDataTypes.INT64, { order: 1, autoGenerate: true })
  id?: string;

  @Field(TigrisDataTypes.STRING, { unique: true })
  username!: string;

  @Field(TigrisDataTypes.DATE_TIME, { timestamp: "createdAt" })
  createdAt?: Date;
}

export default User;

See @Field(TigrisDataTypes.STRING, { unique: true }).

Describe alternatives you've considered

Open to alternatives, but I couldn't think of any. Happy to discuss.

@ovaistariq ovaistariq added the enhancement New feature or request label Feb 21, 2023
@ovaistariq
Copy link
Collaborator

@garrensmith @himank this could be done as an extension of secondary indexes.

@ovaistariq
Copy link
Collaborator

@leggetter what you can do to get this behavior is what I have done here: https://github.com/tigrisdata/tigris-examples-ts/blob/main/rest-express/src/api/routes/signup.ts

@himank
Copy link
Collaborator

himank commented Feb 21, 2023

@garrensmith @himank this could be done as an extension of secondary indexes.

That's the plan and it would be an easy extension for us.

@leggetter
Copy link
Author

@leggetter what you can do to get this behavior is what I have done here: https://github.com/tigrisdata/tigris-examples-ts/blob/main/rest-express/src/api/routes/signup.ts

Yes, specifically this https://github.com/tigrisdata/tigris-examples-ts/blob/main/rest-express/src/api/routes/signup.ts#L47-L58

The email, in this case, has to be unique.

In my case, I want the username to be unique.

@garrensmith
Copy link
Contributor

@leggetter you should be able to replace the email with email with username and make username the primary key and it will work the same

@leggetter
Copy link
Author

leggetter commented Feb 21, 2023

@leggetter you should be able to replace the email with email with username and make username the primary key and it will work the same

@garrensmith - Gotcha. I know I can:

  1. manage this myself if I want to, as per the linked example
  2. change the field I'm using as a primary key to username

However, I'm suggesting a scenario where the developer wants to keep a separate ID as the primary key because the username could change but would like one field (the ID) to be consistent. My suggestion with this ticket is that Tigris supports a unique constraint on non-primary key fields.

Examples:

  • Twitch
  • Twitter
  • YouTube (handle)
  • GitHub

All of these places and types of apps and more, I believe, will have a unique ID that doesn't change along with a username that is also globally unique on the platform, but you can change it. Having the database manage this globally unique constraint seems to be a common requirement.

@ovaistariq
Copy link
Collaborator

I agree that we need support for the field's uniqueness. These suggestions are just a workaround until we have that feature available.

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

No branches or pull requests

4 participants