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.
125 lines (96 loc) • 2.2 kB
text/typescript
import {
Collection,
Entity,
Property,
ManyToOne,
OneToMany,
PrimaryKey,
Cascade,
Ref,
PrimaryKeyProp,
Primary,
sql,
} from '@mikro-orm/core';
import { MikroORM } from '@mikro-orm/mysql';
import { randomUUID } from 'crypto';
()
export class Category {
({
length: 36,
})
id!: string;
({
length: 3,
default: sql.now(3),
})
createdAt?: Date;
(() => Article, attr => attr.category, {
cascade: [Cascade.ALL],
})
articles = new Collection<Article>(this);
}
()
export class Article {
({
length: 36,
})
id!: string;
({
length: 3,
default: sql.now(3),
})
createdAt?: Date;
(() => Category, {
primary: true,
ref: true,
})
category!: Ref<Category>;
[PrimaryKeyProp]?: ['id', 'category'];
(() => ArticleAttribute, attr => attr.article, {
cascade: [Cascade.ALL],
})
attributes = new Collection<ArticleAttribute>(this);
}
()
export class ArticleAttribute {
({ length: 36 })
id!: string;
({
length: 3,
default: sql.now(3),
})
createdAt?: Date;
(() => Article, {
primary: true,
ref: true,
})
article!: Ref<Article>;
[PrimaryKeyProp]?: ['id', 'article'];
}
type T = Primary<ArticleAttribute>;
let orm: MikroORM;
beforeAll(async () => {
orm = await MikroORM.init({
dbName: 'mikro_orm_3965',
entities: [Article],
port: 3308,
});
await orm.schema.refreshDatabase();
});
afterAll(() => orm.close(true));
test('3965', async () => {
const category = new Category();
category.id = randomUUID();
const article = new Article();
article.id = randomUUID();
category.articles.add(article);
const articleAttribute = new ArticleAttribute();
articleAttribute.id = randomUUID();
article.attributes.add(articleAttribute);
expect(category.createdAt).toBeUndefined();
await orm.em.persistAndFlush(category);
expect(category.createdAt).toBeDefined();
expect(category.createdAt).toBeInstanceOf(Date);
const miss = await orm.em.findOne(ArticleAttribute, ['a', ['1', '1']]);
expect(miss).toBeNull();
});