Can-Local-Store

1.0.1 · abandoned · verified Wed Apr 22

can-local-store is a client-side data persistence library for the CanJS framework, providing a localStorage-backed database for `can/Map` and `can/List` instances. It enables developers to automatically serialize and deserialize data to and from the browser's `localStorage` API, making it easy to store and retrieve application state or user data directly in the browser. The current stable version is 1.0.1, last released in October 2017. Due to its age and lack of updates since then, the package is considered abandoned, with no ongoing development or maintenance. Its key differentiator was its deep integration with CanJS's observable data structures, allowing seamless persistence without boilerplate. However, modern CanJS applications or new projects would typically opt for more actively maintained state management solutions or direct `localStorage` wrappers.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates defining a CanJS Map-based model with `can-local-store`, then performing CRUD (Create, Read, Update, Delete) operations that persist data in the browser's `localStorage`.

import Map from 'can-local-store/map';

// Define a Todo model that uses can-local-store/map
interface TodoAttributes {
  id?: number;
  name: string;
  completed: boolean;
}

const Todo = Map.extend<TodoAttributes>('todo', {
  // `todo` is the localStorage key for this model's data
  seal: false, // Recommended for can-define-stream compatibility
  init: function(this: any) {
    if (!this.id) {
      // Simple ID generation for example purposes
      this.id = Date.now();
    }
  }
});

async function runExample() {
  console.log('--- can-local-store example ---');

  // Create a new todo item
  const newTodo = new Todo({ name: 'Learn can-local-store', completed: false });
  await newTodo.save(); // Saves to localStorage under the key 'todo'
  console.log('Created todo:', newTodo.serialize());
  console.log('localStorage content (key "todo"):', localStorage.getItem('todo'));

  // Find all todos (loads from localStorage)
  const todos = await Todo.findAll({});
  console.log('All todos after creation:', todos.serialize());

  // Update an existing todo
  const firstTodo = todos.get(0);
  if (firstTodo) {
    firstTodo.completed = true;
    await firstTodo.save(); // Updates in localStorage
    console.log('Updated todo:', firstTodo.serialize());
    console.log('localStorage content (key "todo"):', localStorage.getItem('todo'));
  }

  // Find a specific todo by ID
  const foundTodo = await Todo.findOne({ id: newTodo.id });
  console.log('Found todo by ID:', foundTodo?.serialize());

  // Delete a todo
  if (foundTodo) {
    await foundTodo.destroy(); // Removes from localStorage
    console.log('Deleted todo:', foundTodo.serialize());
    console.log('localStorage content (key "todo"):', localStorage.getItem('todo'));
  }

  // Verify deletion by fetching all remaining todos
  const remainingTodos = await Todo.findAll({});
  console.log('Remaining todos:', remainingTodos.serialize());

  // Clean up localStorage completely for this example
  localStorage.removeItem('todo');
  console.log('localStorage "todo" key cleared.');
}

runExample().catch(console.error);

view raw JSON →