Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(mongo): fix querying object embedded JSON properties
Closes #4755
- Loading branch information
Showing
3 changed files
with
90 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { ObjectId } from 'bson'; | ||
import { Entity, PrimaryKey, Property, Embeddable, Embedded, wrap } from '@mikro-orm/core'; | ||
import { MikroORM } from '@mikro-orm/mongodb'; | ||
import { TsMorphMetadataProvider } from '@mikro-orm/reflection'; | ||
|
||
@Embeddable() | ||
class ApiIpApiRequest { | ||
|
||
@Property() | ||
body?: Record<string, unknown>; | ||
|
||
@Property() | ||
headers?: Record<string, unknown>; | ||
|
||
@Property() | ||
method!: string; | ||
|
||
@Property() | ||
url!: string; | ||
|
||
} | ||
|
||
@Embeddable() | ||
class ApiIpApiResponse { | ||
|
||
@Property() | ||
json?: Record<string, unknown>; | ||
|
||
} | ||
|
||
@Entity() | ||
class ApiIpApi { | ||
|
||
@PrimaryKey() | ||
_id!: ObjectId; | ||
|
||
@Embedded(() => ApiIpApiRequest, { object: true }) | ||
request?: ApiIpApiRequest; | ||
|
||
@Embedded(() => ApiIpApiResponse, { object: true }) | ||
response?: ApiIpApiResponse; | ||
|
||
} | ||
|
||
let orm: MikroORM; | ||
|
||
beforeAll(async () => { | ||
orm = await MikroORM.init({ | ||
entities: [ApiIpApi], | ||
clientUrl: 'mongodb://localhost:27017/mikro-orm-test-4755', | ||
metadataProvider: TsMorphMetadataProvider, | ||
cache: { enabled: false }, | ||
}); | ||
await orm.em.insert(ApiIpApi, { | ||
request: { | ||
body: { ip: '123.123.123', foo: 'bar', lol: true, num: 123 }, | ||
method: 'post', | ||
url: 'url', | ||
}, | ||
}); | ||
}); | ||
|
||
afterAll(() => orm.close(true)); | ||
|
||
test('GH #4755', async () => { | ||
const foo = await orm.em.findOneOrFail(ApiIpApi, { | ||
request: { | ||
body: { | ||
ip: '123.123.123', | ||
}, | ||
}, | ||
}); | ||
expect(wrap(foo).toObject()).toEqual({ | ||
id: foo._id.toString(), | ||
request: { | ||
body: { ip: '123.123.123', foo: 'bar', lol: true, num: 123 }, | ||
method: 'post', | ||
url: 'url', | ||
}, | ||
}); | ||
}); |