Skip to content

Commit

Permalink
fix: change the module export style
Browse files Browse the repository at this point in the history
BREAKING CHANGE: all modules will be exported from the main entrypoint
  • Loading branch information
yujiosaka committed Oct 24, 2023
1 parent 87a37b7 commit e68074b
Show file tree
Hide file tree
Showing 32 changed files with 141 additions and 153 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ $ npm install cronyx
Here's how you can schedule an hourly task with Cronyx and manage the job's lifecycle manually:

```ts
import Cronyx from "cronyx";
// MysqlJobStore, PostgresJobStore and RedisJobStore are also available out of the box
import { MongodbJobStore } from "cronyx/job-store";
import Cronyx, { MongodbJobStore } from "cronyx";

const jobStore = await MongodbJobStore.connect("mongodb://mongo:27017/db");
const cronyx = new Cronyx({ jobStore });
Expand Down
3 changes: 1 addition & 2 deletions examples/basic/automatic-job-management.ts
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env bun
import Cronyx from "../../src";
import { MongodbJobStore } from "../../src/job-store";
import Cronyx, { MongodbJobStore } from "../../src";

const jobStore = await MongodbJobStore.connect(Bun.env.MONGO_URI!);
const cronyx = new Cronyx({ jobStore });
Expand Down
3 changes: 1 addition & 2 deletions examples/basic/manual-job-management.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env bun
import Cronyx from "../../src";
import { MongodbJobStore } from "../../src/job-store";
import Cronyx, { MongodbJobStore } from "../../src";

const jobStore = await MongodbJobStore.connect(Bun.env.MONGO_URI!);
const cronyx = new Cronyx({ jobStore });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env bun
import Cronyx from "../../src";
import { MongodbJobStore } from "../../src/job-store";
import Cronyx, { MongodbJobStore } from "../../src";

const jobStore = await MongodbJobStore.connect(Bun.env.MONGO_URI!);
const cronyx = new Cronyx({ jobStore });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env bun
import Cronyx from "../../src";
import { MongodbJobStore } from "../../src/job-store";
import Cronyx, { MongodbJobStore } from "../../src";

const jobStore = await MongodbJobStore.connect(Bun.env.MONGO_URI!);
const cronyx = new Cronyx({ jobStore });
Expand Down
5 changes: 2 additions & 3 deletions examples/job-stores/custom-job-store.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#!/usr/bin/env bun
import { isEqual } from "date-fns";
import { v4 } from "uuid";
import Cronyx from "../../src";
import { BaseJobLock } from "../../src/job-lock";
import { BaseJobStore } from "../../src/job-store";
import Cronyx, { BaseJobLock } from "../../src";
import type BaseJobStore from "../../src/job-store";

// Create a new cache by extending BaseJobStore interface
export default class MemoryJobStore implements BaseJobStore<string> {
Expand Down
3 changes: 1 addition & 2 deletions examples/job-stores/mongodb-job-store.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env bun
import Cronyx from "../../src";
import { MongodbJobStore } from "../../src/job-store";
import Cronyx, { MongodbJobStore } from "../../src";

const jobStore = await MongodbJobStore.connect(Bun.env.MONGO_URI!);
const cronyx = new Cronyx({ jobStore });
Expand Down
3 changes: 1 addition & 2 deletions examples/job-stores/mysql-job-store.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env bun
import Cronyx from "../../src";
import { MysqlJobStore } from "../../src/job-store";
import Cronyx, { MysqlJobStore } from "../../src";

const jobStore = await MysqlJobStore.connect({ type: "mysql", url: Bun.env.MYSQL_URI });
const cronyx = new Cronyx({ jobStore });
Expand Down
3 changes: 1 addition & 2 deletions examples/job-stores/postgres-job-store.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env bun
import Cronyx from "../../src";
import { PostgresJobStore } from "../../src/job-store";
import Cronyx, { PostgresJobStore } from "../../src";

const jobStore = await PostgresJobStore.connect({ type: "postgres", url: Bun.env.POSTGRES_URI });
const cronyx = new Cronyx({ jobStore });
Expand Down
3 changes: 1 addition & 2 deletions examples/job-stores/redis-job-store.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env bun
import Cronyx from "../../src";
import { RedisJobStore } from "../../src/job-store";
import Cronyx, { RedisJobStore } from "../../src";

const jobStore = await RedisJobStore.connect({ url: Bun.env.REDIS_URI });
const cronyx = new Cronyx({ jobStore });
Expand Down
6 changes: 0 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@
"module": "dist/index.ts",
"types": "dist/index.d.ts",
"main": "dist/index.js",
"exports": {
".": "./dist/index.js",
"./job-store": "./dist/job-store/index.js",
"./job-lock": "./dist/job-lock/index.js",
"./*": "./dist/*.js"
},
"scripts": {
"build": "tsc -p tsconfig.build.json",
"format": "prettier **/*.{md,ts,cts,json} -w",
Expand Down
62 changes: 59 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,63 @@
import type { Duration } from "date-fns";
import type Job from "./job";
import type { JobLockId } from "./job-lock";
import JobRunner from "./job-runner";
import type BaseJobStore from "./job-store/base";
import type BaseJobStore from "./job-store";

/**
* @public
*/
export type { default as BaseJobLock, JobLockId } from "./job-lock";

/**
* @public
*/
export type { default as MongodbJobLock } from "./job-lock/mongodb";

/**
* @public
*/
export type { default as RedisJobLock } from "./job-lock/redis";

/**
* @public
*/
export type { default as TypeormJobLock } from "./job-lock/typeorm";

/**
* @public
*/
export type { default as BaseJobStore } from "./job-store";

/**
* @public
*/
export { default as MongodbJobStore } from "./job-store/mongodb";

/**
* @public
*/
export { default as RedisJobStore } from "./job-store/redis";

/**
* @public
*/
export { default as MysqlJobStore } from "./job-store/typeorm/mysql";

/**
* @public
*/
export { default as PostgresJobStore } from "./job-store/typeorm/postgres";

/**
* @public
*/
export { default as Job } from "./job";

/**
* @public
*/
export { CronyxError } from "./error";

/**
* @public
Expand All @@ -17,7 +73,7 @@ export enum Source {
* @public
*/
export type CronyxOptions<S extends Source> = {
jobStore: BaseJobStore<S>;
jobStore: BaseJobStore<JobLockId<S>>;
timezone?: string;
};

Expand All @@ -40,7 +96,7 @@ export type RequestJobOptions =
* @public
*/
export default class Cronyx<S extends Source> {
#jobStore: BaseJobStore<S>;
#jobStore: BaseJobStore<JobLockId<S>>;
#timezone: string | undefined;

constructor(options: CronyxOptions<S>) {
Expand Down
28 changes: 0 additions & 28 deletions src/job-lock/base.ts

This file was deleted.

31 changes: 20 additions & 11 deletions src/job-lock/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
/**
* @public
*/
export type { default as BaseJobLock, JobLockId } from "./base";

/**
* @public
*/
export { default as MongodbJobLock } from "./mongodb";
import type { Types } from "mongoose";
import type { Source } from "..";

/**
* @public
*/
export { default as RedisJobLock } from "./redis";
export type JobLockId<S extends Source> = S extends Source.Mongodb
? Types.ObjectId
: S extends Source.Redis
? string
: S extends Source.Mysql
? string
: S extends Source.Postgres
? string
: never;

/**
* @public
*/
export { default as TypeormJobLock } from "./typeorm";
export default interface BaseJobLock<T> {
_id: T | null;
jobName: string;
jobInterval: number;
jobIntervalEndedAt: Date;
isActive: boolean;
createdAt: Date;
updatedAt: Date;
}
2 changes: 1 addition & 1 deletion src/job-lock/mock.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { z } from "zod";
import type BaseJobLock from ".";
import type { Optional } from "../util";
import type BaseJobLock from "./base";

type MockJobLockCreationAttributes = Optional<
BaseJobLock<null>,
Expand Down
2 changes: 1 addition & 1 deletion src/job-lock/mongodb.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Schema } from "mongoose";
import type { Types } from "mongoose";
import type BaseJobLock from "./base";
import type BaseJobLock from ".";

/**
* @public
Expand Down
2 changes: 1 addition & 1 deletion src/job-lock/redis.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { v4 } from "uuid";
import { z } from "zod";
import type BaseJobLock from ".";
import type { Optional } from "../util";
import type BaseJobLock from "./base";

type RedisJobLockCreationAttributes = Optional<
BaseJobLock<string>,
Expand Down
2 changes: 1 addition & 1 deletion src/job-lock/typeorm.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Column, CreateDateColumn, Entity, Index, PrimaryGeneratedColumn, UpdateDateColumn } from "typeorm";
import type BaseJobLock from "./base";
import type BaseJobLock from ".";

/**
* @public
Expand Down
10 changes: 5 additions & 5 deletions src/job-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { differenceInMilliseconds } from "date-fns";
import type { Source } from ".";
import { CronyxError } from "./error";
import Job from "./job";
import type BaseJobLock from "./job-lock/base";
import type { JobLockId } from "./job-lock/base";
import type BaseJobLock from "./job-lock";
import type { JobLockId } from "./job-lock";
import MockJobLock from "./job-lock/mock";
import type BaseJobStore from "./job-store/base";
import type BaseJobStore from "./job-store";
import { addInterval, getLastDeactivatedJobIntervalEndedAt, log, subInterval } from "./util";

type JobRunnerOptions = {
Expand All @@ -22,7 +22,7 @@ type JobRunnerOptions = {
* @internal
*/
export default class JobRunner<S extends Source> {
#jobStore: BaseJobStore<S>;
#jobStore: BaseJobStore<JobLockId<S>>;
#timezone: string;
#jobName: string;
#jobInterval: Duration | string | number;
Expand All @@ -33,7 +33,7 @@ export default class JobRunner<S extends Source> {
#jobIntervalStartedAt: Date | undefined;

constructor(
jobStore: BaseJobStore<S>,
jobStore: BaseJobStore<JobLockId<S>>,
jobName: string,
jobInterval: Duration | string | number,
options?: JobRunnerOptions,
Expand Down
19 changes: 0 additions & 19 deletions src/job-store/base.ts

This file was deleted.

33 changes: 13 additions & 20 deletions src/job-store/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
/**
* @public
*/
export type { default as BaseJobStore } from "./base";

/**
* @public
*/
export { default as MongodbJobStore } from "./mongodb";

/**
* @public
*/
export { default as RedisJobStore } from "./redis";

/**
* @public
*/
export { default as MysqlJobStore } from "./typeorm/mysql";
import type BaseJobLock from "../job-lock";

/**
* @public
*/
export { default as PostgresJobStore } from "./typeorm/postgres";
export default interface BaseJobStore<T> {
close(): Promise<void>;
fetchLastJobLock(jobName: string): Promise<BaseJobLock<T> | null>;
activateJobLock(
jobName: string,
jobInterval: number,
jobIntervalEndedAt: Date,
retryIntervalStartedAt: Date,
): Promise<BaseJobLock<T> | null>;
deactivateJobLock(jobName: string, jobId: T): Promise<BaseJobLock<T>>;
removeJobLock(jobName: string, jobId: T): Promise<void>;
}
5 changes: 2 additions & 3 deletions src/job-store/mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import { MongoError } from "mongodb";
import type { MongoClientOptions } from "mongodb";
import { createConnection } from "mongoose";
import type { Connection, Model, Types } from "mongoose";
import type { Source } from "..";
import type BaseJobStore from ".";
import type MongodbJobLock from "../job-lock/mongodb";
import { mongodbJobLockSchema } from "../job-lock/mongodb";
import type BaseJobStore from "./base";

/**
* @public
*/
export default class MongodbJobStore implements BaseJobStore<Source.Mongodb> {
export default class MongodbJobStore implements BaseJobStore<Types.ObjectId> {
#conn: Connection;
#model: Model<MongodbJobLock>;

Expand Down
Loading

0 comments on commit e68074b

Please sign in to comment.