diff options
author | eug-vs <eug-vs@keemail.me> | 2020-12-02 15:26:25 +0300 |
---|---|---|
committer | eug-vs <eug-vs@keemail.me> | 2020-12-02 15:26:25 +0300 |
commit | f8ebf581f8272615f48ce355c6b2cbf8248c73bd (patch) | |
tree | 9473834f4045a9375dc0db48f042f4a6df0e016c /lib | |
parent | 994e0df96267b3de4d82926555ac4c4c7eefe14a (diff) | |
download | mongo-cronjob-f8ebf581f8272615f48ce355c6b2cbf8248c73bd.tar.gz |
feat: export client class
Diffstat (limited to 'lib')
-rw-r--r-- | lib/client.ts | 21 | ||||
-rw-r--r-- | lib/connection.ts | 12 | ||||
-rw-r--r-- | lib/event.model.ts | 8 | ||||
-rw-r--r-- | lib/index.ts | 3 | ||||
-rw-r--r-- | lib/log.model.ts | 9 |
5 files changed, 46 insertions, 7 deletions
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<Context> { + public connection: Connection; + public Event: EventModel<Context>; + public Log: LogModel; + + constructor(connection: Connection, contextSchema: Schema) { + this.connection = connection; + this.Log = createLogModel(connection); + this.Event = createEventModel<Context>(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<string, Model<any, any>>; + dropCollection: (name: string) => Promise<void>; +} + + +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<Context> extends EventDocument<Context> { log(message: string): Promise<LogDocument>; @@ -20,8 +21,9 @@ export interface EventModel<Context> extends Model<Event<Context>> { const { CronJob } = cron; -const createEventModel = <Context>(name: string, contextSchema: Schema): EventModel<Context> => { +const createEventModel = <Context>(connection: Connection, contextSchema: Schema): EventModel<Context> => { const schema = createEventSchema(contextSchema); + const LogModel: LogModel = connection.models['Log']; // Schema methods schema.method('log', function (message: string) { @@ -93,7 +95,7 @@ const createEventModel = <Context>(name: string, contextSchema: Schema): EventMo this.nextRunAt = this.computeNextRunAt(); }); - return model<Event<Context>, EventModel<Context>>(name, schema); + return connection.model<Event<Context>, EventModel<Context>>('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<LogDocument, Model<LogDocument>>('Log', schema); +export type LogModel = Model<LogDocument>; +const createLogModel = (connection: Connection): LogModel => { + return connection.model<LogDocument, LogModel>('Log', schema); +}; -export default LogModel; + +export default createLogModel; |