-
-
Notifications
You must be signed in to change notification settings - Fork 496
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): fix snapshotting of composite properties
Closes #1079
- Loading branch information
Showing
3 changed files
with
142 additions
and
7 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,133 @@ | ||
import { Entity, PrimaryKey, MikroORM, ManyToOne, Enum, PrimaryKeyType, Property, BigIntType, Logger } from '@mikro-orm/core'; | ||
import { PostgreSqlDriver } from '@mikro-orm/postgresql'; | ||
import { v4 } from 'uuid'; | ||
|
||
@Entity() | ||
class User { | ||
|
||
@PrimaryKey({ type: BigIntType }) | ||
_id!: string; | ||
|
||
} | ||
|
||
@Entity() | ||
class Wallet { | ||
|
||
[PrimaryKeyType]: [string, string]; | ||
|
||
@PrimaryKey() | ||
currencyRef!: string; | ||
|
||
@ManyToOne({ primary: true, entity: () => User }) | ||
owner!: User; | ||
|
||
@Property({ type: String, nullable: false, default: '0' }) | ||
mainBalance!: string; | ||
|
||
} | ||
|
||
class AbstractDeposit { | ||
|
||
@Property({ type: String, nullable: false }) | ||
amount!: string; | ||
|
||
@Property({ type: String, nullable: false }) | ||
gatewayKey!: string; | ||
|
||
@Property() | ||
createdAt: Date = new Date(); | ||
|
||
@Property({ onUpdate: () => new Date() }) | ||
updatedAt: Date = new Date(); | ||
|
||
} | ||
|
||
enum DepositStatus { | ||
UNPAID = 'UNPAID', | ||
PENDING = 'PENDING', | ||
COMPLETED = 'COMPLETED', | ||
FAILED = 'FAILED' | ||
} | ||
|
||
|
||
@Entity() | ||
export class Deposit extends AbstractDeposit { | ||
|
||
[PrimaryKeyType]: [string, string, string]; | ||
|
||
@PrimaryKey() | ||
txRef!: string; | ||
|
||
@ManyToOne({ primary: true, entity: () => Wallet }) | ||
wallet!: Wallet; | ||
|
||
@Enum({ | ||
nullable: false, | ||
items: () => DepositStatus, | ||
}) | ||
status: DepositStatus = DepositStatus.UNPAID; | ||
|
||
} | ||
|
||
describe('GH issue 1079', () => { | ||
|
||
let orm: MikroORM<PostgreSqlDriver>; | ||
|
||
beforeAll(async () => { | ||
orm = await MikroORM.init({ | ||
entities: [User, Wallet, Deposit, AbstractDeposit], | ||
dbName: `mikro_orm_test_gh_1079`, | ||
type: 'postgresql', | ||
}); | ||
await orm.getSchemaGenerator().ensureDatabase(); | ||
await orm.getSchemaGenerator().dropSchema(); | ||
await orm.getSchemaGenerator().createSchema(); | ||
}); | ||
|
||
afterAll(() => orm.close(true)); | ||
|
||
test(`GH issue 1079`, async () => { | ||
const user = new User(); | ||
const wallet = new Wallet(); | ||
const deposit = new Deposit(); | ||
user._id = '1'; | ||
wallet.currencyRef = 'USD'; | ||
wallet.owner = user; | ||
wallet.mainBalance = '456'; | ||
deposit.wallet = wallet; | ||
deposit.amount = '123'; | ||
deposit.txRef = '456'; | ||
deposit.gatewayKey = '789'; | ||
|
||
const mock = jest.fn(); | ||
const logger = new Logger(mock, ['query']); | ||
Object.assign(orm.config, { logger }); | ||
|
||
await orm.em.fork().persistAndFlush(deposit); | ||
|
||
const w = await orm.em.findOneOrFail(Wallet, { | ||
currencyRef: 'USD', | ||
owner: { _id: '1' }, | ||
}); | ||
|
||
const deposit2 = orm.em.create(Deposit, { | ||
wallet: w, | ||
gatewayKey: 'STRIPE', | ||
amount: '98765', | ||
txRef: v4(), | ||
}); | ||
await orm.em.persistAndFlush(deposit2); | ||
|
||
const queries: string[] = mock.mock.calls.map(c => c[0]); | ||
expect(queries[0]).toMatch(`begin`); | ||
expect(queries[1]).toMatch(`insert into "user" ("_id") values ($1) returning "_id"`); | ||
expect(queries[2]).toMatch(`insert into "wallet" ("currency_ref", "main_balance", "owner__id") values ($1, $2, $3)`); | ||
expect(queries[3]).toMatch(`insert into "deposit" ("amount", "created_at", "gateway_key", "status", "tx_ref", "updated_at", "wallet_currency_ref", "wallet_owner__id") values ($1, $2, $3, $4, $5, $6, $7, $8)`); | ||
expect(queries[4]).toMatch(`commit`); | ||
expect(queries[5]).toMatch(`select "e0".* from "wallet" as "e0" left join "user" as "e1" on "e0"."owner__id" = "e1"."_id" where "e0"."currency_ref" = $1 and "e1"."_id" = $2 limit $3`); | ||
expect(queries[6]).toMatch(`begin`); | ||
expect(queries[7]).toMatch(`insert into "deposit" ("amount", "created_at", "gateway_key", "status", "tx_ref", "updated_at", "wallet_currency_ref", "wallet_owner__id") values ($1, $2, $3, $4, $5, $6, $7, $8)`); | ||
expect(queries[8]).toMatch(`commit`); | ||
}); | ||
|
||
}); |