undeexcepturi
Version:
TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.
144 lines (115 loc) • 2.54 kB
text/typescript
import {
Entity,
ManyToOne,
OneToOne,
PrimaryKey,
Property,
Ref,
Unique,
MikroORM,
OneToMany,
Collection,
} from '@mikro-orm/sqlite';
({ tableName: 'servers_clients_tags' })
({
properties: ['purchase', 'name'],
})
class Note {
()
readonly id?: number;
(() => Purchases, {
nullable: false,
})
purchase?: Ref<Purchases>;
({ length: 100 })
name!: string;
({ length: 2000 })
value!: string;
}
()
class Purchases {
()
readonly id?: number;
({ entity: () => Account, ref: true })
account?: Ref<Account>;
(() => Note, x => x.purchase)
notes = new Collection<Note>(this);
}
()
class Account {
()
readonly id?: number;
(() => Address, billingDetail => billingDetail.account, {
ref: true,
nullable: true,
})
address?: Ref<Address>;
(() => Purchases, x => x.account)
serverClients = new Collection<Purchases>(this);
}
()
class Address {
()
readonly id?: number;
({ length: 100 })
company!: string;
({
entity: () => Account,
unique: 'billing_details_account_id_key',
owner: true,
ref: true,
})
account!: Ref<Account>;
}
let orm: MikroORM;
beforeAll(async () => {
orm = await MikroORM.init({
entities: [Note, Purchases, Account, Address],
dbName: ':memory:',
loadStrategy: 'select-in',
});
await orm.schema.refreshDatabase();
const billingDetail = orm.em.create(Address, {
company: 'testBillingDetail',
account: orm.em.create(Account, {}),
});
const serverClient = orm.em.create(Purchases, {
account: billingDetail.account,
});
orm.em.create(Note, {
purchase: serverClient,
name: 'testKey',
value: 'testValue',
});
await orm.em.flush();
});
afterAll(async () => {
await orm.close(true);
});
test(`GH issue 5165 upsert one`, async () => {
const purchase = await orm.em.findOneOrFail(Purchases, { id: 1 }, {
populate: ['notes'],
});
await orm.em.upsert(Note, {
purchase,
name: 'testKey',
value: 'newTestValue',
});
});
test(`GH issue 5165 upsert many`, async () => {
const purchase = await orm.em.findOneOrFail(Purchases, { id: 1 }, {
populate: ['notes'],
});
await orm.em.upsertMany(Note, [
{
purchase,
name: 'testKey',
value: 'newTestValue',
},
{
purchase,
name: 'testKey2',
value: 'newTestValue2',
},
]);
});