{"id":18817849,"url":"https://github.com/vitorluizc/persistence","last_synced_at":"2026-03-05T01:34:16.085Z","repository":{"id":47999039,"uuid":"166107354","full_name":"VitorLuizC/persistence","owner":"VitorLuizC","description":"💾 Persistence provides a pretty easy API to handle Storage's implementations.","archived":false,"fork":false,"pushed_at":"2021-08-11T04:33:05.000Z","size":627,"stargazers_count":19,"open_issues_count":9,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-09T06:35:46.309Z","etag":null,"topics":["ava","bili","indexeddb","javascript","localstorage","sessionstorage","storage","storage-engine","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/VitorLuizC.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}},"created_at":"2019-01-16T20:31:00.000Z","updated_at":"2022-06-06T08:26:59.000Z","dependencies_parsed_at":"2022-08-12T16:11:29.465Z","dependency_job_id":null,"html_url":"https://github.com/VitorLuizC/persistence","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VitorLuizC%2Fpersistence","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VitorLuizC%2Fpersistence/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VitorLuizC%2Fpersistence/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VitorLuizC%2Fpersistence/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/VitorLuizC","download_url":"https://codeload.github.com/VitorLuizC/persistence/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248795318,"owners_count":21162748,"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":["ava","bili","indexeddb","javascript","localstorage","sessionstorage","storage","storage-engine","typescript"],"created_at":"2024-11-08T00:13:39.628Z","updated_at":"2026-03-05T01:34:11.040Z","avatar_url":"https://github.com/VitorLuizC.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# `@vitorluizc/persistence`\n\n[![Build Status](https://travis-ci.org/VitorLuizC/persistence.svg?branch=master)](https://travis-ci.org/VitorLuizC/persistence)\n[![License](https://badgen.net/github/license/VitorLuizC/persistence)](./LICENSE)\n[![Library minified size](https://badgen.net/bundlephobia/min/@vitorluizc/persistence)](https://bundlephobia.com/result?p=@vitorluizc/persistence)\n[![Library minified + gzipped size](https://badgen.net/bundlephobia/minzip/@vitorluizc/persistence)](https://bundlephobia.com/result?p=@vitorluizc/persistence)\n\nPersistence provides a pretty easy API to handle Storage's implementations.\n\n- :zap: Pretty fast and **smaller than 0.3KB** (minified + gzipped ESM);\n- :label: Type definitions to TS developers and IDE/Editors autocomplete/intellisense;\n- :package: ESM, CommonJS and UMD distributions (CDN uses UMD as default);\n\n## Installation\n\nThis library is published in the NPM registry and can be installed using any compatible package manager.\n\n```sh\nnpm install @vitorluizc/persistence --save\n\n# For Yarn, use the command below.\nyarn add @vitorluizc/persistence\n```\n\n### Installation from CDN\n\nThis module has an UMD bundle available through JSDelivr and Unpkg CDNs.\n\n```html\n\u003cscript src=\"https://cdn.jsdelivr.net/npm/@vitorluizc/persistence\"\u003e\u003c/script\u003e\n\n\u003cscript\u003e\n  // module will be available through `persistence` function.\n\n  var name = persistence('name', { placeholder: 'Unknown' });\n\n  name.get();\n  //=\u003e 'Unknown'\n\u003c/script\u003e\n```\n\n## Usage\n\nIt exports a factory function to create persistence. A persistence can easily `set`, `get` and `delete` JSON parseable values to any `Storage` implementation (ex. `SessionStorage` and `LocalStorage`).\n\n```ts\nimport createPersistence from '@vitorluizc/persistence';\n\nconst persistence = createPersistence\u003cstring\u003e('Name', {\n  timeout: 1000 * 60 * 60 * 24, // A day in milliseconds\n  storage: window.sessionStorage,\n  placeholder: ''\n});\n\n// Setups field's value as a persistent state on session.\nconst field = document.querySelector('input[name=\"name\"]');\nfield.value = persistence.get();\nfield.addEventListener('input', () =\u003e persistence.set(field.value));\n```\n\n## API\n\n- **`createPersistence`**\n\n  The default exported factory function. It receives storage's key and, optionally, **`PersistenceOptions`** as arguments and returns a **`Persistence`**.\n\n  ```js\n  import createPersistence from '@vitorluizc/persistence';\n\n  const persistence = createPersistence('Storage\\'s key', { placeholder: 'None' });\n  ```\n\n  \u003cdetails\u003e\n    \u003csummary\u003eTypeScript type definitions.\u003c/summary\u003e\n\n  \u003cbr /\u003e\n\n  ```ts\n  declare const createPersistence: {\n    \u003cT = any, U = T\u003e (\n      name: string,\n      options: PersistenceOptions \u0026 {\n        placeholder: U;\n      }\n    ): Persistence\u003cT, U\u003e;\n\n    \u003cT = any, U = T | undefined\u003e (\n      name: string,\n      options?: PersistenceOptions\u003cU\u003e\n    ): Persistence\u003cT, U\u003e;\n  };\n\n  export default createPersistence;\n  ```\n  \u003c/details\u003e\n\n- **`PersistenceOptions`**\n\n  Options used as second argument on **`createPersistence`** to set timeout, storage and placeholder value to **`Persistence`**.\n\n  ```ts\n  import createPersistence, { PersistenceOptions } from '@vitorluizc/persistence';\n\n  const options: PersistenceOptions\u003cboolean\u003e = {\n    timeout: 1000 * 60 * 60, // 1 hour in milliseconds.\n    placeholder: false\n  };\n\n  const persistence = createPersistence\u003cboolean\u003e('isSignedIn', options);\n  ```\n\n  \u003cdetails\u003e\n    \u003csummary\u003eTypeScript type definitions.\u003c/summary\u003e\n\n  \u003cbr /\u003e\n\n  ```ts\n  export interface PersistenceOptions \u003cT = any\u003e {\n    storage?: IStorage;\n    timeout?: number;\n    placeholder?: T;\n  }\n  ```\n  \u003c/details\u003e\n\n- **`Persistence`**\n\n  An object with a pretty easy API to handle a Storage implementation.\n\n  - **`get`**: Get value from persistent storage.\n\n  - **`set`**: Set a value to a persistent storage.\n\n  - **`delete`**: Delete value from persistent storage.\n\n  ```tsx\n  import createPersistence, { Persistence } from '@vitorluizc/persistence';\n\n  // ...\n\n  type SignUpFormState = { nickname: string, };\n\n  const state: Persistence\u003cSignUpFormState\u003e = createPersistence('state@name', {\n    timeout: 1000 * 60 * 60 * 24, // A day in milliseconds\n    placeholder: {\n      nickname: ''\n    },\n  });\n\n\n  $nickname.value = state.get().nickname;\n  $nickname.on('keydown', (e) =\u003e state.set({ nickname: e.target.value }));\n\n  $signUpForm.on('submit', (e) =\u003e {\n    services.signUp(state.get());\n\n    state.delete();\n  });\n  ```\n\n  \u003cdetails\u003e\n    \u003csummary\u003eTypeScript type definitions.\u003c/summary\u003e\n\n  \u003cbr /\u003e\n\n  ```ts\n  export interface Persistence \u003cT = any, U = (T | undefined)\u003e {\n    get (): T | U;\n    set (value: T): void;\n    delete (): void;\n  }\n  ```\n  \u003c/details\u003e\n\n## License\n\nReleased under [MIT License](./LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvitorluizc%2Fpersistence","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvitorluizc%2Fpersistence","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvitorluizc%2Fpersistence/lists"}