convex-helpers
Advanced tools
Comparing version 0.1.31 to 0.1.32
@@ -17,2 +17,8 @@ /** | ||
* - Start a migration from an explicit cursor. | ||
* | ||
* Ideas for the future: | ||
* - Only run the migration on documents created before the migration started. | ||
* - Allow a migration to run on a time range. e.g. while a bug was live. | ||
* - Allow running a migration in reverse order, to prioritize newer documents. | ||
* - Allow scheduling multiple batches at once. Maybe partition by time. | ||
*/ | ||
@@ -34,2 +40,6 @@ import { DocumentByInfo, DocumentByName, FunctionReference, GenericDatabaseReader, GenericDataModel, GenericMutationCtx, MutationBuilder, NamedTableInfo, RegisteredMutation, Scheduler, TableNamesInDataModel } from "convex/server"; | ||
type MigrationMetadata = ObjectType<typeof migrationsFields>; | ||
type MigrationMetadataDoc<TableName extends string> = MigrationMetadata & { | ||
_id: GenericId<TableName>; | ||
_creationTime: number; | ||
}; | ||
export declare const migrationsTable: import("convex/server").TableDefinition<{ | ||
@@ -105,15 +115,3 @@ _creationTime: number; | ||
fn: string; | ||
}, Promise<{ | ||
workerId?: GenericId<"_scheduled_functions"> | undefined; | ||
latestEnd?: number | undefined; | ||
name: string; | ||
table: string; | ||
isDone: boolean; | ||
cursor: string | null; | ||
processed: number; | ||
latestStart: number; | ||
} & { | ||
_id: GenericId<MigrationTable>; | ||
_creationTime: number; | ||
}>>; | ||
}, Promise<MigrationMetadataDoc<MigrationTable>>>; | ||
/** | ||
@@ -176,2 +174,12 @@ * Start a migration from a server function via a function reference. | ||
}, fnRefs: FunctionReference<"mutation", "internal", MigrationArgs>[]): Promise<void>; | ||
export type MigrationStatus<TableName extends string> = (MigrationMetadataDoc<TableName> | { | ||
name: string; | ||
status: "not found"; | ||
workerId: undefined; | ||
isDone: false; | ||
}) & { | ||
workerStatus?: "pending" | "inProgress" | "success" | "failed" | "canceled" | undefined; | ||
batchSize?: any; | ||
next?: any; | ||
}; | ||
/** | ||
@@ -185,39 +193,9 @@ * Get the status of a migration or all migrations. | ||
*/ | ||
export declare function getStatus<DataModel extends GenericDataModel>(ctx: { | ||
export declare function getStatus<DataModel extends GenericDataModel, MigrationTable extends MigrationTableNames<DataModel>>(ctx: { | ||
db: GenericDatabaseReader<DataModel>; | ||
}, migrationTable: MigrationTableNames<DataModel>, migrations?: FunctionReference<"mutation", "internal", MigrationArgs>[]): Promise<({ | ||
workerId?: GenericId<"_scheduled_functions"> | undefined; | ||
latestEnd?: number | undefined; | ||
name: string; | ||
table: string; | ||
isDone: boolean; | ||
cursor: string | null; | ||
processed: number; | ||
latestStart: number; | ||
} | { | ||
name: string; | ||
status: string; | ||
workerId: undefined; | ||
isDone: boolean; | ||
} | { | ||
workerStatus: "pending" | "inProgress" | "success" | "failed" | "canceled" | undefined; | ||
batchSize: any; | ||
next: any; | ||
workerId?: GenericId<"_scheduled_functions"> | undefined; | ||
latestEnd?: number | undefined; | ||
name: string; | ||
table: string; | ||
isDone: boolean; | ||
cursor: string | null; | ||
processed: number; | ||
latestStart: number; | ||
} | { | ||
workerStatus: "pending" | "inProgress" | "success" | "failed" | "canceled" | undefined; | ||
batchSize: any; | ||
next: any; | ||
name: string; | ||
status: string; | ||
workerId: undefined; | ||
isDone: boolean; | ||
})[]>; | ||
}, { migrationTable, migrations, limit, }: { | ||
migrationTable: MigrationTable; | ||
migrations?: FunctionReference<"mutation", "internal", MigrationArgs>[]; | ||
limit?: number; | ||
}): Promise<MigrationStatus<MigrationTable>[]>; | ||
/** | ||
@@ -224,0 +202,0 @@ * Cancels a migration if it's in progress. |
@@ -17,2 +17,8 @@ /** | ||
* - Start a migration from an explicit cursor. | ||
* | ||
* Ideas for the future: | ||
* - Only run the migration on documents created before the migration started. | ||
* - Allow a migration to run on a time range. e.g. while a bug was live. | ||
* - Allow running a migration in reverse order, to prioritize newer documents. | ||
* - Allow scheduling multiple batches at once. Maybe partition by time. | ||
*/ | ||
@@ -362,3 +368,3 @@ import { defineTable, getFunctionName, makeFunctionReference, } from "convex/server"; | ||
*/ | ||
export async function getStatus(ctx, migrationTable, migrations) { | ||
export async function getStatus(ctx, { migrationTable, migrations, limit, }) { | ||
const docs = migrations | ||
@@ -377,4 +383,4 @@ ? await asyncMap(migrations, async (m) => (await ctx.db | ||
.order("desc") | ||
.take(10)); | ||
return Promise.all(docs.map(async (migration) => { | ||
.take(limit ?? 10)); | ||
return Promise.all(docs.reverse().map(async (migration) => { | ||
const { workerId, isDone } = migration; | ||
@@ -408,3 +414,3 @@ if (isDone) | ||
} | ||
if (!state.isDone) { | ||
if (state.isDone) { | ||
return state; | ||
@@ -411,0 +417,0 @@ } |
{ | ||
"name": "convex-helpers", | ||
"version": "0.1.31", | ||
"version": "0.1.32", | ||
"description": "A collection of useful code to complement the official convex package.", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -17,2 +17,8 @@ /** | ||
* - Start a migration from an explicit cursor. | ||
* | ||
* Ideas for the future: | ||
* - Only run the migration on documents created before the migration started. | ||
* - Allow a migration to run on a time range. e.g. while a bug was live. | ||
* - Allow running a migration in reverse order, to prioritize newer documents. | ||
* - Allow scheduling multiple batches at once. Maybe partition by time. | ||
*/ | ||
@@ -56,2 +62,6 @@ import { | ||
type MigrationMetadata = ObjectType<typeof migrationsFields>; | ||
type MigrationMetadataDoc<TableName extends string> = MigrationMetadata & { | ||
_id: GenericId<TableName>; | ||
_creationTime: number; | ||
}; | ||
export const migrationsTable = defineTable(migrationsFields).index("name", [ | ||
@@ -123,6 +133,3 @@ "name", | ||
const migrationTableName = opts?.migrationTable; | ||
type MigrationDoc = ObjectType<typeof migrationsFields> & { | ||
_id: GenericId<MigrationTable>; | ||
_creationTime: number; | ||
}; | ||
type MigrationDoc = MigrationMetadataDoc<MigrationTable>; | ||
const migrationRef = makeFunctionReference<"mutation", MigrationArgs>; | ||
@@ -443,2 +450,17 @@ /** | ||
export type MigrationStatus<TableName extends string> = ( | ||
| MigrationMetadataDoc<TableName> | ||
| { name: string; status: "not found"; workerId: undefined; isDone: false } | ||
) & { | ||
workerStatus?: | ||
| "pending" | ||
| "inProgress" | ||
| "success" | ||
| "failed" | ||
| "canceled" | ||
| undefined; | ||
batchSize?: any; | ||
next?: any; | ||
}; | ||
/** | ||
@@ -452,7 +474,17 @@ * Get the status of a migration or all migrations. | ||
*/ | ||
export async function getStatus<DataModel extends GenericDataModel>( | ||
export async function getStatus< | ||
DataModel extends GenericDataModel, | ||
MigrationTable extends MigrationTableNames<DataModel>, | ||
>( | ||
ctx: { db: GenericDatabaseReader<DataModel> }, | ||
migrationTable: MigrationTableNames<DataModel>, | ||
migrations?: FunctionReference<"mutation", "internal", MigrationArgs>[] | ||
) { | ||
{ | ||
migrationTable, | ||
migrations, | ||
limit, | ||
}: { | ||
migrationTable: MigrationTable; | ||
migrations?: FunctionReference<"mutation", "internal", MigrationArgs>[]; | ||
limit?: number; | ||
} | ||
): Promise<MigrationStatus<MigrationTable>[]> { | ||
const docs = migrations | ||
@@ -465,7 +497,7 @@ ? await asyncMap( | ||
.withIndex("name", (q) => q.eq("name", getFunctionName(m) as any)) | ||
.unique()) as MigrationMetadata | null) ?? { | ||
.unique()) as MigrationMetadataDoc<MigrationTable> | null) ?? { | ||
name: getFunctionName(m), | ||
status: "not found", | ||
status: "not found" as const, | ||
workerId: undefined, | ||
isDone: false, | ||
isDone: false as const, | ||
} | ||
@@ -476,6 +508,6 @@ ) | ||
.order("desc") | ||
.take(10)) as MigrationMetadata[]); | ||
.take(limit ?? 10)) as MigrationMetadataDoc<MigrationTable>[]); | ||
return Promise.all( | ||
docs.map(async (migration) => { | ||
docs.reverse().map(async (migration) => { | ||
const { workerId, isDone } = migration; | ||
@@ -516,3 +548,3 @@ if (isDone) return migration; | ||
} | ||
if (!state.isDone) { | ||
if (state.isDone) { | ||
return state; | ||
@@ -519,0 +551,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
489606
8906