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
31 changes: 31 additions & 0 deletions examples/sqlite/README.md
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions examples/sqlite/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
43 changes: 43 additions & 0 deletions examples/sqlite/src/registry.ts
Original file line number Diff line number Diff line change
@@ -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(),
Comment on lines +34 to +37
Copy link

Choose a reason for hiding this comment

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

Potential data exposure issue: The getHistory action returns all columns from the messages table without any filtering or sanitization. If the database schema is later extended to include sensitive fields (like IP addresses, user IDs, or other metadata), this query would expose all that data to any client that calls this action. The query should explicitly specify which columns to return rather than using SELECT *.

Suggested change
getHistory: (c) =>
c.db
.prepare(`SELECT * FROM messages ORDER BY timestamp DESC LIMIT 100`)
.all(),
getHistory: (c) =>
c.db
.prepare(`SELECT id, text, username, timestamp FROM messages ORDER BY timestamp DESC LIMIT 100`)
.all(),

Spotted by Diamond

Fix in Graphite


Is this helpful? React 👍 or 👎 to let us know.

},
});

export const registry = setup({
use: { chat },
});
3 changes: 3 additions & 0 deletions examples/sqlite/src/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { registry } from "./registry";

registry.runServer();
44 changes: 44 additions & 0 deletions examples/sqlite/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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/**/*"]
}
4 changes: 4 additions & 0 deletions examples/sqlite/turbo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "https://turbo.build/schema.json",
"extends": ["//"]
}
19 changes: 19 additions & 0 deletions pnpm-lock.yaml

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

Loading