- A simple dependency injection model with
MonkModule.forRoot()
,MonkModule.forRootAsync()
andMonkModule.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.
npm install --save nest-monk monk
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
is the connection string to the Mongo server and database.
options
are the monk options to be passed into the instantiation. See monk Manager documentation for more details.
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 {}
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.
// user.model.ts
// default collection name is "users". no indexes will be configured.
class User {
_id: string;
name: string;
}
// user.model.ts
@Model({
collectionName: 'app_users',
collectionOptions: o => o.createIndex('name', { unique: true }),
})
class User {
_id: string;
name: string;
}
This is the name of the collection stored in the Mongo database.
This will be executed against the collection. This is where modifications to the collection should go, i.e. creation of indexes.
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 {}
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 {}
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();
}
}
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. UsesetProperties
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);
}
}
- Author - Kerry Ritter and BeerMoneyDev
nest-monk is MIT licensed.
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.