{"id":19050968,"url":"https://github.com/tomasbasham/ember-cli-persistence","last_synced_at":"2025-07-20T20:05:29.722Z","repository":{"id":57223417,"uuid":"50777797","full_name":"tomasbasham/ember-cli-persistence","owner":"tomasbasham","description":"An ember-cli addon to interface with front end persistence containers","archived":false,"fork":false,"pushed_at":"2022-12-01T23:21:45.000Z","size":529,"stargazers_count":1,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-04T04:28:31.575Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/tomasbasham.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-01-31T14:15:50.000Z","updated_at":"2019-02-18T12:54:39.000Z","dependencies_parsed_at":"2023-01-23T08:15:44.894Z","dependency_job_id":null,"html_url":"https://github.com/tomasbasham/ember-cli-persistence","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/tomasbasham/ember-cli-persistence","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomasbasham%2Fember-cli-persistence","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomasbasham%2Fember-cli-persistence/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomasbasham%2Fember-cli-persistence/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomasbasham%2Fember-cli-persistence/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tomasbasham","download_url":"https://codeload.github.com/tomasbasham/ember-cli-persistence/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomasbasham%2Fember-cli-persistence/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266189677,"owners_count":23890065,"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":[],"created_at":"2024-11-08T23:16:47.975Z","updated_at":"2025-07-20T20:05:29.705Z","avatar_url":"https://github.com/tomasbasham.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ember-cli-persistence [![Build Status](https://travis-ci.com/tomasbasham/ember-cli-persistence.svg?branch=master)](https://travis-ci.com/tomasbasham/ember-cli-persistence) [![Maintainability](https://api.codeclimate.com/v1/badges/c70f1ba7c215d29664b7/maintainability)](https://codeclimate.com/github/tomasbasham/ember-cli-persistence/maintainability)\n\nAn [Ember CLI](https://ember-cli.com/) addon to interface with front end\npersistence containers.\n\nMost modern applications require the ability to persist data and state for the\nruntime of the application. This data may be temporary and only last the\nduration of the session or could perhaps be longer lasting, requiring that the\napplication be able to fetch this data at a later point in time.\n\nThis addon provides a simple `persistence` service allowing your applications\nto make use of the `LocalStorage` and `SessionStorage` containers found within\nmost modern HTML5 capable browsers. In instances where these two storage\ncontainers are not available this addon provides an `Ephemeral` container that\nsimply acts as an in-memory key/value store for the duration of the\napplications runtime.\n\nThis addon is built upon the\n[ember-cli-adpater-pattern](https://github.com/tomasbasham/ember-cli-adapter-pattern)\nallowing you to easily create your own storage container adapters.\n\n## Compatibility\n\n* Ember.js v2.18 or above\n* Ember CLI v2.13 or above\n\n## Installation\n\nFrom within your Ember CLI project directory run:\n```\nember install ember-cli-persistence\n```\n\n## Usage\n\nThis addon implements a service to interface with several front end persistence\ncontainers by providing an abstract API that hides the implementation details\nof each persistence container adapter.\n\n### Configuration\n\nBefore the `persistence` service can be used it first must be configured\nthrough `config/environment`. This allows you to define which of the\npersistence containers you want to make available to your application through\nthe `persistence` service.\n\n##### Configuration Example\n\n```JavaScript\n// config/environment.js\nmodule.exports = function(environment) {\n  let ENV = {\n    persistence: {\n      containers: [\n        {\n          name: 'Local',\n          config: {\n            namespace: 'local-enterprise'\n          }\n        },\n        {\n          name: 'Session',\n          config: {\n            namespace: 'session-enterprise'\n          }\n        },\n        {\n          name: 'Ephemeral',\n          config: {\n            namespace: 'ephemeral-enterprise'\n          }\n        }\n      ]\n    }\n  };\n\n  return ENV;\n};\n```\n\nThis configures your application to use all 3 adapters bundled with this addon\nand also namespaces each of key with the specified string.\n\n#### Namespacing\n\nAs mentioned above you are able to namespace each of the keys you send to the\npersistence containers. This is completely optional but recommended if you have\na lot of persisted data where key names may conflict. Key names are constructed\ninternally by the bundled adapters and take the form `namespace:key`.\n\n#### Containers\n\nThe containers array takes a series of objects defining the configuration of\neach adapter. This is a requirement of\n[ember-cli-adpater-pattern](https://github.com/tomasbasham/ember-cli-adapter-pattern)\nwhere each object may take an additional series of key/value pairs. This addon\nhowever only requires the name of the adapter, in pascal case.\n\n### Injection\n\nThis addon makes no assumptions about what ember objects you want to make the\n`persistence` service available. Therefore in order to make the service\navailable you need to implement you own injections.\n\n##### Injection Initializer Example\n\n```JavaScript\n// app/initializers/persistence.js\nexport function initialize(application) {\n  application.inject('controller', 'persistence', 'service:persistence');\n  application.inject('route', 'persistence', 'service:persistence');\n};\n\nexport default { initialize };\n```\n\nThis will make the `persistence` service available to all controllers and\nroutes. It is however unlikely that you will require the service to be injected\ninto every controller or route of your applications. Therefore it is\nrecommended that you include the service on a per object basis.\n\n##### Injection Controller Example\n\n```JavaScript\n// app/controllers/application.js\nimport Controller from '@ember/controller';\nimport { inject } from '@ember/service';\n\nexport default Controller.extend({\n  persistence: inject()\n});\n```\n\nThis will create a dependency on the application controller and inject the\n`persistence` service into this controller only. This can be repeated across\nall objects that need access to the service.\n\n### Persistence Service\n\nThe `persistence` service implements an abstract API that currently supports\nthe following methods:\n\n* `setItem`\n* `getItem`\n* `removeItem`\n* `key`\n* `clear`\n* `length`\n\nWhen using this API, by default the service will call the corresponding method\non each of the adapters unless a specific adapter is specified. This means that\nif you were to call `setItem` on the service, it would in turn call `setItem`\non each of the adapters and thus store the same data within each of the\npersistence containers configured in `config/environment`.\n\n##### All Adapters Example\n\n```JavaScript\n// app/controllers/application.js\nimport Controller from '@ember/controller';\n\nimport { get } from '@ember/object';\nimport { inject } from '@ember/service';\n\nexport default Controller.extend({\n  persistence: inject(),\n\n  actions: {\n    storeTime() {\n      const persistence = get(this, 'persistence');\n      persistence.setItem({ key: 'time', value: Date() });\n    }\n  }\n});\n```\n\nThis is likely not the desired behaviour and is a consequence of the\n[ember-cli-adpater-pattern](https://github.com/tomasbasham/ember-cli-adapter-pattern).\nIf you only want to store the data in a single persistence container you must\nspecify the name.\n\n##### Single Adapter Example\n\n```JavaScript\n// app/controllers/application.js\nimport Controller from '@ember/controller';\n\nimport { get } from '@ember/object';\nimport { inject } from '@ember/service';\n\nexport default Controller.extend({\n  persistence: inject(),\n\n  actions: {\n    storeTime() {\n      const persistence = get(this, 'persistence');\n      persistence.setItem('Local', { key: 'time', value: Date() });\n    }\n  }\n});\n```\n\nThis will only store the data using the `LocalStorage` adapter.\n\nEach API call will be wrapped within a promise that resolves with the returned\nresult of the adapter mapped to the adapter name. This means that for any\nmethod called on the service where a return value is expected you must use the\nPromise API to access it.\n\n##### Promise Single Adapter Example\n\n```JavaScript\n// app/controllers/application.js\nimport Controller from '@ember/controller';\n\nimport { get } from '@ember/object';\nimport { inject } from '@ember/service';\n\nexport default Controller.extend({\n  persistence: inject(),\n\n  actions: {\n    getTime() {\n      const persistence = get(this, 'persistence');\n      persistence.getItem('Local', { key: 'time' }).then(function(values) {\n        // Print the value returned from the LocalStorage adapter.\n        console.log(values['Local']);\n      });\n    }\n  }\n});\n```\n\n## License\n\nThis project is licensed under the [MIT License](LICENSE.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomasbasham%2Fember-cli-persistence","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftomasbasham%2Fember-cli-persistence","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomasbasham%2Fember-cli-persistence/lists"}