@decaf-ts/for-typeorm 0.0.6

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 (109) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +93 -0
  3. package/dist/for-typeorm.cjs +2553 -0
  4. package/dist/for-typeorm.esm.cjs +2538 -0
  5. package/lib/TypeORMAdapter.cjs +1129 -0
  6. package/lib/TypeORMAdapter.d.ts +221 -0
  7. package/lib/TypeORMDispatch.cjs +134 -0
  8. package/lib/TypeORMDispatch.d.ts +87 -0
  9. package/lib/TypeORMEventSubscriber.cjs +96 -0
  10. package/lib/TypeORMEventSubscriber.d.ts +56 -0
  11. package/lib/TypeORMRepository.cjs +209 -0
  12. package/lib/TypeORMRepository.d.ts +125 -0
  13. package/lib/constants.cjs +43 -0
  14. package/lib/constants.d.ts +39 -0
  15. package/lib/errors.cjs +28 -0
  16. package/lib/errors.d.ts +21 -0
  17. package/lib/esm/TypeORMAdapter.d.ts +221 -0
  18. package/lib/esm/TypeORMAdapter.js +1124 -0
  19. package/lib/esm/TypeORMDispatch.d.ts +87 -0
  20. package/lib/esm/TypeORMDispatch.js +130 -0
  21. package/lib/esm/TypeORMEventSubscriber.d.ts +56 -0
  22. package/lib/esm/TypeORMEventSubscriber.js +93 -0
  23. package/lib/esm/TypeORMRepository.d.ts +125 -0
  24. package/lib/esm/TypeORMRepository.js +206 -0
  25. package/lib/esm/constants.d.ts +39 -0
  26. package/lib/esm/constants.js +40 -0
  27. package/lib/esm/errors.d.ts +21 -0
  28. package/lib/esm/errors.js +24 -0
  29. package/lib/esm/index.d.ts +22 -0
  30. package/lib/esm/index.js +25 -0
  31. package/lib/esm/indexes/generator.d.ts +50 -0
  32. package/lib/esm/indexes/generator.js +95 -0
  33. package/lib/esm/indexes/index.d.ts +1 -0
  34. package/lib/esm/indexes/index.js +2 -0
  35. package/lib/esm/overrides/Column.d.ts +74 -0
  36. package/lib/esm/overrides/Column.js +70 -0
  37. package/lib/esm/overrides/CreateDateColumn.d.ts +2 -0
  38. package/lib/esm/overrides/CreateDateColumn.js +9 -0
  39. package/lib/esm/overrides/Entity.d.ts +11 -0
  40. package/lib/esm/overrides/Entity.js +28 -0
  41. package/lib/esm/overrides/PrimaryColumn.d.ts +20 -0
  42. package/lib/esm/overrides/PrimaryColumn.js +53 -0
  43. package/lib/esm/overrides/PrimaryGeneratedColumn.d.ts +24 -0
  44. package/lib/esm/overrides/PrimaryGeneratedColumn.js +51 -0
  45. package/lib/esm/overrides/UpdateDateColumn.d.ts +2 -0
  46. package/lib/esm/overrides/UpdateDateColumn.js +9 -0
  47. package/lib/esm/overrides/utils.d.ts +2 -0
  48. package/lib/esm/overrides/utils.js +29 -0
  49. package/lib/esm/query/Paginator.d.ts +86 -0
  50. package/lib/esm/query/Paginator.js +124 -0
  51. package/lib/esm/query/Statement.d.ts +131 -0
  52. package/lib/esm/query/Statement.js +242 -0
  53. package/lib/esm/query/constants.d.ts +52 -0
  54. package/lib/esm/query/constants.js +74 -0
  55. package/lib/esm/query/index.d.ts +4 -0
  56. package/lib/esm/query/index.js +5 -0
  57. package/lib/esm/query/translate.d.ts +34 -0
  58. package/lib/esm/query/translate.js +42 -0
  59. package/lib/esm/raw/postgres.d.ts +36 -0
  60. package/lib/esm/raw/postgres.js +2 -0
  61. package/lib/esm/sequences/Sequence.d.ts +67 -0
  62. package/lib/esm/sequences/Sequence.js +117 -0
  63. package/lib/esm/sequences/index.d.ts +1 -0
  64. package/lib/esm/sequences/index.js +2 -0
  65. package/lib/esm/types.d.ts +67 -0
  66. package/lib/esm/types.js +28 -0
  67. package/lib/esm/utils.d.ts +16 -0
  68. package/lib/esm/utils.js +29 -0
  69. package/lib/index.cjs +42 -0
  70. package/lib/index.d.ts +22 -0
  71. package/lib/indexes/generator.cjs +98 -0
  72. package/lib/indexes/generator.d.ts +50 -0
  73. package/lib/indexes/index.cjs +18 -0
  74. package/lib/indexes/index.d.ts +1 -0
  75. package/lib/overrides/Column.cjs +73 -0
  76. package/lib/overrides/Column.d.ts +74 -0
  77. package/lib/overrides/CreateDateColumn.cjs +12 -0
  78. package/lib/overrides/CreateDateColumn.d.ts +2 -0
  79. package/lib/overrides/Entity.cjs +31 -0
  80. package/lib/overrides/Entity.d.ts +11 -0
  81. package/lib/overrides/PrimaryColumn.cjs +56 -0
  82. package/lib/overrides/PrimaryColumn.d.ts +20 -0
  83. package/lib/overrides/PrimaryGeneratedColumn.cjs +54 -0
  84. package/lib/overrides/PrimaryGeneratedColumn.d.ts +24 -0
  85. package/lib/overrides/UpdateDateColumn.cjs +12 -0
  86. package/lib/overrides/UpdateDateColumn.d.ts +2 -0
  87. package/lib/overrides/utils.cjs +32 -0
  88. package/lib/overrides/utils.d.ts +2 -0
  89. package/lib/query/Paginator.cjs +128 -0
  90. package/lib/query/Paginator.d.ts +86 -0
  91. package/lib/query/Statement.cjs +246 -0
  92. package/lib/query/Statement.d.ts +131 -0
  93. package/lib/query/constants.cjs +77 -0
  94. package/lib/query/constants.d.ts +52 -0
  95. package/lib/query/index.cjs +21 -0
  96. package/lib/query/index.d.ts +4 -0
  97. package/lib/query/translate.cjs +45 -0
  98. package/lib/query/translate.d.ts +34 -0
  99. package/lib/raw/postgres.cjs +3 -0
  100. package/lib/raw/postgres.d.ts +36 -0
  101. package/lib/sequences/Sequence.cjs +121 -0
  102. package/lib/sequences/Sequence.d.ts +67 -0
  103. package/lib/sequences/index.cjs +18 -0
  104. package/lib/sequences/index.d.ts +1 -0
  105. package/lib/types.cjs +31 -0
  106. package/lib/types.d.ts +67 -0
  107. package/lib/utils.cjs +32 -0
  108. package/lib/utils.d.ts +16 -0
  109. package/package.json +128 -0
