-
Notifications
You must be signed in to change notification settings - Fork 41
feat(examples): add sqlite example #1176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 08-14-feat_examples_add_drizzle_example
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
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" | ||
} |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Spotted by Diamond |
||||||||||||||||||
}, | ||||||||||||||||||
}); | ||||||||||||||||||
|
||||||||||||||||||
export const registry = setup({ | ||||||||||||||||||
use: { chat }, | ||||||||||||||||||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { registry } from "./registry"; | ||
|
||
registry.runServer(); |
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/**/*"] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"$schema": "https://turbo.build/schema.json", | ||
"extends": ["//"] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.