Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions src/staged-query/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,21 @@ function isUpdater<T, U>(
*/
export class SingleSelection<T> extends Datum<T> {
/**
* Update fields of this selection with the given values
* Update fields of this selection with values computed from each document
*
* @param document Partial document with new values
* @param document Function building the partial document from the current one
* @returns Number of modified documents
*/
public update(document: DeepPartial<T> & object): Query<number>;
public update<U>(
document: (val: ValueProxy<T>) => U,
): ExtractType<U> extends DeepPartial<T> ? Query<number> : never;
/**
* Update fields of this selection with the given values
*
* @param document Partial document with new values
* @returns Number of modified documents
*/
public update(document: DeepPartial<T> & object): Query<number>;
public update<U>(
document: (DeepPartial<T> & object) | ((val: ValueProxy<T>) => U),
) {
Expand Down Expand Up @@ -90,15 +96,21 @@ export class SingleSelection<T> extends Datum<T> {
*/
export class Selection<T> extends Stream<T> {
/**
* Update fields of this selection with the given values
* Update fields of this selection with values computed from each document
*
* @param document Partial document with new values
* @param document Function building the partial document from the current one
* @returns Number of modified documents
*/
public update(document: DeepPartial<T> & object): Query<number>;
public update<U>(
document: (val: ValueProxy<T>) => U,
): ExtractType<U> extends DeepPartial<T> ? Query<number> : never;
/**
* Update fields of this selection with the given values
*
* @param document Partial document with new values
* @returns Number of modified documents
*/
public update(document: DeepPartial<T> & object): Query<number>;
public update<U>(
document: (DeepPartial<T> & object) | ((val: ValueProxy<T>) => U),
) {
Expand Down
7 changes: 7 additions & 0 deletions src/tests/helpers/same-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ export type Same<A, B> = [A] extends [B]
? true
: false
: false;

/** Resolves to `true` when `A` is `any`, else to `false`. `Same` cannot tell, as `any` is assignable both ways. */
type IsAny<A> = 0 extends 1 & A ? true : false;

/** Like `Same`, but `any` on either side resolves to `false`. */
export type Exact<A, B> =
IsAny<A> extends true ? false : IsAny<B> extends true ? false : Same<A, B>;
33 changes: 33 additions & 0 deletions src/tests/update_result.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { expect } from "chai";
import { Schema, type Query } from "..";
import type { Exact } from "./helpers/same-type";
import { Vehicle } from "./datasets/vehicles";

const schema = new Schema<{ vehicles: Vehicle }>("test-update-result", {
vehicles: Vehicle,
});
const vehicles = schema.instance("default").table("vehicles");

describe("Update result", () => {
it("Resolves an update call to a query of the modified count", () => {
const viaDocument = vehicles.get("a").update({ price: 1 });
const viaUpdater = vehicles.get("a").update(() => ({ price: 2 }));
const document: Exact<typeof viaDocument, Query<number>> = true;
const updater: Exact<typeof viaUpdater, Query<number>> = true;
expect(document && updater).to.equal(true);
});

it("Resolves the update of a table, read through its type, to the modified count", () => {
const table: Exact<
Awaited<ReturnType<ReturnType<typeof vehicles.update>["run"]>>,
number
> = true;
const single: Exact<
Awaited<
ReturnType<ReturnType<ReturnType<typeof vehicles.get>["update"]>["run"]>
>,
number
> = true;
expect(table && single).to.equal(true);
});
});
Loading