@@ -0,0 +1,221 @@
1
+ import { Adapter, RelationsMetadata, Sequence, type SequenceOptions } from "@decaf-ts/core";
2
+ import { BaseError, Context, OperationKeys } from "@decaf-ts/db-decorators";
3
+ import "reflect-metadata";
4
+ import { type Constructor, Model } from "@decaf-ts/decorator-validation";
5
+ import { TypeORMStatement } from "./query";
6
+ import { TypeORMFlags, TypeORMQuery, TypeORMTableSpec } from "./types";
7
+ import { TypeORMRepository } from "./TypeORMRepository";
8
+ import { TypeORMDispatch } from "./TypeORMDispatch";
9
+ import { DataSource } from "typeorm";
10
+ import { DataSourceOptions } from "typeorm/data-source/DataSourceOptions";
11
+ export declare function createdByOnPostgresCreateUpdate<M extends Model, R extends TypeORMRepository<M>, V extends RelationsMetadata>(this: R, context: Context<TypeORMFlags>, data: V, key: keyof M, model: M): Promise<void>;
12
+ /**
13
+ * @description Adapter for TypeORM-backed persistence operations.
14
+ * @summary Implements the Decaf.ts Adapter over a TypeORM DataSource, providing CRUD operations, query/statement factories, sequence management, error parsing, and decoration helpers.
15
+ * @template Y The native configuration type (TypeORM DataSourceOptions).
16
+ * @template F The repository flags type.
17
+ * @template C The context type.
18
+ * @param {DataSourceOptions} scope The DataSource options for the adapter.
19
+ * @param {string} flavour The flavour of the adapter.
20
+ * @param {string} [alias] Optional alias for the adapter.
21
+ * @class TypeORMAdapter
22
+ * @example
23
+ * const adapter = new TypeORMAdapter({ type: 'postgres', /* ... *\/ });
24
+ * await adapter.initialize();
25
+ * const repo = new (adapter.repository<User>())(adapter, User);
26
+ * const created = await repo.create(new User({ name: 'Alice' }));
27
+ *
28
+ * @mermaid
29
+ * sequenceDiagram
30
+ * participant App
31
+ * participant Adapter as TypeORMAdapter
32
+ * participant Repo as TypeORMRepository
33
+ * participant DS as TypeORM DataSource
34
+ *
35
+ * App->>Adapter: new TypeORMAdapter(opts)
36
+ * Adapter->>DS: initialize()
37
+ * App->>Adapter: repository()
38
+ * Adapter-->>App: TypeORMRepository
39
+ * App->>Repo: create(model)
40
+ * Repo->>Adapter: prepare/create/revert
41
+ * Adapter-->>Repo: Model
42
+ * Repo-->>App: Model
43
+ */
44
+ export declare class TypeORMAdapter extends Adapter<DataSourceOptions, TypeORMQuery, TypeORMFlags, Context<TypeORMFlags>> {
45
+ private _dataSource?;
46
+ get dataSource(): DataSource;
47
+ constructor(options: DataSourceOptions, alias?: string);
48
+ protected flags<M extends Model>(operation: OperationKeys, model: Constructor<M>, flags: Partial<TypeORMFlags>): Promise<TypeORMFlags>;
49
+ protected Dispatch(): TypeORMDispatch;
50
+ repository<M extends Model>(): Constructor<TypeORMRepository<M>>;
51
+ /**
52
+ * @description Creates a new Postgres statement for querying
53
+ * @summary Factory method that creates a new PostgresStatement instance for building queries
54
+ * @template M - The model type
55
+ * @return {TypeORMStatement<M, any>} A new PostgresStatement instance
56
+ */
57
+ Statement<M extends Model>(): TypeORMStatement<M, any>;
58
+ /**
59
+ * @description Creates a new PostgreSQL sequence
60
+ * @summary Factory method that creates a new PostgreSQLSequence instance for managing sequences
61
+ * @param {SequenceOptions} options - The options for the sequence
62
+ * @return {Promise<Sequence>} A promise that resolves to a new Sequence instance
63
+ */
64
+ Sequence(options: SequenceOptions): Promise<Sequence>;
65
+ /**
66
+ * @description Initializes the adapter by creating indexes for all managed models
67
+ * @summary Sets up the necessary database indexes for all models managed by this adapter
68
+ * @return {Promise<void>} A promise that resolves when initialization is complete
69
+ */
70
+ initialize(): Promise<void>;
71
+ /**
72
+ * @description Creates indexes for the given models
73
+ * @summary Abstract method that must be implemented to create database indexes for the specified models
74
+ * @template M - The model type
75
+ * @param {...Constructor<M>} models - The model constructors to create indexes for
76
+ * @return {Promise<void>} A promise that resolves when all indexes are created
77
+ */
78
+ protected index<M extends Model>(...models: Constructor<M>[]): Promise<void>;
79
+ /**
80
+ * @description Executes a raw SQL query against the database
81
+ * @summary Abstract method that must be implemented to execute raw SQL queries
82
+ * @template R - The result type
83
+ * @param {TypeORMQuery} q - The query to execute
84
+ * @return {Promise<R>} A promise that resolves to the query result
85
+ */
86
+ raw<R>(q: TypeORMQuery): Promise<R>;
87
+ prepare<M extends Model>(model: M, pk: keyof M, child?: boolean): {
88
+ record: Record<string, any>;
89
+ id: string;
90
+ transient?: Record<string, any>;
91
+ };
92
+ revert<M extends Model>(obj: Record<string, any>, clazz: string | Constructor<M>, pk: keyof M, id: string | number | bigint, transient?: Record<string, any>): M;
93
+ /**
94
+ * @description Creates a new record in the database
95
+ * @summary Abstract method that must be implemented to create a new record
96
+ * @param {string} tableName - The name of the table
97
+ * @param {string|number} id - The ID of the record
98
+ * @param {Record<string, any>} model - The model to create
99
+ * @param {...any[]} args - Additional arguments
100
+ * @return {Promise<Record<string, any>>} A promise that resolves to the created record
101
+ */
102
+ create(tableName: string, id: string | number, model: Record<string, any>, ...args: any[]): Promise<Record<string, any>>;
103
+ /**
104
+ * @description Reads a record from the database
105
+ * @summary Abstract method that must be implemented to read a record
106
+ * @param {string} tableName - The name of the table
107
+ * @param {string|number} id - The ID of the record
108
+ * @param {string} pk - primary key colum
109
+ * @return {Promise<Record<string, any>>} A promise that resolves to the read record
110
+ */
111
+ read(tableName: string, id: string | number, pk: string): Promise<Record<string, any>>;
112
+ /**
113
+ * @description Updates a record in the database
114
+ * @summary Abstract method that must be implemented to update a record
115
+ * @param {string} tableName - The name of the table
116
+ * @param {string|number} id - The ID of the record
117
+ * @param {Record<string, any>} model - The model to update
118
+ * @param {string} pk - Additional arguments
119
+ * @return A promise that resolves to the updated record
120
+ */
121
+ update(tableName: string, id: string | number, model: Record<string, any>, ...args: any[]): Promise<Record<string, any>>;
122
+ /**
123
+ * @description Deletes a record from the database
124
+ * @summary Abstract method that must be implemented to delete a record
125
+ * @param {string} tableName - The name of the table
126
+ * @param {string|number} id - The ID of the record
127
+ * @param {string} pk - Additional arguments
128
+ * @return A promise that resolves to the deleted record
129
+ */
130
+ delete(tableName: string, id: string | number, pk: string, ...args: any[]): Promise<Record<string, any>>;
131
+ createAll(tableName: string, id: (string | number)[], model: Record<string, any>[], ...args: any[]): Promise<Record<string, any>[]>;
132
+ readAll(tableName: string, id: (string | number | bigint)[], pk: string, ...args: any[]): Promise<Record<string, any>[]>;
133
+ updateAll(tableName: string, ids: string[] | number[], model: Record<string, any>[], pk: string, ...args: any[]): Promise<Record<string, any>[]>;
134
+ deleteAll(tableName: string, ids: (string | number | bigint)[], pk: string, ...args: any[]): Promise<Record<string, any>[]>;
135
+ /**
136
+ * @description Parses an error and converts it to a BaseError
137
+ * @summary Converts various error types to appropriate BaseError subtypes
138
+ * @param {Error|string} err - The error to parse
139
+ * @param {string} [reason] - Optional reason for the error
140
+ * @return {BaseError} The parsed error as a BaseError
141
+ */
142
+ parseError(err: Error | string, reason?: string): BaseError;
143
+ /**
144
+ * @description Checks if an attribute is reserved
145
+ * @summary Determines if an attribute name is reserved in PostgreSQL
146
+ * @param {string} attr - The attribute name to check
147
+ * @return {boolean} True if the attribute is reserved, false otherwise
148
+ */
149
+ protected isReserved(attr: string): boolean;
150
+ /**
151
+ * @description Static method to parse an error and convert it to a BaseError
152
+ * @summary Converts various error types to appropriate BaseError subtypes based on PostgreSQL error codes and messages
153
+ * @param {Error|string} err - The error to parse
154
+ * @param {string} [reason] - Optional reason for the error
155
+ * @return {BaseError} The parsed error as a BaseError
156
+ * @mermaid
157
+ * sequenceDiagram
158
+ * participant Caller
159
+ * participant parseError
160
+ * participant ErrorTypes
161
+ *
162
+ * Caller->>parseError: err, reason
163
+ * Note over parseError: Check if err is already a BaseError
164
+ * alt err is BaseError
165
+ * parseError-->>Caller: return err
166
+ * else err is string
167
+ * Note over parseError: Extract code from string
168
+ * alt code matches "duplicate key|already exists"
169
+ * parseError->>ErrorTypes: new ConflictError(code)
170
+ * ErrorTypes-->>Caller: ConflictError
171
+ * else code matches "does not exist|not found"
172
+ * parseError->>ErrorTypes: new NotFoundError(code)
173
+ * ErrorTypes-->>Caller: NotFoundError
174
+ * end
175
+ * else err has code property
176
+ * Note over parseError: Extract code and reason
177
+ * else
178
+ * Note over parseError: Use err.message as code
179
+ * end
180
+ *
181
+ * Note over parseError: Switch on PostgreSQL error code
182
+ * alt code is 23505 (unique_violation)
183
+ * parseError->>ErrorTypes: new ConflictError(reason)
184
+ * ErrorTypes-->>Caller: ConflictError
185
+ * else code is 23503 (foreign_key_violation)
186
+ * parseError->>ErrorTypes: new ConflictError(reason)
187
+ * ErrorTypes-->>Caller: ConflictError
188
+ * else code is 42P01 (undefined_table)
189
+ * parseError->>ErrorTypes: new NotFoundError(reason)
190
+ * ErrorTypes-->>Caller: NotFoundError
191
+ * else code is 42703 (undefined_column)
192
+ * parseError->>ErrorTypes: new NotFoundError(reason)
193
+ * ErrorTypes-->>Caller: NotFoundError
194
+ * else code is 42P07 (duplicate_table)
195
+ * parseError->>ErrorTypes: new ConflictError(reason)
196
+ * ErrorTypes-->>Caller: ConflictError
197
+ * else code is 42P16 (invalid_table_definition)
198
+ * parseError->>ErrorTypes: new IndexError(err)
199
+ * ErrorTypes-->>Caller: IndexError
200
+ * else code matches "ECONNREFUSED"
201
+ * parseError->>ErrorTypes: new ConnectionError(err)
202
+ * ErrorTypes-->>Caller: ConnectionError
203
+ * else
204
+ * parseError->>ErrorTypes: new InternalError(err)
205
+ * ErrorTypes-->>Caller: InternalError
206
+ * end
207
+ */
208
+ protected static parseError(err: Error | string, reason?: string): BaseError;
209
+ static connect(config: DataSourceOptions): Promise<DataSource>;
210
+ static createDatabase(dataSource: DataSource, dbName: string): Promise<void>;
211
+ static createNotifyFunction(dataSource: DataSource, user: string): Promise<void>;
212
+ static deleteDatabase(dataSource: DataSource, dbName: string, user?: string): Promise<void>;
213
+ static createUser(dataSource: DataSource, dbName: string, user: string, password: string): Promise<void>;
214
+ static deleteUser(client: DataSource, user: string, admin: string): Promise<void>;
215
+ private static parseTypeToPostgres;
216
+ private static parseValidationToPostgres;
217
+ private static parseRelationsToPostgres;
218
+ static createTable<M extends Model>(client: DataSource, model: Constructor<M>): Promise<Record<string, TypeORMTableSpec>>;
219
+ static getCurrentUser(client: DataSource): Promise<string>;
220
+ static decoration(): void;
221
+ }