@yihuangdb/storage-object
Version:
A Node.js storage object layer library using Redis OM
237 lines • 6.33 kB
TypeScript
/**
* @module quick-start
* @description Simplified API for quick prototyping and simple use cases.
*
* This module provides the simplest possible API for using StorageObject.
* Perfect for prototypes, scripts, and applications that don't need advanced features.
*
* @example
* ```typescript
* import { storage } from '@yihuangdb/storage-object/quick';
*
* // Create a storage with just field types
* const users = await storage('users', {
* name: 'text',
* email: 'string',
* age: 'number'
* });
*
* // CRUD operations
* const user = await users.create({ name: 'John', email: 'john@example.com', age: 30 });
* const found = await users.findById(user.entityId);
* await users.update(user.entityId, { age: 31 });
* await users.delete(user.entityId);
* ```
*/
import { StorageObject } from './storage';
import { FieldType } from './storage-schema';
/**
* Simple field definition - just the type as a string
*/
export type SimpleSchema = Record<string, FieldType>;
/**
* Create or get a storage with minimal configuration.
*
* This is the simplest way to create a storage. It automatically:
* - Creates the storage if it doesn't exist
* - Returns the existing storage if it does
* - Indexes all fields by default
* - Uses JSON storage mode
* - Enables optimistic locking
*
* @param name - Name of the storage
* @param schema - Simple schema definition (just field types)
* @returns Promise resolving to the storage instance
*
* @example
* ```typescript
* // Super simple - just field types
* const users = await storage('users', {
* name: 'text',
* email: 'string',
* age: 'number',
* isActive: 'boolean',
* tags: 'string[]',
* createdAt: 'date'
* });
*
* // Start using it immediately
* await users.create({
* name: 'John',
* email: 'john@example.com',
* age: 30,
* isActive: true
* });
* ```
*/
export declare function storage<T extends Record<string, any> = any>(name: string, schema?: SimpleSchema): Promise<StorageObject<T>>;
/**
* Quick storage creation with inline operations.
*
* @param name - Name of the storage
* @returns Object with storage operations
*
* @example
* ```typescript
* const { create, find, update, remove } = quick('users');
*
* // Define schema on first create
* const user = await create(
* { name: 'John', email: 'john@example.com' },
* { name: 'text', email: 'string' } // Schema only needed once
* );
*
* // Subsequent operations don't need schema
* const users = await find({ name: 'John' });
* await update(user.entityId, { email: 'newemail@example.com' });
* await remove(user.entityId);
* ```
*/
export declare function quick<T extends Record<string, any> = any>(name: string): {
/**
* Create a new entity
*/
create: (data: Partial<T>, schema?: SimpleSchema) => Promise<import("./storage").StorageEntity<T>>;
/**
* Find entities by query
*/
find: (query: Partial<T>) => Promise<import("./storage").StorageEntity<T>[]>;
/**
* Find one entity by query
*/
findOne: (query: Partial<T>) => Promise<import("./storage").StorageEntity<T> | null>;
/**
* Find entity by ID
*/
findById: (id: string) => Promise<import("./storage").StorageEntity<T> | null>;
/**
* Find all entities
*/
findAll: () => Promise<import("./storage").StorageEntity<T>[]>;
/**
* Update entity by ID
*/
update: (id: string, data: Partial<T>) => Promise<import("./storage").StorageEntity<T> | null>;
/**
* Delete entity by ID
*/
remove: (id: string) => Promise<boolean>;
/**
* Count entities
*/
count: () => Promise<number>;
/**
* Clear all data
*/
clear: () => Promise<void>;
/**
* Get the underlying storage instance for advanced operations
*/
instance: () => Promise<StorageObject<T>>;
};
/**
* Define a typed storage schema for better TypeScript support.
*
* @example
* ```typescript
* interface User {
* name: string;
* email: string;
* age: number;
* isActive: boolean;
* }
*
* const userSchema = define<User>({
* name: 'text',
* email: 'string',
* age: 'number',
* isActive: 'boolean'
* });
*
* const users = await storage<User>('users', userSchema);
*
* // Now TypeScript knows the shape of your data
* const user = await users.create({
* name: 'John',
* email: 'john@example.com',
* age: 30,
* isActive: true
* });
* ```
*/
export declare function define<T extends Record<string, any>>(schema: {
[K in keyof T]: FieldType;
}): SimpleSchema;
/**
* Connect to Redis with custom configuration.
* Optional - StorageObject uses default localhost connection if not called.
*
* @param config - Redis connection options
*
* @example
* ```typescript
* // Connect to custom Redis instance
* await connect({
* host: 'redis.example.com',
* port: 6379,
* password: 'secret'
* });
*
* // Now all storage operations will use this connection
* const users = await storage('users', { name: 'text' });
* ```
*/
export declare function connect(config: {
host?: string;
port?: number;
password?: string;
db?: number;
}): Promise<void>;
/**
* List all available storages.
*
* @returns Array of storage names
*
* @example
* ```typescript
* const names = await list();
* console.log('Available storages:', names);
* // ['users', 'products', 'orders']
* ```
*/
export declare function list(): Promise<string[]>;
/**
* Delete a storage and all its data.
*
* @param name - Name of the storage to delete
* @returns True if deleted, false if not found
*
* @example
* ```typescript
* await remove('old-storage');
* ```
*/
export declare function remove(name: string): Promise<boolean>;
/**
* Clear all data from a storage without deleting it.
*
* @param name - Name of the storage to clear
*
* @example
* ```typescript
* await clear('users');
* console.log('All user data cleared');
* ```
*/
export declare function clear(name: string): Promise<void>;
export declare const QuickStart: {
storage: typeof storage;
quick: typeof quick;
define: typeof define;
connect: typeof connect;
list: typeof list;
remove: typeof remove;
clear: typeof clear;
};
export default storage;
//# sourceMappingURL=quick-start.d.ts.map