From 28ca85566ad8ba27aefea2accc03210cec0eae3d Mon Sep 17 00:00:00 2001 From: Noa Date: Mon, 23 Feb 2026 17:30:03 -0600 Subject: [PATCH] Fix keynote-2 module (#4412) # Description of Changes Updates to v2 bindings # Expected complexity level and risk 1 # Testing - [x] Compiles --- templates/keynote-2/module_bindings/index.ts | 30 +++++------ .../{accounts_type.ts => types.ts} | 4 +- .../module_bindings/types/procedures.ts | 10 ++++ .../module_bindings/types/reducers.ts | 16 ++++++ templates/keynote-2/package.json | 2 +- .../.cargo/config.toml | 0 templates/keynote-2/rust_module/.gitignore | 18 +++++++ .../{spacetimedb => rust_module}/Cargo.toml | 2 +- .../{spacetimedb => rust_module}/src/lib.rs | 2 +- templates/keynote-2/spacetimedb/.gitignore | 21 ++------ templates/keynote-2/spacetimedb/package.json | 18 +++++++ templates/keynote-2/spacetimedb/src/index.ts | 53 +++++++++++++++++++ templates/keynote-2/spacetimedb/tsconfig.json | 24 +++++++++ 13 files changed, 160 insertions(+), 40 deletions(-) rename templates/keynote-2/module_bindings/{accounts_type.ts => types.ts} (79%) create mode 100644 templates/keynote-2/module_bindings/types/procedures.ts create mode 100644 templates/keynote-2/module_bindings/types/reducers.ts rename templates/keynote-2/{spacetimedb => rust_module}/.cargo/config.toml (100%) create mode 100644 templates/keynote-2/rust_module/.gitignore rename templates/keynote-2/{spacetimedb => rust_module}/Cargo.toml (73%) rename templates/keynote-2/{spacetimedb => rust_module}/src/lib.rs (97%) create mode 100644 templates/keynote-2/spacetimedb/package.json create mode 100644 templates/keynote-2/spacetimedb/src/index.ts create mode 100644 templates/keynote-2/spacetimedb/tsconfig.json diff --git a/templates/keynote-2/module_bindings/index.ts b/templates/keynote-2/module_bindings/index.ts index 68d54b556..ead1a67fc 100644 --- a/templates/keynote-2/module_bindings/index.ts +++ b/templates/keynote-2/module_bindings/index.ts @@ -1,7 +1,7 @@ // THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE // WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. -// This was generated using spacetimedb cli version 1.11.3 (commit 02449737ca3b29e7e39679fccbef541a50f32094). +// This was generated using spacetimedb cli version 2.0.0 (commit 044efda9054d37b62625b8a9424e12a77f8bcd55). /* eslint-disable */ /* tslint:disable */ @@ -12,6 +12,7 @@ import { TypeBuilder as __TypeBuilder, Uuid as __Uuid, convertToAccessorMap as __convertToAccessorMap, + makeQueryBuilder as __makeQueryBuilder, procedureSchema as __procedureSchema, procedures as __procedures, reducerSchema as __reducerSchema, @@ -25,33 +26,28 @@ import { type Event as __Event, type EventContextInterface as __EventContextInterface, type Infer as __Infer, + type QueryBuilder as __QueryBuilder, type ReducerEventContextInterface as __ReducerEventContextInterface, type RemoteModule as __RemoteModule, type SubscriptionEventContextInterface as __SubscriptionEventContextInterface, type SubscriptionHandleImpl as __SubscriptionHandleImpl, } from "spacetimedb"; -// Import and reexport all reducer arg types +// Import all reducer arg schemas import CreateAccountReducer from "./create_account_reducer"; -export { CreateAccountReducer }; import SeedReducer from "./seed_reducer"; -export { SeedReducer }; import TransferReducer from "./transfer_reducer"; -export { TransferReducer }; -// Import and reexport all procedure arg types +// Import all procedure arg schemas -// Import and reexport all table handle types +// Import all table schema definitions import AccountsRow from "./accounts_table"; -export { AccountsRow }; -// Import and reexport all types -import Accounts from "./accounts_type"; -export { Accounts }; +/** Type-only namespace exports for generated type groups. */ /** The schema information for all tables in this module. This is defined the same was as the tables would have been defined in the server. */ -const tablesSchema = __schema( - __table({ +const tablesSchema = __schema({ + accounts: __table({ name: 'accounts', indexes: [ { name: 'id', algorithm: 'btree', columns: [ @@ -62,7 +58,7 @@ const tablesSchema = __schema( { name: 'accounts_id_key', constraint: 'unique', columns: ['id'] }, ], }, AccountsRow), -); +}); /** The schema information for all reducers in this module. This is defined the same way as the reducers would have been defined in the server, except the body of the reducer is omitted in code generation. */ const reducersSchema = __reducers( @@ -78,7 +74,7 @@ const proceduresSchema = __procedures( /** The remote SpacetimeDB module schema, both runtime and type information. */ const REMOTE_MODULE = { versionInfo: { - cliVersion: "1.11.3" as const, + cliVersion: "2.0.0" as const, }, tables: tablesSchema.schemaType.tables, reducers: reducersSchema.reducersType.reducers, @@ -89,8 +85,8 @@ const REMOTE_MODULE = { typeof proceduresSchema >; -/** The tables available in this remote SpacetimeDB module. */ -export const tables = __convertToAccessorMap(tablesSchema.schemaType.tables); +/** The tables available in this remote SpacetimeDB module. Each table reference doubles as a query builder. */ +export const tables: __QueryBuilder = __makeQueryBuilder(tablesSchema.schemaType); /** The reducers available in this remote SpacetimeDB module. */ export const reducers = __convertToAccessorMap(reducersSchema.reducersType.reducers); diff --git a/templates/keynote-2/module_bindings/accounts_type.ts b/templates/keynote-2/module_bindings/types.ts similarity index 79% rename from templates/keynote-2/module_bindings/accounts_type.ts rename to templates/keynote-2/module_bindings/types.ts index ae4ac1e0b..1056de191 100644 --- a/templates/keynote-2/module_bindings/accounts_type.ts +++ b/templates/keynote-2/module_bindings/types.ts @@ -10,9 +10,9 @@ import { type Infer as __Infer, } from "spacetimedb"; -export default __t.object("Accounts", { +export const Accounts = __t.object("Accounts", { id: __t.u32(), balance: __t.i64(), }); - +export type Accounts = __Infer; diff --git a/templates/keynote-2/module_bindings/types/procedures.ts b/templates/keynote-2/module_bindings/types/procedures.ts new file mode 100644 index 000000000..d5ac825c9 --- /dev/null +++ b/templates/keynote-2/module_bindings/types/procedures.ts @@ -0,0 +1,10 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { type Infer as __Infer } from "spacetimedb"; + +// Import all procedure arg schemas + + diff --git a/templates/keynote-2/module_bindings/types/reducers.ts b/templates/keynote-2/module_bindings/types/reducers.ts new file mode 100644 index 000000000..a7faba02d --- /dev/null +++ b/templates/keynote-2/module_bindings/types/reducers.ts @@ -0,0 +1,16 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { type Infer as __Infer } from "spacetimedb"; + +// Import all reducer arg schemas +import CreateAccountReducer from "../create_account_reducer"; +import SeedReducer from "../seed_reducer"; +import TransferReducer from "../transfer_reducer"; + +export type CreateAccountParams = __Infer; +export type SeedParams = __Infer; +export type TransferParams = __Infer; + diff --git a/templates/keynote-2/package.json b/templates/keynote-2/package.json index 4f406439f..38ccff2f6 100644 --- a/templates/keynote-2/package.json +++ b/templates/keynote-2/package.json @@ -36,7 +36,7 @@ "drizzle-orm": "^0.44.7", "express": "^5.1.0", "hdr-histogram-js": "^3.0.1", - "spacetimedb": "1.11.4", + "spacetimedb": "^2.0", "sql.js": "^1.13.0", "undici": "^6.19.2" } diff --git a/templates/keynote-2/spacetimedb/.cargo/config.toml b/templates/keynote-2/rust_module/.cargo/config.toml similarity index 100% rename from templates/keynote-2/spacetimedb/.cargo/config.toml rename to templates/keynote-2/rust_module/.cargo/config.toml diff --git a/templates/keynote-2/rust_module/.gitignore b/templates/keynote-2/rust_module/.gitignore new file mode 100644 index 000000000..cb7692153 --- /dev/null +++ b/templates/keynote-2/rust_module/.gitignore @@ -0,0 +1,18 @@ +# Generated by Cargo +# will have compiled files and executables +debug/ +target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk + +# MSVC Windows builds of rustc generate these, which store debugging information +*.pdb + +# Spacetime ignore +/.spacetime +spacetime.local.json diff --git a/templates/keynote-2/spacetimedb/Cargo.toml b/templates/keynote-2/rust_module/Cargo.toml similarity index 73% rename from templates/keynote-2/spacetimedb/Cargo.toml rename to templates/keynote-2/rust_module/Cargo.toml index d54c7f016..c8f3d11a9 100644 --- a/templates/keynote-2/spacetimedb/Cargo.toml +++ b/templates/keynote-2/rust_module/Cargo.toml @@ -9,5 +9,5 @@ edition = "2021" crate-type = ["cdylib"] [dependencies] -spacetimedb = "=1.9.0" +spacetimedb = { path = "../../../crates/bindings" } log = "0.4" \ No newline at end of file diff --git a/templates/keynote-2/spacetimedb/src/lib.rs b/templates/keynote-2/rust_module/src/lib.rs similarity index 97% rename from templates/keynote-2/spacetimedb/src/lib.rs rename to templates/keynote-2/rust_module/src/lib.rs index 00fda765b..19c6ab70d 100644 --- a/templates/keynote-2/spacetimedb/src/lib.rs +++ b/templates/keynote-2/rust_module/src/lib.rs @@ -1,7 +1,7 @@ use log::info; use spacetimedb::{reducer, ReducerContext, Table}; -#[spacetimedb::table(name = accounts, public)] +#[spacetimedb::table(accessor = accounts, public)] #[derive(Debug, Clone)] pub struct Accounts { #[primary_key] diff --git a/templates/keynote-2/spacetimedb/.gitignore b/templates/keynote-2/spacetimedb/.gitignore index cb7692153..1fc24a6df 100644 --- a/templates/keynote-2/spacetimedb/.gitignore +++ b/templates/keynote-2/spacetimedb/.gitignore @@ -1,18 +1,3 @@ -# Generated by Cargo -# will have compiled files and executables -debug/ -target/ - -# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries -# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html -Cargo.lock - -# These are backup files generated by rustfmt -**/*.rs.bk - -# MSVC Windows builds of rustc generate these, which store debugging information -*.pdb - -# Spacetime ignore -/.spacetime -spacetime.local.json +node_modules/ +dist/ +.cursor/ diff --git a/templates/keynote-2/spacetimedb/package.json b/templates/keynote-2/spacetimedb/package.json new file mode 100644 index 000000000..3f8c4e675 --- /dev/null +++ b/templates/keynote-2/spacetimedb/package.json @@ -0,0 +1,18 @@ +{ + "name": "spacetime-module", + "version": "1.0.0", + "description": "", + "scripts": { + "build": "spacetime build", + "publish": "spacetime publish" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "spacetimedb": "workspace:*" + }, + "devDependencies": { + "typescript": "^5.9.3" + } +} diff --git a/templates/keynote-2/spacetimedb/src/index.ts b/templates/keynote-2/spacetimedb/src/index.ts new file mode 100644 index 000000000..023dfadcd --- /dev/null +++ b/templates/keynote-2/spacetimedb/src/index.ts @@ -0,0 +1,53 @@ +import { schema, table, t, SenderError } from 'spacetimedb/server'; + +const spacetimedb = schema({ + account: table( + { name: 'account' }, + { + id: t.u32().primaryKey().index('hash'), + balance: t.i64(), + }, + ), +}); +export default spacetimedb; + +export const seed = spacetimedb.reducer( + { n: t.u32(), balance: t.i64() }, + (ctx, { n, balance }) => { + const accounts = ctx.db.account; + + for (const row of accounts) { + accounts.delete(row); + } + + for (let id = 0; id < n; id++) { + accounts.insert({ id, balance }); + } + }, +); + +export const transfer = spacetimedb.reducer( + { from: t.u32(), to: t.u32(), amount: t.u32() }, + (ctx, { from, to, amount: amt }) => { + const accounts = ctx.db.account; + const byId = accounts.id; + + const fromRow = byId.find(from)!; + const toRow = byId.find(to)!; + + const amount = BigInt(amt); + if (fromRow.balance < amount) { + throw new SenderError('insufficient_funds'); + } + + byId.update({ + id: from, + balance: fromRow.balance - amount, + }); + + byId.update({ + id: to, + balance: toRow.balance + amount, + }); + }, +); diff --git a/templates/keynote-2/spacetimedb/tsconfig.json b/templates/keynote-2/spacetimedb/tsconfig.json new file mode 100644 index 000000000..77c6124cb --- /dev/null +++ b/templates/keynote-2/spacetimedb/tsconfig.json @@ -0,0 +1,24 @@ + +/* + * This tsconfig is used for TypeScript projects created with `spacetimedb init + * --lang typescript`. You can modify it as needed for your project, although + * some options are required by SpacetimeDB. + */ +{ + "compilerOptions": { + "strict": true, + "skipLibCheck": true, + "moduleResolution": "bundler", + "jsx": "react-jsx", + + /* The following options are required by SpacetimeDB + * and should not be modified + */ + "target": "ESNext", + "lib": ["ES2021", "dom"], + "module": "ESNext", + "isolatedModules": true, + "noEmit": true + }, + "include": ["./**/*"] +} \ No newline at end of file