@bunnykit/orm 0.1.16 → 0.1.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -160,7 +160,7 @@ export declare class Builder<T = Record<string, any>> {
160
160
  paginate(perPage?: number, page?: number): Promise<Paginator<T>>;
161
161
  chunk(count: number, callback: (items: T[]) => void | Promise<void>): Promise<void>;
162
162
  each(count: number, callback: (item: T) => void | Promise<void>): Promise<void>;
163
- cursor(keyset?: Record<string, any>): AsyncGenerator<T>;
163
+ cursor(chunkSize?: number): AsyncGenerator<T>;
164
164
  lazy(count?: number): AsyncGenerator<T>;
165
165
  insert(data: ModelAttributeInput<T> | ModelAttributeInput<T>[]): Promise<any>;
166
166
  insertGetId(data: ModelAttributeInput<T>, idColumn?: ModelColumn<T>): Promise<any>;
@@ -718,34 +718,40 @@ export class Builder {
718
718
  }
719
719
  });
720
720
  }
721
- async *cursor(keyset) {
721
+ async *cursor(chunkSize = 1000) {
722
722
  const model = this.model;
723
723
  const primaryKey = model ? model.primaryKey || "id" : "id";
724
724
  const orderColumn = this.orders[0]?.column || primaryKey;
725
725
  const orderDirection = this.orders[0]?.direction || "asc";
726
- const builder = this.clone();
727
- builder.orders = [{ column: orderColumn, direction: orderDirection }];
728
- builder.offsetValue = undefined;
729
- if (keyset) {
730
- const op = orderDirection === "asc" ? ">" : "<";
731
- builder.wheres.push({
732
- type: "basic",
733
- column: orderColumn,
734
- operator: op,
735
- value: keyset[orderColumn],
736
- boolean: "and",
737
- scope: undefined,
738
- });
739
- }
740
- const items = await builder.limit(1).get();
741
- if (items.length === 0)
742
- return;
743
- yield items[0];
744
- const nextKeyset = items[0] && typeof items[0] === "object"
745
- ? { [orderColumn]: items[0][orderColumn] }
746
- : undefined;
747
- if (nextKeyset) {
748
- yield* this.cursor(nextKeyset);
726
+ let lastValue = undefined;
727
+ while (true) {
728
+ const builder = this.clone();
729
+ builder.orders = [{ column: orderColumn, direction: orderDirection }];
730
+ builder.offsetValue = undefined;
731
+ builder.limitValue = chunkSize;
732
+ if (lastValue !== undefined) {
733
+ const op = orderDirection === "asc" ? ">" : "<";
734
+ builder.wheres.push({
735
+ type: "basic",
736
+ column: orderColumn,
737
+ operator: op,
738
+ value: lastValue,
739
+ boolean: "and",
740
+ scope: undefined,
741
+ });
742
+ }
743
+ const items = await builder.get();
744
+ if (items.length === 0)
745
+ break;
746
+ for (const item of items) {
747
+ yield item;
748
+ }
749
+ if (items.length < chunkSize)
750
+ break;
751
+ const lastItem = items[items.length - 1];
752
+ lastValue = lastItem && typeof lastItem === "object"
753
+ ? lastItem[orderColumn]
754
+ : undefined;
749
755
  }
750
756
  }
751
757
  async *lazy(count = 1000) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bunnykit/orm",
3
- "version": "0.1.16",
3
+ "version": "0.1.17",
4
4
  "description": "An Eloquent-inspired ORM for Bun's native SQL client supporting SQLite, MySQL, and PostgreSQL.",
5
5
  "license": "MIT",
6
6
  "packageManager": "bun@1.3.12",