quaerateum
Version:
Simple 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 JS.
110 lines (81 loc) • 2.17 kB
text/typescript
import {
AfterCreate, AfterDelete, AfterUpdate, BeforeCreate, BeforeDelete, BeforeUpdate,
Cascade, Collection, Entity, EntityAssigner, ManyToMany, ManyToOne, OneToMany, Property,
} from '../../lib';
import { Book } from './Book';
import { AuthorRepository } from '../repositories/AuthorRepository';
import { BaseEntity } from './BaseEntity';
export class Author extends BaseEntity {
static beforeDestroyCalled = 0;
static afterDestroyCalled = 0;
name: string;
email: string;
age: number;
termsAccepted = false;
identities: string[];
born: Date;
books = new Collection<Book>(this);
friends: Collection<Author> = new Collection<Author>(this);
favouriteBook: Book;
favouriteAuthor: Author;
version: number;
versionAsString: string;
constructor(name: string, email: string) {
super();
this.name = name;
this.email = email;
this.foo = 'bar';
}
beforeCreate() {
this.version = 1;
}
beforeCreate2() {
// do sth else
}
afterCreate() {
this.versionAsString = 'v' + this.version;
}
beforeUpdate() {
this.version += 1;
}
afterUpdate() {
this.versionAsString = 'v' + this.version;
}
beforeDelete() {
Author.beforeDestroyCalled += 1;
}
afterDelete() {
Author.afterDestroyCalled += 1;
}
assign(data: any): void {
EntityAssigner.assign(this, data);
}
toJSON(strict = true, strip = ['id', 'email'], ...args: any[]): { [p: string]: any } {
const o = this.toObject(...args);
o.fooBar = 123;
if (strict) {
strip.forEach(k => delete o[k]);
}
return o;
}
}