convex-helpers
Advanced tools
Comparing version 0.1.64-alpha.1 to 0.1.64-alpha.2
{ | ||
"name": "convex-helpers", | ||
"version": "0.1.64-alpha.1", | ||
"version": "0.1.64-alpha.2", | ||
"description": "A collection of useful code to complement the official convex package.", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -69,2 +69,5 @@ import { Value } from "convex/values"; | ||
}, request: PageRequest<DataModel, T>): Promise<PageResponse<DataModel, T>>; | ||
export declare function streamQuery<DataModel extends GenericDataModel, T extends TableNamesInDataModel<DataModel>>(ctx: { | ||
db: GenericDatabaseReader<DataModel>; | ||
}, request: Omit<PageRequest<DataModel, T>, "targetMaxRows" | "absoluteMaxRows">): AsyncGenerator<[DocumentByName<DataModel, T>, IndexKey]>; | ||
/** | ||
@@ -174,3 +177,3 @@ * Simpified version of `getPage` that you can use for one-off queries that | ||
} | ||
declare class PaginatorIndexRange { | ||
export declare class PaginatorIndexRange { | ||
indexFields: string[]; | ||
@@ -191,3 +194,2 @@ private hasSuffix; | ||
} | ||
export {}; | ||
//# sourceMappingURL=pagination.d.ts.map |
@@ -10,2 +10,28 @@ import { convexToJson, jsonToConvex } from "convex/values"; | ||
export async function getPage(ctx, request) { | ||
const absoluteMaxRows = request.absoluteMaxRows ?? Infinity; | ||
const targetMaxRows = request.targetMaxRows ?? DEFAULT_TARGET_MAX_ROWS; | ||
const absoluteLimit = request.endIndexKey | ||
? absoluteMaxRows | ||
: Math.min(absoluteMaxRows, targetMaxRows); | ||
const page = []; | ||
const indexKeys = []; | ||
const stream = streamQuery(ctx, request); | ||
for await (const [doc, indexKey] of stream) { | ||
if (page.length >= absoluteLimit) { | ||
return { | ||
page, | ||
hasMore: true, | ||
indexKeys, | ||
}; | ||
} | ||
page.push(doc); | ||
indexKeys.push(indexKey); | ||
} | ||
return { | ||
page, | ||
hasMore: false, | ||
indexKeys, | ||
}; | ||
} | ||
export async function* streamQuery(ctx, request) { | ||
const index = request.index ?? "by_creation_time"; | ||
@@ -25,9 +51,2 @@ const indexFields = getIndexFields(request); | ||
const split = splitRange(indexFields, startIndexKey, endIndexKey, startBoundType, endBoundType); | ||
const absoluteMaxRows = request.absoluteMaxRows ?? Infinity; | ||
const targetMaxRows = request.targetMaxRows ?? DEFAULT_TARGET_MAX_ROWS; | ||
const absoluteLimit = request.endIndexKey | ||
? absoluteMaxRows | ||
: Math.min(absoluteMaxRows, targetMaxRows); | ||
const page = []; | ||
const indexKeys = []; | ||
for (const range of split) { | ||
@@ -39,18 +58,5 @@ const query = ctx.db | ||
for await (const doc of query) { | ||
if (page.length >= absoluteLimit) { | ||
return { | ||
page, | ||
hasMore: true, | ||
indexKeys, | ||
}; | ||
} | ||
page.push(doc); | ||
indexKeys.push(getIndexKey(doc, indexFields)); | ||
yield [doc, getIndexKey(doc, indexFields)]; | ||
} | ||
} | ||
return { | ||
page, | ||
hasMore: false, | ||
indexKeys, | ||
}; | ||
} | ||
@@ -428,3 +434,3 @@ // | ||
} | ||
class PaginatorIndexRange { | ||
export class PaginatorIndexRange { | ||
indexFields; | ||
@@ -431,0 +437,0 @@ hasSuffix = false; |
@@ -101,2 +101,35 @@ import { Value, convexToJson, jsonToConvex } from "convex/values"; | ||
): Promise<PageResponse<DataModel, T>> { | ||
const absoluteMaxRows = request.absoluteMaxRows ?? Infinity; | ||
const targetMaxRows = request.targetMaxRows ?? DEFAULT_TARGET_MAX_ROWS; | ||
const absoluteLimit = request.endIndexKey | ||
? absoluteMaxRows | ||
: Math.min(absoluteMaxRows, targetMaxRows); | ||
const page: DocumentByName<DataModel, T>[] = []; | ||
const indexKeys: IndexKey[] = []; | ||
const stream = streamQuery(ctx, request); | ||
for await (const [doc, indexKey] of stream) { | ||
if (page.length >= absoluteLimit) { | ||
return { | ||
page, | ||
hasMore: true, | ||
indexKeys, | ||
}; | ||
} | ||
page.push(doc); | ||
indexKeys.push(indexKey); | ||
} | ||
return { | ||
page, | ||
hasMore: false, | ||
indexKeys, | ||
}; | ||
} | ||
export async function* streamQuery< | ||
DataModel extends GenericDataModel, | ||
T extends TableNamesInDataModel<DataModel>, | ||
>( | ||
ctx: { db: GenericDatabaseReader<DataModel> }, | ||
request: Omit<PageRequest<DataModel, T>, "targetMaxRows" | "absoluteMaxRows">, | ||
): AsyncGenerator<[DocumentByName<DataModel, T>, IndexKey]> { | ||
const index = request.index ?? "by_creation_time"; | ||
@@ -126,9 +159,2 @@ const indexFields = getIndexFields(request); | ||
); | ||
const absoluteMaxRows = request.absoluteMaxRows ?? Infinity; | ||
const targetMaxRows = request.targetMaxRows ?? DEFAULT_TARGET_MAX_ROWS; | ||
const absoluteLimit = request.endIndexKey | ||
? absoluteMaxRows | ||
: Math.min(absoluteMaxRows, targetMaxRows); | ||
const page: DocumentByName<DataModel, T>[] = []; | ||
const indexKeys: IndexKey[] = []; | ||
for (const range of split) { | ||
@@ -140,18 +166,5 @@ const query = ctx.db | ||
for await (const doc of query) { | ||
if (page.length >= absoluteLimit) { | ||
return { | ||
page, | ||
hasMore: true, | ||
indexKeys, | ||
}; | ||
} | ||
page.push(doc); | ||
indexKeys.push(getIndexKey(doc, indexFields)); | ||
yield [doc, getIndexKey(doc, indexFields)]; | ||
} | ||
} | ||
return { | ||
page, | ||
hasMore: false, | ||
indexKeys, | ||
}; | ||
} | ||
@@ -592,3 +605,3 @@ | ||
class PaginatorIndexRange { | ||
export class PaginatorIndexRange { | ||
private hasSuffix = false; | ||
@@ -595,0 +608,0 @@ public lowerBoundIndexKey: IndexKey | undefined = undefined; |
Sorry, the diff of this file is not supported yet
848242
19516