From 569cb0457b4cdcffdf325bdfe8959d55e48efc25 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Fri, 4 Dec 2020 01:12:54 +0300 Subject: feat!: remove context schema from client This will allow for multiple event types in one Model. --- lib/client.ts | 9 ++++----- lib/event.model.ts | 13 ++++++------- lib/event.schema.ts | 8 ++++---- package-lock.json | 2 +- package.json | 2 +- test/client.ts | 14 +------------- test/scheduler.test.ts | 10 +++++++++- 7 files changed, 26 insertions(+), 32 deletions(-) diff --git a/lib/client.ts b/lib/client.ts index eafdda5..15bd076 100644 --- a/lib/client.ts +++ b/lib/client.ts @@ -1,18 +1,17 @@ -import { Schema } from 'mongoose'; import Connection from './connection'; import createEventModel, { EventModel } from './event.model'; import createLogModel, { LogModel } from './log.model'; -class Client { +class Client { public connection: Connection; - public Event: EventModel; + public Event: EventModel; public Log: LogModel; - constructor(connection: Connection, contextSchema: Schema) { + constructor(connection: Connection) { this.connection = connection; this.Log = createLogModel(connection); - this.Event = createEventModel(connection, contextSchema); + this.Event = createEventModel(connection); } } diff --git a/lib/event.model.ts b/lib/event.model.ts index bea533d..665a35f 100644 --- a/lib/event.model.ts +++ b/lib/event.model.ts @@ -1,11 +1,11 @@ import { model, Schema, Model } from 'mongoose'; import cron from 'cron'; -import createEventSchema, { EventDocument } from './event.schema'; +import schema, { EventDocument } from './event.schema'; import { LogDocument } from './log.schema'; import { LogModel } from './log.model'; import Connection from './connection'; -export interface Event extends EventDocument { +export interface Event extends EventDocument { log(message: string): Promise; start(): void; complete(): void; @@ -14,15 +14,14 @@ export interface Event extends EventDocument { getLogs(): Promise; } -export interface EventModel extends Model> { +export interface EventModel extends Model> { findNextEvents(): Event[]; findMissedEvents(): Event[]; } const { CronJob } = cron; -const createEventModel = (connection: Connection, contextSchema: Schema): EventModel => { - const schema = createEventSchema(contextSchema); +const createEventModel = (connection: Connection): EventModel => { const LogModel: LogModel = connection.models['Log']; // Schema methods @@ -91,11 +90,11 @@ const createEventModel = (connection: Connection, contextSchema: Schema }); // Hooks - schema.pre>('save', async function () { + schema.pre('save', async function () { this.nextRunAt = this.computeNextRunAt(); }); - return connection.model, EventModel>('Event', schema); + return connection.model('Event', schema); }; diff --git a/lib/event.schema.ts b/lib/event.schema.ts index 8b9db8b..664b577 100644 --- a/lib/event.schema.ts +++ b/lib/event.schema.ts @@ -1,6 +1,6 @@ import { Schema, Document } from 'mongoose'; -export interface EventDocument extends Document { +export interface EventDocument extends Document { type: string; schedule: string; status?: 'notStarted' | 'running' | 'complete' | 'failed'; @@ -10,7 +10,7 @@ export interface EventDocument extends Document { lastRunAt?: Date; } -const createEventSchema = (contextSchema: Schema) => new Schema({ +const schema = new Schema({ type: { type: String, required: true @@ -24,11 +24,11 @@ const createEventSchema = (contextSchema: Schema) => new Schema({ default: 'notStarted' }, error: String, - context: contextSchema, + context: {}, nextRunAt: Date, lastRunAt: Date }, { timestamps: true }); -export default createEventSchema; +export default schema; diff --git a/package-lock.json b/package-lock.json index cdaa0f6..b7b6c1c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "mongo-cronjob", - "version": "0.1.1", + "version": "0.1.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 0465eed..d06b61b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mongo-cronjob", - "version": "0.1.1", + "version": "0.1.2", "description": "Cron-based job scheduler integrated with mongoDB", "main": "dist/index.js", "scripts": { diff --git a/test/client.ts b/test/client.ts index 182859b..5f96339 100644 --- a/test/client.ts +++ b/test/client.ts @@ -1,19 +1,7 @@ -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); +const client = new Client(connection); export default client; diff --git a/test/scheduler.test.ts b/test/scheduler.test.ts index 1736ed4..c341cc8 100644 --- a/test/scheduler.test.ts +++ b/test/scheduler.test.ts @@ -1,6 +1,14 @@ import { expect } from 'chai'; import Scheduler from '../lib/scheduler'; -import client, { Event } from './client'; +import { Event as EventBase } from '../lib/event.model'; +import client from './client'; + +interface Context { + message: string; +} + +type Event = EventBase; + const Model = client.Event; const sleep = async (time: number) => new Promise(res => setTimeout(res, time)); -- cgit v1.2.3