Skip to content

[Feature] deleteWhere and updateWhere #358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
daveycodez opened this issue May 20, 2025 · 0 comments
Open

[Feature] deleteWhere and updateWhere #358

daveycodez opened this issue May 20, 2025 · 0 comments

Comments

@daveycodez
Copy link

daveycodez commented May 20, 2025

It would be amazing to be able to perform deletes and updates based on where conditions instead of just by ID's.

Here are 2 functions I created for the HttpClient, it would be really powerful to have this available on both the Triplit client and the HttpClient.

import type {
    CollectionNameFromModels,
    HttpClient,
    Models,
    UpdatePayload,
    WhereFilter
} from "@triplit/client"

export async function deleteWhere<M extends Models<M> = Models>(
    httpClient: HttpClient<M>,
    collectionName: CollectionNameFromModels<M>,
    where: WhereFilter<M, CollectionNameFromModels<M>>[],
    batchSize = 500
) {
    let total = 0

    while (true) {
        const entities = (await httpClient.fetch({
            collectionName,
            where,
            limit: batchSize
        })) as unknown as { id: string }[]

        await Promise.all(entities.map((entity) => httpClient.delete(collectionName, entity.id)))

        total += entities.length

        if (entities.length < batchSize) break
    }

    return total
}

export async function updateWhere<M extends Models<M> = Models>(
    httpClient: HttpClient<M>,
    collectionName: CollectionNameFromModels<M>,
    where: WhereFilter<M, CollectionNameFromModels<M>>[],
    update: UpdatePayload<M, CollectionNameFromModels<M>>,
    batchSize = 500
) {
    let total = 0

    while (true) {
        const entities = (await httpClient.fetch({
            collectionName,
            where,
            limit: batchSize
        })) as unknown as { id: string }[]

        await Promise.all(
            entities.map((entity) => httpClient.update(collectionName, entity.id, update))
        )

        total += entities.length

        if (entities.length < batchSize) break
    }

    return total
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant