gitoriaLog in with ident

mpackdb

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit8788872587888725release 1.0.7caramboleyo87888725/src/Cursor.js

1.4 KB

  1. /**
  2. * Cursor for iterating over query results
  3. */
  4. export class Cursor {
  5. #db;
  6. #queryFn;
  7. #iterator;
  8. #options;
  9. /**
  10. * Creates a new Cursor
  11. * @param {MPackDB} db - The database instance
  12. * @param {function} [queryFn] - Query function to filter records
  13. * @param {Object} [options] - Query options
  14. */
  15. constructor(db, queryFn = () => true, options = {}) {
  16. this.#db = db;
  17. this.#queryFn = queryFn;
  18. this.#options = options;
  19. }
  20. /**
  21. * Makes the cursor awaitable (converts to array)
  22. * @param {function} resolve - Promise resolve function
  23. * @param {function} reject - Promise reject function
  24. */
  25. then(resolve, reject) {
  26. // This makes the cursor "awaitable"
  27. this.toArray().then(resolve, reject);
  28. }
  29. /**
  30. * Async iterator implementation
  31. * @yields {Object} Database records
  32. */
  33. async *[Symbol.asyncIterator]() {
  34. if (!this.#iterator) {
  35. this.#iterator = this.#db.recordGenerator(this.#queryFn, this.#options);
  36. }
  37. yield* this.#iterator;
  38. }
  39. /**
  40. * Gets the next record from the cursor
  41. * @returns {Promise<{value: Object, done: boolean}>} Iterator result
  42. */
  43. next() {
  44. if (!this.#iterator) {
  45. this.#iterator = this.#db.recordGenerator(this.#queryFn, this.#options);
  46. }
  47. return this.#iterator.next();
  48. }
  49. /**
  50. * Converts the cursor results to an array
  51. * @returns {Promise<Object[]>} Array of all matching records
  52. */
  53. async toArray() {
  54. const results = [];
  55. for await (const record of this) {
  56. results.push(record);
  57. }
  58. return results;
  59. }
  60. }
  61. export default Cursor;

Branches

Latest commits

  • 87888725release 1.0.7caramboleyo
  • 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