-
-
Notifications
You must be signed in to change notification settings - Fork 495
/
Book.ts
72 lines (53 loc) · 2.01 KB
/
Book.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { ObjectId } from 'bson';
import { EntityDTO, IdentifiedReference, Dictionary, Collection, Cascade, Entity, Index, ManyToMany, ManyToOne, PrimaryKey, Property, Unique, wrap, Filter, OptionalProps } from '@mikro-orm/core';
import { Publisher } from './Publisher';
import { Author } from './Author';
import { BookTag } from './book-tag';
import { BaseEntity3 } from './BaseEntity3';
import { BookRepository } from '../repositories/BookRepository';
@Entity({ tableName: 'books-table', repository: () => BookRepository })
@Unique({ properties: ['title', 'author'] })
@Index({ properties: 'title', type: 'fulltext' })
@Index({ options: { point: '2dsphere', title: -1 } })
@Filter({ name: 'writtenBy', cond: args => ({ author: args.author }) })
export class Book extends BaseEntity3<Book> {
[OptionalProps]?: 'createdAt';
@PrimaryKey()
_id!: ObjectId;
@Property()
createdAt: Date = new Date();
@Property()
title: string;
@Property({ lazy: true, nullable: true })
perex?: string;
@ManyToOne(() => Author)
author: Author;
@ManyToOne(() => Publisher, { wrappedReference: true, cascade: [Cascade.PERSIST, Cascade.REMOVE], nullable: true })
@Index({ name: 'publisher_idx' })
publisher!: IdentifiedReference<Publisher, '_id' | 'id'> | null;
@ManyToMany(() => BookTag)
tags = new Collection<BookTag>(this);
@Property({ type: 'json', nullable: true })
metaObject?: Dictionary<unknown>;
@Property({ nullable: true })
metaArray?: any[];
@Property({ nullable: true })
metaArrayOfStrings?: string[];
@Property({ nullable: true })
@Index({ type: '2dsphere' })
point?: [number, number];
@Property({ nullable: true })
tenant?: number;
constructor(title: string, author?: Author) {
super();
this.title = title;
this.author = author!;
}
toJSON(strict = true, strip = ['metaObject', 'metaArray', 'metaArrayOfStrings'], ...args: any[]): EntityDTO<this> {
const o = wrap(this).toObject(...args);
if (strict) {
strip.forEach(k => delete o[k]);
}
return o;
}
}