Skip to content
Draft
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,5 @@ Cargo.lock
**/.wrangler
**/.DS_Store
.aider*

packages/**/tsup.config.bundled_*.mjs
2 changes: 1 addition & 1 deletion examples/drizzle/drizzle/meta/0000_snapshot.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"version": "6",
"dialect": "sqlite",
"id": "22f3d49c-97d5-46ca-b0f1-99950c3efec7",
"id": "5175c351-75eb-4f1d-9211-7ee520f0b9c2",
"prevId": "00000000-0000-0000-0000-000000000000",
"tables": {
"users_table": {
Expand Down
11 changes: 2 additions & 9 deletions examples/drizzle/drizzle/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,8 @@
{
"idx": 0,
"version": "6",
"when": 1750711614205,
"tag": "0000_wonderful_iron_patriot",
"breakpoints": true
},
{
"idx": 1,
"version": "6",
"when": 1750716663518,
"tag": "0001_rich_susan_delgado",
"when": 1750976447195,
"tag": "0000_moaning_tomorrow_man",
"breakpoints": true
}
]
Expand Down
4 changes: 2 additions & 2 deletions examples/drizzle/drizzle/migrations.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import journal from './meta/_journal.json';
import m0000 from './0000_wonderful_iron_patriot.sql';
import m0000 from './0000_moaning_tomorrow_man.sql';

export default {
journal,
migrations: {
m0000,
m0000
}
}

10 changes: 0 additions & 10 deletions examples/drizzle/hooks.js

This file was deleted.

6 changes: 4 additions & 2 deletions examples/drizzle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@
"private": true,
"type": "module",
"scripts": {
"dev": "tsx --watch src/server.ts",
"dev": "tsx --loader @rivetkit/sql-loader --watch src/server.ts",
"check-types": "tsc --noEmit"
},
"devDependencies": {
"@rivetkit/actor": "workspace:*",
"@rivetkit/sql-loader": "workspace:*",
"@types/node": "^22.13.9",
"rivetkit": "workspace:*",
"tsx": "^3.12.7",
"typescript": "^5.5.2"
},
"dependencies": {
"@rivetkit/db": "workspace:0.9.0-rc.1",
"better-sqlite3": "^12.1.1",
"drizzle-kit": "^0.31.2",
"drizzle-orm": "^0.44.2"
},
Expand Down
15 changes: 0 additions & 15 deletions examples/drizzle/register.js

This file was deleted.

30 changes: 30 additions & 0 deletions examples/drizzle/scripts/connect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/// <reference types="node" />
import { createClient } from "@rivetkit/actor/client";
import type { Registry } from "../src/registry";

async function main() {
const client = createClient<Registry>(
process.env.ENDPOINT ?? "http://127.0.0.1:8080",
);

const contacts = client.contacts.getOrCreate();

// counter.on("newCount", (count: number) => console.log("Event:", count));

for (let i = 0; i < 5; i++) {
const out = await contacts.insert({
name: `User ${i}`,
age: 20 + i,
email: `example+${i}@example.com`,
});
console.log("Inserted:", out);
}

console.log("Reading all users:");
const users = await contacts.read();
console.log(users);

// await counter.dispose();
}

main();
15 changes: 7 additions & 8 deletions examples/drizzle/src/db/schema.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// import { int, sqliteTable, text } from "@rivetkit/db/drizzle";
import { int, sqliteTable, text } from "@rivetkit/db/drizzle";

// export const usersTable = sqliteTable("users_table", {
// id: int().primaryKey({ autoIncrement: true }),
// name: text().notNull(),
// age: int().notNull(),
// email: text().notNull().unique(),
// email2: text().notNull().unique(),
// });
export const usersTable = sqliteTable("users_table", {
id: int().primaryKey({ autoIncrement: true }),
name: text().notNull(),
age: int().notNull(),
email: text().notNull().unique(),
});
55 changes: 31 additions & 24 deletions examples/drizzle/src/registry.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
// import { actor, setup } from "rivetkit";
// import { db } from "@rivetkit/db/drizzle";
// import * as schema from "./db/schema";
// import migrations from "../drizzle/migrations";
import { actor, setup } from "@rivetkit/actor";
import { db } from "@rivetkit/db/drizzle";
import * as schema from "./db/schema";
import migrations from "../drizzle/migrations";

// export const counter = actor({
// db: db({ schema, migrations }),
// state: {
// count: 0,
// },
// onAuth: () => {
// // Configure auth here
// },
// actions: {
// increment: (c, x: number) => {
// // createState or state fix fix fix
// c.db.c.state.count += x;
// return c.state.count;
// },
// },
// });
export const contacts = actor({
db: db({ schema, migrations }),
onAuth: async (c) => {},
actions: {
insert: async (c, record: { name: string; age: number; email: string }) => {
// Example of using the DB
const result = await c.db.insert(schema.usersTable).values(record);
return result;
},
read: async (c) => {
// Example of reading from the DB
const users = await c.db.query.usersTable.findMany();
return users;
},
search: async (c, query: string) => {
// Example of searching in the DB
const users = await c.db.query.usersTable.findMany({
where: (table, { ilike }) => ilike(table.name, `%${query}%`),
});
return users;
},
},
});

// export const registry = setup({
// use: { counter },
// });
export const registry = setup({
use: { contacts },
});

// export type Registry = typeof registry;
export type Registry = typeof registry;
8 changes: 2 additions & 6 deletions examples/drizzle/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
// import { registry } from "./registry";
// import { createMemoryDriver } from "@rivetkit/memory";
// import { serve } from "@rivetkit/nodejs";
import { registry } from "./registry";

// serve(registry, {
// driver: createMemoryDriver(),
// });
await registry.runServer();
12 changes: 11 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@
"types": "./dist/test/mod.d.cts",
"default": "./dist/test/mod.cjs"
}
},
"./db": {
"import": {
"types": "./dist/db/mod.d.ts",
"default": "./dist/db/mod.js"
},
"require": {
"types": "./dist/db/mod.d.cts",
"default": "./dist/bd/mod.cjs"
}
}
},
"engines": {
Expand All @@ -143,7 +153,7 @@
"sideEffects": false,
"scripts": {
"dev": "pnpm build --watch",
"build": "tsup src/mod.ts src/client/mod.ts src/common/log.ts src/common/websocket.ts src/actor/errors.ts src/topologies/coordinate/mod.ts src/topologies/partition/mod.ts src/utils.ts src/drivers/rivet/mod.ts src/driver-helpers/mod.ts src/driver-test-suite/mod.ts src/test/mod.ts",
"build": "tsup src/mod.ts src/client/mod.ts src/common/log.ts src/common/websocket.ts src/actor/errors.ts src/topologies/coordinate/mod.ts src/topologies/partition/mod.ts src/utils.ts src/drivers/rivet/mod.ts src/driver-helpers/mod.ts src/driver-test-suite/mod.ts src/test/mod.ts src/db/mod.ts",
"check-types": "tsc --noEmit",
"boop": "tsc --outDir dist/test -d",
"test": "vitest run",
Expand Down
11 changes: 6 additions & 5 deletions packages/core/src/actor/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ import type { ConnId } from "./connection";
import type { ActorContext } from "./context";
import type { SaveStateOptions } from "./instance";
import type { Schedule } from "./schedule";
import { Registry } from "@/registry/mod";
import { Client } from "@/client/client";
import type { Registry } from "@/registry/mod";
import type { Client } from "@/client/client";
import type { AnyDatabaseClient, DatabaseProviderOf } from "@/db/mod";

/**
* Context for a remote procedure call.
*
* @typeParam A Actor this action belongs to
*/
export class ActionContext<S, CP, CS, V, I, AD, DB> {
export class ActionContext<S, CP, CS, V, I, AD, DB extends AnyDatabaseClient> {
#actorContext: ActorContext<S, CP, CS, V, I, AD, DB>;

/**
Expand All @@ -24,7 +25,7 @@ export class ActionContext<S, CP, CS, V, I, AD, DB> {
*/
constructor(
actorContext: ActorContext<S, CP, CS, V, I, AD, DB>,
public readonly conn: Conn<S, CP, CS, V, I, AD, DB>,
public readonly conn: Conn<S, CP, CS, V, I, AD, DatabaseProviderOf<DB>>,
) {
this.#actorContext = actorContext;
}
Expand Down Expand Up @@ -95,7 +96,7 @@ export class ActionContext<S, CP, CS, V, I, AD, DB> {
/**
* Gets the map of connections.
*/
get conns(): Map<ConnId, Conn<S, CP, CS, V, I, AD, DB>> {
get conns(): Map<ConnId, Conn<S, CP, CS, V, I, AD, DatabaseProviderOf<DB>>> {
return this.#actorContext.conns;
}

Expand Down
Loading
Loading