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
|
@ -1,10 +1,11 @@
|
||||||
{
|
{
|
||||||
"name": "@andyburke/fsdb",
|
"name": "@andyburke/fsdb",
|
||||||
"version": "0.0.2",
|
"version": "0.1.0",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"exports": {
|
"exports": {
|
||||||
".": "./fsdb.ts",
|
".": "./fsdb.ts",
|
||||||
"./cli": "./cli.ts"
|
"./cli": "./cli.ts",
|
||||||
|
"./indexers": "./indexers.ts"
|
||||||
},
|
},
|
||||||
|
|
||||||
"tasks": {
|
"tasks": {
|
||||||
|
|
8
fsdb.ts
8
fsdb.ts
|
@ -24,6 +24,7 @@ export interface FSDB_INDEXER<T> {
|
||||||
lookup(value: string, options?: FSDB_SEARCH_OPTIONS): Promise<string[]>;
|
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>> {
|
export class FSDB_COLLECTION<T extends Record<string, any>> {
|
||||||
private config: FSDB_COLLECTION_CONFIG;
|
private config: FSDB_COLLECTION_CONFIG;
|
||||||
public INDEX: Record<string, FSDB_INDEXER<any>>;
|
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);
|
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 {
|
public get_organized_item_path(item: any, id_field?: string): string {
|
||||||
const id: string = item[id_field ?? this.config.id_field];
|
const id: string = item[id_field ?? this.config.id_field];
|
||||||
const path_elements: string[] = this.config.organize(id);
|
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;
|
return resolved_item_path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Get the "organized" path for the given id within the database. */
|
||||||
public get_organized_id_path(id: string): string {
|
public get_organized_id_path(id: string): string {
|
||||||
return this.get_organized_item_path({ id }, 'id');
|
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> {
|
async get(id: string): Promise<T | null> {
|
||||||
const id_path: string = this.get_organized_id_path(id);
|
const id_path: string = this.get_organized_id_path(id);
|
||||||
const item_exists: boolean = await fs.exists(id_path);
|
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);
|
return JSON.parse(content);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Create an item in the collection. */
|
||||||
async create(item: T): Promise<T> {
|
async create(item: T): Promise<T> {
|
||||||
const item_path: string = this.get_organized_item_path(item);
|
const item_path: string = this.get_organized_item_path(item);
|
||||||
const item_exists: boolean = await fs.exists(item_path);
|
const item_exists: boolean = await fs.exists(item_path);
|
||||||
|
@ -141,6 +146,7 @@ export class FSDB_COLLECTION<T extends Record<string, any>> {
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Update the given item in the collection, requiring the id to be stable. */
|
||||||
async update(item: T): Promise<T> {
|
async update(item: T): Promise<T> {
|
||||||
const item_path: string = this.get_organized_item_path(item);
|
const item_path: string = this.get_organized_item_path(item);
|
||||||
const item_exists: boolean = await fs.exists(item_path);
|
const item_exists: boolean = await fs.exists(item_path);
|
||||||
|
@ -156,6 +162,7 @@ export class FSDB_COLLECTION<T extends Record<string, any>> {
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Delete the given item from the collection. */
|
||||||
async delete(item: T): Promise<T | null> {
|
async delete(item: T): Promise<T | null> {
|
||||||
const item_path = this.get_organized_item_path(item);
|
const item_path = this.get_organized_item_path(item);
|
||||||
const item_exists = await fs.exists(item_path);
|
const item_exists = await fs.exists(item_path);
|
||||||
|
@ -192,6 +199,7 @@ export class FSDB_COLLECTION<T extends Record<string, any>> {
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Use indexes to search for matching items. */
|
||||||
async find(criteria: Record<string, any>, input_options?: FSDB_SEARCH_OPTIONS): Promise<T[]> {
|
async find(criteria: Record<string, any>, input_options?: FSDB_SEARCH_OPTIONS): Promise<T[]> {
|
||||||
if (Deno.env.get('FSDB_PERF')) performance.mark('fsdb_find_begin');
|
if (Deno.env.get('FSDB_PERF')) performance.mark('fsdb_find_begin');
|
||||||
|
|
||||||
|
|
3
indexers.ts
Normal file
3
indexers.ts
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
import { FSDB_INDEXER_SYMLINKS } from './indexers/symlinks.ts';
|
||||||
|
|
||||||
|
export default FSDB_INDEXER_SYMLINKS;
|
Loading…
Add table
Add a link
Reference in a new issue