Skip to content

Instantly share code, notes, and snippets.

import { Connection, EntityManager, IDatabaseDriver } from "@mikro-orm/core";
export type MyContext = {
em: EntityManager<any> & EntityManager<IDatabaseDriver<Connection>>
}
import { Entity, PrimaryKey, Property } from "@mikro-orm/core";
import { Field, ObjectType, Int } from "type-graphql";
@ObjectType() //you can stack the decorators
@Entity()
export class Post {
@Field(() => Int) //exposes these to the graphql schema
@PrimaryKey()
id!: number;
import { MyContext } from "src/types";
import { Resolver, Query, Ctx } from "type-graphql";
import { Post } from "../entities/Post";
@Resolver() //decorator
export class PostResolver {
//queries or mutations:
@Query(() => [Post]) //this sets graphql type
// posts(
// // @Ctx() ctx: MyContext
import "reflect-metadata";
import { MikroORM } from "@mikro-orm/core";
import { __prod__ } from "./constants";
import microConfig from "./mikro-orm.config";
import express from "express";
import { ApolloServer } from "apollo-server-express";
import { buildSchema } from "type-graphql";
import { HelloResolver } from "./resolvers/hello";
import { PostResolver } from "./resolvers/post";
import { Resolver, Query } from "type-graphql";
@Resolver()
export class HelloResolver {
//queries or mutations:
@Query(() => String)
hello() {
return "hello world";
}
}
import { MikroORM } from "@mikro-orm/core"; //create mikro orm instance
import { __prod__ } from "./constants";
import microConfig from "./mikro-orm.config";
import express from 'express';
import { ApolloServer } from 'apollo-server-express';
import { buildSchema } from 'type-graphql';
import { HelloResolver } from "./resolvers/hello";
const main = async () => {
import { MikroORM } from "@mikro-orm/core"; //create mikro orm instance
import { __prod__ } from "./constants";
import microConfig from "./mikro-orm.config";
import express from 'express';
const main = async () => {
const orm = await MikroORM.init(microConfig);
await orm.getMigrator().up();
import { MikroORM } from "@mikro-orm/core"; //create mikro orm instance
import { __prod__ } from "./constants";
import { Post } from "./entities/Post";
import microConfig from "./mikro-orm.config";
const main = async () => {
const orm = await MikroORM.init(microConfig);
await orm.getMigrator().up(); //auto run migration in code
// const post = orm.em.create(Post, { title: "My First Post" });
// await orm.em.persistAndFlush(post);
import { MikroORM } from "@mikro-orm/core"; //create mikro orm instance
import { __prod__ } from "./constants";
import { Post } from "./entities/Post";
import microConfig from "./mikro-orm.config";
const main = async () => {
const orm = await MikroORM.init(microConfig);
await orm.getMigrator().up(); //auto run migration in code
import { Migration } from '@mikro-orm/migrations';
export class Migration20210618175137 extends Migration {
async up(): Promise<void> {
this.addSql('create table "post" ("id" serial primary key, "created_at" timestamptz(0) not null, "updated_at" timestamptz(0) not null, "title" text not null);');
}
}