DEV Community

Haad Baig
Haad Baig

Posted on • Originally published at Medium on

Composite/Compound Keys in MongoDB using Mongoose in NestJS

Composite/Compound Keys in MongoDB using Mongoose in Nest-JS

So lately I have been exploring NestJS with Mongo DB and mongoose. Since its a new framework it have been quite a challenge to find any help online. So here I am to provide as much help as I could to make your life easy.

Disclaimer: The GitHub repo I provide was using multiple databases even though in example I used single database_, that's why you will see the_ Mongoose.Module.forFeature({}) for user and hobby modules in app.module file however both these mongoose module are using one database i.e. ‘db1’_ . Don’t get bothered, you can use single database and the code will run just fine._

So today I will cover the creation of composite/compound keys with one to many relation using mongoose in NestJS. I will also show you the changes you need to do in schema file in various case scenarios. Even though I will provide the entire code base that I will use in this example, but it will help you have some understanding of:

  1. How to create a simple NestJS app.
  2. How to create schema in NestJS using mongoose.
  3. How to create module in NestJS
  4. What is referenced relation and embedded relation.
  5. How to write API in NestJS

Also I will only mention the code of my schema file because to create composite keys, only schema files need to be modified, and than you write your APIs using repository, service and controller file according t your desire and business logic.

Lets start, First of all create a NestJS application using command:

nest new application_name

This will provide you with simple boiler plate code to work with. Now create a module, in this example I will use simple User module. You can use command :

nest g module module_name

Now lets create a schema. In NestJS, we use decorators throughout the application and as much as they look scary they make our life way too easy. Here is the code for my user-schema:

_export type UserDocument = User & Document;_

_@Schema({_

_})_

_export class User {_

_@Prop()_

_email: string;_

_@Prop()_

_age: number;_

_// @Prop({type: Types.ObjectId, ref: () => Address, refPath: ‘db2’})_

_// address: Address_

_@Prop()_

_hobby: Hobby[]_

_}_
Enter fullscreen mode Exit fullscreen mode

export const UserSchema = SchemaFactory.createForClass(User);

UserSchema.index({email: 1, hobby: 1}, {unique: true})

Now here I have defined a schema with an embedded object of another schema object i.e. Hobby. In NestJS, just like node, while using mongoose you need to tell the schema about composite keys by passing the names of entities in index function. The numbers ahead of the names just represent the sorting order. By adding the unique flag in last line, we assign unique constraints to this key. That is, if combination of this key repeats than it will through database error.

Here I have made it more complex by creating composite key with array of embedded objects. I will mention the changes you you need to do to create composite keys in different scenarios:

  1. Referenced Objects:

In side Prop() decorator add the type of the prop as Types.ObjectId and define the reference schema of the prop by passing name as string. Updated line will look like this.

@Prop({type: Types.ObjectId, ref: ‘Hobby’})

hobby: Hobby[]

Now before insertion, the database will check the combination of email with the ObjectIDs present in array and if any possible pair repeats, it will through error.

  1. Without any relation:

To create composite keys using the entities of same schema, In this case lets make a composite key using email and age. All you need to change is the information inside the UserSchema.index function. Updated line will be:

UserSchema.index({email: 1, age: 1}, {unique: true})

Now before insertion, the database will check the combination of email with the age and if pair repeats, it will through error.

Now its pretty obvious but I think its its still worth mentioning that:

We can create composite keys by combining more than 2 entities, all you need to do is tell the schema about the entities that you want to include in composite key in Schema.Index({ },{unique: true}) function.

That’s all from this article. I hope it turn out to be useful for any of the developer who come here with the hope that they will find answers.

Here’s link to the working code base:

GitHub - haadbaig/NestJS

Reach out to me on Medium/Twitter/LinkedIn if you want me to write an article on any other topic of NestJS.

Top comments (0)