convex-helpers 0.1.68 → 0.1.69
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.
- package/README.md +28 -26
- package/package.json +1 -1
- package/react/sessions.js +1 -1
- package/react/sessions.ts +2 -2
package/README.md
CHANGED
|
@@ -609,20 +609,17 @@ these validators help:
|
|
|
609
609
|
3. Add shorthand for a union of `literals`, a `nullable` field, a `deprecated`
|
|
610
610
|
field, and `brandedString`. To learn more about branded strings see
|
|
611
611
|
[this article](https://stack.convex.dev/using-branded-types-in-validators).
|
|
612
|
-
4.
|
|
613
|
-
|
|
612
|
+
4. Add a `doc(schema, "tableName")` helper to validate a document with system
|
|
613
|
+
fields included.
|
|
614
|
+
5. Add a `typedV(schema)` helper that is a `v` replacement that also has:
|
|
615
|
+
- `doc("tableName")` that works like `doc` above.
|
|
616
|
+
- `id("tableName")` that is typed to tables in your schema.
|
|
614
617
|
|
|
615
618
|
Example:
|
|
616
619
|
|
|
617
|
-
```
|
|
618
|
-
|
|
619
|
-
import {
|
|
620
|
-
literals,
|
|
621
|
-
partial,
|
|
622
|
-
deprecated,
|
|
623
|
-
brandedString,
|
|
624
|
-
} from "convex-helpers/validators";
|
|
625
|
-
import { omit, pick } from "convex-helpers";
|
|
620
|
+
```ts
|
|
621
|
+
// convex/schema.ts
|
|
622
|
+
import { literals, deprecated, brandedString } from "convex-helpers/validators";
|
|
626
623
|
import { Infer } from "convex/values";
|
|
627
624
|
|
|
628
625
|
// Define a validator that requires an Email string type.
|
|
@@ -630,40 +627,45 @@ export const emailValidator = brandedString("email");
|
|
|
630
627
|
// Define the Email type based on the branded string.
|
|
631
628
|
export type Email = Infer<typeof emailValidator>;
|
|
632
629
|
|
|
633
|
-
export const Account = Table("accounts", {
|
|
634
|
-
balance: nullable(v.bigint()),
|
|
635
|
-
status: literals("active", "inactive"),
|
|
636
|
-
email: emailValidator,
|
|
637
|
-
|
|
638
|
-
oldField: deprecated,
|
|
639
|
-
});
|
|
640
|
-
|
|
641
|
-
// convex/schema.ts
|
|
642
630
|
export default defineSchema({
|
|
643
|
-
accounts:
|
|
631
|
+
accounts: defineTable({
|
|
632
|
+
balance: nullable(v.bigint()),
|
|
633
|
+
status: literals("active", "inactive"),
|
|
634
|
+
email: emailValidator,
|
|
635
|
+
oldField: deprecated,
|
|
636
|
+
}).index("status", ["status"]),
|
|
644
637
|
//...
|
|
645
638
|
});
|
|
646
639
|
|
|
647
640
|
// some module
|
|
641
|
+
import { doc, typedV, partial } from "convex-helpers/validators";
|
|
642
|
+
import { omit, pick } from "convex-helpers";
|
|
643
|
+
import schema from "./schema";
|
|
644
|
+
|
|
645
|
+
// You could export this from your schema file, or define it where you need it.
|
|
646
|
+
const vv = typedV(schema);
|
|
647
|
+
|
|
648
648
|
export const replaceUser = internalMutation({
|
|
649
649
|
args: {
|
|
650
|
-
id:
|
|
650
|
+
id: vv.id("accounts"),
|
|
651
651
|
replace: object({
|
|
652
652
|
// You can provide the document with or without system fields.
|
|
653
|
-
...
|
|
654
|
-
...partial(
|
|
653
|
+
...schema.tables.accounts.validator.fields,
|
|
654
|
+
...partial(systemFields("accounts")),
|
|
655
655
|
}),
|
|
656
656
|
},
|
|
657
|
+
returns: doc(schema, "accounts"), // See below for vv.doc
|
|
657
658
|
handler: async (ctx, args) => {
|
|
658
659
|
await ctx.db.replace(args.id, args.replace);
|
|
660
|
+
return await ctx.db.get(args.id);
|
|
659
661
|
},
|
|
660
662
|
});
|
|
661
663
|
|
|
662
664
|
// A validator just for balance & email: { balance: v.union(...), email: ..}
|
|
663
|
-
const balanceAndEmail = pick(
|
|
665
|
+
const balanceAndEmail = pick(vv.doc("accounts").fields, ["balance", "email"]);
|
|
664
666
|
|
|
665
667
|
// A validator for all the fields except balance.
|
|
666
|
-
const accountWithoutBalance = omit(
|
|
668
|
+
const accountWithoutBalance = omit(vv.doc("accounts").fields, ["balance"]);
|
|
667
669
|
```
|
|
668
670
|
|
|
669
671
|
## Filter
|
package/package.json
CHANGED
package/react/sessions.js
CHANGED
|
@@ -102,7 +102,7 @@ export const SessionProvider = ({ useStorage, storageKey, idGenerator, ssrFriend
|
|
|
102
102
|
export function useSessionQuery(query, ...args) {
|
|
103
103
|
const [sessionId] = useSessionId();
|
|
104
104
|
const skip = args[0] === "skip" || !sessionId;
|
|
105
|
-
const originalArgs = args[0] === "skip" ? {} : args[0] ?? {};
|
|
105
|
+
const originalArgs = args[0] === "skip" ? {} : (args[0] ?? {});
|
|
106
106
|
const newArgs = skip ? "skip" : { ...originalArgs, sessionId };
|
|
107
107
|
return useQuery(query, ...[newArgs]);
|
|
108
108
|
}
|
package/react/sessions.ts
CHANGED
|
@@ -167,7 +167,7 @@ export function useSessionQuery<Query extends SessionFunction<"query">>(
|
|
|
167
167
|
): FunctionReturnType<Query> | undefined {
|
|
168
168
|
const [sessionId] = useSessionId();
|
|
169
169
|
const skip = args[0] === "skip" || !sessionId;
|
|
170
|
-
const originalArgs = args[0] === "skip" ? {} : args[0] ?? {};
|
|
170
|
+
const originalArgs = args[0] === "skip" ? {} : (args[0] ?? {});
|
|
171
171
|
|
|
172
172
|
const newArgs = skip ? "skip" : { ...originalArgs, sessionId };
|
|
173
173
|
|
|
@@ -256,7 +256,7 @@ export function useSessionId(): readonly [
|
|
|
256
256
|
if (!ctx.ssrFriendly && ctx.sessionId === undefined) {
|
|
257
257
|
throw new Error("Session ID invalid. Clear your storage?");
|
|
258
258
|
}
|
|
259
|
-
return [ctx.sessionId
|
|
259
|
+
return [ctx.sessionId, ctx.refreshSessionId, ctx.sessionIdPromise] as const;
|
|
260
260
|
}
|
|
261
261
|
|
|
262
262
|
/**
|