0.3.0 • Published 2 years ago
berry-orm v0.3.0
Berry ORM
A lightweight ORM with ❗SUPER AWESOME TYPINGS❗
npm i berry-orm
NOTE! Berry ORM is a generic object relation mapper that is only responsible for mapping relations and you can freely combine it with other data management solutions.
Requires: TypeScript 4.1/4.2/4.3/4.4/4.5
Basic Usage
Defining Entities
@Entity()
class Book extends BaseEntity<Book, "id"> {
@Primary()
@Field()
id!: number;
@Field()
name!: string;
@Relation({
target: () => Author,
inverse: "books",
})
@Field()
author!: Author;
}
@Entity()
class Author extends BaseEntity<Author, "id"> {
@Primary()
@Field()
id!: number;
@Field()
name!: string;
@Relation({
target: () => Book,
inverse: "author",
multi: true,
})
@Field()
books!: Collection<Book>;
}
Resolving Data
const orm = new BerryOrm({ entities: [Book, Author] });
const book1 = orm.em.resolve(Book, {
id: 1,
name: "1000 Ways to Code",
author: 1,
});
book1[RESOLVED]; // true
book1.author[RESOLVED]; // false
const book2 = orm.em.resolve(Book, {
id: 2,
name: "2000 Ways to Code",
author: { id: 1, name: "Char2s" },
});
book2[RESOLVED]; // true
book2.author[RESOLVED]; // true
book1.author == book2.author; // true
Exporting Entities
const orm = new BerryOrm({ entities: [Book, Author] });
const book = orm.em.resolve(Book, {
id: 1,
name: "1000 Ways to Code",
author: { id: 1, name: "Char2s" },
});
const data = orm.em.export(book, { author: { books: { author: true } } });
data.author.books[0].author.