@iamkirbki/database-handler-core 3.1.4 → 4.0.0

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.
Files changed (71) hide show
  1. package/dist/abstract/Model.d.ts +39 -16
  2. package/dist/abstract/Model.d.ts.map +1 -1
  3. package/dist/abstract/Model.js +148 -36
  4. package/dist/abstract/{Schema.d.ts → SchemaTableBuilder.d.ts} +3 -8
  5. package/dist/abstract/SchemaTableBuilder.d.ts.map +1 -0
  6. package/dist/abstract/{Schema.js → SchemaTableBuilder.js} +1 -3
  7. package/dist/base/Database.d.ts +13 -0
  8. package/dist/base/Database.d.ts.map +1 -0
  9. package/dist/base/Database.js +27 -0
  10. package/dist/base/Query.d.ts +43 -0
  11. package/dist/base/Query.d.ts.map +1 -0
  12. package/dist/base/Query.js +87 -0
  13. package/dist/base/Record.d.ts +23 -0
  14. package/dist/base/Record.d.ts.map +1 -0
  15. package/dist/base/Record.js +72 -0
  16. package/dist/base/Table.d.ts +27 -0
  17. package/dist/base/Table.d.ts.map +1 -0
  18. package/dist/base/Table.js +128 -0
  19. package/dist/helpers/QueryStatementBuilder.d.ts +4 -35
  20. package/dist/helpers/QueryStatementBuilder.d.ts.map +1 -1
  21. package/dist/helpers/QueryStatementBuilder.js +6 -37
  22. package/dist/index.d.ts +13 -9
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +8 -8
  25. package/dist/interfaces/IController.d.ts +8 -0
  26. package/dist/interfaces/IController.d.ts.map +1 -0
  27. package/dist/interfaces/IController.js +1 -0
  28. package/dist/interfaces/IDatabaseAdapter.d.ts.map +1 -1
  29. package/dist/interfaces/IMigration.d.ts +6 -0
  30. package/dist/interfaces/IMigration.d.ts.map +1 -0
  31. package/dist/interfaces/IMigration.js +1 -0
  32. package/dist/interfaces/ISchemaBuilder.d.ts +7 -0
  33. package/dist/interfaces/ISchemaBuilder.d.ts.map +1 -0
  34. package/dist/interfaces/ISchemaBuilder.js +1 -0
  35. package/dist/runtime/Container.d.ts +12 -0
  36. package/dist/runtime/Container.d.ts.map +1 -0
  37. package/dist/runtime/Container.js +29 -0
  38. package/dist/runtime/Repository.d.ts +17 -0
  39. package/dist/runtime/Repository.d.ts.map +1 -0
  40. package/dist/runtime/Repository.js +101 -0
  41. package/dist/types/index.d.ts +1 -4
  42. package/dist/types/index.d.ts.map +1 -1
  43. package/dist/types/index.js +1 -0
  44. package/dist/types/model.d.ts +71 -0
  45. package/dist/types/model.d.ts.map +1 -0
  46. package/dist/types/model.js +2 -0
  47. package/dist/types/table.d.ts +6 -2
  48. package/dist/types/table.d.ts.map +1 -1
  49. package/package.json +5 -4
  50. package/dist/Database.d.ts +0 -98
  51. package/dist/Database.d.ts.map +0 -1
  52. package/dist/Database.js +0 -126
  53. package/dist/Query.d.ts +0 -143
  54. package/dist/Query.d.ts.map +0 -1
  55. package/dist/Query.js +0 -205
  56. package/dist/Record.d.ts +0 -125
  57. package/dist/Record.d.ts.map +0 -1
  58. package/dist/Record.js +0 -174
  59. package/dist/Table.d.ts +0 -158
  60. package/dist/Table.d.ts.map +0 -1
  61. package/dist/Table.js +0 -258
  62. package/dist/abstract/Controller.d.ts +0 -13
  63. package/dist/abstract/Controller.d.ts.map +0 -1
  64. package/dist/abstract/Controller.js +0 -6
  65. package/dist/abstract/Migration.d.ts +0 -6
  66. package/dist/abstract/Migration.d.ts.map +0 -1
  67. package/dist/abstract/Migration.js +0 -2
  68. package/dist/abstract/Schema.d.ts.map +0 -1
  69. package/dist/abstract/User.d.ts +0 -8
  70. package/dist/abstract/User.d.ts.map +0 -1
  71. package/dist/abstract/User.js +0 -6
