npm package: @graviola/simple-local-data-store
Description: a datastore that uses zustand in memory data structure to store data
Latest version: 1.0.2

# Simple Local Data Store

A simple in-memory implementation of the AbstractDatastore interface using Zustand.

## Features

- Fully in-memory storage using Zustand state management
- Complete implementation of the AbstractDatastore interface
- Simple data organization by type and entity ID
- Supports sorting, pagination, and text search
- Implements async iterables for streamed data access
- Useful for testing and prototyping

## Installation

```bash
# Using npm
npm install @graviola/simple-local-data-store zustand

# Using yarn
yarn add @graviola/simple-local-data-store zustand

# Using bun
bun add @graviola/simple-local-data-store zustand
```

## Usage

### Basic Setup

```typescript
import { initLocalStore } from '@graviola/simple-local-data-store';
import type { JSONSchema7 } from 'json-schema';

// Define your schema
const schema: JSONSchema7 = {
  type: 'object',
  properties: {
    name: { type: 'string' },
    description: { type: 'string' },
    // ... other properties
  },
  required: ['name']
};

// Set up type mapping functions
const typeNameToTypeIRI = (typeName: string) => `http://example.org/ontology#${typeName}`;
const typeIRItoTypeName = (typeIRI: string) => typeIRI.replace('http://example.org/ontology#', '');

// Initialize the store
const datastore = initLocalStore({
  schema,
  typeNameToTypeIRI,
  typeIRItoTypeName,
  defaultLimit: 100
});
```

### Creating and Retrieving Documents

```typescript
// Create a new document
await datastore.upsertDocument(
  'Person', 
  'http://example.org/people/john-doe',
  {
    name: 'John Doe',
    email: 'john@example.com',
    age: 30
  }
);

// Get the document
const person = await datastore.loadDocument('Person', 'http://example.org/people/john-doe');
console.log(person); // { name: 'John Doe', email: 'john@example.com', age: 30, @id: '...', @type: '...' }

// Check if a document exists
const exists = await datastore.existsDocument('Person', 'http://example.org/people/john-doe');
console.log(exists); // true
```

### Finding Documents

```typescript
// Add more sample data
await datastore.upsertDocument('Person', 'http://example.org/people/jane-doe', { name: 'Jane Doe', age: 28 });
await datastore.upsertDocument('Person', 'http://example.org/people/bob-smith', { name: 'Bob Smith', age: 42 });

// List all people
const allPeople = await datastore.listDocuments('Person');
console.log(allPeople.length); // 3

// Find people by search term
const searchResults = await datastore.findDocuments('Person', { search: 'doe' });
console.log(searchResults.length); // 2 (John Doe and Jane Doe)

// Find with sorting
const sortedResults = await datastore.findDocuments('Person', {
  sorting: [{ id: 'age', desc: true }]
});
console.log(sortedResults[0].name); // 'Bob Smith' (age 42, highest first)

// Find with pagination
const pagedResults = await datastore.findDocuments('Person', {
  pagination: { pageIndex: 0, pageSize: 2 }
});
console.log(pagedResults.length); // 2 (first page with 2 items)
```

### Using Raw Store Access

For direct manipulation:

```typescript
import { 
  useDataStore, 
  upsertDocument, 
  findDocuments, 
  clearStore 
} from '@graviola/simple-local-data-store';

// Directly manipulate the store
upsertDocument('Task', 'task-1', { title: 'Complete project' });

// Subscribe to store changes in a React component
function TaskList() {
  const tasks = useDataStore((state) => 
    Object.values(state.documents.Task || {})
  );
  
  return (
    <ul>
      {tasks.map(task => <li key={task['@id']}>{task.title}</li>)}
    </ul>
  );
}

// Clear all data
clearStore();
```

## More Information

This package is part of the Ereignis Metadata Framework.