{"id":19793593,"url":"https://github.com/darky/effector-mikroorm","last_synced_at":"2025-10-26T15:13:53.256Z","repository":{"id":170115213,"uuid":"645483413","full_name":"darky/effector-mikroorm","owner":"darky","description":"Use MikroORM Entities inside Effector Stores and achieve auto persistence in DB","archived":false,"fork":false,"pushed_at":"2023-05-29T11:10:06.000Z","size":108,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-23T20:47:19.214Z","etag":null,"topics":["effector","mikroorm"],"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-05-25T18:55:52.000Z","updated_at":"2023-05-28T17:51:50.000Z","dependencies_parsed_at":null,"dependency_job_id":"77e2714b-bdfc-4450-b517-e8127c689715","html_url":"https://github.com/darky/effector-mikroorm","commit_stats":null,"previous_names":["darky/effector-mikroorm"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darky%2Feffector-mikroorm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darky%2Feffector-mikroorm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darky%2Feffector-mikroorm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darky%2Feffector-mikroorm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/darky","download_url":"https://codeload.github.com/darky/effector-mikroorm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241133146,"owners_count":19915347,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["effector","mikroorm"],"created_at":"2024-11-12T07:10:29.014Z","updated_at":"2025-10-26T15:13:53.149Z","avatar_url":"https://github.com/darky.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# effector-mikroorm\n\nUse MikroORM Entities inside Effector Stores and achieve auto persistence in DB\n\n## Knowledge requirements\n\nBasic knowledge of [Effector](https://effector.dev/) 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 **effector-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: [/* init MikroORM Entities */]\n  })\n)\n\napp.use(async (ctx, next) =\u003e {\n  await wrapEffectorMikroorm(orm, async () =\u003e { return await next() });\n})\n```\n\nFurther, simply use Effector and MikroORM \"as is\" in code and auto persistence in DB will \"magically\" works 🪄 \u003cbr/\u003e\nOnly need to use few utils like `em` and `sideEffect`, 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 { createEffect, createEvent, createStore } from 'effector'\nimport { em, entityConstructor, onPersist, scope, sideEffect, wrapEffectorMikroorm } from 'effector-mikroorm'\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 fetchUserFx = createEffect(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})\nconst createUser = createEvent\u003cPartial\u003cUserEntity\u003e\u003e()\nconst updateUser = createEvent\u003cUserEntity\u003e()\nconst deleteUser = createEvent\u003cnumber\u003e()\n\nconst $user = createStore\u003cUserEntity | null\u003e(null)\n\n$user.on(fetchUserFx.doneData, (_, userFetched) =\u003e userFetched)\n$user.on(createUser, (_, userPayload) =\u003e new UserEntity(userPayload))\n$user.on(updateUser, (state, userPayload) =\u003e wrap(state).assign(userPayload))\n$user.on(deleteUser, state =\u003e {\n  // for deleting Entity, just assign `$forDelete` to it\n  return wrap(state).assign({ $forDelete: true })\n})\n\n// `wrapEffectorMikroorm` here just for example\n// Need to use `wrapEffectorMikroorm` as middleware of your framework, see example above\nawait wrapEffectorMikroorm(orm, async () =\u003e {\n  // `sideEffect` is just little wrapper around Effector `allSettled`\n  // it consider Effector Store mutation inside specific life cycle\n  await sideEffect(createUser, { name: 'Vasya' })\n  // Optional hook, which will be called after DB persist\n  onPersist(async () =\u003e {\n    // `scope` returns Effector Scope related to this life cycle\n    scope().getState($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 wrapEffectorMikroorm(orm, async () =\u003e {\n  await fetchUserFx(1)\n  await sideEffect(updateUser, { id: 1, name: 'Petya' })\n})\n\n// user Vasya realized that he is Petya in DB now\n\nawait wrapEffectorMikroorm(orm, async () =\u003e {\n  await fetchUserFx(1)\n  await sideEffect(deleteUser, 1)\n})\n\n// user Petya go away from DB\n```\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdarky%2Feffector-mikroorm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdarky%2Feffector-mikroorm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdarky%2Feffector-mikroorm/lists"}