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 { Connection, EntityManager, IDatabaseDriver } from "@mikro-orm/core"; | |
export type MyContext = { | |
em: EntityManager<any> & EntityManager<IDatabaseDriver<Connection>> | |
} |
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 { 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; |
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 { 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 |
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 "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"; |
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 { Resolver, Query } from "type-graphql"; | |
@Resolver() | |
export class HelloResolver { | |
//queries or mutations: | |
@Query(() => String) | |
hello() { | |
return "hello world"; | |
} | |
} |
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 { 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 () => { | |
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 { 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(); |
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 { 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); |
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 { 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 |
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 { 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);'); | |
} | |
} |
NewerOlder