mpackdb
All repositories: gitoria
1.4 KB
/*** Cursor for iterating over query results*/export class Cursor {#db;#queryFn;#iterator;#options;/*** Creates a new Cursor* @param {MPackDB} db - The database instance* @param {function} [queryFn] - Query function to filter records* @param {Object} [options] - Query options*/constructor(db, queryFn = () => true, options = {}) {this.#db = db;this.#queryFn = queryFn;this.#options = options;}/*** Makes the cursor awaitable (converts to array)* @param {function} resolve - Promise resolve function* @param {function} reject - Promise reject function*/then(resolve, reject) {// This makes the cursor "awaitable"this.toArray().then(resolve, reject);}/*** Async iterator implementation* @yields {Object} Database records*/async *[Symbol.asyncIterator]() {if (!this.#iterator) {this.#iterator = this.#db.recordGenerator(this.#queryFn, this.#options);}yield* this.#iterator;}/*** Gets the next record from the cursor* @returns {Promise<{value: Object, done: boolean}>} Iterator result*/next() {if (!this.#iterator) {this.#iterator = this.#db.recordGenerator(this.#queryFn, this.#options);}return this.#iterator.next();}/*** Converts the cursor results to an array* @returns {Promise<Object[]>} Array of all matching records*/async toArray() {const results = [];for await (const record of this) {results.push(record);}return results;}}export default Cursor;
Branches
- mastermain branch
Latest commits
- c4cdb9b6node: import prefixes (Deno compat) + pre-existing index-state WIPcaramboleyo
- 0afb8f4bupdate now must be a callbackcaramboleyo
- cde73eb4release 1.0.6caramboleyo
- d01dda02add index hints, intersection, boundingBox; remove findByIndexcaramboleyo
- b8ffc1a0release 1.0.5caramboleyo
- d47876a1reimplemented lost features like indexed find and more testscaramboleyo
- 7f08da9afixed insert ignoring model definitioncaramboleyo
- 705774a9added flush before findcaramboleyo
- b4db6391initial commitcaramboleyo