neosqlite 1.0.15 → 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.15",
4
+ "version": "1.0.17",
5
5
  "main": "lib/src/index.js",
6
6
  "scripts": {
7
7
  "build": "tsc",
@@ -21,12 +21,15 @@ const runQuery = (config, params, fn) => {
21
21
  const result = fn();
22
22
  const endTime = process.hrtime(startTime);
23
23
  const endTimeMs = endTime[0] * 1_000 + endTime[1] / 1_000_000;
24
- if (config.onQueryComplete || config.onQueryLog) {
24
+ if (config.onQueryComplete || config.onQueryLog || config.onQueryTime) {
25
25
  const queryString = (0, exports.getFullQueryString)(params);
26
26
  config.onQueryComplete?.({ query: queryString, time: endTimeMs });
27
27
  if (typeof params === "object" && params.logQuery) {
28
28
  config.onQueryLog?.(queryString);
29
29
  }
30
+ if (typeof params === "object" && params.timeQuery) {
31
+ config.onQueryTime?.(endTimeMs);
32
+ }
30
33
  }
31
34
  return result;
32
35
  };
@@ -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
  }
@@ -4,6 +4,8 @@ export type NeosqliteConfig = {
4
4
  file: ":memory:" | string;
5
5
  /** When a query is logged using `logQuery: true`, you will receive the full query string via this callback */
6
6
  onQueryLog?: (query: string) => void;
7
+ /** When a query is timed using `timeQuery: true` you will receive the full query time via this callback (in ms) */
8
+ onQueryTime?: (time: number) => void;
7
9
  /** When a query finishes, gather data about the query from this callback */
8
10
  onQueryComplete?: (data: OnQueryFinishData) => void;
9
11
  };
@@ -39,6 +41,8 @@ export type OnQueryFinishData = {
39
41
  export type QueryParams = {
40
42
  /** The SQL query to execute */
41
43
  sql: string;
44
+ /** When true, logs the time that a query took to onQuery */
45
+ timeQuery?: boolean;
42
46
  /** When true, the query string will be sent to onQueryLog */
43
47
  logQuery?: boolean;
44
48
  /** The values to bind to the query string */
@@ -73,7 +77,14 @@ export interface CreateJobsOptions {
73
77
  /** The maximum number of jobs to execute at once. Defaults to 10 */
74
78
  maxJobs?: number;
75
79
  }
76
- export interface JobOptions {
80
+ export interface JobOptions<T extends Record<string, any> = Record<string, any>> {
77
81
  /** The priority of the job */
78
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
+ };
79
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.15",
4
+ "version": "1.0.17",
5
5
  "main": "lib/src/index.js",
6
6
  "scripts": {
7
7
  "build": "tsc",
@@ -25,7 +25,7 @@ export const runQuery = (config: NeosqliteConfig, params: ExecuteQueryParams, fn
25
25
  const endTime = process.hrtime(startTime);
26
26
  const endTimeMs = endTime[0] * 1_000 + endTime[1] / 1_000_000;
27
27
 
28
- if (config.onQueryComplete || config.onQueryLog) {
28
+ if (config.onQueryComplete || config.onQueryLog || config.onQueryTime) {
29
29
  const queryString = getFullQueryString(params);
30
30
 
31
31
  config.onQueryComplete?.({ query: queryString, time: endTimeMs });
@@ -33,6 +33,10 @@ export const runQuery = (config: NeosqliteConfig, params: ExecuteQueryParams, fn
33
33
  if (typeof params === "object" && params.logQuery) {
34
34
  config.onQueryLog?.(queryString);
35
35
  }
36
+
37
+ if (typeof params === "object" && params.timeQuery) {
38
+ config.onQueryTime?.(endTimeMs);
39
+ }
36
40
  }
37
41
 
38
42
  return result;
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
@@ -7,6 +7,9 @@ export type NeosqliteConfig = {
7
7
  /** When a query is logged using `logQuery: true`, you will receive the full query string via this callback */
8
8
  onQueryLog?: (query: string) => void;
9
9
 
10
+ /** When a query is timed using `timeQuery: true` you will receive the full query time via this callback (in ms) */
11
+ onQueryTime?: (time: number) => void;
12
+
10
13
  /** When a query finishes, gather data about the query from this callback */
11
14
  onQueryComplete?: (data: OnQueryFinishData) => void;
12
15
  };
@@ -52,6 +55,9 @@ export type QueryParams = {
52
55
  /** The SQL query to execute */
53
56
  sql: string;
54
57
 
58
+ /** When true, logs the time that a query took to onQuery */
59
+ timeQuery?: boolean;
60
+
55
61
  /** When true, the query string will be sent to onQueryLog */
56
62
  logQuery?: boolean;
57
63
 
@@ -98,7 +104,16 @@ export interface CreateJobsOptions {
98
104
  maxJobs?: number;
99
105
  }
100
106
 
101
- export interface JobOptions {
107
+ export interface JobOptions<T extends Record<string, any> = Record<string, any>> {
102
108
  /** The priority of the job */
103
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
+ };
104
119
  }