feature: export indexers
docs: very beginnings of documentation
This commit is contained in:
parent
ce024ba87a
commit
d9dd449927
3 changed files with 14 additions and 2 deletions
8
fsdb.ts
8
fsdb.ts
|
@ -24,6 +24,7 @@ export interface FSDB_INDEXER<T> {
|
|||
lookup(value: string, options?: FSDB_SEARCH_OPTIONS): Promise<string[]>;
|
||||
}
|
||||
|
||||
/** Represents a collection of like items within the database on disk. */
|
||||
export class FSDB_COLLECTION<T extends Record<string, any>> {
|
||||
private config: FSDB_COLLECTION_CONFIG;
|
||||
public INDEX: Record<string, FSDB_INDEXER<any>>;
|
||||
|
@ -84,6 +85,7 @@ export class FSDB_COLLECTION<T extends Record<string, any>> {
|
|||
Deno.writeTextFileSync(collection_info_file_path, collection_info_json);
|
||||
}
|
||||
|
||||
/** Get the "organized" path for the given item within the database. */
|
||||
public get_organized_item_path(item: any, id_field?: string): string {
|
||||
const id: string = item[id_field ?? this.config.id_field];
|
||||
const path_elements: string[] = this.config.organize(id);
|
||||
|
@ -91,6 +93,7 @@ export class FSDB_COLLECTION<T extends Record<string, any>> {
|
|||
return resolved_item_path;
|
||||
}
|
||||
|
||||
/** Get the "organized" path for the given id within the database. */
|
||||
public get_organized_id_path(id: string): string {
|
||||
return this.get_organized_item_path({ id }, 'id');
|
||||
}
|
||||
|
@ -114,6 +117,7 @@ export class FSDB_COLLECTION<T extends Record<string, any>> {
|
|||
}
|
||||
}
|
||||
|
||||
/** Get an item from the collection given its id. */
|
||||
async get(id: string): Promise<T | null> {
|
||||
const id_path: string = this.get_organized_id_path(id);
|
||||
const item_exists: boolean = await fs.exists(id_path);
|
||||
|
@ -126,6 +130,7 @@ export class FSDB_COLLECTION<T extends Record<string, any>> {
|
|||
return JSON.parse(content);
|
||||
}
|
||||
|
||||
/** Create an item in the collection. */
|
||||
async create(item: T): Promise<T> {
|
||||
const item_path: string = this.get_organized_item_path(item);
|
||||
const item_exists: boolean = await fs.exists(item_path);
|
||||
|
@ -141,6 +146,7 @@ export class FSDB_COLLECTION<T extends Record<string, any>> {
|
|||
return item;
|
||||
}
|
||||
|
||||
/** Update the given item in the collection, requiring the id to be stable. */
|
||||
async update(item: T): Promise<T> {
|
||||
const item_path: string = this.get_organized_item_path(item);
|
||||
const item_exists: boolean = await fs.exists(item_path);
|
||||
|
@ -156,6 +162,7 @@ export class FSDB_COLLECTION<T extends Record<string, any>> {
|
|||
return item;
|
||||
}
|
||||
|
||||
/** Delete the given item from the collection. */
|
||||
async delete(item: T): Promise<T | null> {
|
||||
const item_path = this.get_organized_item_path(item);
|
||||
const item_exists = await fs.exists(item_path);
|
||||
|
@ -192,6 +199,7 @@ export class FSDB_COLLECTION<T extends Record<string, any>> {
|
|||
return item;
|
||||
}
|
||||
|
||||
/** Use indexes to search for matching items. */
|
||||
async find(criteria: Record<string, any>, input_options?: FSDB_SEARCH_OPTIONS): Promise<T[]> {
|
||||
if (Deno.env.get('FSDB_PERF')) performance.mark('fsdb_find_begin');
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue