aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eug-vs@keemail.me>2020-12-02 15:26:25 +0300
committereug-vs <eug-vs@keemail.me>2020-12-02 15:26:25 +0300
commitf8ebf581f8272615f48ce355c6b2cbf8248c73bd (patch)
tree9473834f4045a9375dc0db48f042f4a6df0e016c
parent994e0df96267b3de4d82926555ac4c4c7eefe14a (diff)
downloadmongo-cronjob-f8ebf581f8272615f48ce355c6b2cbf8248c73bd.tar.gz
feat: export client class
-rw-r--r--lib/client.ts21
-rw-r--r--lib/connection.ts12
-rw-r--r--lib/event.model.ts8
-rw-r--r--lib/index.ts3
-rw-r--r--lib/log.model.ts9
-rw-r--r--test/client.ts19
-rw-r--r--test/event.model.test.ts7
-rw-r--r--test/scheduler.test.ts10
8 files changed, 74 insertions, 15 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;
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<Context>;
+
+const contextSchema = new Schema({
+ message: String
+});
+
+const client = new Client<Context>(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<void>(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<void>(res => {
- scheduler.registerHandler('test', async (event: CustomEvent) => {
+ scheduler.registerHandler('test', async (event: Event) => {
await sleep(2000);
await event.log(event.context.message)
res();