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.
99 lines (79 loc) • 2.48 kB
text/typescript
import { v4 } from 'uuid';
import {
Cascade,
Collection,
Entity,
Filter,
Formula,
Ref,
Index,
ManyToMany,
ManyToOne,
OneToOne,
OptionalProps,
PrimaryKey,
Property,
QueryOrder,
ref,
rel,
t,
sql,
} from '@mikro-orm/core';
import { Publisher2 } from './Publisher2';
import { Author2 } from './Author2';
import { BookTag2 } from './BookTag2';
import { Test2 } from './Test2';
()
({ name: 'expensive', cond: { price: { $gt: 1000 } } })
({ name: 'long', cond: () => ({ [sql`length(perex)`]: { $gt: 10000 } }) })
({ name: 'hasAuthor', cond: { author: { $ne: null } }, default: true })
({ name: 'writtenBy', cond: args => ({ author: { name: args.name } }) })
export class Book2 {
[OptionalProps]?: 'createdAt';
({ name: 'uuid_pk', type: t.uuid })
uuid = v4();
({ default: sql.now(3), length: 3 })
createdAt = new Date();
({ type: 'fulltext' })
({ nullable: true, default: '' })
title?: string;
({ type: t.text, nullable: true, lazy: true, ref: true })
perex?: Ref<string>;
({ type: t.decimal, precision: 8, scale: 2, nullable: true })
price?: number;
(alias => `${alias}.price * 1.19`)
priceTaxed?: string;
({ type: t.double, nullable: true })
double?: number;
({ nullable: true, type: t.json })
meta?: Book2Meta;
({ entity: 'Author2', cascade: [] })
author: Author2;
(() => Publisher2, { cascade: [Cascade.PERSIST, Cascade.REMOVE], nullable: true, ref: true })
publisher?: Ref<Publisher2>;
({ cascade: [], mappedBy: 'book', nullable: true })
test?: Test2;
({ entity: () => BookTag2, cascade: [], fixedOrderColumn: 'order' })
tags = new Collection<BookTag2>(this);
(() => BookTag2, undefined, { pivotTable: 'book_to_tag_unordered', orderBy: { name: QueryOrder.ASC } })
tagsUnordered = new Collection<BookTag2>(this);
constructor(title: string, author: number | Author2, price?: number, publisher?: number | Publisher2) {
this.title = title;
this.author = rel(Author2, author);
this.publisher = ref(Publisher2, publisher);
if (price) {
this.price = price;
}
}
}
export interface Book2Meta {
category?: string;
items?: number;
valid?: boolean;
nested?: {
foo: string;
bar?: number;
num?: number;
deep?: { baz: number; qux: boolean; str?: string };
};
}