feature: export indexers

docs: very beginnings of documentation
This commit is contained in:
Andy Burke 2025-06-13 21:00:13 -07:00
parent ce024ba87a
commit d9dd449927
3 changed files with 14 additions and 2 deletions

View file

@ -1,10 +1,11 @@
{
"name": "@andyburke/fsdb",
"version": "0.0.2",
"version": "0.1.0",
"license": "MIT",
"exports": {
".": "./fsdb.ts",
"./cli": "./cli.ts"
"./cli": "./cli.ts",
"./indexers": "./indexers.ts"
},
"tasks": {

View file

@ -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');

3
indexers.ts Normal file
View file

@ -0,0 +1,3 @@
import { FSDB_INDEXER_SYMLINKS } from './indexers/symlinks.ts';
export default FSDB_INDEXER_SYMLINKS;