@joktec/mysql 0.2.14 → 0.2.16
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 +214 -2
- package/dist/__tests__/mysql.column.decorator.spec.js +515 -1
- package/dist/__tests__/mysql.column.decorator.spec.js.map +1 -1
- package/dist/__tests__/mysql.dialect.spec.js +1 -0
- package/dist/__tests__/mysql.dialect.spec.js.map +1 -1
- package/dist/decorators/column.decorator.d.ts +4 -5
- package/dist/decorators/column.decorator.d.ts.map +1 -1
- package/dist/decorators/column.decorator.js +151 -5
- package/dist/decorators/column.decorator.js.map +1 -1
- package/dist/decorators/columns/array.column.d.ts +2 -1
- package/dist/decorators/columns/array.column.d.ts.map +1 -1
- package/dist/decorators/columns/array.column.js +7 -2
- package/dist/decorators/columns/array.column.js.map +1 -1
- package/dist/decorators/columns/column.factory.d.ts +3 -3
- package/dist/decorators/columns/column.factory.d.ts.map +1 -1
- package/dist/decorators/columns/column.factory.js +4 -2
- package/dist/decorators/columns/column.factory.js.map +1 -1
- package/dist/decorators/columns/column.type.d.ts +107 -4
- package/dist/decorators/columns/column.type.d.ts.map +1 -1
- package/dist/decorators/columns/column.util.d.ts +13 -6
- package/dist/decorators/columns/column.util.d.ts.map +1 -1
- package/dist/decorators/columns/column.util.js +85 -4
- package/dist/decorators/columns/column.util.js.map +1 -1
- package/dist/decorators/columns/enum.column.d.ts +2 -2
- package/dist/decorators/columns/enum.column.d.ts.map +1 -1
- package/dist/decorators/columns/enum.column.js.map +1 -1
- package/dist/decorators/columns/index.d.ts +3 -0
- package/dist/decorators/columns/index.d.ts.map +1 -1
- package/dist/decorators/columns/index.js +3 -0
- package/dist/decorators/columns/index.js.map +1 -1
- package/dist/decorators/columns/nested.column.d.ts +2 -2
- package/dist/decorators/columns/nested.column.d.ts.map +1 -1
- package/dist/decorators/columns/nested.column.js +2 -2
- package/dist/decorators/columns/nested.column.js.map +1 -1
- package/dist/decorators/columns/number.column.d.ts +2 -2
- package/dist/decorators/columns/number.column.d.ts.map +1 -1
- package/dist/decorators/columns/number.column.js +2 -1
- package/dist/decorators/columns/number.column.js.map +1 -1
- package/dist/decorators/columns/object.column.d.ts +4 -0
- package/dist/decorators/columns/object.column.d.ts.map +1 -0
- package/dist/decorators/columns/object.column.js +13 -0
- package/dist/decorators/columns/object.column.js.map +1 -0
- package/dist/decorators/columns/string.column.d.ts +2 -2
- package/dist/decorators/columns/string.column.d.ts.map +1 -1
- package/dist/decorators/columns/string.column.js +10 -7
- package/dist/decorators/columns/string.column.js.map +1 -1
- package/dist/decorators/columns/swagger.column.d.ts +2 -2
- package/dist/decorators/columns/swagger.column.d.ts.map +1 -1
- package/dist/decorators/columns/swagger.column.js +10 -1
- package/dist/decorators/columns/swagger.column.js.map +1 -1
- package/dist/decorators/columns/timestamp.column.d.ts +6 -0
- package/dist/decorators/columns/timestamp.column.d.ts.map +1 -0
- package/dist/decorators/columns/timestamp.column.js +55 -0
- package/dist/decorators/columns/timestamp.column.js.map +1 -0
- package/dist/decorators/columns/transform.column.d.ts +2 -2
- package/dist/decorators/columns/transform.column.d.ts.map +1 -1
- package/dist/decorators/columns/transform.column.js +5 -2
- package/dist/decorators/columns/transform.column.js.map +1 -1
- package/dist/decorators/columns/virtual.column.d.ts +4 -0
- package/dist/decorators/columns/virtual.column.d.ts.map +1 -0
- package/dist/decorators/columns/virtual.column.js +23 -0
- package/dist/decorators/columns/virtual.column.js.map +1 -0
- package/dist/decorators/index.d.ts +2 -0
- package/dist/decorators/index.d.ts.map +1 -1
- package/dist/decorators/index.js +1 -0
- package/dist/decorators/index.js.map +1 -1
- package/dist/decorators/table.decorator.d.ts +31 -2
- package/dist/decorators/table.decorator.d.ts.map +1 -1
- package/dist/decorators/table.decorator.js +55 -24
- package/dist/decorators/table.decorator.js.map +1 -1
- package/dist/decorators/timestamp.decorator.d.ts +4 -0
- package/dist/decorators/timestamp.decorator.d.ts.map +1 -0
- package/dist/decorators/timestamp.decorator.js +19 -0
- package/dist/decorators/timestamp.decorator.js.map +1 -0
- package/dist/helpers/index.d.ts +0 -1
- package/dist/helpers/index.d.ts.map +1 -1
- package/dist/helpers/index.js +0 -1
- package/dist/helpers/index.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/models/mysql.model.d.ts.map +1 -1
- package/dist/models/mysql.model.js +4 -11
- package/dist/models/mysql.model.js.map +1 -1
- package/dist/mysql.repo.d.ts +1 -2
- package/dist/mysql.repo.d.ts.map +1 -1
- package/dist/mysql.repo.js +0 -15
- package/dist/mysql.repo.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/dist/helpers/mysql.finder.d.ts +0 -15
- package/dist/helpers/mysql.finder.d.ts.map +0 -1
- package/dist/helpers/mysql.finder.js +0 -128
- package/dist/helpers/mysql.finder.js.map +0 -1
package/README.md
CHANGED
|
@@ -30,8 +30,9 @@ yarn add @joktec/mysql
|
|
|
30
30
|
- `@Tables`
|
|
31
31
|
- `@Column`
|
|
32
32
|
- `@PrimaryColumn`
|
|
33
|
+
- `@PrimaryGeneratedColumn`
|
|
34
|
+
- `@TimestampColumn`
|
|
33
35
|
- `MysqlHelper`
|
|
34
|
-
- `MysqlFinder`
|
|
35
36
|
- `MysqlNamingStrategy`
|
|
36
37
|
- selected TypeORM exports.
|
|
37
38
|
|
|
@@ -192,6 +193,14 @@ Keep entity definitions in the consuming app or package. Keep app-specific query
|
|
|
192
193
|
|
|
193
194
|
`@joktec/mysql` exposes schema-first decorators that wrap TypeORM metadata together with Swagger, `class-validator`, and `class-transformer` metadata. Use them when an entity should be reused as the source class for mapped DTOs.
|
|
194
195
|
|
|
196
|
+
The wrapper philosophy is pragmatic:
|
|
197
|
+
|
|
198
|
+
- use `@Column` as the main property-level wrapper for common TypeORM column, relation, relation-id, version, view, and virtual metadata
|
|
199
|
+
- keep `@PrimaryColumn` / `@PrimaryGeneratedColumn` and `@TimestampColumn` separate because primary keys and timestamps have strong business semantics
|
|
200
|
+
- infer validation, transform, and Swagger metadata from one source of truth whenever possible
|
|
201
|
+
- keep raw TypeORM available for rare advanced cases instead of wrapping every decorator
|
|
202
|
+
- use `immutable` as the API read-only hint, while `update: false` remains the TypeORM write behavior
|
|
203
|
+
|
|
195
204
|
```ts
|
|
196
205
|
import { Column, MysqlModel, PrimaryColumn, Tables } from '@joktec/mysql';
|
|
197
206
|
|
|
@@ -211,10 +220,164 @@ export class ProfileBadge extends MysqlModel {
|
|
|
211
220
|
}
|
|
212
221
|
```
|
|
213
222
|
|
|
214
|
-
`@Column` accepts normal TypeORM column options and adds optional schema metadata such as `hidden`, `
|
|
223
|
+
`@Column` accepts normal TypeORM column options and adds optional schema metadata such as `hidden`, `optional`, `expose`, `nested`, `each`, `example`, `deprecated`, `immutable`, `swagger`, `decorators`, `required`, `isEmail`, `isPhone`, `isHexColor`, `isUrl`, `isInt`, `isUUID`, `isObject`, `minlength`, `maxlength`, `min`, `max`, `index`, and `check`.
|
|
215
224
|
|
|
216
225
|
`@PrimaryColumn` supports TypeORM generated strategies (`increment`, `uuid`, `rowid`, `identity`) and JokTec-managed `uuidv7`. `uuidv7` is stored as a 36-character varchar and generated before insert when the entity does not already have an id.
|
|
217
226
|
|
|
227
|
+
`@TimestampColumn('create' | 'update' | 'delete')` wraps TypeORM create/update/delete timestamp columns and adds the shared validation, transform, Swagger, and GraphQL metadata used by the column wrapper.
|
|
228
|
+
|
|
229
|
+
### Read-Only and Immutable Metadata
|
|
230
|
+
|
|
231
|
+
`immutable` controls generated Swagger `readOnly` metadata. It is intentionally named to match the Mongo schema wrapper terminology.
|
|
232
|
+
|
|
233
|
+
TypeORM `update: false` still controls ORM write behavior. When `immutable` is not set, `update: false` is also treated as Swagger `readOnly`.
|
|
234
|
+
|
|
235
|
+
Priority:
|
|
236
|
+
|
|
237
|
+
1. `swagger.readOnly` is the final override
|
|
238
|
+
2. `immutable` controls API read-only metadata
|
|
239
|
+
3. `update: false` falls back to API read-only metadata
|
|
240
|
+
|
|
241
|
+
Examples:
|
|
242
|
+
|
|
243
|
+
```ts
|
|
244
|
+
class AuditEntity extends MysqlModel {
|
|
245
|
+
@Column({ type: 'varchar', length: 36, update: false })
|
|
246
|
+
createdBy?: string;
|
|
247
|
+
|
|
248
|
+
@Column({ type: 'varchar', length: 36, immutable: true })
|
|
249
|
+
updatedBy?: string;
|
|
250
|
+
|
|
251
|
+
@Column({ type: 'varchar', update: false, immutable: false })
|
|
252
|
+
serviceInsertOnlyButClientWritable?: string;
|
|
253
|
+
}
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
The wrapper defaults read-only Swagger metadata for naturally system-managed or computed fields:
|
|
257
|
+
|
|
258
|
+
- `@PrimaryColumn(...)` and `@PrimaryGeneratedColumn(...)`
|
|
259
|
+
- `@TimestampColumn('create' | 'update' | 'delete')`
|
|
260
|
+
- `@Column({ kind: 'version' })`
|
|
261
|
+
- `@Column({ kind: 'view' })`
|
|
262
|
+
- `@Column({ kind: 'virtual' })` getter fields
|
|
263
|
+
- `@Column({ kind: 'virtual', mode: 'sql' })`
|
|
264
|
+
- `@Column({ kind: 'relation-id' })`
|
|
265
|
+
- `@Column({ kind: 'tree', tree: 'level' })`
|
|
266
|
+
|
|
267
|
+
Special TypeORM column modes are exposed through `@Column({ kind })` instead of standalone wrapper names:
|
|
268
|
+
|
|
269
|
+
```ts
|
|
270
|
+
class ProfileStats {
|
|
271
|
+
@Column({ kind: 'version' })
|
|
272
|
+
version?: number;
|
|
273
|
+
|
|
274
|
+
@Column({ kind: 'view', name: 'display_name' })
|
|
275
|
+
displayName?: string;
|
|
276
|
+
|
|
277
|
+
@Column('int', {
|
|
278
|
+
kind: 'virtual',
|
|
279
|
+
mode: 'sql',
|
|
280
|
+
query: alias => `SELECT COUNT(*) FROM follows WHERE follows.profile_id = ${alias}.id`,
|
|
281
|
+
})
|
|
282
|
+
followerCount?: number;
|
|
283
|
+
|
|
284
|
+
@Column({ kind: 'virtual', comment: 'Display label' })
|
|
285
|
+
get label(): string {
|
|
286
|
+
return `${this.displayName}`;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
`kind: 'virtual'` defaults to metadata-only TypeScript getters. Use `mode: 'sql'` for TypeORM SQL-calculated virtual columns that require a `query(alias)` option.
|
|
292
|
+
|
|
293
|
+
Relations and relation ids can also be expressed through `@Column`:
|
|
294
|
+
|
|
295
|
+
```ts
|
|
296
|
+
class ArticleEntity extends MysqlModel {
|
|
297
|
+
@Column('uuid', { nullable: true, index: 'IDX_article_author_id', isUUID: true })
|
|
298
|
+
authorId?: string;
|
|
299
|
+
|
|
300
|
+
@Column({
|
|
301
|
+
kind: 'relation',
|
|
302
|
+
relation: 'many-to-one',
|
|
303
|
+
type: () => UserEntity,
|
|
304
|
+
inverseSide: user => user.articles,
|
|
305
|
+
joinColumn: { name: 'author_id' },
|
|
306
|
+
nullable: true,
|
|
307
|
+
})
|
|
308
|
+
author?: UserEntity;
|
|
309
|
+
|
|
310
|
+
@Column({
|
|
311
|
+
kind: 'relation-id',
|
|
312
|
+
relationId: (article: ArticleEntity) => article.author,
|
|
313
|
+
nullable: true,
|
|
314
|
+
})
|
|
315
|
+
resolvedAuthorId?: string;
|
|
316
|
+
}
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
Relation wrappers keep the Swagger type as the same lazy resolver passed to `type`. Do not add `swagger: { type: () => Entity }` for normal relations; reserve `swagger` for genuine OpenAPI shape overrides. This keeps circular or two-way entity relations compact and avoids eager schema evaluation.
|
|
320
|
+
|
|
321
|
+
`index` and `check` can be declared on normal, version, relation, and relation-id columns when the constraint belongs to one property. Use `@Tables({ checks: [...] })` for composite table checks.
|
|
322
|
+
|
|
323
|
+
`@Tables` supports common class-level TypeORM metadata:
|
|
324
|
+
|
|
325
|
+
```ts
|
|
326
|
+
@Tables({ name: 'profiles', checks: [{ name: 'CHK_score', expression: 'score >= 0' }] })
|
|
327
|
+
export class ProfileEntity extends MysqlModel {}
|
|
328
|
+
|
|
329
|
+
@Tables({ kind: 'view', name: 'active_profiles', expression: 'SELECT * FROM profiles WHERE deleted_at IS NULL' })
|
|
330
|
+
export class ActiveProfileView {}
|
|
331
|
+
|
|
332
|
+
@Tables({ inheritance: { column: { name: 'kind', type: 'varchar' } } })
|
|
333
|
+
export class BaseContent extends MysqlModel {}
|
|
334
|
+
|
|
335
|
+
@Tables({ kind: 'child', discriminatorValue: 'article' })
|
|
336
|
+
export class ArticleContent extends BaseContent {}
|
|
337
|
+
|
|
338
|
+
@Tables({ tree: 'closure-table' })
|
|
339
|
+
export class CategoryEntity extends MysqlModel {}
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
JSON-like columns can model raw records or nested classes:
|
|
343
|
+
|
|
344
|
+
```ts
|
|
345
|
+
class Preference {
|
|
346
|
+
@Column({ required: true })
|
|
347
|
+
theme!: string;
|
|
348
|
+
|
|
349
|
+
@Column({ default: true })
|
|
350
|
+
publicProfile?: boolean;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
class InsightMetric {
|
|
354
|
+
@Column({ required: true })
|
|
355
|
+
key!: string;
|
|
356
|
+
|
|
357
|
+
@Column('int', { required: true })
|
|
358
|
+
value!: number;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
@Tables<CreatorInsight>({ name: 'creator_insights' })
|
|
362
|
+
export class CreatorInsight extends MysqlModel {
|
|
363
|
+
@PrimaryColumn('uuidv7')
|
|
364
|
+
id?: string;
|
|
365
|
+
|
|
366
|
+
@Column('jsonb', { nullable: true })
|
|
367
|
+
preference?: Preference;
|
|
368
|
+
|
|
369
|
+
@Column('jsonb', { nullable: true, nested: InsightMetric, each: true })
|
|
370
|
+
metrics?: InsightMetric[];
|
|
371
|
+
|
|
372
|
+
@Column({ kind: 'virtual', comment: 'Computed score label', optional: true })
|
|
373
|
+
get scoreLabel(): string {
|
|
374
|
+
return 'standard';
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
`@joktec/mysql` intentionally does not support Mongo ObjectId columns or TypeORM MongoDB connections. Use `@joktec/mongo` for Mongo schemas and ObjectId references.
|
|
380
|
+
|
|
218
381
|
Guidelines:
|
|
219
382
|
|
|
220
383
|
- Prefer numeric auto-increment primary keys for write-heavy MySQL tables.
|
|
@@ -224,6 +387,55 @@ Guidelines:
|
|
|
224
387
|
- Add indexes that match common filters and cursor sort order. A cursor using `createdAt + id` should have a matching composite index where possible.
|
|
225
388
|
- `@Tables` provides common TypeORM entity/index wiring, but database-specific search/index behavior should still be verified per dialect.
|
|
226
389
|
|
|
390
|
+
### Migration Notes
|
|
391
|
+
|
|
392
|
+
Recent schema-first decorator changes affect applications that still use raw TypeORM, Swagger, `class-validator`, and `class-transformer` decorators together on each entity property.
|
|
393
|
+
|
|
394
|
+
When migrating an entity:
|
|
395
|
+
|
|
396
|
+
- Migrate one property at a time and replace the whole property decorator stack when the wrapper option can express the same behavior.
|
|
397
|
+
- Replace `@PrimaryGeneratedColumn()` with `@PrimaryColumn('increment')`.
|
|
398
|
+
- Replace `@PrimaryGeneratedColumn('uuid')` with `@PrimaryColumn('uuid')`, or `@PrimaryColumn('uuidv7')` when the app wants framework-generated time-ordered UUIDs.
|
|
399
|
+
- Replace raw TypeORM `@CreateDateColumn`, `@UpdateDateColumn`, and `@DeleteDateColumn` with `@TimestampColumn('create' | 'update' | 'delete')` when the shared metadata wrapper is desired.
|
|
400
|
+
- Replace raw TypeORM `@VersionColumn`, `@VirtualColumn`, and `@ViewColumn` with `@Column({ kind: 'version' })`, `@Column({ kind: 'virtual', mode: 'sql', query })`, or `@Column({ kind: 'view' })`.
|
|
401
|
+
- Replace relation decorator stacks such as `@ManyToOne` + `@JoinColumn` + Swagger metadata with `@Column({ kind: 'relation', ... })` when the wrapper can express the same relationship.
|
|
402
|
+
- Replace TypeORM `@RelationId` with `@Column({ kind: 'relation-id', relationId })` for relation id properties.
|
|
403
|
+
- Replace duplicate validation decorators such as `@IsNotEmpty`, `@IsOptional`, `@IsEmail`, `@IsInt`, `@IsUUID`, `@IsObject`, `@MinLength`, `@MaxLength`, `@Min`, and `@Max` with `@Column` options where possible.
|
|
404
|
+
- Replace simple `@Expose`, grouped expose, hidden fields, and Swagger property metadata with `hidden`, `groups`, `expose`, `example`, `comment`, `deprecated`, and `swagger` options where possible.
|
|
405
|
+
- Replace repeated `swagger: { readOnly: true }` with `immutable: true` when the API contract is read-only. Use `update: false` when TypeORM should skip updates after insert.
|
|
406
|
+
- Keep custom validators or transforms in `decorators: [...]` when there is no wrapper option.
|
|
407
|
+
- Do not migrate Mongo/ObjectId-style columns into this package.
|
|
408
|
+
|
|
409
|
+
Common mappings:
|
|
410
|
+
|
|
411
|
+
| Legacy decorators | Schema-first wrapper |
|
|
412
|
+
| --- | --- |
|
|
413
|
+
| `@PrimaryGeneratedColumn()` | `@PrimaryColumn('increment')` |
|
|
414
|
+
| `@PrimaryGeneratedColumn('uuid')` | `@PrimaryColumn('uuid')` |
|
|
415
|
+
| app-generated ordered UUID id | `@PrimaryColumn('uuidv7')` |
|
|
416
|
+
| TypeORM `@CreateDateColumn(...)` | `@TimestampColumn('create', ...)` |
|
|
417
|
+
| TypeORM `@UpdateDateColumn(...)` | `@TimestampColumn('update', ...)` |
|
|
418
|
+
| TypeORM `@DeleteDateColumn(...)` | `@TimestampColumn('delete', ...)` |
|
|
419
|
+
| TypeORM `@VersionColumn(...)` | `@Column({ kind: 'version', ... })` |
|
|
420
|
+
| TypeORM `@VirtualColumn(...)` | `@Column({ kind: 'virtual', mode: 'sql', query, ... })` |
|
|
421
|
+
| TypeORM `@ViewColumn(...)` | `@Column({ kind: 'view', ... })` |
|
|
422
|
+
| `@Column(...)` | `@Column(...)` from `@joktec/mysql` |
|
|
423
|
+
| `@Index(...)` on one property | `@Column({ index: true | 'IDX_name' | { name, options } })` |
|
|
424
|
+
| `@Check(...)` on one property | `@Column({ check: 'sql expression' | { name, expression } })` |
|
|
425
|
+
| `@ManyToOne(...)` + `@JoinColumn(...)` | `@Column({ kind: 'relation', relation: 'many-to-one', joinColumn, ... })` |
|
|
426
|
+
| `@OneToMany(...)` | `@Column({ kind: 'relation', relation: 'one-to-many', ... })` |
|
|
427
|
+
| `@RelationId(...)` | `@Column({ kind: 'relation-id', relationId })` |
|
|
428
|
+
| `@IsInt()` | `@Column({ isInt: true })` or an integer column type |
|
|
429
|
+
| `@IsUUID()` | `@Column({ isUUID: true })` |
|
|
430
|
+
| `@IsObject()` | `@Column({ isObject: true })` or a JSON column |
|
|
431
|
+
| `@Type(() => Number)` | inferred by numeric design type or pass through `decorators` for custom cases |
|
|
432
|
+
| `@ValidateNested()` + `@Type(() => Preference)` | `@Column('jsonb', { nested: Preference })` |
|
|
433
|
+
| `@ValidateNested({ each: true })` + `@Type(() => Preference)` | `@Column('jsonb', { nested: Preference, each: true })` |
|
|
434
|
+
| `@Expose()` + `@ApiProperty(...)` on a getter | `@Column({ kind: 'virtual', ... })` |
|
|
435
|
+
| Swagger `readOnly: true` | `@Column({ immutable: true })` or `update: false` when ORM updates must also be blocked |
|
|
436
|
+
|
|
437
|
+
Raw TypeORM decorators remain available for advanced cases that are intentionally outside the wrapper surface, including listeners, non-primary `@Generated`, `@ForeignKey`, and Postgres `@Exclusion`.
|
|
438
|
+
|
|
227
439
|
## Error Contract
|
|
228
440
|
|
|
229
441
|
`MysqlCatch` normalizes common TypeORM driver errors into stable framework codes, including duplicate key, foreign key violation, not-null violation, unknown column, deadlock, lock timeout, connection failure, and transaction conflict. Raw driver details remain attached for logging/debugging, but application code should branch on the stable framework message.
|