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.
84 lines (63 loc) • 1.88 kB
text/typescript
import { Entity, MikroORM, PrimaryKey, Property, Type } from '@mikro-orm/sqlite';
import { parse, stringify, v4 as uuid } from 'uuid';
class UUID extends Type<string, Buffer> {
override convertToJSValue(value: Buffer) {
return stringify(value);
}
override convertToDatabaseValue(value: string) {
return Buffer.from(parse(value));
}
override getColumnType() {
return 'binary(16)';
}
}
()
class User {
({ type: UUID })
id = uuid();
({ nullable: true })
name?: string;
}
describe('GH issue 1263', () => {
let orm: MikroORM;
beforeAll(async () => {
orm = await MikroORM.init({
entities: [User],
dbName: ':memory:',
});
await orm.schema.createSchema();
});
afterAll(async () => {
await orm.close(true);
});
test(`GH issue 1263`, async () => {
const testCases: ((id: string) => Promise<any>)[] = [
async id => orm.em.nativeDelete(User, await orm.em.findOneOrFail(User, id)),
async id => orm.em.removeAndFlush(await orm.em.findOneOrFail(User, id)),
id => orm.em.nativeDelete(User, id),
id => orm.em.nativeDelete(User, { id }),
id => orm.em.nativeDelete(User, [id]),
id => orm.em.nativeDelete(User, [id, { id }]),
];
for (const testCase of testCases) {
const id = uuid();
const user = new User();
user.id = id;
await orm.em.persist(user).flush();
orm.em.clear();
await testCase(id);
const userCount = await orm.em.count(User, id);
expect(userCount).toBe(0);
}
{
const user = new User();
user.id = uuid();
user.name = 'foo';
await orm.em.persist(user).flush();
user.name = 'foo bar';
await orm.em.flush();
const userCount = await orm.em.count(User, { name: 'foo bar' });
expect(userCount).toBe(1);
}
});
});