neosqlite 1.0.13 → 1.0.15

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.13",
4
+ "version": "1.0.15",
5
5
  "main": "lib/src/index.js",
6
6
  "scripts": {
7
7
  "build": "tsc",
@@ -16,7 +16,7 @@ export declare const runQuery: (config: NeosqliteConfig, params: ExecuteQueryPar
16
16
  * // Returns
17
17
  * "SELECT * FROM users WHERE id = :id"
18
18
  */
19
- export declare const queryString: (...args: (string | boolean | undefined)[]) => string;
19
+ export declare const queryString: (...args: (string | boolean | undefined | null)[]) => string;
20
20
  /**
21
21
  * Take a list and return an object with its sql placeholder names, and
22
22
  * its prepared arguments
@@ -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 OR IGNORE INTO " + this.jobsTable, "(name, data, runAt, priority, cron)", "VALUES (:name, :data, :runAt, :priority, :cron)"),
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/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.13",
4
+ "version": "1.0.15",
5
5
  "main": "lib/src/index.js",
6
6
  "scripts": {
7
7
  "build": "tsc",
@@ -50,7 +50,7 @@ export const runQuery = (config: NeosqliteConfig, params: ExecuteQueryParams, fn
50
50
  * // Returns
51
51
  * "SELECT * FROM users WHERE id = :id"
52
52
  */
53
- export const queryString = (...args: (string | boolean | undefined)[]) => {
53
+ export const queryString = (...args: (string | boolean | undefined | null)[]) => {
54
54
  const filteredArgs = args.filter(Boolean);
55
55
  return filteredArgs.join(NEWLINE_CHAR);
56
56
  };
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 OR IGNORE INTO " + this.jobsTable,
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
  );