find

find = async <U extends T = T>(
  filter: Filter<U>,
  readConfig: ReadConfig & { page?: number; perPage?: number } = {},
  findOptions: FindOptions = {},
): Promise<FindResult<U>>
const { results: users, count: usersCount } = await userService.find(
  { status: 'active' },
);
Fetches documents that matches the filter. Returns an object with the following fields(FindResult):
FieldDescription
resultsdocuments, that matches the filter
counttotal number of documents, that matches the filter
pagesCounttotal number of documents, that matches the filter divided by the number of documents per page
Pass page and perPage params to get a paginated result. Otherwise, all documents will be returned. Parameters Returns Promise<FindResult<U>>.

findOne

findOne = async <U extends T = T>(
  filter: Filter<U>,
  readConfig: ReadConfig = {},
  findOptions: FindOptions = {},
): Promise<U | null>
const user = await userService.findOne({ _id: u._id });
Fetches the first document that matches the filter. Returns null if document was not found. Parameters Returns Promise<U | null>.

updateOne

updateOne = async <U extends T = T>(
  filter: Filter<U>,
  updateFn: (doc: U) => Partial<U>,
  updateConfig: UpdateConfig = {},
  updateOptions: UpdateOptions = {},
): Promise<U | null>
const updatedUserWithEvent = await userService.updateOne(
  { _id: u._id },
  (doc) => ({ fullName: 'Updated fullname' }),
);

const updatedUser = await userService.updateOne(
  { _id: u._id },
  (doc) => ({ fullName: 'Updated fullname' }),
  { publishEvents: false }
);
Updates a single document and returns it. Returns null if document was not found. Parameters
  • filter: Filter<U>;
  • updateFn: (doc: U) => Partial<U>;
    Function that accepts current document and returns object containing fields to update.
  • updateConfig: UpdateConfig;
  • updateOptions: UpdateOptions;
Returns Promise<U | null>.

updateMany

updateMany = async <U extends T = T>(
  filter: Filter<U>,
  updateFn: (doc: U) => Partial<U>,
  updateConfig: UpdateConfig = {},
  updateOptions: UpdateOptions = {},
): Promise<U[]>
const updatedUsers = await userService.updateMany(
  { status: 'active' },
  (doc) => ({ isEmailVerified: true }),
);
Updates multiple documents that match the query. Returns array with updated documents. Parameters
  • filter: Filter<U>;
  • updateFn: (doc: U) => Partial<U>;
    Function that accepts current document and returns object containing fields to update.
  • updateConfig: UpdateConfig;
  • updateOptions: UpdateOptions;
Returns Promise<U[]>.

insertOne

insertOne = async <U extends T = T>(
  object: Partial<U>,
  createConfig: CreateConfig = {},
  insertOneOptions: InsertOneOptions = {},
): Promise<U>
const user = await userService.insertOne({
  fullName: 'John',
});
Inserts a single document into a collection and returns it. Parameters Returns Promise<U>.

insertMany

insertMany = async <U extends T = T>(
  objects: Partial<U>[],
  createConfig: CreateConfig = {},
  bulkWriteOptions: BulkWriteOptions = {},
): Promise<U[]>
const users = await userService.insertMany([
  { fullName: 'John' },
  { fullName: 'Kobe' },
]);
Inserts multiple documents into a collection and returns them. Parameters Returns Promise<U[]>.

deleteSoft

deleteSoft = async <U extends T = T>(
  filter: Filter<U>,
  deleteConfig: DeleteConfig = {},
  deleteOptions: DeleteOptions = {},
): Promise<U[]>
const deletedUsers = await userService.deleteSoft(
  { status: 'deactivated' },
);
Adds deletedOn field to the documents that match the query and returns them. Parameters Returns Promise<U[]>.

deleteOne

deleteOne = async <U extends T = T>(
  filter: Filter<U>,
  deleteConfig: DeleteConfig = {},
  deleteOptions: DeleteOptions = {},
): Promise<U | null>
const deletedUser = await userService.deleteOne(
  { _id: u._id },
);
Deletes a single document and returns it. Returns null if document was not found. Parameters Returns Promise<U | null>.

deleteMany

deleteMany = async <U extends T = T>(
  filter: Filter<U>,
  deleteConfig: DeleteConfig = {},
  deleteOptions: DeleteOptions = {},
): Promise<U[]>
const deletedUsers = await userService.deleteMany(
  { status: 'deactivated' },
);
Deletes multiple documents that match the query. Returns array with deleted documents. Parameters Returns Promise<U[]>.

replaceOne

