scottwrobinson / camo

A class-based ES6 ODM for Mongo-like databases.
556 stars 80 forks source link

Unique constraint on multiple fields #78

Open petersimun opened 8 years ago

petersimun commented 8 years ago

i know it's possible to define in model that field is unique. Is it possible to do so over multiple columns/fields?

Thanks Peter

scottwrobinson commented 8 years ago

Let me make sure I'm understanding your request correctly. If you had a model, Person, with unique-constrained fields firstName and lastName, you want to make sure that only one Person document can have a given firstName and lastName? So there couldn't be two {firstName: 'Scott', lastName: 'Robinson'} instances in the DB, correct?

petersimun commented 8 years ago

exactly!

michaeljota commented 8 years ago

You can do it straightforward. Just set it in the schema, or the constructor. :)

petersimun commented 8 years ago

how? can you provide me an example please?

michaeljota commented 8 years ago

It's in the docs. (README)

The default option supports both values and no-argument functions (like Date.now). Currently the supported options/validators are:

type: The value's type (required) default: The value to be assigned if none is provided (optional) min: The minimum value a Number can be (optional) max: The maximum value a Number can be (optional) choices: A list of possible values (optional) match: A regex string that should match the value (optional) validate: A 1-argument function that returns false if the value is invalid (optional) unique: A boolean value indicating if a 'unique' index should be set (optional) required: A boolean value indicating if a key value is required (optional)