@hexaijs/postgres 0.4.0 → 0.5.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.
Files changed (58) hide show
  1. package/dist/helpers-vPAudN_S.d.ts +125 -0
  2. package/dist/index.d.ts +64 -8
  3. package/dist/index.js +828 -29
  4. package/dist/index.js.map +1 -1
  5. package/dist/test.d.ts +9 -8
  6. package/dist/test.js +683 -246
  7. package/dist/test.js.map +1 -1
  8. package/package.json +7 -7
  9. package/dist/config/index.d.ts +0 -3
  10. package/dist/config/index.d.ts.map +0 -1
  11. package/dist/config/index.js +0 -19
  12. package/dist/config/index.js.map +0 -1
  13. package/dist/config/postgres-config-spec.d.ts +0 -32
  14. package/dist/config/postgres-config-spec.d.ts.map +0 -1
  15. package/dist/config/postgres-config-spec.js +0 -49
  16. package/dist/config/postgres-config-spec.js.map +0 -1
  17. package/dist/config/postgres-config.d.ts +0 -59
  18. package/dist/config/postgres-config.d.ts.map +0 -1
  19. package/dist/config/postgres-config.js +0 -181
  20. package/dist/config/postgres-config.js.map +0 -1
  21. package/dist/helpers.d.ts +0 -57
  22. package/dist/helpers.d.ts.map +0 -1
  23. package/dist/helpers.js +0 -276
  24. package/dist/helpers.js.map +0 -1
  25. package/dist/index.d.ts.map +0 -1
  26. package/dist/postgres-event-store.d.ts +0 -18
  27. package/dist/postgres-event-store.d.ts.map +0 -1
  28. package/dist/postgres-event-store.js +0 -83
  29. package/dist/postgres-event-store.js.map +0 -1
  30. package/dist/postgres-unit-of-work.d.ts +0 -24
  31. package/dist/postgres-unit-of-work.d.ts.map +0 -1
  32. package/dist/postgres-unit-of-work.js +0 -308
  33. package/dist/postgres-unit-of-work.js.map +0 -1
  34. package/dist/run-hexai-migrations.d.ts +0 -3
  35. package/dist/run-hexai-migrations.d.ts.map +0 -1
  36. package/dist/run-hexai-migrations.js +0 -17
  37. package/dist/run-hexai-migrations.js.map +0 -1
  38. package/dist/run-migrations.d.ts +0 -11
  39. package/dist/run-migrations.d.ts.map +0 -1
  40. package/dist/run-migrations.js +0 -202
  41. package/dist/run-migrations.js.map +0 -1
  42. package/dist/test-fixtures/config.d.ts +0 -5
  43. package/dist/test-fixtures/config.d.ts.map +0 -1
  44. package/dist/test-fixtures/config.js +0 -14
  45. package/dist/test-fixtures/config.js.map +0 -1
  46. package/dist/test-fixtures/hooks.d.ts +0 -8
  47. package/dist/test-fixtures/hooks.d.ts.map +0 -1
  48. package/dist/test-fixtures/hooks.js +0 -77
  49. package/dist/test-fixtures/hooks.js.map +0 -1
  50. package/dist/test-fixtures/index.d.ts +0 -3
  51. package/dist/test-fixtures/index.d.ts.map +0 -1
  52. package/dist/test-fixtures/index.js +0 -19
  53. package/dist/test-fixtures/index.js.map +0 -1
  54. package/dist/test.d.ts.map +0 -1
  55. package/dist/types.d.ts +0 -14
  56. package/dist/types.d.ts.map +0 -1
  57. package/dist/types.js +0 -11
  58. package/dist/types.js.map +0 -1