replaceOne: (
  filter: Filter<T>,
  replacement: Partial<T>,
  readConfig: ReadConfig = {},
  replaceOptions: ReplaceOptions = {},
): Promise<UpdateResult | Document>
await usersService.replaceOne(
  { _id: u._id },
  { fullName: fullNameToUpdate },
);
Replaces a single document within the collection based on the filter. Doesn’t validate schema or publish events. Parameters Returns Promise<UpdateResult | Document>.

atomic.updateOne

updateOne: (
  filter: Filter<T>,
  updateFilter: UpdateFilter<T>,
  readConfig: ReadConfig = {},
  updateOptions: UpdateOptions = {},
): Promise<UpdateResult>
await userService.atomic.updateOne(
  { _id: u._id },
  { $set: { fullName: `${u.firstName} ${u.lastName}` } },
);
Updates a single document. Doesn’t validate schema or publish events. Parameters Returns Promise<UpdateResult>.

atomic.updateMany

updateMany: (
  filter: Filter<T>,
  updateFilter: UpdateFilter<T>,
  readConfig: ReadConfig = {},
  updateOptions: UpdateOptions = {},
): Promise<Document | UpdateResult>
await userService.atomic.updateMany(
  { firstName: { $exists: true }, lastName: { $exists: true } },
  { $set: { fullName: `${u.firstName} ${u.lastName}` } },
);
Updates all documents that match the specified filter. Doesn’t validate schema or publish events. Parameters Returns Promise<UpdateResult | Document>.

exists

exists(
  filter: Filter<T>,
  readConfig: ReadConfig = {},
  findOptions: FindOptions = {},
): Promise<boolean>
const isUserExists = await userService.exists(
  { email: '[email protected]' },
);
Returns true if document exists, otherwise false. Parameters Returns Promise<boolean>.

countDocuments

countDocuments(
  filter: Filter<T>,
  readConfig: ReadConfig = {},
  countDocumentOptions: CountDocumentsOptions = {},
): Promise<boolean>
const documentsCount = await userService.countDocuments(
  { status: 'active' },
);
Returns amount of documents that matches the query. Parameters Returns Promise<number>.

distinct

distinct(
  key: string,
  filter: Filter<T>,
  readConfig: ReadConfig = {},
  distinctOptions: DistinctOptions = {},
): Promise<any[]>
const statesList = await userService.distinct('states');
Returns distinct values for a specified field across a single collection or view and returns the results in an array. Parameters Returns Promise<any[]>.

aggregate

aggregate: (
  pipeline: any[],
  options: AggregateOptions = {},
): Promise<any[]>
const sortedActiveUsers = await userService.aggregate([
  { $match: { status: "active" } },
  { $sort: { firstName: -1, lastName: -1 } }
]);
Executes an aggregation framework pipeline and returns array with aggregation result of documents. Parameters Returns Promise<any[]>.

watch

watch: (
  pipeline: Document[] | undefined,
  options: ChangeStreamOptions = {},
): Promise<any>
const watchCursor = userService.watch();
Creates a new Change Stream, watching for new changes and returns a cursor. Parameters Returns Promise<any>.

drop

drop: (
  recreate: boolean = false,
): Promise<void>
await userService.drop();
Removes a collection from the database. The method also removes any indexes associated with the dropped collection. Parameters
  • recreate: boolean; Should create collection after deletion.
Returns Promise<void>.

indexExists

indexExists: (
  indexes: string | string[],
  indexInformationOptions: IndexInformationOptions = {},
): Promise<boolean>
const isIndexExists = await usersService.indexExists(index);
Checks if one or more indexes exist on the collection, fails on first non-existing index. Parameters Returns Promise<string | void>.

createIndex

createIndex: (
  indexSpec: IndexSpecification,
  options: CreateIndexesOptions = {},
): Promise<string | void>
await usersService.createIndex({ fullName: 1 });
Creates collection index. Parameters Returns Promise<string | void>.

createIndexes

createIndexes: (
  indexSpecs: IndexDescription[],
  options: CreateIndexesOptions = {},
): Promise<string[] | void>
await usersService.createIndexes([
  { key: { fullName: 1 } },
  { key: { createdOn: 1 } },
]);
Creates one or more indexes on a collection. Parameters Returns Promise<string[] | void>.

dropIndex

dropIndex: (
  indexName: string,
  options: DropIndexesOptions = {},
): Promise<void | Document>
await userService.dropIndex({ firstName: 1, lastName: -1 });
Removes the specified index from a collection. Parameters Returns Promise<void | Document>.

dropIndexes

dropIndexes: (
  options: DropIndexesOptions = {},
): Promise<void | Document>
Removes all but the _id index from a collection.
await userService.dropIndexes();
Parameters Returns Promise<void | Document>.