@@ -0,0 +1 @@
1
+ {"version":3,"file":"model.d.ts","sourceRoot":"","sources":["../../src/types/model.ts"],"names":[],"mappings":"AAAA,oCAAoC;AAEpC,OAAO,KAAK,MAAM,sBAAsB,CAAC;AACzC,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAErC,MAAM,MAAM,cAAc,GACpB,WA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AC,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,MAAM,QAAQ,GAAG;IACnB,IAAI,EAAE,QAAQ,GAAG,SAAS,GAAG,WAAW,CAAC;IACzC,KAAK,EAAE,OAAO,GAAG,KAAK,CAAC,UAAU,CAAC,CAAC;IACnC,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACrB,CAAA;AAED,MAAM,WAAW,aAAa;IAC1B,UAAU,CAAC,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI,CAAC;CACrC;AAED,MAAM,MAAM,mBAAmB,GAAG;IAC9B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC;CACvB,CAAA"}
@@ -0,0 +1,2 @@
1
+ /** Model configuration and types */
2
+ export {};
@@ -1,5 +1,5 @@
1
- import { QueryWhereParameters } from "index";
2
- import Table from "../Table.js";
1
+ import { QueryValues, QueryWhereParameters } from "../index";
2
+ import Table from "../base/Table.js";
3
3
  export type TableColumnInfo = {
4
4
  cid: number;
5
5
  name: string;
@@ -8,6 +8,9 @@ export type TableColumnInfo = {
8
8
  dflt_value: unknown;
9
9
  pk: number;
10
10
  };
11
+ export type columnType = {
12
+ [key: string]: QueryValues;
13
+ };
11
14
  export type ReadableTableColumnInfo = {
12
15
  name: string;
13
16
  type: string;
@@ -25,5 +28,6 @@ export type Join = {
25
28
  fromTable: Table;
26
29
  joinType: 'INNER' | 'LEFT' | 'RIGHT' | 'FULL';
27
30
  on: QueryWhereParameters | QueryWhereParameters[];
31
+ where?: QueryWhereParameters | QueryWhereParameters[];
28
32
  };
29
33
  //# sourceMappingURL=table.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"table.d.ts","sourceRoot":"","sources":["../../src/types/table.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,OAAO,CAAC;AAC7C,OAAO,KAAK,MAAM,aAAa,CAAC;AAEhC,MAAM,MAAM,e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MAAM,GAAG,WAAW,CAAA;CAAE,CAAC;AAExD,MAAM,MAAM,uBAAuB,GAAG;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,OAAO,CAAC;IAClB,YAAY,EAAE,OAAO,CAAC;IACtB,YAAY,EAAE,OAAO,CAAC;CACzB,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAC3B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC;IACvB,aAAa,CAAC,EAAE,OAAO,CAAC;CAC3B,CAAC;AAEF,MAAM,MAAM,IAAI,GAAG;IACf,SAAS,EAAE,KAAK,CAAC;IACjB,QAAQ,EAAE,OAAO,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,CAAC;IAC9C,EAAE,EAAE,oBAAoB,GAAG,oBAAoB,EAAE,CAAC;IAClD,KAAK,CAAC,EAAE,oBAAoB,GAAG,oBAAoB,EAAE,CAAC;CACzD,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iamkirbki/database-handler-core",
3
- "version": "3.1.4",
3
+ "version": "4.0.0",
4
4
  "author": "iamkirbki",
5
5
  "description": "Core database abstractions and interfaces",
6
6
  "license": "ISC",
@@ -19,13 +19,14 @@
19
19
  ],
20
20
  "repository": {
21
21
  "type": "git",
22
- "url": "https://github.com/IamKirbki/bettersqlite3-handler.git",
22
+ "url": "https://github.com/IamKirbki/npm-database-handler.git",
23
23
  "directory": "packages/core"
24
24
  },
25
25
  "scripts": {
26
- "build": "tsc"
26
+ "build": "tsc --build && tsc-alias"
27
27
  },
28
28
  "devDependencies": {
29
- "typescript": "^5.9.3"
29
+ "typescript": "^5.9.3",
30
+ "tsc-alias": "^1.8.16"
30
31
  }
31
32
  }
