Skip to content

defineCollection(options): CollectionDefinition & object

To have define any collection in your database you haveto use below method in your db.[js|ts] file and export an array of CollectionDefinition instances.

Example

typescript
import { defineCollection } from '@modular-rest/server';

export default [
  defineCollection({
    database: 'users',
    collection: 'info',
    // schema: Schema,
    // permissions: Permission[]
    // trigger: DatabaseTrigger[]
  })
]

// Access the model directly:
const userCollection = defineCollection({...});
const UserModel = userCollection.model;
const users = await UserModel.find();

Parameters

ParameterTypeDescription
options{ collection: string; database: string; mongoOption: MongoOption; permissions: Permission[]; schema: Schema<any>; triggers: DatabaseTrigger[]; }The options for the collection
options.collectionstringThe name of the collection to be configured
options.databasestringThe name of the database where the collection resides
options.mongoOption?MongoOptionOptional MongoDB connection options. If not provided, will use config.mongo if available. This is used to pre-create the model before server startup.
options.permissionsPermission[]List of permissions controlling access to the collection
options.schemaSchema<any>Mongoose schema definition for the collection See https://mongoosejs.com/docs/5.x/docs/guide.html
options.triggers?DatabaseTrigger[]Optional database triggers for custom operations

Returns

CollectionDefinition & object

A CollectionDefinition instance with a model property that returns the mongoose model