You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Databases and ORMs generally allow fields to be defined with a default value. Doing this now in ponder is a little janky. For example, I have a schema where I want to keep track of points users accrue (all users start at 0).
Current Implementation
In order to implement this right now, I need to either explicitly set points to 0 whenever I create a new user or set points to be an optional value. The former would be extremely tedious as I create new accounts in many different watchers, and its much cleaner to just do
await Account.upsert({
id: event.args.from,
});
then explicitly set points to 0 across all watchers that create accounts but have nothing to do with points. However the latter requires consumers of the indexer to explicitly cast null to 0 on the points field of accounts. Currently, my has to do this.
Great idea, this would be a nice quality of life improvement. I propose a slightly different API:
import{createSchema}from"@ponder/core";exportdefaultcreateSchema((p)=>({Account: p.createTable({id: p.hex(),tokens: p.many("TokenBalance.ownerId"),points: p.bigint().default(0n),// Set a default value of 0 for points// ...}),}));
which would align better with the existing .optional() and .references(...) builder pattern we have.
Implementation-wise, we should be able to use a SQL DEFAULT for both SQLite and Postgres. At a type level, adding .default(...) would make the column optional on insert, but required on select.
My initial thought to just supply it as a value to the type function made more sense to me initially, but with the optional() and .references() builder pattern I'd agree that a .default(x) would fit better within the system.
Databases and ORMs generally allow fields to be defined with a default value. Doing this now in ponder is a little janky. For example, I have a schema where I want to keep track of points users accrue (all users start at 0).
Current Implementation
In order to implement this right now, I need to either explicitly set
points
to 0 whenever I create a new user or set points to be an optional value. The former would be extremely tedious as I create new accounts in many different watchers, and its much cleaner to just dothen explicitly set points to 0 across all watchers that create accounts but have nothing to do with points. However the latter requires consumers of the indexer to explicitly cast null to 0 on the points field of accounts. Currently, my has to do this.
Usage Example
Here's an example of what this might look like in
ponder.schema.ts
with the requested feature.Then I don't need to change anything about my indexer code except increment the points counter.
The text was updated successfully, but these errors were encountered: