0.1.1 • Published 4 years ago

svelte-cachedb v0.1.1

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

svelte-cachedb

A JSON database leveraging Svelte's writable stores and local storage

Getting Started

  1. Install svelte-cachedb: yarn add -D svelte-cachedb
  2. Add context provider to Svelte

    <!-- src/index.svelte -->
    <script>
      import CacheDB from 'svelte-cachedb'
      import App from './App.svelte'
    </script>
    
    <CacheDB>
      <App />
    </CacheDB>
  3. Access with getContext('cachedb')

    <script>
      import { getContext } from 'svelte'
      const { db } = getContext('cachedb')
    
      // updates to db store will automatically write out to localStorage
      db.update((d) => ({ ...d, newProp: 'newValue' }))
    </script>

Customization

PropertyDefaultDescription
contextKeycachedbkey used to store in Svelte's context (i.e. getContext('cachedb'))
dbNamecachedbname used for localStorage database name
dbKeysvelte-cachedbvalue used to store database in localStorage (i.e. localStorage.getItem('svelte-cachedb'))
version1value used for storing database version
autoSavetruewhen the db store updates, autosaves to localStorage

In Action

<script>
  import CacheDB from 'svelte-cachedb'
  import App from './App.svelte'
</script>

<CacheDB
  contextKey="cachedb"
  dbName="cachedb"
  dbKey="svelte-cachedb"
  version={1}
  autoSave={true}
>
  <App />
</CacheDBcontextKey="cachedb">