Skip to content
Open
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
2 changes: 2 additions & 0 deletions convex/_generated/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type * as functions from "../functions.js";
import type * as games from "../games.js";
import type * as lib_functions from "../lib/functions.js";
import type * as lib_middlewareUtils from "../lib/middlewareUtils.js";
import type * as lib_validators from "../lib/validators.js";
import type * as message from "../message.js";
import type * as model_cards from "../model/cards.js";
import type * as model_game from "../model/game.js";
Expand Down Expand Up @@ -49,6 +50,7 @@ declare const fullApi: ApiFromModules<{
games: typeof games;
"lib/functions": typeof lib_functions;
"lib/middlewareUtils": typeof lib_middlewareUtils;
"lib/validators": typeof lib_validators;
message: typeof message;
"model/cards": typeof model_cards;
"model/game": typeof model_game;
Expand Down
18 changes: 2 additions & 16 deletions convex/dealCards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,12 @@ import { PaginationResult, paginationOptsValidator } from 'convex/server'
import { v } from 'convex/values'
import { Doc } from './_generated/dataModel'
import { queryWithEnt } from './lib/functions'
import { betterV } from './lib/validators'

export default queryWithEnt({
args: { gameId: v.id('Games'), paginationOpts: paginationOptsValidator },
returns: v.object({
page: v.array(
v.object({
GameId: v.id('Games'),
blue: v.boolean(),
green: v.boolean(),
orange: v.boolean(),
proset: v.union(v.null(), v.id('Prosets')),
purple: v.boolean(),
rank: v.float64(),
red: v.boolean(),
selectedBy: v.union(v.null(), v.id('Players')),
yellow: v.boolean(),
_id: v.id('PlayingCards'),
_creationTime: v.number(),
})
),
page: v.array(betterV.doc('PlayingCards')),
isDone: v.boolean(),
continueCursor: v.string(),
splitCursor: v.optional(v.union(v.string(), v.null())),
Expand Down
27 changes: 10 additions & 17 deletions convex/games.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
mutationWithGame,
queryWithGame,
} from './lib/functions'
import { betterV } from './lib/validators'
import * as Games from './model/game'
import * as Players from './model/player'
import * as User from './model/user'
Expand Down Expand Up @@ -75,23 +76,15 @@ const playerFields = {
export const getInfo = queryWithGame({
args: {},
returns: v.object({
game: v.object({
name: v.string(),
selectingPlayer: v.union(v.null(), v.id('Players')),
selectionStartTime: v.union(v.null(), v.number()),
inProgress: v.boolean(),
isPublic: v.optional(v.boolean()),
// system fields
_id: v.id('Games'),
_creationTime: v.number(),
// fields added by ents
}),
currentPlayer: v.object({
...playerFields,
isGuest: v.boolean(),
showOnboarding: v.boolean(),
}),
otherPlayers: v.array(v.object(playerFields)),
game: betterV.doc('Games'),
currentPlayer: betterV.mergeObjects(
betterV.doc('Players'),
v.object({
isGuest: v.boolean(),
showOnboarding: v.boolean(),
})
),
otherPlayers: v.array(betterV.doc('Players')),
// record
playerToProsets: v.any(),
}),
Expand Down
37 changes: 37 additions & 0 deletions convex/lib/validators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Validator, v } from 'convex/values'
import { Doc, TableNames } from '../_generated/dataModel'
import schema from '../schema'

export const betterV = {
id: <T extends TableNames>(tableName: T) => {
return v.id(tableName)
},
doc: <T extends TableNames>(tableName: T): Validator<Doc<T>, false, any> => {
const validator = v.object(
(schema.tables[tableName] as any).documentSchema
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made normal Convex schema expose the validator as documentType. Ents have a private documentSchema, but I think we should ideally make it behave like documentType with vanilla Convex

) as Validator<any, any, any>
if (validator?.kind !== 'object') {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm realizing now that this isn't right -- it should account for a union of objects as well as any (and probably record, once that exists)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how does this pattern of being able to work directly with some validators if you know their .kind (curious why the ? is necessary here) but sometimes you just don't know? I guess that's not the pattern yet since ObjectValidator, StringValidator etc. is not exported yet

throw new Error(`Not an object validator`)
}
return v.object({
...validator.fields,
_id: v.id(tableName),
_creationTime: v.number(),
})
},
mergeObjects: <
O1 extends Record<string, any>,
O2 extends Record<string, any>
>(
validator1: Validator<O1, false, any>,
validator2: Validator<O2, false, any>
): Validator<Omit<O1, keyof O2> & O2, false, any> => {
if (validator1.kind !== 'object' || validator2.kind !== 'object') {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted something like a spread. Zod seems to call this .extend or .merge (https://zod.dev/?id=extend).

This also probably should allow for unions of objects, and maybe also any + record

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we expose all the validator types so you can use them here?

throw new Error('Not object validators')
}
return v.object({
...validator1.fields,
...validator2.fields,
}) as any
},
}
11 changes: 2 additions & 9 deletions convex/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { v } from 'convex/values'
import { Doc } from './_generated/dataModel'
import { internalMutation } from './_generated/server'
import { mutationWithGame, queryWithGame } from './lib/functions'
import { betterV } from './lib/validators'
import * as Message from './model/message'

export const send = mutationWithGame({
Expand All @@ -28,15 +29,7 @@ export const remove = internalMutation({

export const list = queryWithGame({
args: {},
returns: v.array(
v.object({
content: v.string(),
player: v.union(v.null(), v.string()),
GameId: v.id('Games'),
_id: v.id('Messages'),
_creationTime: v.number(),
})
),
returns: v.array(betterV.doc('Messages')),
handler: async (ctx): Promise<Array<Doc<'Messages'>>> => {
const messages = await ctx.game.edge('Messages')
return messages.filter(
Expand Down
17 changes: 2 additions & 15 deletions convex/users.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { v } from 'convex/values'
import { mutationWithEnt, queryWithEnt } from './lib/functions'
import { betterV } from './lib/validators'
import * as User from './model/user'

export const getOrCreate = mutationWithEnt({
Expand All @@ -13,21 +14,7 @@ export const getOrCreate = mutationWithEnt({

export const getOrNull = queryWithEnt({
args: { sessionId: v.string() },
returns: v.union(
v.null(),
v.object({
// system fields
_id: v.id('Users'),
_creationTime: v.number(),
// fields I copied from my schema
name: v.string(),
showOnboarding: v.boolean(),
isGuest: v.boolean(),
// field with a unique constraint
identifier: v.string(),
// ent fields that I had to kinda guess and check
})
),
returns: v.union(v.null(), betterV.doc('Users')),
handler: async (ctx, { sessionId }) => {
const userOrNull = await User.getOrNull(ctx, { sessionId })
if (userOrNull !== null) {
Expand Down
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"@mui/material": "^5.12.1",
"@types/react-transition-group": "^4.4.5",
"canvas-confetti": "^1.6.0",
"convex": "file:../convex-helpers/convex-1.12.1.tgz",
"convex": "file:../convex/npm-packages/convex/convex-1.12.1.tgz",
"convex-ents": "^0.7.6",
"convex-helpers": "file:../convex-helpers/packages/convex-helpers/convex-helpers-0.1.41.tgz",
"itertools": "^1.7.1",
Expand Down