Skip to content

Commit

Permalink
feat: added opaque types support over primitives in find-options (#9560)
Browse files Browse the repository at this point in the history
* added opaque types support over primitives in find-options
* removed lock-verify because of its deprecation
* fixing auto type mapping
  • Loading branch information
pleerock committed Nov 21, 2022
1 parent 85fa9c6 commit 4ec04fa
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 6 deletions.
11 changes: 6 additions & 5 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ commands:
steps:
- when:
condition:
equal: [ << parameters.databases >>, "" ]
equal: [<< parameters.databases >>, ""]
steps:
- run:
name: "Enabling Databases in ORM config"
command: cp ormconfig.circleci-common.json ./ormconfig.json
- unless:
condition:
equal: [ << parameters.databases >>, "" ]
equal: [<< parameters.databases >>, ""]
steps:
- run:
name: "Enabling Databases in ORM config"
Expand All @@ -40,9 +40,10 @@ commands:
- restore_cache:
name: Restore node_modules cache
key: node_modules-<< parameters.cache-key >>-{{ checksum "package-lock.json" }}
- run:
name: Verify `package.json` and `package-lock.json` are in sync
command: npx lock-verify
# removed this item because lock-verify is deprecated
# - run:
# name: Verify `package.json` and `package-lock.json` are in sync
# command: npx lock-verify
- run:
# This uses `npm install` instead of `npm ci`
# because of https://github.com/npm/cli/issues/558
Expand Down
6 changes: 6 additions & 0 deletions src/find-options/FindOptionsOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ export type FindOptionsOrderProperty<Property> = Property extends Promise<
? FindOptionsOrderProperty<NonNullable<I>>
: Property extends Function
? never
: Property extends string
? FindOptionsOrderValue
: Property extends number
? FindOptionsOrderValue
: Property extends boolean
? FindOptionsOrderValue
: Property extends Buffer
? FindOptionsOrderValue
: Property extends Date
Expand Down
6 changes: 6 additions & 0 deletions src/find-options/FindOptionsRelations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ export type FindOptionsRelationsProperty<Property> = Property extends Promise<
? FindOptionsRelationsProperty<NonNullable<I>> | boolean
: Property extends Array<infer I>
? FindOptionsRelationsProperty<NonNullable<I>> | boolean
: Property extends string
? never
: Property extends number
? never
: Property extends boolean
? never
: Property extends Function
? never
: Property extends Buffer
Expand Down
6 changes: 6 additions & 0 deletions src/find-options/FindOptionsSelect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ export type FindOptionsSelectProperty<Property> = Property extends Promise<
? FindOptionsSelectProperty<I> | boolean
: Property extends Array<infer I>
? FindOptionsSelectProperty<I> | boolean
: Property extends string
? boolean
: Property extends number
? boolean
: Property extends boolean
? boolean
: Property extends Function
? never
: Property extends Buffer
Expand Down
8 changes: 7 additions & 1 deletion src/find-options/FindOptionsWhere.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ export type FindOptionsWhereProperty<Property> = Property extends Promise<
? Property | FindOperator<Property>
: Property extends ObjectID
? Property | FindOperator<Property>
: Property extends string
? Property | FindOperator<Property>
: Property extends number
? Property | FindOperator<Property>
: Property extends boolean
? Property | FindOperator<Property>
: Property extends object
?
| FindOptionsWhere<Property>
Expand All @@ -28,7 +34,7 @@ export type FindOptionsWhereProperty<Property> = Property extends Promise<
| boolean
: Property | FindOperator<Property>

/** :
/**
* Used for find operations.
*/
export type FindOptionsWhere<Entity> = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Column, Entity, PrimaryColumn } from "../../../../../src"

export type WithType<T> = T & { type: "Post" }

@Entity()
export class Post {
@PrimaryColumn({ type: Number })
id: number & { type: "Post" }

@Column({ type: String })
title: string & { type: "Post" }

@Column({ type: Boolean })
isEdited: boolean & { type: "Post" }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import "reflect-metadata"
import "../../../utils/test-setup"
import { DataSource } from "../../../../src"
import {
closeTestingConnections,
createTestingConnections,
reloadTestingDatabases,
} from "../../../utils/test-utils"
import { Post, WithType } from "./entity/Post"

describe("find options > opaque-types-over-primitives", () => {
let dataSources: DataSource[]
before(
async () =>
(dataSources = await createTestingConnections({
__dirname,
})),
)
beforeEach(() => reloadTestingDatabases(dataSources))
after(() => closeTestingConnections(dataSources))

async function prepareData(dataSource: DataSource) {
const post1 = new Post()
post1.id = 1 as WithType<number>
post1.title = "Hello" as WithType<string>
post1.isEdited = true as WithType<boolean>
await dataSource.manager.save(post1)
}

it("should work in select", () =>
Promise.all(
dataSources.map(async (dataSource) => {
await prepareData(dataSource)

const posts1 = await dataSource
.createQueryBuilder(Post, "post")
.setFindOptions({
select: {
id: true,
title: true,
isEdited: true,
},
})
.getMany()

posts1.should.be.eql([
{ id: 1, title: "Hello", isEdited: true },
])
}),
))

it("should work in where", () =>
Promise.all(
dataSources.map(async (dataSource) => {
await prepareData(dataSource)

const posts = await dataSource
.createQueryBuilder(Post, "post")
.setFindOptions({
where: {
id: 1 as WithType<number>,
},
})
.getMany()

posts.should.be.eql([
{
id: 1,
title: "Hello",
isEdited: true,
},
])
}),
))

it("should work in order by", () =>
Promise.all(
dataSources.map(async (dataSource) => {
await prepareData(dataSource)

const posts1 = await dataSource
.createQueryBuilder(Post, "post")
.setFindOptions({
order: {
id: "asc",
title: "asc",
isEdited: "asc",
},
})
.getMany()
posts1.should.be.eql([
{
id: 1,
title: "Hello",
isEdited: true,
},
])
}),
))
})

0 comments on commit 4ec04fa

Please sign in to comment.