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 +++++++-- test/client.ts | 19 +++++++++++++++++++ test/event.model.test.ts | 7 ++++--- test/scheduler.test.ts | 10 +++++----- 8 files changed, 74 insertions(+), 15 deletions(-) create mode 100644 lib/client.ts create mode 100644 lib/connection.ts create mode 100644 test/client.ts 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; diff --git a/test/client.ts b/test/client.ts new file mode 100644 index 0000000..182859b --- /dev/null +++ b/test/client.ts @@ -0,0 +1,19 @@ +import { Schema } from 'mongoose'; +import { Client } from '../lib'; +import { Event as EventBase } from '../lib/event.model'; +import connection from './utils/dbConnection'; + +interface Context { + message: string; +} + +export type Event = EventBase; + +const contextSchema = new Schema({ + message: String +}); + +const client = new Client(connection, contextSchema); + +export default client; + diff --git a/test/event.model.test.ts b/test/event.model.test.ts index d7a4601..b06b268 100644 --- a/test/event.model.test.ts +++ b/test/event.model.test.ts @@ -1,10 +1,11 @@ import { expect } from 'chai'; -import Model from './model'; -import connection from './utils/dbConnection'; +import client from './client'; + +const Model = client.Event; describe('Event model', () => { after(async () => { - connection.dropCollection('customevents'); + client.connection.dropCollection('events'); }); it('Should assign status and nextRunAt on creation', async () => { diff --git a/test/scheduler.test.ts b/test/scheduler.test.ts index ffcd623..1736ed4 100644 --- a/test/scheduler.test.ts +++ b/test/scheduler.test.ts @@ -1,12 +1,12 @@ import { expect } from 'chai'; import Scheduler from '../lib/scheduler'; -import Model, { CustomEvent } from './model'; -import connection from './utils/dbConnection'; +import client, { Event } from './client'; +const Model = client.Event; const sleep = async (time: number) => new Promise(res => setTimeout(res, time)); describe('Scheduler', async () => { - let event: CustomEvent; + let event: Event; let scheduler: Scheduler; beforeEach(() => { @@ -28,14 +28,14 @@ describe('Scheduler', async () => { }); after(async () => { - connection.dropCollection('customevents'); + client.connection.dropCollection('events'); }); it('Should run event', async () => { // Wait until job is run await new Promise(res => { - scheduler.registerHandler('test', async (event: CustomEvent) => { + scheduler.registerHandler('test', async (event: Event) => { await sleep(2000); await event.log(event.context.message) res(); -- cgit v1.2.3