JSPM

  • Created
  • Published
  • Downloads 19
  • Score
    100M100P100Q51766F
  • License MIT

Web browser (client) storage interface for IndexedDB, WebSQL, LocalStorage, and in memory data with Schema validation.

Package Exports

  • client-web-storage
  • client-web-storage/ClientStore
  • client-web-storage/CustomTypes/ArrayOf
  • client-web-storage/CustomTypes/CustomType
  • client-web-storage/CustomTypes/OneOf
  • client-web-storage/CustomTypes/SchemaId
  • client-web-storage/MemoryStore
  • client-web-storage/Schema
  • client-web-storage/SchemaValue
  • client-web-storage/client
  • client-web-storage/client-web-storage.min
  • client-web-storage/default-config
  • client-web-storage/helpers/index
  • client-web-storage/helpers/use-client-store
  • client-web-storage/helpers/with-client-store
  • client-web-storage/index
  • client-web-storage/types
  • client-web-storage/utils/error-messages
  • client-web-storage/utils/generate-uuid
  • client-web-storage/utils/get-default-value
  • client-web-storage/utils/get-schema-type-and-default-value-from-value
  • client-web-storage/utils/is-empty-string
  • client-web-storage/utils/is-nil
  • client-web-storage/utils/is-object-literal
  • client-web-storage/utils/is-of-supported-type
  • client-web-storage/utils/is-same-value-type
  • client-web-storage/utils/is-supported-type
  • client-web-storage/utils/is-supported-type-value
  • client-web-storage/utils/is-valid-object-literal
  • client-web-storage/utils/object-to-schema

Readme

Client Web Storage

Browser storage interface for IndexedDB, WebSQL, LocalStorage, and in memory data with basic Schema and data validation.

npm

Documentation

Documentation

API References

Example

// todo.store.ts

import {ClientStore} from "client-web-storage";

interface ToDo {
    name: string;
    description: string;
    complete: boolean;
}

// create a store providing the name and schema object
// with default values or javasctipt types
const todoStore = new ClientStore<ToDo>("todo", {
    $name: String,
    description: "No Description",
    complete: false
});

Works with any web library or framework. Here is an example using React.

// app.tsx

import {useClientStore} from "client-web-storage";
import {todoStore} from "./stores/todo.store";

const App = () => {
    const {items, processing, error} = useClientStore<Todo>(todoStore);
    
    const handleCreatItem = async () => {
        await todoStore.createItem({
            name: "Go to Gym" // only name is required (marked with $)
        });
        
        /*  Creates item in the store
        {
          _id: "123e4567-e89b-12d3-a456-426614174000", // generated id
          _createdDate: "January, 4th 2022",
          _lastUpdatedDate: "January, 4th 2022",
          name: "Go to Gym",
          description: "No Description",
          complete: false,
        }
        */
    }
        
    if(processing) {
        return <Spinner />
    }
    
    if(error) {
        return <p className="error">{error.message}</p>
    }
    
    return (
        <>
            <button type="button" onClick={handleCreatItem}>create todo</button>
            <FlatList list={items} renderItem={renderTodo}/>
        </>
    )
}

Installation

In Node Projects:

npm install client-web-storage
import {Schema, ClientStore} from "client-web-storage";

In the Browser

<!-- use the latest version -->
<script src="https://unpkg.com/client-web-storage/dist/client-web-storage.min.js"></script>

<!-- use a specific version -->
<script src="https://unpkg.com/client-web-storage@1.0.0/dist/client-web-storage.min.js"></script>
<script>
  const {Schema, ClientStore} = window.CWS;
</script>