{"id":25120123,"url":"https://github.com/userlike/settler","last_synced_at":"2025-07-19T17:14:48.178Z","repository":{"id":38418320,"uuid":"262752317","full_name":"userlike/settler","owner":"userlike","description":null,"archived":false,"fork":false,"pushed_at":"2023-07-18T22:58:39.000Z","size":2251,"stargazers_count":5,"open_issues_count":4,"forks_count":0,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-03-29T18:36:31.672Z","etag":null,"topics":["io-ts","migration","typescript"],"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/userlike.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-05-10T09:26:52.000Z","updated_at":"2025-02-07T23:22:38.000Z","dependencies_parsed_at":"2023-02-06T12:16:24.342Z","dependency_job_id":null,"html_url":"https://github.com/userlike/settler","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/userlike%2Fsettler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/userlike%2Fsettler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/userlike%2Fsettler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/userlike%2Fsettler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/userlike","download_url":"https://codeload.github.com/userlike/settler/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250318258,"owners_count":21410902,"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":["io-ts","migration","typescript"],"created_at":"2025-02-08T05:19:17.786Z","updated_at":"2025-04-22T20:29:11.328Z","avatar_url":"https://github.com/userlike.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## ⛺ Settler\n\n`settler` is a typesafe migration utility to migrate data between versions, similar to database migrations.\nUtilizes [io-ts](https://github.com/gcanti/io-ts) codecs.\n\nThis is aimed at having migration capability for persisted, versioned and JSON encoded data.\n\n### Install\n\n```sh\nyarn add @userlike/settler\n```\n\n### Usage\n\n- Only forward migrations are supported.\n- `settler` enforces correct number of migrations and correct types for migration functions.\n- Migrations are based on `io-ts`. The migrations object produced by `mkVersionedCodec` is actually an io-ts codec.\n\n  - `migrations.decode` decodes versioned data and applies migration functions to migrate it to the last version.\n  - `migrations.encode` encodes data by wrapping it with an object and embedding version info; which can later be decoded, and potentially migrated.\n  - `decode(encode(myData))` is equal to `myData`.\n  - Typical use case:\n\n    ```ts\n    // persist\n    localStorage.setItem(\"myData\", JSON.stringify(migrations.encode(myData)));\n\n    // restore\n    migrations.decode(JSON.parse(localStorage.getItem(\"myData\")));\n    ```\n\n#### Further examples\n\n```ts\nimport { mkVersionedCodec } from \"@userlike/settler\";\nimport * as io from \"io-ts\";\nimport { right } from \"fp-ts/lib/Either\";\n\n// Migration definitions for 3 versions.\nconst migrations = mkVersionedCodec([\n  { version: \"1\", codec: io.strict({ isOpened: io.boolean }) },\n  {\n    version: \"2\",\n    codec: io.strict({ isOpened: io.boolean, ids: io.array(io.number) }),\n  },\n  { version: \"3\", codec: io.strict({ ids: io.array(io.number) }) },\n])([\n  // from v1 to v2\n  ({ isOpened }) =\u003e ({ isOpened, ids: [] }),\n  // from v2 to v3\n  ({ ids }) =\u003e ({ ids }),\n]);\n\n// =========================\n\n// Migrating from version 1\nexpect(\n  migrations.decode({\n    version: \"1\",\n    data: {\n      isOpened: true,\n    }\n  })\n).toEqual(right({ ids: [] }));\n\n// Migrating from version 2\nexpect(\n  migrations.decode({\n    version: \"2\",\n    data: {\n      isOpened: true,\n      ids: [1, 2, 3],\n    },\n  })\n).toEqual(right({ ids: [1, 2, 3] }));\n\n// Migrating from version 3, noop\nexpect(\n  migrations.decode({\n    version: \"3\",\n    data: {\n      ids: [1, 2, 3],\n    },\n  })\n).toEqual(right({ ids: [1, 2, 3] }));\n\n// Encoding versioned data\n// Can use this to persist\nexpect(\n  migrations.encode({\n    ids: [1, 2, 3],\n  })\n).toEqual({\n  version: \"3\",\n  data: {\n    ids: [1, 2, 3],\n  },\n});\n```\n\nSee [tests](./settler/src/index.test.ts).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuserlike%2Fsettler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fuserlike%2Fsettler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuserlike%2Fsettler/lists"}