neosqlite 1.0.16 → 1.0.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "neosqlite",
3
3
  "description": "A lightweight wrapper around better-sqlite3 that adds developer-friendly features like job scheduling, migrations, error handling, query logging, SQL utilities, and more",
4
- "version": "1.0.16",
4
+ "version": "1.0.17",
5
5
  "main": "lib/src/index.js",
6
6
  "scripts": {
7
7
  "build": "tsc",
@@ -21,7 +21,7 @@ export declare class NeosqliteJobs {
21
21
  /**
22
22
  * Register a job to be run
23
23
  */
24
- register<T extends Record<string, any> = Record<string, any>>(name: string, options: JobOptions, fn: (data: T) => void, onFailure?: (data: T, error: Error) => void): void;
24
+ register<T extends Record<string, any> = Record<string, any>>(name: string, options: JobOptions<T>): void;
25
25
  /**
26
26
  * Queue a job to run instantly based on its
27
27
  * priority
@@ -45,8 +45,8 @@ class NeosqliteJobs {
45
45
  /**
46
46
  * Register a job to be run
47
47
  */
48
- register(name, options, fn, onFailure) {
49
- this.jobsRegistry[name] = { name, options, fn, onFailure };
48
+ register(name, options) {
49
+ this.jobsRegistry[name] = { name, options: options };
50
50
  }
51
51
  /**
52
52
  * Queue a job to run instantly based on its
@@ -161,13 +161,14 @@ class NeosqliteJobs {
161
161
  const id = Number(row["id"]);
162
162
  const name = String(row["name"]);
163
163
  const attempts = Number(row["attempts"]);
164
+ const data = (0, util_1.Jsonify)(String(row["data"])) ?? {};
164
165
  const cron = row["cron"] ? String(row["cron"]) : null;
165
- const data = row["data"] ? (0, util_1.Jsonify)(String(row["data"])) : null;
166
166
  const job = this.jobsRegistry[name];
167
167
  if (!job)
168
168
  return;
169
+ const handler = job.options.handler(data);
169
170
  try {
170
- await Promise.resolve(job.fn(data));
171
+ await Promise.resolve(handler.onRun());
171
172
  this.db.write({
172
173
  sql: (0, util_1.queryString)("UPDATE " + this.jobsTable, "SET status = :status, updatedAt = CURRENT_TIMESTAMP", "WHERE id = :id"),
173
174
  args: { id, status: types_1.JobStatus.Completed },
@@ -194,8 +195,8 @@ class NeosqliteJobs {
194
195
  sql: (0, util_1.queryString)("UPDATE " + this.jobsTable, "SET status = :status, updatedAt = CURRENT_TIMESTAMP", "WHERE id = :id"),
195
196
  args: { id, status: updatedStatus },
196
197
  });
197
- if (updatedStatus === types_1.JobStatus.Failed && job.onFailure) {
198
- await Promise.resolve(job.onFailure(data, err));
198
+ if (updatedStatus === types_1.JobStatus.Failed && handler.onFailure) {
199
+ await Promise.resolve(handler.onFailure(err));
199
200
  }
200
201
  }
201
202
  }
@@ -77,7 +77,14 @@ export interface CreateJobsOptions {
77
77
  /** The maximum number of jobs to execute at once. Defaults to 10 */
78
78
  maxJobs?: number;
79
79
  }
80
- export interface JobOptions {
80
+ export interface JobOptions<T extends Record<string, any> = Record<string, any>> {
81
81
  /** The priority of the job */
82
82
  priority?: number;
83
+ /** The handler for the runner */
84
+ handler: (data: T) => {
85
+ /** The runner function for the job, receives the data passed when creating the job */
86
+ onRun: () => void;
87
+ /** The failure function for the job, receives the data passed when creating the job and the error that was thrown */
88
+ onFailure?: (error: Error) => void;
89
+ };
83
90
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "neosqlite",
3
3
  "description": "A lightweight wrapper around better-sqlite3 that adds developer-friendly features like job scheduling, migrations, error handling, query logging, SQL utilities, and more",
4
- "version": "1.0.16",
4
+ "version": "1.0.17",
5
5
  "main": "lib/src/index.js",
6
6
  "scripts": {
7
7
  "build": "tsc",
package/src/jobs/index.ts CHANGED
@@ -7,8 +7,6 @@ import { CreateJobsOptions, JobOptions, JobStatus, Row, NeosqliteClient } from "
7
7
  interface JobRegistryValue {
8
8
  name: string;
9
9
  options: JobOptions;
10
- fn: (data: any) => void;
11
- onFailure?: (data: any, error: any) => void;
12
10
  }
13
11
 
14
12
  export class NeosqliteJobs {
@@ -57,13 +55,8 @@ export class NeosqliteJobs {
57
55
  /**
58
56
  * Register a job to be run
59
57
  */
60
- public register<T extends Record<string, any> = Record<string, any>>(
61
- name: string,
62
- options: JobOptions,
63
- fn: (data: T) => void,
64
- onFailure?: (data: T, error: Error) => void,
65
- ) {
66
- this.jobsRegistry[name] = { name, options, fn, onFailure };
58
+ public register<T extends Record<string, any> = Record<string, any>>(name: string, options: JobOptions<T>) {
59
+ this.jobsRegistry[name] = { name, options: options as JobOptions };
67
60
  }
68
61
 
69
62
  /**
@@ -240,14 +233,16 @@ export class NeosqliteJobs {
240
233
  const name = String(row["name"]);
241
234
  const attempts = Number(row["attempts"]);
242
235
 
236
+ const data = Jsonify(String(row["data"])) ?? {};
243
237
  const cron = row["cron"] ? String(row["cron"]) : null;
244
- const data = row["data"] ? Jsonify(String(row["data"])) : null;
245
238
 
246
239
  const job = this.jobsRegistry[name];
247
240
  if (!job) return;
248
241
 
242
+ const handler = job.options.handler(data);
243
+
249
244
  try {
250
- await Promise.resolve(job.fn(data));
245
+ await Promise.resolve(handler.onRun());
251
246
 
252
247
  this.db.write({
253
248
  sql: queryString("UPDATE " + this.jobsTable, "SET status = :status, updatedAt = CURRENT_TIMESTAMP", "WHERE id = :id"),
@@ -282,8 +277,8 @@ export class NeosqliteJobs {
282
277
  args: { id, status: updatedStatus },
283
278
  });
284
279
 
285
- if (updatedStatus === JobStatus.Failed && job.onFailure) {
286
- await Promise.resolve(job.onFailure(data, err));
280
+ if (updatedStatus === JobStatus.Failed && handler.onFailure) {
281
+ await Promise.resolve(handler.onFailure(err as Error));
287
282
  }
288
283
  }
289
284
  }
package/src/types.ts CHANGED
@@ -104,7 +104,16 @@ export interface CreateJobsOptions {
104
104
  maxJobs?: number;
105
105
  }
106
106
 
107
- export interface JobOptions {
107
+ export interface JobOptions<T extends Record<string, any> = Record<string, any>> {
108
108
  /** The priority of the job */
109
109
  priority?: number;
110
+
111
+ /** The handler for the runner */
112
+ handler: (data: T) => {
113
+ /** The runner function for the job, receives the data passed when creating the job */
114
+ onRun: () => void;
115
+
116
+ /** The failure function for the job, receives the data passed when creating the job and the error that was thrown */
117
+ onFailure?: (error: Error) => void;
118
+ };
110
119
  }