pg-query-sdk 1.0.0 → 1.0.1
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/README.md +94 -365
- package/dist/cjs/builders/ConditionBuilder.d.ts +34 -0
- package/dist/cjs/builders/ConditionBuilder.js +34 -0
- package/dist/cjs/builders/QueryBuilder.d.ts +112 -1
- package/dist/cjs/builders/QueryBuilder.js +94 -1
- package/dist/cjs/core/Database.d.ts +26 -0
- package/dist/cjs/core/Database.js +26 -0
- package/dist/cjs/core/ParamContext.d.ts +16 -0
- package/dist/cjs/core/ParamContext.js +16 -0
- package/dist/cjs/core/QueryExecutor.d.ts +26 -0
- package/dist/cjs/core/QueryExecutor.js +26 -0
- package/dist/cjs/core/TransactionManager.d.ts +13 -0
- package/dist/cjs/core/TransactionManager.js +13 -0
- package/dist/cjs/core/UnitOfWork.d.ts +19 -0
- package/dist/cjs/core/UnitOfWork.js +19 -0
- package/dist/cjs/dialects/Dialect.d.ts +13 -0
- package/dist/cjs/dialects/MysqlDialect.d.ts +12 -0
- package/dist/cjs/dialects/MysqlDialect.js +12 -0
- package/dist/cjs/dialects/PostgresDialect.d.ts +13 -0
- package/dist/cjs/dialects/PostgresDialect.js +13 -0
- package/dist/cjs/index.d.ts +28 -0
- package/dist/cjs/index.js +28 -0
- package/dist/cjs/orm/Repository.d.ts +34 -0
- package/dist/cjs/orm/Repository.js +34 -0
- package/dist/cjs/query/ConditionBuilder.d.ts +33 -0
- package/dist/cjs/query/ConditionBuilder.js +33 -0
- package/dist/cjs/query/QueryBuilder.d.ts +154 -0
- package/dist/cjs/query/QueryBuilder.js +136 -0
- package/dist/esm/builders/ConditionBuilder.d.ts +34 -0
- package/dist/esm/builders/ConditionBuilder.js +34 -0
- package/dist/esm/builders/QueryBuilder.d.ts +112 -1
- package/dist/esm/builders/QueryBuilder.js +94 -1
- package/dist/esm/core/Database.d.ts +26 -0
- package/dist/esm/core/Database.js +26 -0
- package/dist/esm/core/ParamContext.d.ts +16 -0
- package/dist/esm/core/ParamContext.js +16 -0
- package/dist/esm/core/QueryExecutor.d.ts +26 -0
- package/dist/esm/core/QueryExecutor.js +26 -0
- package/dist/esm/core/TransactionManager.d.ts +13 -0
- package/dist/esm/core/TransactionManager.js +13 -0
- package/dist/esm/core/UnitOfWork.d.ts +19 -0
- package/dist/esm/core/UnitOfWork.js +19 -0
- package/dist/esm/dialects/Dialect.d.ts +13 -0
- package/dist/esm/dialects/MysqlDialect.d.ts +12 -0
- package/dist/esm/dialects/MysqlDialect.js +12 -0
- package/dist/esm/dialects/PostgresDialect.d.ts +13 -0
- package/dist/esm/dialects/PostgresDialect.js +13 -0
- package/dist/esm/index.d.ts +28 -0
- package/dist/esm/index.js +28 -0
- package/dist/esm/orm/Repository.d.ts +34 -0
- package/dist/esm/orm/Repository.js +34 -0
- package/dist/esm/query/ConditionBuilder.d.ts +33 -0
- package/dist/esm/query/ConditionBuilder.js +33 -0
- package/dist/esm/query/QueryBuilder.d.ts +154 -0
- package/dist/esm/query/QueryBuilder.js +136 -0
- package/package.json +1 -1
|
@@ -1,9 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/**
|
|
4
|
+
* Manages database transactions, providing a method to execute a callback within a transaction.
|
|
5
|
+
*/
|
|
3
6
|
class TransactionManager {
|
|
7
|
+
/**
|
|
8
|
+
* Creates an instance of TransactionManager.
|
|
9
|
+
* @param pool - The PostgreSQL connection pool to use for transactions.
|
|
10
|
+
*/
|
|
4
11
|
constructor(pool) {
|
|
5
12
|
this.pool = pool;
|
|
6
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* Executes a given callback function within a database transaction.
|
|
16
|
+
* The transaction is committed if the callback succeeds, and rolled back if an error occurs.
|
|
17
|
+
* @param callback - The function to execute within the transaction. It receives a PoolClient instance.
|
|
18
|
+
* @returns A Promise that resolves to the result of the callback function.
|
|
19
|
+
*/
|
|
7
20
|
async transaction(callback) {
|
|
8
21
|
const client = await this.pool.connect();
|
|
9
22
|
try {
|
|
@@ -1,10 +1,29 @@
|
|
|
1
1
|
import Repository from "../orm/Repository";
|
|
2
|
+
/**
|
|
3
|
+
* Manages a list of new, dirty, and removed entities for transactional operations.
|
|
4
|
+
*/
|
|
2
5
|
export default class UnitOfWork {
|
|
3
6
|
private newEntities;
|
|
4
7
|
private dirtyEntities;
|
|
5
8
|
private removedEntities;
|
|
9
|
+
/**
|
|
10
|
+
* Registers an entity as new, to be inserted on commit.
|
|
11
|
+
* @param entity - The entity to register.
|
|
12
|
+
*/
|
|
6
13
|
registerNew(entity: any): void;
|
|
14
|
+
/**
|
|
15
|
+
* Registers an entity as dirty, to be updated on commit.
|
|
16
|
+
* @param entity - The entity to register.
|
|
17
|
+
*/
|
|
7
18
|
registerDirty(entity: any): void;
|
|
19
|
+
/**
|
|
20
|
+
* Registers an entity as removed, to be deleted on commit.
|
|
21
|
+
* @param entity - The entity to register.
|
|
22
|
+
*/
|
|
8
23
|
registerRemoved(entity: any): void;
|
|
24
|
+
/**
|
|
25
|
+
* Commits all registered changes (insertions, updates, deletions) using the provided repository.
|
|
26
|
+
* @param repository - The repository to use for committing changes.
|
|
27
|
+
*/
|
|
9
28
|
commit(repository: Repository<any>): Promise<void>;
|
|
10
29
|
}
|
|
@@ -1,20 +1,39 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/**
|
|
4
|
+
* Manages a list of new, dirty, and removed entities for transactional operations.
|
|
5
|
+
*/
|
|
3
6
|
class UnitOfWork {
|
|
4
7
|
constructor() {
|
|
5
8
|
this.newEntities = [];
|
|
6
9
|
this.dirtyEntities = [];
|
|
7
10
|
this.removedEntities = [];
|
|
8
11
|
}
|
|
12
|
+
/**
|
|
13
|
+
* Registers an entity as new, to be inserted on commit.
|
|
14
|
+
* @param entity - The entity to register.
|
|
15
|
+
*/
|
|
9
16
|
registerNew(entity) {
|
|
10
17
|
this.newEntities.push(entity);
|
|
11
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* Registers an entity as dirty, to be updated on commit.
|
|
21
|
+
* @param entity - The entity to register.
|
|
22
|
+
*/
|
|
12
23
|
registerDirty(entity) {
|
|
13
24
|
this.dirtyEntities.push(entity);
|
|
14
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* Registers an entity as removed, to be deleted on commit.
|
|
28
|
+
* @param entity - The entity to register.
|
|
29
|
+
*/
|
|
15
30
|
registerRemoved(entity) {
|
|
16
31
|
this.removedEntities.push(entity);
|
|
17
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* Commits all registered changes (insertions, updates, deletions) using the provided repository.
|
|
35
|
+
* @param repository - The repository to use for committing changes.
|
|
36
|
+
*/
|
|
18
37
|
async commit(repository) {
|
|
19
38
|
for (const e of this.newEntities)
|
|
20
39
|
await repository.insert(e);
|
|
@@ -1,4 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Defines the interface for a database dialect, providing methods for placeholder generation and identifier wrapping.
|
|
3
|
+
*/
|
|
1
4
|
export interface Dialect {
|
|
5
|
+
/**
|
|
6
|
+
* Generates a parameter placeholder for the given index.
|
|
7
|
+
* @param index - The index of the parameter.
|
|
8
|
+
* @returns The dialect-specific parameter placeholder.
|
|
9
|
+
*/
|
|
2
10
|
placeholder(index: number): string;
|
|
11
|
+
/**
|
|
12
|
+
* Wraps an identifier (e.g., table name, column name) with dialect-specific quoting.
|
|
13
|
+
* @param identifier - The identifier to wrap.
|
|
14
|
+
* @returns The wrapped identifier.
|
|
15
|
+
*/
|
|
3
16
|
wrapIdentifier(identifier: string): string;
|
|
4
17
|
}
|
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
import { Dialect } from "./Dialect";
|
|
2
|
+
/**
|
|
3
|
+
* Implements the Dialect interface for MySQL, providing MySQL-specific placeholder and identifier wrapping.
|
|
4
|
+
*/
|
|
2
5
|
export default class MysqlDialect implements Dialect {
|
|
6
|
+
/**
|
|
7
|
+
* Returns a MySQL-specific parameter placeholder '?'.
|
|
8
|
+
* @returns The string '?'.
|
|
9
|
+
*/
|
|
3
10
|
placeholder(): string;
|
|
11
|
+
/**
|
|
12
|
+
* Wraps a MySQL identifier with backticks.
|
|
13
|
+
* @param id - The identifier to wrap.
|
|
14
|
+
* @returns The backtick-wrapped identifier.
|
|
15
|
+
*/
|
|
4
16
|
wrapIdentifier(id: string): string;
|
|
5
17
|
}
|
|
@@ -1,9 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/**
|
|
4
|
+
* Implements the Dialect interface for MySQL, providing MySQL-specific placeholder and identifier wrapping.
|
|
5
|
+
*/
|
|
3
6
|
class MysqlDialect {
|
|
7
|
+
/**
|
|
8
|
+
* Returns a MySQL-specific parameter placeholder '?'.
|
|
9
|
+
* @returns The string '?'.
|
|
10
|
+
*/
|
|
4
11
|
placeholder() {
|
|
5
12
|
return '?';
|
|
6
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* Wraps a MySQL identifier with backticks.
|
|
16
|
+
* @param id - The identifier to wrap.
|
|
17
|
+
* @returns The backtick-wrapped identifier.
|
|
18
|
+
*/
|
|
7
19
|
wrapIdentifier(id) {
|
|
8
20
|
return `\`${id}\``;
|
|
9
21
|
}
|
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
import { Dialect } from "./Dialect";
|
|
2
|
+
/**
|
|
3
|
+
* Implements the Dialect interface for PostgreSQL, providing PostgreSQL-specific placeholder and identifier wrapping.
|
|
4
|
+
*/
|
|
2
5
|
export default class PostgresDialect implements Dialect {
|
|
6
|
+
/**
|
|
7
|
+
* Returns a PostgreSQL-specific parameter placeholder (e.g., '$1', '$2').
|
|
8
|
+
* @param index - The index of the parameter.
|
|
9
|
+
* @returns The dollar-prefixed, indexed placeholder.
|
|
10
|
+
*/
|
|
3
11
|
placeholder(index: number): string;
|
|
12
|
+
/**
|
|
13
|
+
* Wraps a PostgreSQL identifier with double quotes.
|
|
14
|
+
* @param id - The identifier to wrap.
|
|
15
|
+
* @returns The double-quoted identifier.
|
|
16
|
+
*/
|
|
4
17
|
wrapIdentifier(id: string): string;
|
|
5
18
|
}
|
|
@@ -1,9 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/**
|
|
4
|
+
* Implements the Dialect interface for PostgreSQL, providing PostgreSQL-specific placeholder and identifier wrapping.
|
|
5
|
+
*/
|
|
3
6
|
class PostgresDialect {
|
|
7
|
+
/**
|
|
8
|
+
* Returns a PostgreSQL-specific parameter placeholder (e.g., '$1', '$2').
|
|
9
|
+
* @param index - The index of the parameter.
|
|
10
|
+
* @returns The dollar-prefixed, indexed placeholder.
|
|
11
|
+
*/
|
|
4
12
|
placeholder(index) {
|
|
5
13
|
return `$${index}`;
|
|
6
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Wraps a PostgreSQL identifier with double quotes.
|
|
17
|
+
* @param id - The identifier to wrap.
|
|
18
|
+
* @returns The double-quoted identifier.
|
|
19
|
+
*/
|
|
7
20
|
wrapIdentifier(id) {
|
|
8
21
|
return `"${id}"`;
|
|
9
22
|
}
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,7 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Re-exports the QueryExecutor class from the core module.
|
|
3
|
+
* @module QueryExecutor
|
|
4
|
+
*/
|
|
1
5
|
export { default as QueryExecutor } from './core/QueryExecutor';
|
|
6
|
+
/**
|
|
7
|
+
* Re-exports the QueryBuilder class from the builders module.
|
|
8
|
+
* @module QueryBuilder
|
|
9
|
+
*/
|
|
2
10
|
export { default as QueryBuilder } from './builders/QueryBuilder';
|
|
11
|
+
/**
|
|
12
|
+
* Re-exports the ConditionBuilder class from the builders module.
|
|
13
|
+
* @module ConditionBuilder
|
|
14
|
+
*/
|
|
3
15
|
export { default as ConditionBuilder } from './builders/ConditionBuilder';
|
|
16
|
+
/**
|
|
17
|
+
* Re-exports the Database class from the core module.
|
|
18
|
+
* @module Database
|
|
19
|
+
*/
|
|
4
20
|
export { default as Database } from './core/Database';
|
|
21
|
+
/**
|
|
22
|
+
* Re-exports the TransactionManager class from the core module.
|
|
23
|
+
* @module TransactionManager
|
|
24
|
+
*/
|
|
5
25
|
export { default as TransactionManager } from './core/TransactionManager';
|
|
26
|
+
/**
|
|
27
|
+
* Re-exports the PostgresDialect class from the dialects module.
|
|
28
|
+
* @module PostgresDialect
|
|
29
|
+
*/
|
|
6
30
|
export { default as PostgresDialect } from './dialects/PostgresDialect';
|
|
31
|
+
/**
|
|
32
|
+
* Re-exports the Repository class from the orm module.
|
|
33
|
+
* @module Repository
|
|
34
|
+
*/
|
|
7
35
|
export { default as Repository } from './orm/Repository';
|
package/dist/cjs/index.js
CHANGED
|
@@ -4,17 +4,45 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.Repository = exports.PostgresDialect = exports.TransactionManager = exports.Database = exports.ConditionBuilder = exports.QueryBuilder = exports.QueryExecutor = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* Re-exports the QueryExecutor class from the core module.
|
|
9
|
+
* @module QueryExecutor
|
|
10
|
+
*/
|
|
7
11
|
var QueryExecutor_1 = require("./core/QueryExecutor");
|
|
8
12
|
Object.defineProperty(exports, "QueryExecutor", { enumerable: true, get: function () { return __importDefault(QueryExecutor_1).default; } });
|
|
13
|
+
/**
|
|
14
|
+
* Re-exports the QueryBuilder class from the builders module.
|
|
15
|
+
* @module QueryBuilder
|
|
16
|
+
*/
|
|
9
17
|
var QueryBuilder_1 = require("./builders/QueryBuilder");
|
|
10
18
|
Object.defineProperty(exports, "QueryBuilder", { enumerable: true, get: function () { return __importDefault(QueryBuilder_1).default; } });
|
|
19
|
+
/**
|
|
20
|
+
* Re-exports the ConditionBuilder class from the builders module.
|
|
21
|
+
* @module ConditionBuilder
|
|
22
|
+
*/
|
|
11
23
|
var ConditionBuilder_1 = require("./builders/ConditionBuilder");
|
|
12
24
|
Object.defineProperty(exports, "ConditionBuilder", { enumerable: true, get: function () { return __importDefault(ConditionBuilder_1).default; } });
|
|
25
|
+
/**
|
|
26
|
+
* Re-exports the Database class from the core module.
|
|
27
|
+
* @module Database
|
|
28
|
+
*/
|
|
13
29
|
var Database_1 = require("./core/Database");
|
|
14
30
|
Object.defineProperty(exports, "Database", { enumerable: true, get: function () { return __importDefault(Database_1).default; } });
|
|
31
|
+
/**
|
|
32
|
+
* Re-exports the TransactionManager class from the core module.
|
|
33
|
+
* @module TransactionManager
|
|
34
|
+
*/
|
|
15
35
|
var TransactionManager_1 = require("./core/TransactionManager");
|
|
16
36
|
Object.defineProperty(exports, "TransactionManager", { enumerable: true, get: function () { return __importDefault(TransactionManager_1).default; } });
|
|
37
|
+
/**
|
|
38
|
+
* Re-exports the PostgresDialect class from the dialects module.
|
|
39
|
+
* @module PostgresDialect
|
|
40
|
+
*/
|
|
17
41
|
var PostgresDialect_1 = require("./dialects/PostgresDialect");
|
|
18
42
|
Object.defineProperty(exports, "PostgresDialect", { enumerable: true, get: function () { return __importDefault(PostgresDialect_1).default; } });
|
|
43
|
+
/**
|
|
44
|
+
* Re-exports the Repository class from the orm module.
|
|
45
|
+
* @module Repository
|
|
46
|
+
*/
|
|
19
47
|
var Repository_1 = require("./orm/Repository");
|
|
20
48
|
Object.defineProperty(exports, "Repository", { enumerable: true, get: function () { return __importDefault(Repository_1).default; } });
|
|
@@ -1,13 +1,47 @@
|
|
|
1
1
|
import { QueryBuilder, QueryExecutor } from "../index";
|
|
2
2
|
import { Dialect } from "../dialects/Dialect";
|
|
3
|
+
/**
|
|
4
|
+
* Abstract base class for repositories, providing common database operations.
|
|
5
|
+
* @template T The type of the entity managed by the repository.
|
|
6
|
+
*/
|
|
3
7
|
export default abstract class Repository<T> {
|
|
4
8
|
protected table: string;
|
|
5
9
|
protected executor: QueryExecutor;
|
|
6
10
|
protected dialect: Dialect;
|
|
11
|
+
/**
|
|
12
|
+
* Creates an instance of Repository.
|
|
13
|
+
* @param table - The name of the database table associated with this repository.
|
|
14
|
+
* @param executor - The QueryExecutor instance for executing queries.
|
|
15
|
+
* @param dialect - The Dialect instance for database-specific syntax.
|
|
16
|
+
*/
|
|
7
17
|
constructor(table: string, executor: QueryExecutor, dialect: Dialect);
|
|
18
|
+
/**
|
|
19
|
+
* Returns a new QueryBuilder instance for the repository's table.
|
|
20
|
+
* @returns A QueryBuilder instance.
|
|
21
|
+
*/
|
|
8
22
|
qb(): QueryBuilder<T>;
|
|
23
|
+
/**
|
|
24
|
+
* Finds an entity by its ID.
|
|
25
|
+
* @param id - The ID of the entity to find.
|
|
26
|
+
* @returns A Promise that resolves to the found entity or null if not found.
|
|
27
|
+
*/
|
|
9
28
|
findById(id: number): Promise<T | null>;
|
|
29
|
+
/**
|
|
30
|
+
* Inserts a new entity into the database.
|
|
31
|
+
* This method should be implemented by concrete repository classes.
|
|
32
|
+
* @param data - The partial entity data to insert.
|
|
33
|
+
*/
|
|
10
34
|
insert(data: Partial<T>): Promise<void>;
|
|
35
|
+
/**
|
|
36
|
+
* Updates an existing entity in the database.
|
|
37
|
+
* This method should be implemented by concrete repository classes.
|
|
38
|
+
* @param data - The partial entity data to update.
|
|
39
|
+
*/
|
|
11
40
|
update(data: Partial<T>): Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* Deletes an entity from the database.
|
|
43
|
+
* This method should be implemented by concrete repository classes.
|
|
44
|
+
* @param data - The partial entity data to delete (e.g., containing the ID).
|
|
45
|
+
*/
|
|
12
46
|
delete(data: Partial<T>): Promise<void>;
|
|
13
47
|
}
|
|
@@ -1,15 +1,34 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const index_1 = require("../index");
|
|
4
|
+
/**
|
|
5
|
+
* Abstract base class for repositories, providing common database operations.
|
|
6
|
+
* @template T The type of the entity managed by the repository.
|
|
7
|
+
*/
|
|
4
8
|
class Repository {
|
|
9
|
+
/**
|
|
10
|
+
* Creates an instance of Repository.
|
|
11
|
+
* @param table - The name of the database table associated with this repository.
|
|
12
|
+
* @param executor - The QueryExecutor instance for executing queries.
|
|
13
|
+
* @param dialect - The Dialect instance for database-specific syntax.
|
|
14
|
+
*/
|
|
5
15
|
constructor(table, executor, dialect) {
|
|
6
16
|
this.table = table;
|
|
7
17
|
this.executor = executor;
|
|
8
18
|
this.dialect = dialect;
|
|
9
19
|
}
|
|
20
|
+
/**
|
|
21
|
+
* Returns a new QueryBuilder instance for the repository's table.
|
|
22
|
+
* @returns A QueryBuilder instance.
|
|
23
|
+
*/
|
|
10
24
|
qb() {
|
|
11
25
|
return new index_1.QueryBuilder(this.table, this.executor, this.dialect);
|
|
12
26
|
}
|
|
27
|
+
/**
|
|
28
|
+
* Finds an entity by its ID.
|
|
29
|
+
* @param id - The ID of the entity to find.
|
|
30
|
+
* @returns A Promise that resolves to the found entity or null if not found.
|
|
31
|
+
*/
|
|
13
32
|
async findById(id) {
|
|
14
33
|
const rows = await this.qb()
|
|
15
34
|
.where({ id })
|
|
@@ -17,12 +36,27 @@ class Repository {
|
|
|
17
36
|
.execute();
|
|
18
37
|
return rows[0] ?? null;
|
|
19
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* Inserts a new entity into the database.
|
|
41
|
+
* This method should be implemented by concrete repository classes.
|
|
42
|
+
* @param data - The partial entity data to insert.
|
|
43
|
+
*/
|
|
20
44
|
async insert(data) {
|
|
21
45
|
// implementação insert segura
|
|
22
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* Updates an existing entity in the database.
|
|
49
|
+
* This method should be implemented by concrete repository classes.
|
|
50
|
+
* @param data - The partial entity data to update.
|
|
51
|
+
*/
|
|
23
52
|
async update(data) {
|
|
24
53
|
// implementação update segura
|
|
25
54
|
}
|
|
55
|
+
/**
|
|
56
|
+
* Deletes an entity from the database.
|
|
57
|
+
* This method should be implemented by concrete repository classes.
|
|
58
|
+
* @param data - The partial entity data to delete (e.g., containing the ID).
|
|
59
|
+
*/
|
|
26
60
|
async delete(data) {
|
|
27
61
|
// soft delete opcional
|
|
28
62
|
}
|
|
@@ -1,11 +1,44 @@
|
|
|
1
1
|
import ParamContext from '../core/ParamContext';
|
|
2
|
+
/**
|
|
3
|
+
* A builder for constructing SQL WHERE and HAVING clauses.
|
|
4
|
+
*/
|
|
2
5
|
export default class ConditionBuilder {
|
|
3
6
|
private ctx;
|
|
4
7
|
private parts;
|
|
8
|
+
/**
|
|
9
|
+
* Creates an instance of ConditionBuilder.
|
|
10
|
+
* @param ctx - The ParamContext to manage query parameters.
|
|
11
|
+
*/
|
|
5
12
|
constructor(ctx: ParamContext);
|
|
13
|
+
/**
|
|
14
|
+
* Adds one or more WHERE conditions based on an object.
|
|
15
|
+
* Supports direct equality, `null` checks, and custom operators.
|
|
16
|
+
* @param obj - An object where keys are column names and values are the desired values or an object with `op` and `value`.
|
|
17
|
+
* @returns The current ConditionBuilder instance.
|
|
18
|
+
*/
|
|
6
19
|
where(obj: Record<string, any>): this;
|
|
20
|
+
/**
|
|
21
|
+
* Adds a raw SQL expression to the conditions.
|
|
22
|
+
* @param expression - The raw SQL expression.
|
|
23
|
+
* @returns The current ConditionBuilder instance.
|
|
24
|
+
*/
|
|
7
25
|
raw(expression: string): this;
|
|
26
|
+
/**
|
|
27
|
+
* Adds a group of conditions connected by AND.
|
|
28
|
+
* @param cb - A callback function that receives a nested ConditionBuilder to define conditions within the group.
|
|
29
|
+
* @returns The current ConditionBuilder instance.
|
|
30
|
+
*/
|
|
8
31
|
andGroup(cb: (qb: ConditionBuilder) => void): this;
|
|
32
|
+
/**
|
|
33
|
+
* Adds a group of conditions connected by OR.
|
|
34
|
+
* @param cb - A callback function that receives a nested ConditionBuilder to define conditions within the group.
|
|
35
|
+
* @returns The current ConditionBuilder instance.
|
|
36
|
+
*/
|
|
9
37
|
orGroup(cb: (qb: ConditionBuilder) => void): this;
|
|
38
|
+
/**
|
|
39
|
+
* Builds the SQL condition string.
|
|
40
|
+
* @param prefix - The prefix for the condition (e.g., 'WHERE', 'HAVING'). Defaults to 'WHERE'.
|
|
41
|
+
* @returns The built SQL condition string, or an empty string if no conditions were added.
|
|
42
|
+
*/
|
|
10
43
|
build(prefix?: string): string;
|
|
11
44
|
}
|
|
@@ -1,10 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/**
|
|
4
|
+
* A builder for constructing SQL WHERE and HAVING clauses.
|
|
5
|
+
*/
|
|
3
6
|
class ConditionBuilder {
|
|
7
|
+
/**
|
|
8
|
+
* Creates an instance of ConditionBuilder.
|
|
9
|
+
* @param ctx - The ParamContext to manage query parameters.
|
|
10
|
+
*/
|
|
4
11
|
constructor(ctx) {
|
|
5
12
|
this.ctx = ctx;
|
|
6
13
|
this.parts = [];
|
|
7
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Adds one or more WHERE conditions based on an object.
|
|
17
|
+
* Supports direct equality, `null` checks, and custom operators.
|
|
18
|
+
* @param obj - An object where keys are column names and values are the desired values or an object with `op` and `value`.
|
|
19
|
+
* @returns The current ConditionBuilder instance.
|
|
20
|
+
*/
|
|
8
21
|
where(obj) {
|
|
9
22
|
Object.entries(obj).forEach(([key, value]) => {
|
|
10
23
|
if (value === null) {
|
|
@@ -22,10 +35,20 @@ class ConditionBuilder {
|
|
|
22
35
|
});
|
|
23
36
|
return this;
|
|
24
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* Adds a raw SQL expression to the conditions.
|
|
40
|
+
* @param expression - The raw SQL expression.
|
|
41
|
+
* @returns The current ConditionBuilder instance.
|
|
42
|
+
*/
|
|
25
43
|
raw(expression) {
|
|
26
44
|
this.parts.push(expression);
|
|
27
45
|
return this;
|
|
28
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* Adds a group of conditions connected by AND.
|
|
49
|
+
* @param cb - A callback function that receives a nested ConditionBuilder to define conditions within the group.
|
|
50
|
+
* @returns The current ConditionBuilder instance.
|
|
51
|
+
*/
|
|
29
52
|
andGroup(cb) {
|
|
30
53
|
const nested = new ConditionBuilder(this.ctx);
|
|
31
54
|
cb(nested);
|
|
@@ -35,6 +58,11 @@ class ConditionBuilder {
|
|
|
35
58
|
}
|
|
36
59
|
return this;
|
|
37
60
|
}
|
|
61
|
+
/**
|
|
62
|
+
* Adds a group of conditions connected by OR.
|
|
63
|
+
* @param cb - A callback function that receives a nested ConditionBuilder to define conditions within the group.
|
|
64
|
+
* @returns The current ConditionBuilder instance.
|
|
65
|
+
*/
|
|
38
66
|
orGroup(cb) {
|
|
39
67
|
const nested = new ConditionBuilder(this.ctx);
|
|
40
68
|
cb(nested);
|
|
@@ -45,6 +73,11 @@ class ConditionBuilder {
|
|
|
45
73
|
}
|
|
46
74
|
return this;
|
|
47
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* Builds the SQL condition string.
|
|
78
|
+
* @param prefix - The prefix for the condition (e.g., 'WHERE', 'HAVING'). Defaults to 'WHERE'.
|
|
79
|
+
* @returns The built SQL condition string, or an empty string if no conditions were added.
|
|
80
|
+
*/
|
|
48
81
|
build(prefix = 'WHERE') {
|
|
49
82
|
if (!this.parts.length)
|
|
50
83
|
return '';
|