-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Force UTC timezone to make some tests (e.g. typeorm-query-service.spe…
…c) deterministic Implement type-based custom filters + field specific custom filters Filter registration through decorators performed on module initialization Tests
- Loading branch information
1 parent
098f83a
commit 45aff05
Showing
36 changed files
with
1,161 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
packages/query-typeorm/__tests__/__fixtures__/custom-filters.services.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { ColumnType } from 'typeorm'; | ||
import { randomString } from '../../src/common'; | ||
import { CustomFilter, CustomFilterResult } from '../../src/query/custom-filter.registry'; | ||
import { TypeOrmQueryFilter } from '../../src/decorators/typeorm-query-filter.decorator'; | ||
|
||
type IsMultipleOfOpType = 'isMultipleOf'; | ||
|
||
@TypeOrmQueryFilter() | ||
export class IsMultipleOfCustomFilter implements CustomFilter<IsMultipleOfOpType> { | ||
readonly operations: IsMultipleOfOpType[] = ['isMultipleOf']; | ||
|
||
readonly types: ColumnType[] = [Number, 'integer']; | ||
|
||
apply(field: string, cmp: IsMultipleOfOpType, val: unknown, alias?: string): CustomFilterResult { | ||
alias = alias ? alias : ''; | ||
const pname = `param${randomString()}`; | ||
return { | ||
sql: `(${alias}.${field} % :${pname}) == 0`, | ||
params: { [pname]: val }, | ||
}; | ||
} | ||
} | ||
|
||
@TypeOrmQueryFilter() | ||
export class IsMultipleOfDateCustomFilter implements CustomFilter<IsMultipleOfOpType> { | ||
readonly operations: IsMultipleOfOpType[] = ['isMultipleOf']; | ||
|
||
readonly types: ColumnType[] = [Date, 'date', 'datetime']; | ||
|
||
apply(field: string, cmp: IsMultipleOfOpType, val: unknown, alias?: string): CustomFilterResult { | ||
alias = alias ? alias : ''; | ||
const pname = `param${randomString()}`; | ||
return { | ||
sql: `(EXTRACT(EPOCH FROM ${alias}.${field}) / 3600 / 24) % :${pname}) == 0`, | ||
params: { [pname]: val }, | ||
}; | ||
} | ||
} | ||
|
||
type RadiusCustomFilterOp = 'distanceFrom'; | ||
|
||
@TypeOrmQueryFilter({ | ||
autoRegister: false, | ||
}) | ||
export class RadiusCustomFilter implements CustomFilter<RadiusCustomFilterOp> { | ||
readonly operations: RadiusCustomFilterOp[] = ['distanceFrom']; | ||
|
||
apply( | ||
field: string, | ||
cmp: RadiusCustomFilterOp, | ||
val: { point: { lat: number; lng: number }; radius: number }, | ||
alias?: string, | ||
): CustomFilterResult { | ||
alias = alias ? alias : ''; | ||
const plat = `param${randomString()}`; | ||
const plng = `param${randomString()}`; | ||
const prad = `param${randomString()}`; | ||
return { | ||
sql: `ST_Distance(${alias}.${field}, ST_MakePoint(:${plat},:${plng})) <= :${prad}`, | ||
params: { [plat]: val.point.lat, [plng]: val.point.lng, [prad]: val.radius }, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
packages/query-typeorm/__tests__/metadata/typeorm-metadata.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { Column, Entity, PrimaryColumn } from 'typeorm'; | ||
import { getQueryTypeormMetadata, QueryTypeormEntityMetadata } from '../../src/common'; | ||
import { closeTestConnection, createTestConnection, getTestConnection } from '../__fixtures__/connection.fixture'; | ||
import { TestEntityRelationEntity } from '../__fixtures__/test-entity-relation.entity'; | ||
import { TestRelation } from '../__fixtures__/test-relation.entity'; | ||
import { TestEntity } from '../__fixtures__/test.entity'; | ||
|
||
describe('TypeormMetadata', (): void => { | ||
class TestEmbedded { | ||
@Column({ type: 'text' }) | ||
stringType!: string; | ||
|
||
@Column({ type: 'boolean' }) | ||
boolType!: boolean; | ||
} | ||
|
||
@Entity() | ||
class TestMetadataEntity { | ||
@PrimaryColumn({ type: 'text' }) | ||
pk!: string; | ||
|
||
@Column({ type: 'text' }) | ||
stringType!: string; | ||
|
||
@Column({ type: 'boolean' }) | ||
boolType!: boolean; | ||
|
||
@Column({ type: 'integer' }) | ||
numberType!: number; | ||
|
||
@Column({ type: 'date' }) | ||
dateType!: Date; | ||
|
||
@Column({ type: 'datetime' }) | ||
datetimeType!: Date; | ||
|
||
@Column({ type: 'simple-json' }) | ||
jsonType!: any; | ||
|
||
@Column(() => TestEmbedded) | ||
embeddedType!: TestEmbedded; | ||
} | ||
|
||
beforeEach(() => createTestConnection({ extraEntities: [TestMetadataEntity] })); | ||
afterEach(() => closeTestConnection()); | ||
|
||
it('Test metadata', (): void => { | ||
const meta = getQueryTypeormMetadata(getTestConnection()); | ||
console.log(meta); | ||
// Implicit column types | ||
expect(meta.get(TestEntity)).toMatchObject({ | ||
testEntityPk: { metaType: 'property', type: String }, | ||
stringType: { metaType: 'property', type: String }, | ||
dateType: { metaType: 'property', type: Date }, | ||
boolType: { metaType: 'property', type: Boolean }, | ||
oneTestRelation: { metaType: 'relation', type: TestRelation }, | ||
testRelations: { metaType: 'relation', type: TestRelation }, | ||
manyTestRelations: { metaType: 'relation', type: TestRelation }, | ||
manyToManyUniDirectional: { metaType: 'relation', type: TestRelation }, | ||
testEntityRelation: { metaType: 'relation', type: TestEntityRelationEntity }, | ||
} as QueryTypeormEntityMetadata<TestEntity>); | ||
// Explicit column types | ||
expect(meta.get(TestMetadataEntity)).toMatchObject({ | ||
pk: { metaType: 'property', type: 'text' }, | ||
stringType: { metaType: 'property', type: 'text' }, | ||
boolType: { metaType: 'property', type: 'boolean' }, | ||
numberType: { metaType: 'property', type: 'integer' }, | ||
dateType: { metaType: 'property', type: 'date' }, | ||
datetimeType: { metaType: 'property', type: 'datetime' }, | ||
jsonType: { metaType: 'property', type: 'simple-json' }, | ||
} as QueryTypeormEntityMetadata<TestMetadataEntity>); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,31 @@ | ||
import { getQueryServiceToken } from '@nestjs-query/core'; | ||
import { getRepositoryToken } from '@nestjs/typeorm'; | ||
import { Repository } from 'typeorm'; | ||
import { mock, instance } from 'ts-mockito'; | ||
import { createConnection, Repository } from 'typeorm'; | ||
import { instance, mock } from 'ts-mockito'; | ||
import { createTypeOrmQueryServiceProviders } from '../src/providers'; | ||
import { TypeOrmQueryService } from '../src/services'; | ||
import { CustomFilterRegistry } from '../src/query'; | ||
|
||
describe('createTypeOrmQueryServiceProviders', () => { | ||
it('should create a provider for the entity', () => { | ||
it('should create a provider for the entity', async () => { | ||
class TestEntity {} | ||
|
||
// We need a connection in order to extract entity metadata | ||
const conn = await createConnection({ | ||
type: 'sqlite', | ||
database: ':memory:', | ||
dropSchema: true, | ||
entities: [TestEntity], | ||
synchronize: true, | ||
logging: false, | ||
}); | ||
const mockRepo = mock<Repository<TestEntity>>(Repository); | ||
const providers = createTypeOrmQueryServiceProviders([TestEntity]); | ||
const providers = createTypeOrmQueryServiceProviders([TestEntity], conn); | ||
expect(providers).toHaveLength(1); | ||
expect(providers[0].provide).toBe(getQueryServiceToken(TestEntity)); | ||
expect(providers[0].inject).toEqual([getRepositoryToken(TestEntity)]); | ||
expect(providers[0].inject).toEqual([getRepositoryToken(TestEntity), CustomFilterRegistry]); | ||
expect(providers[0].useFactory(instance(mockRepo))).toBeInstanceOf(TypeOrmQueryService); | ||
|
||
await conn.close(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.