expeditavoluptas
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.
154 lines (114 loc) • 3.95 kB
text/typescript
import {
AfterCreate, AfterDelete, AfterUpdate, BeforeCreate, BeforeDelete, BeforeUpdate, Collection, Entity, OneToMany, Property, ManyToOne,
QueryOrder, OnInit, ManyToMany, Index, Unique, OneToOne, Cascade, LoadStrategy, EventArgs, t, OnLoad, OptionalProps,
} from '@mikro-orm/core';
import { Book2 } from './Book2';
import { BaseEntity2 } from './BaseEntity2';
import { Address2 } from './Address2';
export class Author2 extends BaseEntity2 {
[OptionalProps]?: 'createdAt' | 'updatedAt' | 'termsAccepted' | 'version' | 'versionAsString' | 'code' | 'code2' | 'booksTotal' | 'hookParams' | 'hookTest' | 'onLoadCalled';
static beforeDestroyCalled = 0;
static afterDestroyCalled = 0;
createdAt: Date = new Date();
updatedAt: Date = new Date();
name: string;
email: string;
age?: number;
termsAccepted: boolean = false;
optional?: boolean;
identities?: string[];
born?: Date;
bornTime?: string;
books!: Collection<Book2>;
books2!: Collection<Book2>;
address?: Address2;
friends = new Collection<Author2>(this);
following = new Collection<Author2>(this);
followers = new Collection<Author2>(this);
favouriteBook?: Book2;
favouriteAuthor?: Author2;
version!: number;
versionAsString!: string;
code!: string;
booksTotal!: number;
hookParams: any[] = [];
onLoadCalled?: number;
constructor(name: string, email: string) {
super();
this.name = name;
this.email = email;
}
onInit() {
this.code = `${this.email} - ${this.name}`;
this.hookParams = [];
}
onLoad() {
this.onLoadCalled = this.onLoadCalled ? this.onLoadCalled + 1 : 1;
}
beforeCreate(args: EventArgs<this>) {
this.version = 1;
this.hookParams.push(args);
}
afterCreate(args: EventArgs<this>) {
this.versionAsString = 'v' + this.version;
this.hookParams.push(args);
}
beforeUpdate(args: EventArgs<this>) {
this.version += 1;
this.hookParams.push(args);
}
afterUpdate(args: EventArgs<this>) {
this.versionAsString = 'v' + this.version;
this.hookParams.push(args);
}
beforeDelete() {
Author2.beforeDestroyCalled += 1;
}
afterDelete() {
Author2.afterDestroyCalled += 1;
}
getCode() {
return `${this.email} - ${this.name}`;
}
get code2() {
return `${this.email} - ${this.name}`;
}
}