DoneJS StealJS jQuery++ FuncUnit DocumentJS
5.33.3
6.0.0 4.3.0 3.14.1 2.3.35
  • About
  • Guides
  • API Docs
  • Community
  • Contributing
  • Bitovi
    • Bitovi.com
    • Blog
    • Design
    • Development
    • Training
    • Open Source
    • About
    • Contact Us
  • About
  • Guides
  • API Docs
    • Observables
      • can-bind
      • can-compute
      • can-debug
      • can-define
      • can-define/list/list
      • can-define/map/map
      • can-define-backup
      • can-define-stream
      • can-define-stream-kefir
      • can-event-queue
      • can-kefir
      • can-list
      • can-map
      • can-map-compat
      • can-map-define
      • can-observable-array
      • can-observable-object
      • can-observation
      • can-observation-recorder
      • can-observe
      • can-simple-map
      • can-simple-observable
      • can-stream
      • can-stream-kefir
      • can-value
    • Views
      • can-attribute-observable
      • can-component
      • can-stache
      • can-stache-bindings
      • can-stache-converters
      • can-stache-element
      • can-stache-route-helpers
      • can-view-autorender
      • can-view-callbacks
      • can-view-import
      • can-view-live
      • can-view-model
      • can-view-nodelist
      • can-view-parser
      • can-view-scope
      • can-view-target
      • steal-stache
    • Data Modeling
      • can-connect
      • can-connect-feathers
      • can-connect-ndjson
      • can-connect-tag
      • can-fixture
      • can-fixture-socket
      • can-local-store
        • data methods
      • can-memory-store
      • can-ndjson-stream
      • can-query-logic
      • can-realtime-rest-model
      • can-rest-model
      • can-set-legacy
      • can-super-model
    • Routing
      • can-deparam
      • can-param
      • can-route
      • can-route-hash
      • can-route-mock
      • can-route-pushstate
    • JS Utilities
      • can-assign
      • can-define-lazy-value
      • can-diff
      • can-globals
      • can-join-uris
      • can-key
      • can-key-tree
      • can-make-map
      • can-parse-uri
      • can-queues
      • can-string
      • can-string-to-any
      • can-zone-storage
    • DOM Utilities
      • can-ajax
      • can-attribute-encoder
      • can-child-nodes
      • can-control
      • can-dom-data
      • can-dom-events
      • can-dom-mutate
      • can-event-dom-enter
      • can-event-dom-radiochange
      • can-fragment
    • Data Validation
      • can-define-validate-validatejs
      • can-type
      • can-validate
      • can-validate-interface
      • can-validate-legacy
      • can-validate-validatejs
    • Typed Data
      • can-cid
      • can-construct
      • can-construct-super
      • can-data-types
      • can-namespace
      • can-reflect
      • can-reflect-dependencies
      • can-reflect-promise
      • can-types
    • Polyfills
      • can-symbol
      • can-vdom
    • Core
    • Infrastructure
      • can-global
      • can-test-helpers
    • Ecosystem
    • Legacy
  • Community
  • Contributing
  • GitHub
  • Twitter
  • Chat
  • Forum
  • News
Bitovi

can-local-store

  • npm package badge
  • Star
  • Edit on GitHub

Create, update, delete and query data saved in localStorage.

localStore( baseConnection )

Create a database-like store of a single data type. For example:

import localStore from "can-local-store";
import QueryLogic from "can-query-logic";

// Create a store
var todosStore = localStore({
    queryLogic: new QueryLogic({
        identity: ["id"]
    }),
    name: "todos"
});

// Add a list of data to the store
todosStore.updateListData(...);
// Get a list of data from the store
todosStore.getListData(...)
// Create a record in the store
todosStore.createData(...)
// Get a record in the store
todosStore.getData(...)
// Update a record in the store
todosStore.updateData(...)
// Remove a record from the store
todosStore.destroyData(...)
// Clear all records from the store
todosStore.clear()
// Get the queries that are currently stored
todosStore.getQueries()

Parameters

  1. baseConnection {Object}:

    A base can-connect connection or a settings object. baseConnection must have:

    • a queryLogic property that references a can-query-logic instance. The can-query-logic instance is used to determine the behavior of [can-local-store.getListData].
    • a unique name property used as a key to store data in localStorage.

    It can optionally have:

    • a cacheLocalStorageReads property. When set to true, prevents reading from localStorage and parsing the result on every read.

Use

can-local-store is used as a store of query-able data. It can either be used on its own or as part of a can-connect cache connection.

Standalone use

To use localStore, first create one with a queryLogic instance:

import localStore from "can-local-store";
import QueryLogic from "can-query-logic";

// Create a store
var todosStore = localStore({
    queryLogic: new QueryLogic({
        identity: ["id"]
    })
});

Then populate the store with data:

todosStore.updateListData([
    {id: 1, name: "dishes", points: 2},
    {id: 2, name: "lawn", points: 8},
    {id: 3, name: "trash", points: 1},
    {id: 4, name: "car wash", points: 5},
]);

Then you can query the store for data:

todosStore.getListData({
    filter: {points: {$gt: 1}},
    sort: "name",
    page: {start: 0, end: 1}
})
//-> {
//   data: [
//     {id: 4, name: "car wash", points: 5},
//     {id: 1, name: "dishes", points: 2}],
//   count: 3
// }

Use with connection

can-local-store is often used with a caching strategy like fall-through-cache or cache-requests as their cacheConnection. The following gives an example of using it with the connectFallThroughCache:

import {
    DefineMap,
    QueryLogic,
    localStore,
    connectFallThroughCache,
    connectCanMap,
    connectRest,
    connectConstructor,
    connect
} from "can";

// Define a type
const Todo = DefineMap.extend("Todo",{
    id: {identity: true, type:"number"},
    name: "string",
    complete: "boolean"
});

// Create a store
var todosStore = localStore({
    queryLogic: new QueryLogic(Todo),
    name: "todos"
});

var todoConnection = connect([
  connectRest,
  connectMap,
  connectFallThroughCache,
  connectConstructor
],
{
  url: "/services/todos",
  cacheConnection: todosStore
});

Caching localStorage reads

If your data set is large, you might want to avoid reading and parsing localStorage on every access to the local store. To avoid this, you can turn on cacheLocalStorageReads like:

var todosStore = localStore({
    queryLogic: new QueryLogic({
        identity: ["id"]
    }),
    name: "todos",
    cacheLocalStorageReads: true
});

CanJS is part of DoneJS. Created and maintained by the core DoneJS team and Bitovi. Currently 5.33.3.

On this page

Get help

  • Chat with us
  • File an issue
  • Ask questions
  • Read latest news