{"id":19793587,"url":"https://github.com/darky/ts-fp-di-mikroorm","last_synced_at":"2026-02-06T16:33:14.662Z","repository":{"id":174015482,"uuid":"651659233","full_name":"darky/ts-fp-di-mikroorm","owner":"darky","description":"Use MikroORM Entities inside ts-fp-di State and achieve auto persistence in DB","archived":false,"fork":false,"pushed_at":"2024-09-24T09:54:28.000Z","size":87,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-21T00:25:22.631Z","etag":null,"topics":["mikroorm","ts-fp-di"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/darky.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-06-09T18:41:55.000Z","updated_at":"2024-09-24T09:54:32.000Z","dependencies_parsed_at":"2023-10-15T13:26:13.213Z","dependency_job_id":"b8369eb0-885e-4f06-beb0-b896a8819cc2","html_url":"https://github.com/darky/ts-fp-di-mikroorm","commit_stats":{"total_commits":67,"total_committers":1,"mean_commits":67.0,"dds":0.0,"last_synced_commit":"e6605e3ef80714f455feab247fe6188fb9b6c0b0"},"previous_names":["darky/ts-fp-di-mikroorm"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/darky/ts-fp-di-mikroorm","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darky%2Fts-fp-di-mikroorm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darky%2Fts-fp-di-mikroorm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darky%2Fts-fp-di-mikroorm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darky%2Fts-fp-di-mikroorm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/darky","download_url":"https://codeload.github.com/darky/ts-fp-di-mikroorm/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darky%2Fts-fp-di-mikroorm/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29168521,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-06T15:38:29.831Z","status":"ssl_error","status_checked_at":"2026-02-06T15:37:48.592Z","response_time":59,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mikroorm","ts-fp-di"],"created_at":"2024-11-12T07:10:28.280Z","updated_at":"2026-02-06T16:33:14.634Z","avatar_url":"https://github.com/darky.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ts-fp-di-mikroorm\n\nUse MikroORM Entities inside ts-fp-di State and achieve auto persistence in DB\n\n## Knowledge requirements\n\nBasic knowledge of [ts-fp-di](https://github.com/darky/ts-fp-di/) and [MikroORM](https://mikro-orm.io/)\n\n## Get started\n\nFirstly, need to wrap each life cycle of your backend application (each HTTP request/response, handle MQ message, ...) with **ts-fp-di-mikroorm**\u003cbr/\u003e\nExample of middleware for typical Koa application, where each HTTP request will be wrapped:\n\n```ts\nconst orm = await MikroORM.init(\n  defineConfig({\n    /* DB config */\n    entities: [\n      /* init MikroORM Entities */\n    ],\n  })\n)\n\napp.use(async (ctx, next) =\u003e {\n  await wrapTsFpDiMikroorm(orm, async () =\u003e {\n    return await next()\n  })\n})\n```\n\nFurther, simply use ts-fp-di and MikroORM \"as is\" in code and auto persistence in DB will \"magically\" works 🪄 \u003cbr/\u003e\nOnly need to use `em` helper for MikroORM, which can help to consider context of appropriate life cycle\n\n## Example\n\n```ts\nimport { Entity, PrimaryKey, Property, wrap } from '@mikro-orm/core'\nimport { em, entityConstructor, onPersist, wrapTsFpDiMikroorm } from 'ts-fp-di-mikroorm'\nimport { dis, dic } from 'ts-fp-di'\n\n@Entity()\nclass UserEntity {\n  constructor(entity: Partial\u003cUserEntity\u003e) {\n    // just little sugar, for avoiding boilerplate this.key = value\n    entityConstructor(this, entity)\n  }\n\n  @PrimaryKey()\n  id!: number\n\n  @Property()\n  name!: string\n\n  // service property for deleting Entity, see below\n  $forDelete?: boolean\n}\n\nconst fetchUser = async (id: number) =\u003e {\n  // `em()` will return MikroORM Entity Manager for appropriate life cycle\n  // need use `em()` everywhere, when you want to use MikroORM API\n  return em().findOne(UserEntity, { id })\n}\n\n// diOnce, dic, diMap also supported\nconst $user = dis\u003cUserEntity | null\u003e((state, payload) =\u003e\n  state ? wrap(state).assign(payload) : payload instanceof UserEntity ? payload : new UserEntity(payload)\n)\n\n// Entities can be placed in Array, Promise, Set, Map (as values) and fp-ts Some, Right\n// Persistance for them will works\nconst $users = dic\u003cUserEntity[]\u003e()\n\n// `wrapTsFpDiMikroorm` here just for example\n// Need to use `wrapTsFpDiMikroorm` as middleware of your framework, see example above\nawait wrapTsFpDiMikroorm(orm, async () =\u003e {\n  // Mutate $user State for futher mutation\n  $user({ name: 'Vasya' })\n  // Optional hook, which will be called after DB persist\n  onPersist(async () =\u003e {\n    $user() // BTW, $user already contains `id`, because it's already persisted in DB\n  })\n})\n\n// By the way, user Vasya already persisted in DB!\n\nawait wrapTsFpDiMikroorm(orm, async () =\u003e {\n  $user(await fetchUser(1))\n  $user({ name: 'Petya' })\n})\n\n// user Vasya realized that he is Petya in DB now\n\nawait wrapTsFpDiMikroorm(orm, async () =\u003e {\n  $user({ id: 1, name: 'Petya', $forUpdate: true })\n})\n\n// if you know id, you can update entity without fetching\n\nawait wrapTsFpDiMikroorm(orm, async () =\u003e {\n  $user({ name: 'Petya', $forUpsert: true })\n})\n\n// upsert also works, Entity fetching will be happens on flush afterwards\n\nawait wrapTsFpDiMikroorm(orm, async () =\u003e {\n  $user(await fetchUser(1))\n  $user({ $noPersist: true })\n})\n\n// Persistance to DB ignored\n\nawait wrapTsFpDiMikroorm(orm, async () =\u003e {\n  $user(await fetchUser(1))\n  $user({ $forDelete: true })\n})\n\n// user Petya go away from DB\n\nawait wrapTsFpDiMikroorm(orm, async () =\u003e {\n  $users([\n    new UserEntity({ id: 1, name: 'Petya', $forUpdate: true }),\n    new UserEntity({ id: 2, name: 'Vasya', $forUpdate: true }),\n    new UserEntity({ id: 3, name: 'Kolya', $forUpdate: true }),\n  ])\n})\n\n// all company will be persisted, despite the fact that they are in an Array\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdarky%2Fts-fp-di-mikroorm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdarky%2Fts-fp-di-mikroorm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdarky%2Fts-fp-di-mikroorm/lists"}