Skip to content

BeerMoneyDev/nest-monk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nest-monk


A thin wrapping layer around the monk package for clean NestJS dependency injection.

NPM Version Package License NPM Downloads

Features

  • A simple dependency injection model with MonkModule.forRoot(), MonkModule.forRootAsync() and MonkModule.forFeatures().
  • @InjectCollection(MyModel) decorator for injecting Monk collection instances.
  • Simple auto-generated CRUD repositories for models, injectable by @InjectRepository(MyModel).
  • Built-in TypeScript class transformations using the class-transformer package and monk middleware.

How To Use

Install

npm install --save nest-monk monk

Importing

MonkModule.forRoot()

MonkModule.forRoot() is the simplest way to import the Monk dependencies and uses static values.

// app.module.ts
@Module({
  imports: [
    MonkModule.forRoot({
      database: 'mongodb://127.0.0.1:27017',
      options: {
        ssl: true,
      }
    }),
  ],
})
class AppRootModule {}

database

database is the connection string to the Mongo server and database.

options

options are the monk options to be passed into the instantiation. See monk Manager documentation for more details.

MonkModule.forRootAsync()

MonkModule.forRootAsync() allows for a FactoryProvider or ValueProvider dependency declaration to import your module. Note that ExistingProvider and ClassProvider are not yet supported.

// app.module.ts
@Module({
  imports: [
    MonkModule.forRootAsync({
      database: {
        useFactory: async (ms: MongoServer) => {
          const server = await ms.startServer();
          return await server.getConnectionString();
        },
        inject: [MongoServer],
        imports: [MongoServerModule],
      },
    }),
  ],
})
class AppRootModule {}

Registering Model Collections

Models

Models are the shape of the data in your collection. If you would like to let nest-monk use simple defaults, you simply can pass a class to the collection registration (see below), but the @Model() decorator can be used to provide optional overrides to collection settings.

Without decorator

// user.model.ts

// default collection name is "users". no indexes will be configured.
class User {
  _id: string;
  name: string;
}

With decorator

// user.model.ts

@Model({
  collectionName: 'app_users',
  collectionOptions: o => o.createIndex('name', { unique: true }),
})
class User {
  _id: string;
  name: string;
}
collectionName

This is the name of the collection stored in the Mongo database.

collectionOptions

This will be executed against the collection. This is where modifications to the collection should go, i.e. creation of indexes.

forRoot registration

Models can be registered at the root level in the collections array by passing the type symbol to the array. Note, if you register models here, do not register them in feature modules as unexpected behavior is likely.

// app.module.ts

@Module({
  imports: [
    MonkModule.forRoot({
      database: 'mongodb://127.0.0.1:27017',
      collections: [User],
    }),
  ],
})
class AppRootModule {}

forFeatures registration

Models can be registered at submodule levels using the MonkModule.forFeatures() method. Similar to the root-level registration, simply pass an array of Model types.

// user.model.ts
@Model()
class User {
  _id: string;
  name: string;
}

// user.module.ts
@Module({
  imports: [MonkModule.forFeatures([User])],
})
class UserModule {}

Collection injection

Collections can be injected using the @InjectCollection() decorator. Collections are a monk construct that nest-monk exposes through dependency injection.

@Injectable()
class UserService {
  constructor(
    @InjectCollection(User) readonly usersCollection: ICollection<User>,
  ) {}

  async getAll() {
    return await this.usersCollection.find();
  }
}

Repository injection

Repositories can be injected using the @InjectRepository() decorator. The repositories are created by nest-monk and are a simple CRUD interface handy for quickly creating REST controllers.

  • getById(id: string) - Retrieves data by the default mongo _id property.
  • list(query: string | Object) - Retrieves data given the mongo query. Leave empty to retrieve all.
  • add(model: T) - Saves the given model to the database.
  • delete(id: string) - Deletes the given data from the database.
  • edit(id: string, model: T, setProperties?: (keyof T)[]) - Updates the data for the given ID. Use setProperties to define specific properties to set, or leave undefined to set all properties.
@Controller()
class UserController {
  constructor(
    @InjectRepository(User) readonly usersRepository: Repository<User>,
  ) {}

  @Get(':id')
  async getById(id: string) {
    return await this.usersRepository.getById(id);
  }
}

Stay In Touch

License

nest-monk is MIT licensed.

Contributing

Nest-monk is released through semantic-release. Effectively this means that versioning is based off commit messages. Please review angular-changelog-convention and commit under that format. Otherwise semantic-release won't detect commits which should version the library.

About

A thin wrapping layer around the monk package for clean NestJS dependency injection.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •