Skip to content

Instantly share code, notes, and snippets.

View B4nan's full-sized avatar

Martin Adámek B4nan

View GitHub Profile
@B4nan
B4nan / gist:10259532
Created April 9, 2014 11:47 — forked from carlosmcevilly/gist:2221249
Fix commit author before pushed
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"
@B4nan
B4nan / 0_reuse_code.js
Created February 25, 2016 20:42
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@B4nan
B4nan / Book.ts
Last active April 30, 2019 16:25
Entity definition in MikroORM (mongodb)
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;
@B4nan
B4nan / persisting-entities.ts
Last active April 30, 2019 16:24
Persisting entities in MikroORM
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);
@B4nan
B4nan / fetching-entities.ts
Last active April 30, 2019 16:24
Fetching entities in MikroORM
// 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);
@B4nan
B4nan / 01-installation.sh
Last active April 30, 2019 16:23
Installation of MikroORM
# 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
@B4nan
B4nan / identity-map.ts
Last active April 30, 2019 16:24
Usage of Identity Map in MikroORM
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
@B4nan
B4nan / entity-references.ts
Last active April 30, 2019 16:24
Using entity references in MikroORM
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
@B4nan
B4nan / using-repository.ts
Last active April 30, 2019 16:24
Using entity repository in MikroORM
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'],
@B4nan
B4nan / working-with-collections.ts
Created February 26, 2019 19:39
Working with collections in MikroORM
// 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);