@@ -0,0 +1,125 @@
1
+ import * as pg from 'pg';
2
+ import { BaseUnitOfWorkOptions, UnitOfWork } from '@hexaijs/core';
3
+ import { DatabaseConfig } from 'ezcfg';
4
+
5
+ interface PoolOptions {
6
+ size?: number;
7
+ connectionTimeout?: number;
8
+ idleTimeout?: number;
9
+ }
10
+ interface FromEnvOptions {
11
+ /**
12
+ * Environment variable loading mode.
13
+ * - "url": Load from {PREFIX}_URL (default)
14
+ * - "fields": Load from {PREFIX}_HOST, {PREFIX}_PORT, {PREFIX}_DATABASE, {PREFIX}_USER, {PREFIX}_PASSWORD
15
+ */
16
+ mode?: "url" | "fields";
17
+ }
18
+ declare class PostgresConfig implements DatabaseConfig {
19
+ readonly host: string;
20
+ readonly database: string;
21
+ readonly user: string;
22
+ readonly port: number;
23
+ readonly password?: string;
24
+ readonly pool?: PoolOptions;
25
+ constructor(config: {
26
+ database: string;
27
+ user?: string;
28
+ host?: string;
29
+ port?: number;
30
+ password?: string;
31
+ pool?: PoolOptions;
32
+ });
33
+ static fromUrl(value: string): PostgresConfig;
34
+ /**
35
+ * Creates a PostgresConfig from environment variables.
36
+ *
37
+ * @param prefix - Environment variable prefix
38
+ * @param options - Loading options (mode: "url" | "fields")
39
+ * @throws Error if required environment variables are not set
40
+ *
41
+ * @example
42
+ * ```typescript
43
+ * // URL mode (default): reads ASSIGNMENT_DB_URL
44
+ * const config = PostgresConfig.fromEnv("ASSIGNMENT_DB");
45
+ *
46
+ * // Fields mode: reads POSTGRES_HOST, POSTGRES_PORT, POSTGRES_DATABASE, POSTGRES_USER, POSTGRES_PASSWORD
47
+ * const config = PostgresConfig.fromEnv("POSTGRES", { mode: "fields" });
48
+ * ```
49
+ */
50
+ static fromEnv(prefix: string, options?: FromEnvOptions): PostgresConfig;
51
+ private static parseUrl;
52
+ withDatabase(database: string): PostgresConfig;
53
+ withUser(user: string): PostgresConfig;
54
+ withPassword(password: string): PostgresConfig;
55
+ withHost(host: string): PostgresConfig;
56
+ withPort(port: number): PostgresConfig;
57
+ withPoolSize(size: number): PostgresConfig;
58
+ withConnectionTimeout(connectionTimeout: number): PostgresConfig;
59
+ withIdleTimeout(idleTimeout: number): PostgresConfig;
60
+ toString(): string;
61
+ }
62
+
63
+ type ClientFactory = () => pg.ClientBase | Promise<pg.ClientBase>;
64
+ type ClientCleanUp = (client: pg.ClientBase) => void | Promise<void>;
65
+ declare enum IsolationLevel {
66
+ READ_UNCOMMITTED = "read uncommitted",
67
+ READ_COMMITTED = "read committed",
68
+ REPEATABLE_READ = "repeatable read",
69
+ SERIALIZABLE = "serializable"
70
+ }
71
+ interface PostgresTransactionOptions extends BaseUnitOfWorkOptions {
72
+ isolationLevel?: IsolationLevel;
73
+ }
74
+
75
+ interface PostgresUnitOfWork extends UnitOfWork<pg.ClientBase, PostgresTransactionOptions> {
76
+ withClient<T>(fn: (client: pg.ClientBase) => Promise<T>): Promise<T>;
77
+ }
78
+ declare class DefaultPostgresUnitOfWork implements PostgresUnitOfWork {
79
+ private clientFactory;
80
+ private clientCleanUp?;
81
+ private transactionStorage;
82
+ constructor(clientFactory: ClientFactory, clientCleanUp?: ClientCleanUp | undefined);
83
+ getClient(): pg.ClientBase;
84
+ wrap<T = unknown>(fn: (client: pg.ClientBase) => Promise<T>, options?: Partial<PostgresTransactionOptions>): Promise<T>;
85
+ withClient<T>(fn: (client: pg.ClientBase) => Promise<T>): Promise<T>;
86
+ private getCurrentTransaction;
87
+ private resolveOptions;
88
+ private resolveTransaction;
89
+ private createTransaction;
90
+ private executeInContext;
91
+ }
92
+ declare function createPostgresUnitOfWork(pool: pg.Pool): PostgresUnitOfWork;
93
+ declare function createPostgresUnitOfWork(config: PostgresConfig | string): PostgresUnitOfWork;
94
+
95
+ declare class ClientWrapper {
96
+ protected client: pg.Client;
97
+ getClient(): pg.Client;
98
+ constructor(urlOrClient: PostgresConfig | string | pg.Client);
99
+ protected withClient<T = unknown>(work: (client: pg.Client) => Promise<T>): Promise<T>;
100
+ query<R = any>(query: string, params?: any[]): Promise<Array<R>>;
101
+ close(): Promise<void>;
102
+ }
103
+ declare class DatabaseManager extends ClientWrapper {
104
+ createDatabase(name: string): Promise<void>;
105
+ dropDatabase(name: string): Promise<void>;
106
+ }
107
+ declare class TableManager extends ClientWrapper {
108
+ getTableSchema(tableName: string): Promise<Array<{
109
+ column: string;
110
+ type: string;
111
+ }>>;
112
+ tableExists(tableName: string): Promise<boolean>;
113
+ createTable(name: string, columns: Array<{
114
+ name: string;
115
+ property: string;
116
+ }>): Promise<void>;
117
+ dropTable(name: string): Promise<void>;
118
+ truncateTable(name: string): Promise<void>;
119
+ truncateAllTables(): Promise<void>;
120
+ dropAllTables(): Promise<void>;
121
+ private getTableNames;
122
+ }
123
+ declare function ensureConnection(client: pg.ClientBase): Promise<void>;
124
+
125
+ export { type ClientCleanUp as C, DatabaseManager as D, type FromEnvOptions as F, IsolationLevel as I, PostgresConfig as P, TableManager as T, type ClientFactory as a, ClientWrapper as b, DefaultPostgresUnitOfWork as c, type PoolOptions as d, type PostgresTransactionOptions as e, type PostgresUnitOfWork as f, createPostgresUnitOfWork as g, ensureConnection as h };
package/dist/index.d.ts CHANGED
@@ -1,8 +1,64 @@
1
- export * from "./postgres-unit-of-work";
2
- export * from "./run-migrations";
3
- export * from "./run-hexai-migrations";
4
- export { ClientWrapper, DatabaseManager, TableManager, ensureConnection, } from "./helpers";
5
- export * from "./postgres-event-store";
6
- export * from "./types";
7
- export * from "./config";
8
- //# sourceMappingURL=index.d.ts.map
1
+ import { P as PostgresConfig, F as FromEnvOptions } from './helpers-vPAudN_S.js';
2
+ export { C as ClientCleanUp, a as ClientFactory, b as ClientWrapper, D as DatabaseManager, c as DefaultPostgresUnitOfWork, I as IsolationLevel, d as PoolOptions, e as PostgresTransactionOptions, f as PostgresUnitOfWork, T as TableManager, g as createPostgresUnitOfWork, h as ensureConnection } from './helpers-vPAudN_S.js';
3
+ import { Client, PoolClient } from 'pg';
4
+ import { EventStore, Message, StoredEvent, EventStoreFetchResult } from '@hexaijs/core';
5
+ import { ConfigSpec } from 'ezcfg';
6
+
7
+ declare class PostgresConfigSpec implements ConfigSpec<PostgresConfig> {
8
+ private readonly prefix;
9
+ private readonly mode;
10
+ readonly _type = "postgres";
11
+ constructor(prefix: string, mode?: FromEnvOptions["mode"]);
12
+ resolve(errors: string[]): PostgresConfig | undefined;
13
+ }
14
+ /**
15
+ * PostgreSQL database configuration from environment variables.
16
+ * Returns a PostgresConfig instance.
17
+ *
18
+ * @param prefix - Environment variable prefix
19
+ * @param mode - "url" reads {PREFIX}_URL, "fields" reads individual fields
20
+ *
21
+ * @example
22
+ * ```typescript
23
+ * import { defineConfig, env } from "@hexaijs/core";
24
+ * import { postgresConfig } from "@hexaijs/postgres";
25
+ *
26
+ * const getConfig = defineConfig({
27
+ * db: postgresConfig("ORDER_DB"), // reads ORDER_DB_URL
28
+ * db2: postgresConfig("PG", "fields"), // reads PG_HOST, PG_PORT, etc.
29
+ * });
30
+ *
31
+ * getConfig().db.host; // "localhost"
32
+ * getConfig().db.toString(); // "postgres://..."
33
+ * ```
34
+ */
35
+ declare function postgresConfig(prefix: string, mode?: FromEnvOptions["mode"]): PostgresConfigSpec;
36
+
37
+ interface MigrationOptions {
38
+ url: PostgresConfig | string;
39
+ dir: string;
40
+ namespace?: string;
41
+ direction?: "up" | "down";
42
+ count?: number;
43
+ dryRun?: boolean;
44
+ }
45
+ declare function runMigrations({ namespace, url, dir, direction, count, dryRun, }: MigrationOptions): Promise<void>;
46
+
47
+ declare function runHexaiMigrations(dbUrl: string | PostgresConfig): Promise<void>;
48
+
49
+ type PgClient = Client | PoolClient;
50
+ interface PostgresEventStoreConfig {
51
+ tableName?: string;
52
+ }
53
+ declare class PostgresEventStore implements EventStore {
54
+ private readonly client;
55
+ private readonly tableName;
56
+ constructor(client: PgClient, config?: PostgresEventStoreConfig);
57
+ store(event: Message): Promise<StoredEvent>;
58
+ storeAll(events: Message[]): Promise<StoredEvent[]>;
59
+ fetch(afterPosition: number, limit?: number): Promise<EventStoreFetchResult>;
60
+ getLastPosition(): Promise<number>;
61
+ private deserializeRow;
62
+ }
63
+
64
+ export { FromEnvOptions, type MigrationOptions, PostgresConfig, PostgresConfigSpec, PostgresEventStore, type PostgresEventStoreConfig, postgresConfig, runHexaiMigrations, runMigrations };