{"id":20032543,"url":"https://github.com/akicho8/js-memory-record","last_synced_at":"2025-11-25T19:02:21.407Z","repository":{"id":32442228,"uuid":"134031468","full_name":"akicho8/js-memory-record","owner":"akicho8","description":"A simple library that handles a few records easily","archived":false,"fork":false,"pushed_at":"2023-03-19T01:36:14.000Z","size":49736,"stargazers_count":1,"open_issues_count":5,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-04-24T19:24:30.757Z","etag":null,"topics":["database","immutable","javascript","library","simple","small","static"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/akicho8.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":"2018-05-19T05:07:50.000Z","updated_at":"2022-09-18T01:46:05.000Z","dependencies_parsed_at":"2024-11-13T09:49:40.103Z","dependency_job_id":null,"html_url":"https://github.com/akicho8/js-memory-record","commit_stats":{"total_commits":92,"total_committers":1,"mean_commits":92.0,"dds":0.0,"last_synced_commit":"566eb723b8db390ca3865f5bad0b327a0f0226f6"},"previous_names":[],"tags_count":27,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akicho8%2Fjs-memory-record","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akicho8%2Fjs-memory-record/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akicho8%2Fjs-memory-record/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akicho8%2Fjs-memory-record/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/akicho8","download_url":"https://codeload.github.com/akicho8/js-memory-record/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241465057,"owners_count":19967243,"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":["database","immutable","javascript","library","simple","small","static"],"created_at":"2024-11-13T09:38:14.466Z","updated_at":"2025-11-25T19:02:21.294Z","avatar_url":"https://github.com/akicho8.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MemoryRecord\n\n[![npm version](https://badge.fury.io/js/js-memory-record.svg)](https://badge.fury.io/js/js-memory-record)\n[![GitHub version](https://badge.fury.io/gh/akicho8%2Fjs-memory-record.svg)](https://badge.fury.io/gh/akicho8%2Fjs-memory-record)\n\n## Description\n\nA simple library that handles a few records easily.\n\n## Install\n\n```shell\nnpm install js-memory-record\n```\n\n## Simplest Example\n\n```js\nclass Gender extends MemoryRecord {\n  static get define() {\n    return [\n      { key: \"male\"   },\n      { key: \"female\" },\n    ]\n  }\n}\n\ngender = Gender.fetch(\"male\")\ngender.name          // =\u003e \"male\"\ngender.code          // =\u003e 0\n\nGender.fetch(0).name // =\u003e \"male\"\n```\n\n## More Example\n\n### Records Define\n\nReturn an array of Hash structures in `define()`\n\n```js\nimport MemoryRecord from 'js-memory-record'\n\nexport default class Fruit extends MemoryRecord {\n\n  // Record definition.\n\n  static get define() {\n    return [\n      { key: \"apple\",  name: \"Poison Apple\", price: 120, },\n      { key: \"melon\",  name: \"Green Melon\",  price: 800, },\n      { key: \"peach\",  name: \"Pink Piece\",   price: 200, },\n    ]\n  }\n\n  // Define an instance method referencing an attribute.\n  // Define it when accessing other than key, name and other attributes\n\n  get full_name() {\n    return `${this.name} (Now ${this.special_price} Gold)`\n  }\n\n  get special_price() {\n    return Math.ceil(this.price / 2)\n  }\n}\n```\n\n### fetch(key) - Key Access\n\nBasic access by this method.\n\n```js\nFruit.fetch(\"apple\").key        // =\u003e \"apple\"\nFruit.fetch(\"apple\").name       // =\u003e \"Poison Apple\"\nFruit.fetch(\"apple\").code       // =\u003e 0\nFruit.fetch(\"apple\").index      // =\u003e 0\n```\n\n### fetch(code) - Code Access\n\nCode is something like database ID.\nAllocate in order from 0.\nUse it when you want to access by special index.\n\n```js\nFruit.fetch(0).name                     // =\u003e \"Poison Apple\"\nFruit.fetch(0) === Fruit.fetch(\"apple\") // =\u003e true\n```\n\n### Attributes can be referred to as properties\n\n```js\nFruit.fetch(\"apple\").price      // =\u003e 120\n```\n\n### Additional Defined Instance Methods\n\nDefine the Instance Metod freely and return the attributes in an easy-to-use form.\nThis part is one of the merits of introducing this library.\n\n```js\nFruit.fetch(\"apple\").special_price // =\u003e 60\nFruit.fetch(\"apple\").full_name     // =\u003e \"Poison Apple (Now 60 Gold)\"\n```\n\n### Unknown Key Access\n\nThere is no `lookup()` exception.\n\n```js\nFruit.lookup(\"grape\")           // =\u003e null\n```\n\nIn case of `fetch()` we will throw an exception.\n\n```js\nFruit.fetch(\"grape\")            // (Throw Error)\n```\n\nThe error message of the exception is displayed as follows.\n\n```text\nError: Fruit.fetch(\"grape\") does not match anything\nkeys: [\"apple\",\"melon\",\"peach\"]\ncodes: [0,1,2]\n```\n\nThe cause of the error is displayed in an easy-to-understand manner.\n\n### Array Access\n\n```js\nFruit.values                    // =\u003e [{...}, {...}, {...}]\n```\n\n```js\nFruit.values.map(e =\u003e e.key)    // =\u003e [\"apple\", \"melon\", \"peach\"]\n```\n\n### Other Class Methods\n\n```js\nFruit.keys      // =\u003e [\"apple\", \"melon\", \"peach\"]\nFruit.codes     // =\u003e [0, 1, 2]\nFruit.names     // =\u003e [\"Poison Apple\", \"Green Melon\", \"Pink Piece\"],\n```\n\nWe do not use this class methods much. But it may be useful for debugging.\n\n### code can be explicitly specified\n\n```js\nstatic get define() {\n  return [\n    { code: 1, key: \"apple\", },\n    { code: 2, key: \"melon\", },\n    { code: 4, key: \"peach\", },\n  ]\n}\n```\n\n```js\nFruit.codes    // =\u003e [1, 2, 4]\n```\n\nThis is like managing database ID yourself. We do not recommend it.\nIt is only useful if you need consistency with old data.\n\n### How to reset the array of records later\n\nReplace the entire record internally held.\nI do not recommend this method much.\nBut it may be useful in emergency.\n\n```js\nFruit.memory_record_reset([\n  { key: \"foo\" },\n  { key: \"bar\" },\n])\n\nFruit.fetch(\"foo\").key    // =\u003e \"foo\"\n```\n\n## Build Setup\n\n```bash\n# install dependencies\nnpm install\n\n# build for production with minification\nnpm run build\n\n# run unit tests\nnpm test\n```\nmemory_record_reset\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fakicho8%2Fjs-memory-record","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fakicho8%2Fjs-memory-record","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fakicho8%2Fjs-memory-record/lists"}