From 2d4ef89d0ef3233a61107369a951aa630540ab3c Mon Sep 17 00:00:00 2001 From: Kacper Wojciechowski <39823706+jog1t@users.noreply.github.com> Date: Thu, 14 Aug 2025 22:35:28 +0200 Subject: [PATCH] feat(examples): add sqlite example --- examples/sqlite/README.md | 31 +++++++++++++++++++++++ examples/sqlite/package.json | 20 +++++++++++++++ examples/sqlite/src/registry.ts | 43 ++++++++++++++++++++++++++++++++ examples/sqlite/src/server.ts | 3 +++ examples/sqlite/tsconfig.json | 44 +++++++++++++++++++++++++++++++++ examples/sqlite/turbo.json | 4 +++ pnpm-lock.yaml | 19 ++++++++++++++ 7 files changed, 164 insertions(+) create mode 100644 examples/sqlite/README.md create mode 100644 examples/sqlite/package.json create mode 100644 examples/sqlite/src/registry.ts create mode 100644 examples/sqlite/src/server.ts create mode 100644 examples/sqlite/tsconfig.json create mode 100644 examples/sqlite/turbo.json diff --git a/examples/sqlite/README.md b/examples/sqlite/README.md new file mode 100644 index 000000000..2b96d69b7 --- /dev/null +++ b/examples/sqlite/README.md @@ -0,0 +1,31 @@ +# SQLite Integration for RivetKit + +Example project demonstrating SQLite integration with [RivetKit](https://rivetkit.org). + +[Learn More →](https://github.com/rivet-gg/rivetkit) + +[Discord](https://rivet.gg/discord) — [Documentation](https://rivetkit.org) — [Issues](https://github.com/rivet-gg/rivetkit/issues) + +## Getting Started + +### Prerequisites + +- Node.js + +### Installation + +```sh +git clone https://github.com/rivet-gg/rivetkit +cd rivetkit/examples/sqlite +pnpm install +``` + +### Development +```sh +pnpm run dev +``` +Open your browser to https://studio.rivet.gg/ to see your RivetKit server. + +## License + +Apache 2.0 diff --git a/examples/sqlite/package.json b/examples/sqlite/package.json new file mode 100644 index 000000000..27b9b214a --- /dev/null +++ b/examples/sqlite/package.json @@ -0,0 +1,20 @@ +{ + "name": "example-sqlite", + "version": "0.9.9", + "private": true, + "type": "module", + "scripts": { + "dev": "tsx --watch src/server.ts", + "check-types": "tsc --noEmit" + }, + "devDependencies": { + "@types/node": "^22.13.9", + "tsx": "^3.12.7", + "typescript": "^5.5.2" + }, + "dependencies": { + "@rivetkit/db": "workspace:*", + "@rivetkit/actor": "workspace:*" + }, + "stableVersion": "0.8.0" +} diff --git a/examples/sqlite/src/registry.ts b/examples/sqlite/src/registry.ts new file mode 100644 index 000000000..2413aaa5d --- /dev/null +++ b/examples/sqlite/src/registry.ts @@ -0,0 +1,43 @@ +import { actor, setup } from "@rivetkit/actor"; +import { db } from "@rivetkit/db"; + +export const chat = actor({ + onAuth: () => {}, + db: db({ + onMigrate: async (c) => { + await c + .prepare(`CREATE TABLE IF NOT EXISTS messages ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + sender TEXT NOT NULL, + text TEXT NOT NULL, + timestamp INTEGER NOT NULL + )`) + .run(); + }, + }), + actions: { + // Callable functions from clients: https://rivet.gg/docs/actors/actions + sendMessage: async (c, sender: string, text: string) => { + const message = { sender, text, timestamp: Date.now() }; + // State changes are automatically persisted + await c.db + .prepare( + `INSERT INTO messages (sender, text, timestamp) VALUES (?, ?, ?)`, + [sender, text, message.timestamp], + ) + .run(); + // Send events to all connected clients: https://rivet.gg/docs/actors/events + c.broadcast("newMessage", message); + return message; + }, + + getHistory: (c) => + c.db + .prepare(`SELECT * FROM messages ORDER BY timestamp DESC LIMIT 100`) + .all(), + }, +}); + +export const registry = setup({ + use: { chat }, +}); diff --git a/examples/sqlite/src/server.ts b/examples/sqlite/src/server.ts new file mode 100644 index 000000000..11163905a --- /dev/null +++ b/examples/sqlite/src/server.ts @@ -0,0 +1,3 @@ +import { registry } from "./registry"; + +registry.runServer(); diff --git a/examples/sqlite/tsconfig.json b/examples/sqlite/tsconfig.json new file mode 100644 index 000000000..4f308bb30 --- /dev/null +++ b/examples/sqlite/tsconfig.json @@ -0,0 +1,44 @@ +{ + "compilerOptions": { + /* Visit https://aka.ms/tsconfig.json to read more about this file */ + + /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ + "target": "esnext", + /* Specify a set of bundled library declaration files that describe the target runtime environment. */ + "lib": ["esnext"], + /* Specify what JSX code is generated. */ + "jsx": "react-jsx", + "allowArbitraryExtensions": true, + + /* Specify what module code is generated. */ + "module": "esnext", + /* Specify how TypeScript looks up a file from a given module specifier. */ + "moduleResolution": "bundler", + /* Specify type package names to be included without being referenced in a source file. */ + "types": ["node"], + /* Enable importing .json files */ + "resolveJsonModule": true, + + /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */ + "allowJs": true, + /* Enable error reporting in type-checked JavaScript files. */ + "checkJs": false, + + /* Disable emitting files from a compilation. */ + "noEmit": true, + + /* Ensure that each file can be safely transpiled without relying on other imports. */ + "isolatedModules": true, + /* Allow 'import x from y' when a module doesn't have a default export. */ + "allowSyntheticDefaultImports": true, + /* Ensure that casing is correct in imports. */ + "forceConsistentCasingInFileNames": true, + + /* Enable all strict type-checking options. */ + "strict": true, + + /* Skip type checking all .d.ts files. */ + "skipLibCheck": true + }, + "include": ["src/**/*"] +} diff --git a/examples/sqlite/turbo.json b/examples/sqlite/turbo.json new file mode 100644 index 000000000..95960709b --- /dev/null +++ b/examples/sqlite/turbo.json @@ -0,0 +1,4 @@ +{ + "$schema": "https://turbo.build/schema.json", + "extends": ["//"] +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d497338fd..850f2b4ca 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -820,6 +820,25 @@ importers: specifier: ^5.7.3 version: 5.8.3 + examples/sqlite: + dependencies: + '@rivetkit/actor': + specifier: workspace:* + version: link:../../packages/actor + '@rivetkit/db': + specifier: workspace:* + version: link:../../packages/db + devDependencies: + '@types/node': + specifier: ^22.13.9 + version: 22.15.32 + tsx: + specifier: ^3.12.7 + version: 3.14.0 + typescript: + specifier: ^5.5.2 + version: 5.8.3 + examples/starter: dependencies: '@rivetkit/actor':