neosqlite 1.0.14 → 1.0.16
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 +1 -1
- package/lib/src/client/util.js +4 -1
- package/lib/src/jobs/index.js +4 -1
- package/lib/src/types.d.ts +4 -0
- package/package.json +1 -1
- package/src/client/util.ts +5 -1
- package/src/jobs/index.ts +9 -2
- package/src/types.ts +6 -0
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.
|
|
4
|
+
"version": "1.0.16",
|
|
5
5
|
"main": "lib/src/index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"build": "tsc",
|
package/lib/src/client/util.js
CHANGED
|
@@ -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
|
};
|
package/lib/src/jobs/index.js
CHANGED
|
@@ -96,7 +96,7 @@ class NeosqliteJobs {
|
|
|
96
96
|
const interval = cron_parser_1.default.parse(cronString);
|
|
97
97
|
const runAt = interval.next().toDate();
|
|
98
98
|
this.db.write({
|
|
99
|
-
sql: (0, util_1.queryString)("INSERT
|
|
99
|
+
sql: (0, util_1.queryString)("INSERT INTO " + this.jobsTable + " (name, data, runAt, priority, cron)", "VALUES (:name, :data, :runAt, :priority, :cron)", "ON CONFLICT DO UPDATE SET cron = :cron, data = :data, priority = :priority, runAt = :runAt"),
|
|
100
100
|
args: {
|
|
101
101
|
name: job.name,
|
|
102
102
|
priority: job.options.priority ?? 0,
|
|
@@ -116,6 +116,9 @@ class NeosqliteJobs {
|
|
|
116
116
|
this.db.write((0, util_1.queryString)("CREATE TABLE IF NOT EXISTS " + this.jobsTable + " (", " id INTEGER PRIMARY KEY AUTOINCREMENT,", " name TEXT NOT NULL,", " data TEXT,", " runAt DATETIME NOT NULL,", " priority INTEGER DEFAULT 0,", " cron TEXT,", " attempts INTEGER DEFAULT 0,", " status TEXT CHECK (status IN ( " + jobStatuses + ")) DEFAULT '" + types_1.JobStatus.Pending + "',", " updatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,", " createdAt DATETIME DEFAULT CURRENT_TIMESTAMP", ")"));
|
|
117
117
|
// Prevent multiple cron jobs from getting scheduled for the same dates
|
|
118
118
|
this.db.write(`CREATE UNIQUE INDEX IF NOT EXISTS idx_${this.jobsTable}_name_cron_runAt ON ${this.jobsTable} (name, cron, runAt)`);
|
|
119
|
+
// Only allow cron jobs to have one scheduled run at a time, so that updating a crons syntax is easier, and immediately updates
|
|
120
|
+
this.db.write(`CREATE UNIQUE INDEX IF NOT EXISTS idx_${this.jobsTable}_name_cron_status ON ${this.jobsTable} (name, cron, status) WHERE cron IS NOT NULL AND status IN ('${types_1.JobStatus.Pending}');`);
|
|
121
|
+
// Allow faster querying of pending jobs
|
|
119
122
|
this.db.write(`CREATE INDEX IF NOT EXISTS idx_${this.jobsTable}_status_runAt_priority ON ${this.jobsTable} (status, runAt, priority DESC)`);
|
|
120
123
|
}
|
|
121
124
|
/**
|
package/lib/src/types.d.ts
CHANGED
|
@@ -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 */
|
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.
|
|
4
|
+
"version": "1.0.16",
|
|
5
5
|
"main": "lib/src/index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"build": "tsc",
|
package/src/client/util.ts
CHANGED
|
@@ -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
|
@@ -126,9 +126,9 @@ export class NeosqliteJobs {
|
|
|
126
126
|
|
|
127
127
|
this.db.write({
|
|
128
128
|
sql: queryString(
|
|
129
|
-
"INSERT
|
|
130
|
-
"(name, data, runAt, priority, cron)",
|
|
129
|
+
"INSERT INTO " + this.jobsTable + " (name, data, runAt, priority, cron)",
|
|
131
130
|
"VALUES (:name, :data, :runAt, :priority, :cron)",
|
|
131
|
+
"ON CONFLICT DO UPDATE SET cron = :cron, data = :data, priority = :priority, runAt = :runAt",
|
|
132
132
|
),
|
|
133
133
|
args: {
|
|
134
134
|
name: job.name,
|
|
@@ -169,6 +169,13 @@ export class NeosqliteJobs {
|
|
|
169
169
|
this.db.write(
|
|
170
170
|
`CREATE UNIQUE INDEX IF NOT EXISTS idx_${this.jobsTable}_name_cron_runAt ON ${this.jobsTable} (name, cron, runAt)`,
|
|
171
171
|
);
|
|
172
|
+
|
|
173
|
+
// Only allow cron jobs to have one scheduled run at a time, so that updating a crons syntax is easier, and immediately updates
|
|
174
|
+
this.db.write(
|
|
175
|
+
`CREATE UNIQUE INDEX IF NOT EXISTS idx_${this.jobsTable}_name_cron_status ON ${this.jobsTable} (name, cron, status) WHERE cron IS NOT NULL AND status IN ('${JobStatus.Pending}');`,
|
|
176
|
+
);
|
|
177
|
+
|
|
178
|
+
// Allow faster querying of pending jobs
|
|
172
179
|
this.db.write(
|
|
173
180
|
`CREATE INDEX IF NOT EXISTS idx_${this.jobsTable}_status_runAt_priority ON ${this.jobsTable} (status, runAt, priority DESC)`,
|
|
174
181
|
);
|
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
|
|