neosqlite 1.0.0 → 1.0.2

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.0",
4
+ "version": "1.0.2",
5
5
  "main": "lib/src/index.js",
6
6
  "scripts": {
7
7
  "build": "tsc",
@@ -10,9 +10,11 @@ function createClient(config) {
10
10
  const { file } = config;
11
11
  const db = new better_sqlite3_1.default(file);
12
12
  const transaction = (fn) => {
13
+ let result;
13
14
  db.transaction(() => {
14
- fn();
15
+ result = fn();
15
16
  });
17
+ return result;
16
18
  };
17
19
  /**
18
20
  * Read data from the database, returns all matching rows
@@ -50,10 +52,23 @@ function createClient(config) {
50
52
  const statement = db.prepare(query.sql);
51
53
  return (0, util_1.runQuery)(config, params, () => statement.all(query.args));
52
54
  };
55
+ /**
56
+ * Batch writes to the database
57
+ */
58
+ const batchWrite = (statements) => {
59
+ db.transaction(() => {
60
+ for (const statement of statements) {
61
+ const query = (0, util_1.parseQuery)(statement);
62
+ const sqlStatement = db.prepare(query.sql);
63
+ sqlStatement.run(query.args);
64
+ }
65
+ });
66
+ };
53
67
  return {
54
68
  read,
55
69
  readOne,
56
70
  write,
71
+ batchWrite,
57
72
  transaction,
58
73
  writeReturning,
59
74
  pragma: db.pragma,
@@ -1 +1,4 @@
1
1
  export * from "./types";
2
+ export * from "./client/util";
3
+ export { createClient } from "./client";
4
+ export { createNeosqliteMigrationCli } from "./migrations";
package/lib/src/index.js CHANGED
@@ -14,4 +14,10 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.createNeosqliteMigrationCli = exports.createClient = void 0;
17
18
  __exportStar(require("./types"), exports);
19
+ __exportStar(require("./client/util"), exports);
20
+ var client_1 = require("./client");
21
+ Object.defineProperty(exports, "createClient", { enumerable: true, get: function () { return client_1.createClient; } });
22
+ var migrations_1 = require("./migrations");
23
+ Object.defineProperty(exports, "createNeosqliteMigrationCli", { enumerable: true, get: function () { return migrations_1.createNeosqliteMigrationCli; } });
@@ -13,13 +13,15 @@ export type NeosqliteClient = {
13
13
  /** Backup the database */
14
14
  backup: (destinationFile: string, options?: BackupOptions | undefined) => Promise<BackupMetadata>;
15
15
  /** Create a transaction and execute queries within it */
16
- transaction: (fn: Function) => void;
16
+ transaction: <T = void>(fn: () => T) => T;
17
17
  /** Read data from the database, returns all matching rows */
18
18
  read: (params: ExecuteQueryParams) => Row[];
19
19
  /** Read data from the database, returns the first matching row */
20
20
  readOne: (params: ExecuteQueryParams) => Row | undefined;
21
21
  /** Write data to the database, returns all matching rows via RETURNING */
22
22
  writeReturning: (params: ExecuteQueryParams) => Row[];
23
+ /** Batch writes by running all of them at the same time, in one transaction */
24
+ batchWrite: (statements: ExecuteQueryParams[]) => void;
23
25
  /** Write data to the database, returns the number of rows affected */
24
26
  write: (params: ExecuteQueryParams) => {
25
27
  changes: number;
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.0",
4
+ "version": "1.0.2",
5
5
  "main": "lib/src/index.js",
6
6
  "scripts": {
7
7
  "build": "tsc",
@@ -8,10 +8,14 @@ export function createClient(config: NeosqliteConfig): NeosqliteClient {
8
8
  const { file } = config;
9
9
  const db = new Database(file);
10
10
 
11
- const transaction = (fn: Function) => {
11
+ const transaction = <T = void>(fn: () => T): T => {
12
+ let result!: T;
13
+
12
14
  db.transaction(() => {
13
- fn();
15
+ result = fn();
14
16
  });
17
+
18
+ return result;
15
19
  };
16
20
 
17
21
  /**
@@ -58,10 +62,24 @@ export function createClient(config: NeosqliteConfig): NeosqliteClient {
58
62
  return runQuery(config, params, () => statement.all(query.args));
59
63
  };
60
64
 
65
+ /**
66
+ * Batch writes to the database
67
+ */
68
+ const batchWrite = (statements: ExecuteQueryParams[]) => {
69
+ db.transaction(() => {
70
+ for (const statement of statements) {
71
+ const query = parseQuery(statement);
72
+ const sqlStatement = db.prepare(query.sql);
73
+ sqlStatement.run(query.args);
74
+ }
75
+ });
76
+ };
77
+
61
78
  return {
62
79
  read,
63
80
  readOne,
64
81
  write,
82
+ batchWrite,
65
83
  transaction,
66
84
  writeReturning,
67
85
  pragma: db.pragma,
package/src/index.ts CHANGED
@@ -1 +1,5 @@
1
1
  export * from "./types";
2
+
3
+ export * from "./client/util";
4
+ export { createClient } from "./client";
5
+ export { createNeosqliteMigrationCli } from "./migrations";
package/src/types.ts CHANGED
@@ -19,7 +19,7 @@ export type NeosqliteClient = {
19
19
  backup: (destinationFile: string, options?: BackupOptions | undefined) => Promise<BackupMetadata>;
20
20
 
21
21
  /** Create a transaction and execute queries within it */
22
- transaction: (fn: Function) => void;
22
+ transaction: <T = void>(fn: () => T) => T;
23
23
 
24
24
  /** Read data from the database, returns all matching rows */
25
25
  read: (params: ExecuteQueryParams) => Row[];
@@ -30,6 +30,9 @@ export type NeosqliteClient = {
30
30
  /** Write data to the database, returns all matching rows via RETURNING */
31
31
  writeReturning: (params: ExecuteQueryParams) => Row[];
32
32
 
33
+ /** Batch writes by running all of them at the same time, in one transaction */
34
+ batchWrite: (statements: ExecuteQueryParams[]) => void;
35
+
33
36
  /** Write data to the database, returns the number of rows affected */
34
37
  write: (params: ExecuteQueryParams) => { changes: number; lastInsertedRowid: number };
35
38
  };