@@ -1,98 +0,0 @@
1
- import Table from "./Table.js";
2
- import Query from "./Query.js";
3
- import IDatabaseAdapter from "./interfaces/IDatabaseAdapter.js";
4
- /**
5
- * Main Database class for interacting with SQLite databases
6
- *
7
- * @example
8
- * ```typescript
9
- * import { Database } from 'kirbkis-bettersqlite3-handler';
10
- *
11
- * // Create or open a database
12
- * const db = new Database('./myapp.db');
13
- *
14
- * // Access a table
15
- * const users = db.Table('users');
16
- *
17
- * // Create a new table
18
- * const posts = db.CreateTable('posts');
19
- * ```
20
- */
21
- export default class Database {
22
- adapter: IDatabaseAdapter;
23
- /**
24
- * Creates a new Database instance
25
- *
26
- * @param dbPath - Path to the SQLite database file (absolute or relative to process.cwd())
27
- *
28
- * @example
29
- * ```typescript
30
- * // Relative path
31
- * const db = new Database('./data/app.db');
32
- *
33
- * // Absolute path
34
- * const db = new Database('/var/data/app.db');
35
- *
36
- * // In-memory database
37
- * const db = new Database(':memory:');
38
- * ```
39
- */
40
- constructor(adapter: IDatabaseAdapter);
41
- /**
42
- * Get a Table instance to interact with an existing table
43
- *
44
- * @param name - Name of the table
45
- * @returns Table instance for querying and manipulating data
46
- * @throws Error if the table does not exist
47
- *
48
- * @example
49
- * ```typescript
50
- * const users = await db.Table('users');
51
- * const allUsers = await users.Records();
52
- * ```
53
- */
54
- Table(name: string): Promise<Table>;
55
- /**
56
- * Create a new table with specified columns
57
- * Validates table name, column names, and column types before creation
58
- * Uses CREATE TABLE IF NOT EXISTS to avoid errors if table already exists
59
- *
60
- * @param name - Name of the table to create
61
- * @param columns - Object mapping column names to their type definitions
62
- * @returns Table instance for the newly created table
63
- * @throws Error if table name, column names, or column types are invalid
64
- *
65
- * @example
66
- * ```typescript
67
- * // Create a users table
68
- * const users = await db.CreateTable('users', {
69
- * id: 'INTEGER PRIMARY KEY AUTOINCREMENT',
70
- * name: 'TEXT NOT NULL',
71
- * email: 'TEXT UNIQUE',
72
- * age: 'INTEGER',
73
- * created_at: 'DATETIME DEFAULT CURRENT_TIMESTAMP'
74
- * });
75
- *
76
- * // Table is now ready to use
77
- * await users.Insert({ name: 'John', email: 'john@example.com', age: 30 });
78
- * ```
79
- */
80
- CreateTable(name: string, columns: object): Promise<Table>;
81
- /**
82
- * Create a Query object for executing custom SQL queries
83
- *
84
- * @param table - Table object for validation and context
85
- * @param query - The SQL query string with ? placeholders
86
- * @returns Query instance for executing the query
87
- *
88
- * @example
89
- * ```typescript
90
- * const users = db.Table('users');
91
- * const query = db.Query(users, 'SELECT * FROM users WHERE age > ? AND status = ?');
92
- * query.Parameters = { age: 18, status: 'active' };
93
- * const results = query.All();
94
- * ```
95
- */
96
- Query(table: Table, query: string): Query;
97
- }
98
- //# sourceMappingURL=Database.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"Database.d.ts","sourceRoot":"","sources":["../src/Database.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,YAAY,CAAC;AAC/B,OAAO,KAAK,MAAM,YAAY,CAAC;AAC/B,OAAO,gBAAgB,MAAM,kCAAkC,CAAC;AAEhE;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,CAAC,OAAO,OAAO,QAAQ;IACpB,OAAO,EAAE,gBAAgB,CAAC;IAEjC;;;;;;;;;;;;;;;;OAgBG;gBACS,OAAO,EAAE,gBAAgB;IAIrC;;;;;;;;;;;;OAYG;IACU,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,KAAK,CAAC;IAKhD;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;IACU,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,KAAK,CAAC;IAmBvE;;;;;;;;;;;;;;OAcG;IACI,KAAK,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,GAAG,KAAK;CAGjD"}
package/dist/Database.js DELETED
@@ -1,126 +0,0 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
- import Table from "./Table.js";
11
- import Query from "./Query.js";
12
- /**
13
- * Main Database class for interacting with SQLite databases
14
- *
15
- * @example
16
- * ```typescript
17
- * import { Database } from 'kirbkis-bettersqlite3-handler';
18
- *
19
- * // Create or open a database
20
- * const db = new Database('./myapp.db');
21
- *
22
- * // Access a table
23
- * const users = db.Table('users');
24
- *
25
- * // Create a new table
26
- * const posts = db.CreateTable('posts');
27
- * ```
28
- */
29
- export default class Database {
30
- /**
31
- * Creates a new Database instance
32
- *
33
- * @param dbPath - Path to the SQLite database file (absolute or relative to process.cwd())
34
- *
35
- * @example
36
- * ```typescript
37
- * // Relative path
38
- * const db = new Database('./data/app.db');
39
- *
40
- * // Absolute path
41
- * const db = new Database('/var/data/app.db');
42
- *
43
- * // In-memory database
44
- * const db = new Database(':memory:');
45
- * ```
46
- */
47
- constructor(adapter) {
48
- this.adapter = adapter;
49
- }
50
- /**
51
- * Get a Table instance to interact with an existing table
52
- *
53
- * @param name - Name of the table
54
- * @returns Table instance for querying and manipulating data
55
- * @throws Error if the table does not exist
56
- *
57
- * @example
58
- * ```typescript
59
- * const users = await db.Table('users');
60
- * const allUsers = await users.Records();
61
- * ```
62
- */
63
- Table(name) {
64
- return __awaiter(this, void 0, void 0, function* () {
65
- return yield Table.create(name, this.adapter);
66
- });
67
- }
68
- // TODO Make primary key required
69
- /**
70
- * Create a new table with specified columns
71
- * Validates table name, column names, and column types before creation
72
- * Uses CREATE TABLE IF NOT EXISTS to avoid errors if table already exists
73
- *
74
- * @param name - Name of the table to create
75
- * @param columns - Object mapping column names to their type definitions
76
- * @returns Table instance for the newly created table
77
- * @throws Error if table name, column names, or column types are invalid
78
- *
79
- * @example
80
- * ```typescript
81
- * // Create a users table
82
- * const users = await db.CreateTable('users', {
83
- * id: 'INTEGER PRIMARY KEY AUTOINCREMENT',
84
- * name: 'TEXT NOT NULL',
85
- * email: 'TEXT UNIQUE',
86
- * age: 'INTEGER',
87
- * created_at: 'DATETIME DEFAULT CURRENT_TIMESTAMP'
88
- * });
89
- *
90
- * // Table is now ready to use
91
- * await users.Insert({ name: 'John', email: 'john@example.com', age: 30 });
92
- * ```
93
- */
94
- CreateTable(name, columns) {
95
- return __awaiter(this, void 0, void 0, function* () {
96
- const names = Object.keys(columns || {}).map((colName) => {
97
- return colName;
98
- });
99
- const colsDef = names.map(colName => {
100
- const colType = columns[colName];
101
- return `"${colName}" ${colType}`;
102
- }).join(", ");
103
- const stmt = yield this.adapter.prepare(`CREATE TABLE IF NOT EXISTS "${name}" (${colsDef});`);
104
- yield stmt.run();
105
- return yield Table.create(name, this.adapter, true);
106
- });
107
- }
108
- /**
109
- * Create a Query object for executing custom SQL queries
110
- *
111
- * @param table - Table object for validation and context
112
- * @param query - The SQL query string with ? placeholders
113
- * @returns Query instance for executing the query
114
- *
115
- * @example
116
- * ```typescript
117
- * const users = db.Table('users');
118
- * const query = db.Query(users, 'SELECT * FROM users WHERE age > ? AND status = ?');
119
- * query.Parameters = { age: 18, status: 'active' };
120
- * const results = query.All();
121
- * ```
122
- */
123
- Query(table, query) {
124
- return new Query(table, query, this.adapter);
125
- }
126
- }
package/dist/Query.d.ts DELETED
@@ -1,143 +0,0 @@
1
- import Table from "./Table.js";
2
- import { QueryCondition } from "./types/index.js";
3
- import Record from "./Record.js";
4
- import IDatabaseAdapter from "./interfaces/IDatabaseAdapter.js";
5
- /**
6
- * Query class for executing custom SQL queries
7
- *
8
- * Features:
9
- * - Supports named parameters using @fieldName syntax
10
- * - Provides type-safe query execution (Run, All, Get)
11
- * - Transaction support for atomic multi-insert/update operations
12
- *
13
- * @example
14
- * ```typescript
15
- * const users = db.Table('users');
16
- *
17
- * // SELECT query with parameters
18
- * const query = db.Query(users, 'SELECT * FROM users WHERE age > @age AND status = @status');
19
- * query.Parameters = { age: 18, status: 'active' };
20
- * const results = query.All();
21
- *
22
- * // INSERT query
23
- * const insert = db.Query(users, 'INSERT INTO users (name, email) VALUES (@name, @email)');
24
- * insert.Parameters = { name: 'John', email: 'john@example.com' };
25
- * insert.Run();
26
- *
27
- * // Transaction for multiple inserts
28
- * insert.Transaction([
29
- * { name: 'John', email: 'john@example.com' },
30
- * { name: 'Jane', email: 'jane@example.com' }
31
- * ]);
32
- * ```
33
- */
34
- export default class Query {
35
- readonly Table: Table;
36
- private readonly adapter;
37
- private query;
38
- private _parameters;
39
- get Parameters(): QueryCondition;
40
- set Parameters(value: QueryCondition);
41
- /**
42
- * Creates a Query instance (usually called via db.Query() method)
43
- *
44
- * @param Table - Table instance for validation context
45
- * @param Query - SQL query string with @fieldName placeholders for parameters
46
- * @param DB - Database connection instance
47
- *
48
- * @example
49
- * ```typescript
50
- * // Direct instantiation (not recommended - use db.Query() instead)
51
- * const query = new Query(
52
- * usersTable,
53
- * 'SELECT * FROM users WHERE id = @id',
54
- * db
55
- * );
56
- * query.Parameters = { id: 1 };
57
- *
58
- * // Recommended approach
59
- * const query = db.Query(usersTable, 'SELECT * FROM users WHERE id = @id');
60
- * query.Parameters = { id: 1 };
61
- * ```
62
- */
63
- constructor(Table: Table, Query: string, adapter: IDatabaseAdapter);
64
- /**
65
- * Execute a query that modifies data (INSERT, UPDATE, DELETE)
66
- *
67
- * @template Type - Expected return type (typically { lastInsertRowid: number, changes: number })
68
- * @returns Result object with lastInsertRowid and changes count
69
- *
70
- * @example
71
- * ```typescript
72
- * // INSERT query
73
- * const query = db.Query(users, 'INSERT INTO users (name, age) VALUES (@name, @age)');
74
- * query.Parameters = { name: 'John', age: 30 };
75
- * const result = query.Run<{ lastInsertRowid: number, changes: number }>();
76
- * console.log(`Inserted ID: ${result.lastInsertRowid}`);
77
- *
78
- * // UPDATE query
79
- * const update = db.Query(users, 'UPDATE users SET age = @age WHERE id = @id');
80
- * update.Parameters = { age: 31, id: 1 };
81
- * const updateResult = update.Run<{ changes: number }>();
82
- * console.log(`Updated ${updateResult.changes} rows`);
83
- * ```
84
- */
85
- Run<Type>(): Promise<Type>;
86
- /**
87
- * Execute a SELECT query and return all matching rows as Record objects
88
- * Each row is wrapped in a Record instance for convenient updates/deletes
89
- *
90
- * @template Type - Expected row type
91
- * @returns Array of Record objects containing the query results
92
- *
93
- * @example
94
- * ```typescript
95
- * interface User {
96
- * id: number;
97
- * name: string;
98
- * age: number;
99
- * }
100
- *
101
- * const query = db.Query(users, 'SELECT * FROM users WHERE age > @age');
102
- * query.Parameters = { age: 18 };
103
- * const results = query.All<User>();
104
- *
105
- * // Each result is a Record object
106
- * results.forEach(user => {
107
- * console.log(user.values); // { id: 1, name: 'John', age: 30 }
108
- * user.Update({ age: 31 }); // Can update directly
109
- * });
110
- * ```
111
- */
112
- All<Type>(): Promise<Record<Type>[]>;
113
- private convertParamsToObject;
114
- private convertIdToString;
115
- /**
116
- * Execute a SELECT query and return the first matching row as a Record object
117
- * Returns undefined if no rows match the query
118
- *
119
- * @template Type - Expected row type
120
- * @returns Single Record object or undefined if no match found
121
- *
122
- * @example
123
- * ```typescript
124
- * interface User {
125
- * id: number;
126
- * name: string;
127
- * email: string;
128
- * }
129
- *
130
- * const query = db.Query(users, 'SELECT * FROM users WHERE id = @id');
131
- * query.Parameters = { id: 1 };
132
- * const user = query.Get<User>();
133
- *
134
- * if (user) {
135
- * console.log(user.values); // { id: 1, name: 'John', email: 'john@example.com' }
136
- * user.Update({ email: 'newemail@example.com' });
137
- * }
138
- * ```
139
- */
140
- Get<Type>(): Promise<Record<Type> | undefined>;
141
- Transaction(paramList: QueryCondition[]): Promise<void>;
142
- }
143
- //# sourceMappingURL=Query.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"Query.d.ts","sourceRoot":"","sources":["../src/Query.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,YAAY,CAAC;AAC/B,OAAO,EAAE,cAAc,EAAwB,MAAM,kBAAkB,CAAC;AACxE,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,gBAAgB,MAAM,kCAAkC,CAAC;AAEhE;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,MAAM,CAAC,OAAO,OAAO,KAAK;IACxB,SAAgB,KAAK,EAAE,KAAK,CAAC;IAC7B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAmB;IAC3C,OAAO,CAAC,KAAK,CAAc;IAC3B,OAAO,CAAC,WAAW,CAAsB;IAEzC,IAAW,UAAU,IAAI,cAAc,CAEtC;IAED,IAAW,UAAU,CAAC,KAAK,EAAE,cAAc,EAE1C;IAED;;;;;;;;;;;;;;;;;;;;;OAqBG;gBACS,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,gBAAgB;IAMlE;;;;;;;;;;;;;;;;;;;;OAoBG;IACU,GAAG,CAAC,IAAI,KAAK,OAAO,CAAC,IAAI,CAAC;IAKvC;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;IACU,GAAG,CAAC,IAAI,KAAK,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;IAMjD,OAAO,CAAC,qBAAqB;IAa7B,OAAO,CAAC,iBAAiB;IAQzB;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;IACU,GAAG,CAAC,IAAI,KAAK,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,SAAS,CAAC;IAM9C,WAAW,CAAC,SAAS,EAAE,cAAc,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;CAiBrE"}
package/dist/Query.js DELETED
@@ -1,205 +0,0 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
- import Record from "./Record.js";
11
- /**
12
- * Query class for executing custom SQL queries
13
- *
14
- * Features:
15
- * - Supports named parameters using @fieldName syntax
16
- * - Provides type-safe query execution (Run, All, Get)
17
- * - Transaction support for atomic multi-insert/update operations
18
- *
19
- * @example
20
- * ```typescript
21
- * const users = db.Table('users');
22
- *
23
- * // SELECT query with parameters
24
- * const query = db.Query(users, 'SELECT * FROM users WHERE age > @age AND status = @status');
25
- * query.Parameters = { age: 18, status: 'active' };
26
- * const results = query.All();
27
- *
28
- * // INSERT query
29
- * const insert = db.Query(users, 'INSERT INTO users (name, email) VALUES (@name, @email)');
30
- * insert.Parameters = { name: 'John', email: 'john@example.com' };
31
- * insert.Run();
32
- *
33
- * // Transaction for multiple inserts
34
- * insert.Transaction([
35
- * { name: 'John', email: 'john@example.com' },
36
- * { name: 'Jane', email: 'jane@example.com' }
37
- * ]);
38
- * ```
39
- */
40
- export default class Query {
41
- get Parameters() {
42
- return this._parameters;
43
- }
44
- set Parameters(value) {
45
- this._parameters = this.convertParamsToObject(value);
46
- }
47
- /**
48
- * Creates a Query instance (usually called via db.Query() method)
49
- *
50
- * @param Table - Table instance for validation context
51
- * @param Query - SQL query string with @fieldName placeholders for parameters
52
- * @param DB - Database connection instance
53
- *
54
- * @example
55
- * ```typescript
56
- * // Direct instantiation (not recommended - use db.Query() instead)
57
- * const query = new Query(
58
- * usersTable,
59
- * 'SELECT * FROM users WHERE id = @id',
60
- * db
61
- * );
62
- * query.Parameters = { id: 1 };
63
- *
64
- * // Recommended approach
65
- * const query = db.Query(usersTable, 'SELECT * FROM users WHERE id = @id');
66
- * query.Parameters = { id: 1 };
67
- * ```
68
- */
69
- constructor(Table, Query, adapter) {
70
- this.query = "";
71
- this._parameters = {};
72
- this.Table = Table;
73
- this.query = Query;
74
- this.adapter = adapter;
75
- }
76
- /**
77
- * Execute a query that modifies data (INSERT, UPDATE, DELETE)
78
- *
79
- * @template Type - Expected return type (typically { lastInsertRowid: number, changes: number })
80
- * @returns Result object with lastInsertRowid and changes count
81
- *
82
- * @example
83
- * ```typescript
84
- * // INSERT query
85
- * const query = db.Query(users, 'INSERT INTO users (name, age) VALUES (@name, @age)');
86
- * query.Parameters = { name: 'John', age: 30 };
87
- * const result = query.Run<{ lastInsertRowid: number, changes: number }>();
88
- * console.log(`Inserted ID: ${result.lastInsertRowid}`);
89
- *
90
- * // UPDATE query
91
- * const update = db.Query(users, 'UPDATE users SET age = @age WHERE id = @id');
92
- * update.Parameters = { age: 31, id: 1 };
93
- * const updateResult = update.Run<{ changes: number }>();
94
- * console.log(`Updated ${updateResult.changes} rows`);
95
- * ```
96
- */
97
- Run() {
98
- return __awaiter(this, void 0, void 0, function* () {
99
- const stmt = yield this.adapter.prepare(this.query);
100
- return yield stmt.run(this.Parameters);
101
- });
102
- }
103
- /**
104
- * Execute a SELECT query and return all matching rows as Record objects
105
- * Each row is wrapped in a Record instance for convenient updates/deletes
106
- *
107
- * @template Type - Expected row type
108
- * @returns Array of Record objects containing the query results
109
- *
110
- * @example
111
- * ```typescript
112
- * interface User {
113
- * id: number;
114
- * name: string;
115
- * age: number;
116
- * }
117
- *
118
- * const query = db.Query(users, 'SELECT * FROM users WHERE age > @age');
119
- * query.Parameters = { age: 18 };
120
- * const results = query.All<User>();
121
- *
122
- * // Each result is a Record object
123
- * results.forEach(user => {
124
- * console.log(user.values); // { id: 1, name: 'John', age: 30 }
125
- * user.Update({ age: 31 }); // Can update directly
126
- * });
127
- * ```
128
- */
129
- All() {
130
- return __awaiter(this, void 0, void 0, function* () {
131
- const stmt = yield this.adapter.prepare(this.query);
132
- const results = yield stmt.all(this.Parameters);
133
- return results.map(res => new Record(res, this.adapter, this.Table));
134
- });
135
- }
136
- convertParamsToObject(params) {
137
- const paramObject = {};
138
- if (Array.isArray(params)) {
139
- params.forEach(param => {
140
- paramObject[param.column] = param.value;
141
- });
142
- }
143
- else {
144
- Object.assign(paramObject, params);
145
- }
146
- return this.convertIdToString(paramObject);
147
- }
148
- convertIdToString(params) {
149
- if (params.id && typeof params.id === 'number') {
150
- return Object.assign(Object.assign({}, params), { id: params.id.toString() });
151
- }
152
- return params;
153
- }
154
- /**
155
- * Execute a SELECT query and return the first matching row as a Record object
156
- * Returns undefined if no rows match the query
157
- *
158
- * @template Type - Expected row type
159
- * @returns Single Record object or undefined if no match found
160
- *
161
- * @example
162
- * ```typescript
163
- * interface User {
164
- * id: number;
165
- * name: string;
166
- * email: string;
167
- * }
168
- *
169
- * const query = db.Query(users, 'SELECT * FROM users WHERE id = @id');
170
- * query.Parameters = { id: 1 };
171
- * const user = query.Get<User>();
172
- *
173
- * if (user) {
174
- * console.log(user.values); // { id: 1, name: 'John', email: 'john@example.com' }
175
- * user.Update({ email: 'newemail@example.com' });
176
- * }
177
- * ```
178
- */
179
- Get() {
180
- return __awaiter(this, void 0, void 0, function* () {
181
- const stmt = yield this.adapter.prepare(this.query);
182
- const results = yield stmt.get(this.Parameters);
183
- return results ? new Record(results, this.adapter, this.Table) : undefined;
184
- });
185
- }
186
- Transaction(paramList) {
187
- return __awaiter(this, void 0, void 0, function* () {
188
- const stmt = yield this.adapter.prepare(this.query);
189
- const transactionFn = yield this.adapter.transaction((paramsArray) => {
190
- for (const params of paramsArray) {
191
- // Use runSync for better-sqlite3 transactions (must be synchronous)
192
- // For other adapters, this method should be implemented appropriately
193
- if (stmt.runSync) {
194
- stmt.runSync(params);
195
- }
196
- else {
197
- // Fallback: call run without await (may not work for all adapters)
198
- stmt.run(params);
199
- }
200
- }
201
- });
202
- transactionFn(paramList);
203
- });
204
- }
205
- }