From f8ebf581f8272615f48ce355c6b2cbf8248c73bd Mon Sep 17 00:00:00 2001 From: eug-vs Date: Wed, 2 Dec 2020 15:26:25 +0300 Subject: feat: export client class --- lib/client.ts | 21 +++++++++++++++++++++ lib/connection.ts | 12 ++++++++++++ lib/event.model.ts | 8 +++++--- lib/index.ts | 3 +-- lib/log.model.ts | 9 +++++++-- 5 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 lib/client.ts create mode 100644 lib/connection.ts (limited to 'lib') diff --git a/lib/client.ts b/lib/client.ts new file mode 100644 index 0000000..eafdda5 --- /dev/null +++ b/lib/client.ts @@ -0,0 +1,21 @@ +import { Schema } from 'mongoose'; +import Connection from './connection'; +import createEventModel, { EventModel } from './event.model'; +import createLogModel, { LogModel } from './log.model'; + + +class Client { + public connection: Connection; + public Event: EventModel; + public Log: LogModel; + + constructor(connection: Connection, contextSchema: Schema) { + this.connection = connection; + this.Log = createLogModel(connection); + this.Event = createEventModel(connection, contextSchema); + } +} + + +export default Client; + diff --git a/lib/connection.ts b/lib/connection.ts new file mode 100644 index 0000000..17ea59a --- /dev/null +++ b/lib/connection.ts @@ -0,0 +1,12 @@ +import { model, Model } from 'mongoose'; + +// An interface for mongoose.connection +export interface Connection { + model: typeof model; + models: Record>; + dropCollection: (name: string) => Promise; +} + + +export default Connection; + diff --git a/lib/event.model.ts b/lib/event.model.ts index 62675d4..bea533d 100644 --- a/lib/event.model.ts +++ b/lib/event.model.ts @@ -2,7 +2,8 @@ import { model, Schema, Model } from 'mongoose'; import cron from 'cron'; import createEventSchema, { EventDocument } from './event.schema'; import { LogDocument } from './log.schema'; -import LogModel from './log.model'; +import { LogModel } from './log.model'; +import Connection from './connection'; export interface Event extends EventDocument { log(message: string): Promise; @@ -20,8 +21,9 @@ export interface EventModel extends Model> { const { CronJob } = cron; -const createEventModel = (name: string, contextSchema: Schema): EventModel => { +const createEventModel = (connection: Connection, contextSchema: Schema): EventModel => { const schema = createEventSchema(contextSchema); + const LogModel: LogModel = connection.models['Log']; // Schema methods schema.method('log', function (message: string) { @@ -93,7 +95,7 @@ const createEventModel = (name: string, contextSchema: Schema): EventMo this.nextRunAt = this.computeNextRunAt(); }); - return model, EventModel>(name, schema); + return connection.model, EventModel>('Event', schema); }; diff --git a/lib/index.ts b/lib/index.ts index 4f128b1..cb2f7ef 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -1,4 +1,3 @@ -export { default as model } from './event.model'; -export { default as LogModel } from './log.model'; +export { default as Client } from './client'; export { default as Scheduler } from './scheduler'; diff --git a/lib/log.model.ts b/lib/log.model.ts index 7ea9cb9..1904d15 100644 --- a/lib/log.model.ts +++ b/lib/log.model.ts @@ -1,8 +1,13 @@ import { model, Model } from 'mongoose'; import schema, { LogDocument } from './log.schema'; +import Connection from './connection'; -const LogModel = model>('Log', schema); +export type LogModel = Model; +const createLogModel = (connection: Connection): LogModel => { + return connection.model('Log', schema); +}; -export default LogModel; + +export default createLogModel; -- cgit v1.2.3