|
| 1 | +import { Column, Entity, PrimaryColumn } from 'typeorm'; |
| 2 | +import { getQueryTypeormMetadata, QueryTypeormEntityMetadata } from '../../src/common'; |
| 3 | +import { closeTestConnection, createTestConnection, getTestConnection } from '../__fixtures__/connection.fixture'; |
| 4 | +import { TestEntityRelationEntity } from '../__fixtures__/test-entity-relation.entity'; |
| 5 | +import { TestRelation } from '../__fixtures__/test-relation.entity'; |
| 6 | +import { TestEntity } from '../__fixtures__/test.entity'; |
| 7 | + |
| 8 | +describe('TypeormMetadata', (): void => { |
| 9 | + class TestEmbedded { |
| 10 | + @Column({ type: 'text' }) |
| 11 | + stringType!: string; |
| 12 | + |
| 13 | + @Column({ type: 'boolean' }) |
| 14 | + boolType!: boolean; |
| 15 | + } |
| 16 | + |
| 17 | + @Entity() |
| 18 | + class TestMetadataEntity { |
| 19 | + @PrimaryColumn({ type: 'text' }) |
| 20 | + pk!: string; |
| 21 | + |
| 22 | + @Column({ type: 'text' }) |
| 23 | + stringType!: string; |
| 24 | + |
| 25 | + @Column({ type: 'boolean' }) |
| 26 | + boolType!: boolean; |
| 27 | + |
| 28 | + @Column({ type: 'integer' }) |
| 29 | + numberType!: number; |
| 30 | + |
| 31 | + @Column({ type: 'date' }) |
| 32 | + dateType!: Date; |
| 33 | + |
| 34 | + @Column({ type: 'datetime' }) |
| 35 | + datetimeType!: Date; |
| 36 | + |
| 37 | + @Column({ type: 'simple-json' }) |
| 38 | + jsonType!: any; |
| 39 | + |
| 40 | + @Column(() => TestEmbedded) |
| 41 | + embeddedType!: TestEmbedded; |
| 42 | + } |
| 43 | + |
| 44 | + beforeEach(() => createTestConnection({ extraEntities: [TestMetadataEntity] })); |
| 45 | + afterEach(() => closeTestConnection()); |
| 46 | + |
| 47 | + it('Test metadata', (): void => { |
| 48 | + const meta = getQueryTypeormMetadata(getTestConnection()); |
| 49 | + console.log(meta); |
| 50 | + // Implicit column types |
| 51 | + expect(meta.get(TestEntity)).toMatchObject({ |
| 52 | + testEntityPk: { metaType: 'property', type: String }, |
| 53 | + stringType: { metaType: 'property', type: String }, |
| 54 | + dateType: { metaType: 'property', type: Date }, |
| 55 | + boolType: { metaType: 'property', type: Boolean }, |
| 56 | + oneTestRelation: { metaType: 'relation', type: TestRelation }, |
| 57 | + testRelations: { metaType: 'relation', type: TestRelation }, |
| 58 | + manyTestRelations: { metaType: 'relation', type: TestRelation }, |
| 59 | + manyToManyUniDirectional: { metaType: 'relation', type: TestRelation }, |
| 60 | + testEntityRelation: { metaType: 'relation', type: TestEntityRelationEntity }, |
| 61 | + } as QueryTypeormEntityMetadata<TestEntity>); |
| 62 | + // Explicit column types |
| 63 | + expect(meta.get(TestMetadataEntity)).toMatchObject({ |
| 64 | + pk: { metaType: 'property', type: 'text' }, |
| 65 | + stringType: { metaType: 'property', type: 'text' }, |
| 66 | + boolType: { metaType: 'property', type: 'boolean' }, |
| 67 | + numberType: { metaType: 'property', type: 'integer' }, |
| 68 | + dateType: { metaType: 'property', type: 'date' }, |
| 69 | + datetimeType: { metaType: 'property', type: 'datetime' }, |
| 70 | + jsonType: { metaType: 'property', type: 'simple-json' }, |
| 71 | + } as QueryTypeormEntityMetadata<TestMetadataEntity>); |
| 72 | + }); |
| 73 | +}); |
0 commit comments