UNPKG

@aurios/jason

Version:

A simple, lightweight, and embeddable JSON document database built on Bun.

47 lines (46 loc) 2.11 kB
import Collection from "../data/collection.js"; import type { CollectionOptions, Document, JasonDBOptions } from "../types/index.js"; export default class JasonDB<T> { #private; /** * Creates a new JasonDB instance with the given configuration. * @param options - Either a string representing the database name (will be created in current working directory), * or a JasonDBOptions configuration object * @example * // Simple usage * const db = new JasonDB('my-database'); * * // Advanced usage * const db = new JasonDB({ * basename: 'my-database', * path: './custom-location' * }); */ constructor(options: string | JasonDBOptions); /** * Retrieves or creates a collection in the database. * * If a collection with the given name does not exist, it initializes a new collection * with the provided options and stores it. If the collection already exists, it returns the existing one. * * @param name - The name of the collection. * @param options - Optional settings for the collection. * @param options.initialData - An array of initial data to populate the collection. * @param options.schema - A validation function for the collection's documents. * @param options.concurrencyStrategy - The concurrency strategy to use for the collection. * @param options.cacheTimeout - The cache timeout in milliseconds. * @param options.generateMetadata - Whether to generate metadata for the collection. * @returns The collection instance associated with the given name. */ collection<K extends keyof T>(name: K, options?: CollectionOptions<Document<T, K>>): Collection<T, K>; /** * Lists all collections in the database. * * Reads the base directory and returns the names of all subdirectories, * which represent the collections. * * @returns A promise that resolves to an array of collection names. * If an error occurs, it resolves to an empty array. */ listCollections(): Promise<(keyof T)[]>; }