aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eug-vs@keemail.me>2020-12-04 01:12:54 +0300
committereug-vs <eug-vs@keemail.me>2020-12-04 01:14:12 +0300
commit569cb0457b4cdcffdf325bdfe8959d55e48efc25 (patch)
treeedc092e392ef9ec619964a917017a9d40ad6f659
parentd513b23be26bda40d40ec43258345af6e34cfdfd (diff)
downloadmongo-cronjob-569cb0457b4cdcffdf325bdfe8959d55e48efc25.tar.gz
feat!: remove context schema from client
This will allow for multiple event types in one Model.
-rw-r--r--lib/client.ts9
-rw-r--r--lib/event.model.ts13
-rw-r--r--lib/event.schema.ts8
-rw-r--r--package-lock.json2
-rw-r--r--package.json2
-rw-r--r--test/client.ts14
-rw-r--r--test/scheduler.test.ts10
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<Context> {
+class Client {
public connection: Connection;
- public Event: EventModel<Context>;
+ public Event: EventModel;
public Log: LogModel;
- constructor(connection: Connection, contextSchema: Schema) {
+ constructor(connection: Connection) {
this.connection = connection;
this.Log = createLogModel(connection);
- this.Event = createEventModel<Context>(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<Context> extends EventDocument<Context> {
+export interface Event<Context = any> extends EventDocument<Context> {
log(message: string): Promise<LogDocument>;
start(): void;
complete(): void;
@@ -14,15 +14,14 @@ export interface Event<Context> extends EventDocument<Context> {
getLogs(): Promise<LogDocument[]>;
}
-export interface EventModel<Context> extends Model<Event<Context>> {
+export interface EventModel<Context = any> extends Model<Event<Context>> {
findNextEvents(): Event<Context>[];
findMissedEvents(): Event<Context>[];
}
const { CronJob } = cron;
-const createEventModel = <Context>(connection: Connection, contextSchema: Schema): EventModel<Context> => {
- const schema = createEventSchema(contextSchema);
+const createEventModel = (connection: Connection): EventModel => {
const LogModel: LogModel = connection.models['Log'];
// Schema methods
@@ -91,11 +90,11 @@ const createEventModel = <Context>(connection: Connection, contextSchema: Schema
});
// Hooks
- schema.pre<Event<Context>>('save', async function () {
+ schema.pre<Event>('save', async function () {
this.nextRunAt = this.computeNextRunAt();
});
- return connection.model<Event<Context>, EventModel<Context>>('Event', schema);
+ return connection.model<Event, EventModel>('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<Context> extends Document {
+export interface EventDocument<Context = any> extends Document {
type: string;
schedule: string;
status?: 'notStarted' | 'running' | 'complete' | 'failed';
@@ -10,7 +10,7 @@ export interface EventDocument<Context> 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<Context>;
-
-const contextSchema = new Schema({
- message: String
-});
-
-const client = new Client<Context>(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<Context>;
+
const Model = client.Event;
const sleep = async (time: number) => new Promise<void>(res => setTimeout(res, time));