This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
If: | |
- you add and commit with the wrong email address in git, and | |
- your remote has a hook set up to prevent you from pushing with the bad address | |
Then you need to amend the author of your commit before push can succeed: | |
1. fix your email address in git config: | |
$ git config user.name "Your Name" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Use Gists to store code you would like to remember later on | |
console.log(window); // log the "window" object to the console |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ObjectID } from 'mongodb'; | |
import { Collection, Entity, IEntity, ManyToMany, ManyToOne, PrimaryKey, Property } from 'mikro-orm'; | |
import { Author, BookTag, Publisher } from '.'; | |
@Entity() | |
export class Book { | |
@PrimaryKey() | |
_id: ObjectID; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const author = new Author('Jon Snow', 'snow@wall.st'); | |
author.born = new Date(); | |
const publisher = new Publisher('7K publisher'); | |
const book1 = new Book('My Life on The Wall, part 1', author); | |
book1.publisher = publisher; | |
const book2 = new Book('My Life on The Wall, part 2', author); | |
book2.publisher = publisher; | |
const book3 = new Book('My Life on The Wall, part 3', author); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// find all authors with name matching 'Jon', and populate all of their books | |
const authors = await orm.em.find(Author, { name: /Jon/ }, ['books']); | |
for (const author of authors) { | |
console.log(author.name); // Jon Snow | |
for (const book of author.books) { | |
console.log(book.title); // initialized | |
console.log(book.author.isInitialized()); // true | |
console.log(book.author.id); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# using yarn | |
$ yarn add mikro-orm mongodb # for mongo | |
$ yarn add mikro-orm mysql2 # for mysql | |
$ yarn add mikro-orm pg # for postgresql | |
$ yarn add mikro-orm sqlite # for sqlite | |
# or npm | |
$ npm i -s mikro-orm mongodb # for mongo | |
$ npm i -s mikro-orm mysql2 # for mysql | |
$ npm i -s mikro-orm pg # for postgresql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const authorRepository = orm.em.getRepository(Author); | |
const jon = await authorRepository.findOne({ name: 'Jon Snow' }, ['books']); | |
const jon2 = await authorRepository.findOne({ email: 'snow@wall.st' }); | |
const authors = await authorRepository.findAll(['books']); | |
// identity map in action | |
console.log(jon === authors[0]); // true | |
console.log(jon === jon2); // true | |
// as we always have one instance, books will be populated also here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const book = orm.em.findOne(Book, '...'); | |
console.log(book.author); // reference with ID only, instance of Author entity | |
// this will get the same reference as we already have in `book.author` | |
const author = orm.em.getReference(Author, book.author.id); | |
console.log(author.id); // accessing the id will not trigger any db call | |
console.log(author.isInitialized()); // false | |
console.log(author.name); // undefined | |
console.log(author === book.author); // true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { QueryOrder } from 'mikro-orm'; | |
const booksRepository = orm.em.getRepository(Book); | |
// with sorting, limit and offset parameters, populating author references | |
const books = await booksRepository.find({ author: '...' }, ['author'], { title: QueryOrder.DESC }, 2, 1); | |
// or with options object | |
const books = await booksRepository.find({ author: '...' }, { | |
populate: ['author'], |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// find author and populate his books collection | |
const author = orm.em.findOne(Author, '...', ['books']); | |
for (const book of author.books) { | |
console.log(book); // instance of Book | |
} | |
author.books.add(book); | |
console.log(author.books.contains(book)); // true | |
author.books.remove(book); |
OlderNewer