Skip to content

Commit 2d4ef89

Browse files
committed
feat(examples): add sqlite example
1 parent 1ad7f70 commit 2d4ef89

File tree

7 files changed

+164
-0
lines changed

7 files changed

+164
-0
lines changed

examples/sqlite/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# SQLite Integration for RivetKit
2+
3+
Example project demonstrating SQLite integration with [RivetKit](https://rivetkit.org).
4+
5+
[Learn More →](https://github.com/rivet-gg/rivetkit)
6+
7+
[Discord](https://rivet.gg/discord)[Documentation](https://rivetkit.org)[Issues](https://github.com/rivet-gg/rivetkit/issues)
8+
9+
## Getting Started
10+
11+
### Prerequisites
12+
13+
- Node.js
14+
15+
### Installation
16+
17+
```sh
18+
git clone https://github.com/rivet-gg/rivetkit
19+
cd rivetkit/examples/sqlite
20+
pnpm install
21+
```
22+
23+
### Development
24+
```sh
25+
pnpm run dev
26+
```
27+
Open your browser to https://studio.rivet.gg/ to see your RivetKit server.
28+
29+
## License
30+
31+
Apache 2.0

examples/sqlite/package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "example-sqlite",
3+
"version": "0.9.9",
4+
"private": true,
5+
"type": "module",
6+
"scripts": {
7+
"dev": "tsx --watch src/server.ts",
8+
"check-types": "tsc --noEmit"
9+
},
10+
"devDependencies": {
11+
"@types/node": "^22.13.9",
12+
"tsx": "^3.12.7",
13+
"typescript": "^5.5.2"
14+
},
15+
"dependencies": {
16+
"@rivetkit/db": "workspace:*",
17+
"@rivetkit/actor": "workspace:*"
18+
},
19+
"stableVersion": "0.8.0"
20+
}

examples/sqlite/src/registry.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { actor, setup } from "@rivetkit/actor";
2+
import { db } from "@rivetkit/db";
3+
4+
export const chat = actor({
5+
onAuth: () => {},
6+
db: db({
7+
onMigrate: async (c) => {
8+
await c
9+
.prepare(`CREATE TABLE IF NOT EXISTS messages (
10+
id INTEGER PRIMARY KEY AUTOINCREMENT,
11+
sender TEXT NOT NULL,
12+
text TEXT NOT NULL,
13+
timestamp INTEGER NOT NULL
14+
)`)
15+
.run();
16+
},
17+
}),
18+
actions: {
19+
// Callable functions from clients: https://rivet.gg/docs/actors/actions
20+
sendMessage: async (c, sender: string, text: string) => {
21+
const message = { sender, text, timestamp: Date.now() };
22+
// State changes are automatically persisted
23+
await c.db
24+
.prepare(
25+
`INSERT INTO messages (sender, text, timestamp) VALUES (?, ?, ?)`,
26+
[sender, text, message.timestamp],
27+
)
28+
.run();
29+
// Send events to all connected clients: https://rivet.gg/docs/actors/events
30+
c.broadcast("newMessage", message);
31+
return message;
32+
},
33+
34+
getHistory: (c) =>
35+
c.db
36+
.prepare(`SELECT * FROM messages ORDER BY timestamp DESC LIMIT 100`)
37+
.all(),
38+
},
39+
});
40+
41+
export const registry = setup({
42+
use: { chat },
43+
});

examples/sqlite/src/server.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { registry } from "./registry";
2+
3+
registry.runServer();

examples/sqlite/tsconfig.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"compilerOptions": {
3+
/* Visit https://aka.ms/tsconfig.json to read more about this file */
4+
5+
/* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
6+
"target": "esnext",
7+
/* Specify a set of bundled library declaration files that describe the target runtime environment. */
8+
"lib": ["esnext"],
9+
/* Specify what JSX code is generated. */
10+
"jsx": "react-jsx",
11+
"allowArbitraryExtensions": true,
12+
13+
/* Specify what module code is generated. */
14+
"module": "esnext",
15+
/* Specify how TypeScript looks up a file from a given module specifier. */
16+
"moduleResolution": "bundler",
17+
/* Specify type package names to be included without being referenced in a source file. */
18+
"types": ["node"],
19+
/* Enable importing .json files */
20+
"resolveJsonModule": true,
21+
22+
/* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */
23+
"allowJs": true,
24+
/* Enable error reporting in type-checked JavaScript files. */
25+
"checkJs": false,
26+
27+
/* Disable emitting files from a compilation. */
28+
"noEmit": true,
29+
30+
/* Ensure that each file can be safely transpiled without relying on other imports. */
31+
"isolatedModules": true,
32+
/* Allow 'import x from y' when a module doesn't have a default export. */
33+
"allowSyntheticDefaultImports": true,
34+
/* Ensure that casing is correct in imports. */
35+
"forceConsistentCasingInFileNames": true,
36+
37+
/* Enable all strict type-checking options. */
38+
"strict": true,
39+
40+
/* Skip type checking all .d.ts files. */
41+
"skipLibCheck": true
42+
},
43+
"include": ["src/**/*"]
44+
}

examples/sqlite/turbo.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"$schema": "https://turbo.build/schema.json",
3+
"extends": ["//"]
4+
}

pnpm-lock.yaml

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)