@oimdb/async
v1.1.1
Published
Async stores support for OIMDB - async collections and indexes
Downloads
14
Readme
@oimdb/async
Async stores support for OIMDB - async collections and indexes.
Installation
npm install @oimdb/async @oimdb/coreOverview
This package provides async versions of collections and indexes that work with asynchronous stores (IndexedDB, async localStorage, remote databases, etc.). All operations return Promises and go directly to the async store without caching.
Features
- Async Collections -
OIMCollectionAsyncandOIMReactiveCollectionAsync - Async Indexes -
OIMIndexManualAsyncandOIMReactiveIndexManualAsync - No cache - data is not cached in memory, all operations go directly to the store
- Type-safe - full TypeScript support
- Reactive - supports event system from core package
Usage
Async Collection
import { OIMCollectionAsync } from '@oimdb/async';
import { IOIMCollectionStoreAsync } from '@oimdb/async';
// Implement your async store
class MyAsyncStore<TEntity, TPk> implements IOIMCollectionStoreAsync<TEntity, TPk> {
async getOneByPk(pk: TPk): Promise<TEntity | undefined> {
// Your async implementation
}
// ... other methods
}
// Create async collection
const store = new MyAsyncStore<User, string>();
const users = new OIMCollectionAsync<User, string>({ store });
// All operations are async
const user = await users.getOneByPk('user1');
await users.upsertOne({ id: 'user1', name: 'John' });
const allUsers = await users.getAll();Reactive Async Collection
import { OIMReactiveCollectionAsync } from '@oimdb/async';
import { OIMEventQueue, OIMEventQueueSchedulerFactory } from '@oimdb/core';
const queue = new OIMEventQueue({
scheduler: OIMEventQueueSchedulerFactory.createMicrotask()
});
const users = new OIMReactiveCollectionAsync<User, string>(queue, {
store: myAsyncStore
});
// Subscribe to updates
users.updateEventEmitter.subscribeOnKey('user1', () => {
console.log('User1 updated');
});
// Async operations
await users.upsertOne({ id: 'user1', name: 'John' });Async Index
import { OIMIndexManualAsync } from '@oimdb/async';
const index = new OIMIndexManualAsync<string, string>();
// All operations are async
await index.setPks('admin', ['user1', 'user2']);
await index.addPks('admin', ['user3']);
const adminUsers = index.getPksByKey('admin'); // Synchronous read from memoryReactive Async Index
import { OIMReactiveIndexManualAsync } from '@oimdb/async';
import { OIMEventQueue, OIMEventQueueSchedulerFactory } from '@oimdb/core';
const queue = new OIMEventQueue({
scheduler: OIMEventQueueSchedulerFactory.createMicrotask()
});
const index = new OIMReactiveIndexManualAsync<string, string>(queue);
// Subscribe to updates
index.updateEventEmitter.subscribeOnKey('admin', () => {
console.log('Admin users changed');
});
// Async operations
await index.setPks('admin', ['user1', 'user2']);API
Collections
OIMCollectionAsync<TEntity, TPk>- Basic async collectionOIMReactiveCollectionAsync<TEntity, TPk>- Reactive async collection with events
Indexes
OIMIndexManualAsync<TIndexKey, TPk>- Manual async indexOIMReactiveIndexManualAsync<TIndexKey, TPk>- Reactive manual async index
Interfaces
IOIMCollectionStoreAsync<TEntity, TPk>- Interface for async collection storesIOIMIndexStoreAsync<TIndexKey, TPk, TIndex>- Interface for async index stores
Types
TOIMCollectionOptionsAsync<TEntity, TPk>- Options for async collectionsTOIMIndexOptionsAsync<TIndexKey, TPk, TIndex>- Options for async indexes
Differences from Core Package
- All operations are async - methods return
Promise - No cache - data is not cached in memory
- Direct store operations - all operations go directly to the async store
- Separate classes -
OIMCollectionAsyncvsOIMCollection, etc.
Requirements
@oimdb/core^1.1.0 (peer dependency)
License
MIT
