papr
Version:
MongoDB TypeScript-aware Models
90 lines (89 loc) • 2.98 kB
TypeScript
import { Db } from 'mongodb';
import types from './types.ts';
import type { Model } from './model.ts';
import type { SchemaOptions } from './schema.ts';
import type { BaseSchema, ModelOptions } from './utils.ts';
export default class Papr {
db?: Db;
models: Map<string, Model<any, any>>;
options?: ModelOptions;
schemas: Map<string, [BaseSchema, any]>;
/**
* Returns a new instance of `Papr`.
*
* It may be called with some options for before and after [hooks](api/hooks.md) and a maximum execution time for queries.
*
* @name Papr
*
* @param [options] {ModelOptions}
* @param [options.hooks] {Hooks}
* @param [options.maxTime] {number}
*
* @example
* const papr = new Papr();
*
* const paprWithOptions = new Papr({
* hooks: {
* after: [afterHook],
* before: [beforeHook]
* },
* maxTime: 1000
* });
*/
constructor(options?: ModelOptions);
/**
* Initialize existing and future registered models with a mongo db instance.
*
* @param db {mongodb.Db}
*
* @example
* import { MongoClient } from 'mongodb';
*
* const connection = await MongoClient.connect('mongodb://localhost:27017');
*
* papr.initialize(connection.db('test'));
*/
initialize(db: Db): void;
/**
* Builds a model instance and associates its collection name and schema.
*
* @param collectionName {string}
* @param collectionSchema {TSchema}
*
* @returns {Model<TSchema, TOptions>}
*
* @example
* const User = papr.model('users', userSchema);
*/
model<TSchema extends BaseSchema, TOptions extends SchemaOptions<TSchema>>(collectionName: string, collectionSchema: [TSchema, TOptions]): Model<TSchema, TOptions>;
/**
* Updates the validation schema and validation options on the MongoDB collection used by a model.
*
* It uses the [`createCollection`](https://docs.mongodb.com/manual/reference/method/db.createCollection/)
* method for new collections, and the [`collMod`](https://docs.mongodb.com/manual/reference/command/collMod/#dbcmd.collMod)
* command for existing collections.
*
* @param model {Model<TSchema, TOptions>}
*
* @returns {Promise<void>}
*
* @example
* await papr.updateSchema(User);
*/
updateSchema<TSchema extends BaseSchema, TOptions extends SchemaOptions<TSchema>>(model: Model<TSchema, TOptions>): Promise<void>;
/**
* Updates the validation schemas and validation options on all the MongoDB collections registered by models.
*
* @returns {Promise<void>}
*
* @example
* await papr.updateSchemas();
*/
updateSchemas(): Promise<void>;
}
export { types, types as Types };
export * from './hooks.ts';
export * from './model.ts';
export * from './mongodbTypes.ts';
export * from './schema.ts';
export * from './utils.ts';