Skip to content

Instantly share code, notes, and snippets.

View B4nan's full-sized avatar

Martin Adámek B4nan

View GitHub Profile
// first partially load author with `id` and `email` only
const a1 = await em.findOneOrFail(Author, 123, { fields: ['id', 'email'] });
a1.email = 'lol'; // let's change the email
// reloading with same fields won't fire the query (as before)
const a2 = await em.findOneOrFail(Author, 123, { fields: ['email'] });
console.log(a1 === a2); // true, same entity instance, no query was fired
// reloading with additional fields will work without `refresh: true`
const a3 = await em.findOneOrFail(Author, 123, { fields: ['id', 'age'] });
@Entity()
class Owner {
@PrimaryKey()
id!: number;
@Property()
name!: string;
@Embedded(() => [Cat, Dog])
// querying for author will trigger auto-flush if we have new author persisted
const a1 = new Author(...);
em.persist(a1);
const r1 = await em.find(Author, {});
// querying author won't trigger auto-flush if we have new book, but no changes on author
const b4 = new Book(...);
em.persist(b4);
const r2 = await em.find(Author, {});
@B4nan
B4nan / Dockerfile
Last active May 25, 2021 09:22 — forked from jancurn/Dockerfile
Example of an Apify actor stored in a GitHub Gist.
# Here you choose the base Docker image for the actor. Apify provides the following images:
# apify/actor-node-basic
# apify/actor-node-chrome
# apify/actor-node-puppeteer
# However, you can use any other image from Docker Hub.
# For more information, see https://apify.com/docs/actor#base-images
FROM apify/actor-node:16
# Copy all files and directories from the directory to the Docker image
COPY . ./
@B4nan
B4nan / 01-batch-insert.ts
Last active December 21, 2022 09:03
MikroORM 4.1: Bulk updates
for (let i = 1; i <= 5; i++) {
const u = new User(`Peter ${i}`, `peter+${i}@foo.bar`);
em.persist(u);
}
await em.flush();
// insert into `user` (`name`, `email`) values
// ('Peter 1', 'peter+1@foo.bar'),
// ('Peter 2', 'peter+2@foo.bar'),
@B4nan
B4nan / init-pool.ts
Created January 16, 2020 08:14
MikroORM 3: connection pooling
const orm = await MikroORM.init({
entities: [Author, Book],
dbName: 'my-db-name',
pool: { min: 10, max: 20 }, // see https://github.com/vincit/tarn.js#usage for other pool options
});
@B4nan
B4nan / knex-from-connection.ts
Created January 15, 2020 20:47
MikroORM 3: getting Knex.js instance from Connection
const conn = orm.em.getConnection() as AbstractSqlConnection;
// you can make sure the `em` is correctly typed to `EntityManager<AbstractSqlDriver>`
// or one of its implementations:
// const em: EntityManager<AbstractSqlDriver> = orm.em;
const knex = conn.getKnex();
// do what ever you need with `knex`
const res = await knex;
@B4nan
B4nan / using-knex.ts
Created January 15, 2020 20:44
MikroORM 3: using Knex.js
const qb = orm.em.createQueryBuilder(Author);
qb.update({ name: 'test 123', type: PublisherType.GLOBAL }).where({ id: 123, type: PublisherType.LOCAL });
const knex = qb.getKnexQuery(); // instance of Knex' QueryBuilder
// do what ever you need with `knex`
const res = await orm.em.getConnection().execute(knex);
const entities = res.map(a => orm.em.map(Author, a));
console.log(entities); // Author[]
@B4nan
B4nan / disabled-auto-flush.ts
Created January 13, 2020 11:52
MikroORM 3: disabled auto-flushing
orm.em.persist(new Entity()); // no auto-flushing by default
await orm.em.flush();
await orm.em.persist(new Entity(), true); // you can still use second parameter to auto-flush
@B4nan
B4nan / query-typing.ts
Created January 13, 2020 11:47
MikroORM 3: strict query typing
// correct query
em.find(Author, {
favouriteBook: {
author: { name: '...' },
},
age: { $gte: 40 }, // operators are also supported
});
// 2 errors will be reported here
em.find(Author, {