aboutsummaryrefslogtreecommitdiff
path: root/lib
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 /lib
parentd513b23be26bda40d40ec43258345af6e34cfdfd (diff)
downloadmongo-cronjob-569cb0457b4cdcffdf325bdfe8959d55e48efc25.tar.gz
feat!: remove context schema from client
This will allow for multiple event types in one Model.
Diffstat (limited to 'lib')
-rw-r--r--lib/client.ts9
-rw-r--r--lib/event.model.ts13
-rw-r--r--lib/event.schema.ts8
3 files changed, 14 insertions, 16 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;