Skip to content
Closed
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
82 changes: 37 additions & 45 deletions rivetkit-typescript/packages/rivetkit/src/actor/instance/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1322,53 +1322,45 @@ export class ActorInstance<
}

async #setupDatabase() {
if ("db" in this.#config && this.#config.db) {
try {
const client = await this.#config.db.createClient({
actorId: this.#actorId,
overrideRawDatabaseClient: this.driver.overrideRawDatabaseClient
? () =>
this.driver.overrideRawDatabaseClient!(this.#actorId)
: undefined,
overrideDrizzleDatabaseClient:
this.driver.overrideDrizzleDatabaseClient
? () =>
this.driver.overrideDrizzleDatabaseClient!(
this.#actorId,
)
: undefined,
kv: {
batchPut: (entries) =>
this.driver.kvBatchPut(this.#actorId, entries),
batchGet: (keys) => this.driver.kvBatchGet(this.#actorId, keys),
batchDelete: (keys) =>
this.driver.kvBatchDelete(this.#actorId, keys),
},
sqliteVfs: this.driver.sqliteVfs,
});
this.#rLog.info({ msg: "database migration starting" });
await this.#config.db.onMigrate?.(client);
this.#rLog.info({ msg: "database migration complete" });
this.#db = client;
} catch (error) {
if (error instanceof Error) {
this.#rLog.error({
msg: "database setup failed",
error: stringifyError(error),
});
throw error;
}
const wrappedError = new Error(
`Database setup failed: ${String(error)}`,
);
if (!("db" in this.#config) || !this.#config.db) {
return;
}

try {
const client = await this.#config.db.createClient({
actorId: this.#actorId,
overrideRawDatabaseClient: this.driver.overrideRawDatabaseClient
? () => this.driver.overrideRawDatabaseClient!(this.#actorId)
: undefined,
overrideDrizzleDatabaseClient: this.driver.overrideDrizzleDatabaseClient
? () => this.driver.overrideDrizzleDatabaseClient!(this.#actorId)
: undefined,
kv: {
batchPut: (entries) => this.driver.kvBatchPut(this.#actorId, entries),
batchGet: (keys) => this.driver.kvBatchGet(this.#actorId, keys),
batchDelete: (keys) => this.driver.kvBatchDelete(this.#actorId, keys),
},
sqliteVfs: this.driver.sqliteVfs,
});
this.#rLog.info({ msg: "database migration starting" });
await this.#config.db.onMigrate?.(client);
this.#rLog.info({ msg: "database migration complete" });
this.#db = client;
} catch (error) {
if (error instanceof Error) {
this.#rLog.error({
msg: "database setup failed with non-Error object",
error: String(error),
errorType: typeof error,
});
throw wrappedError;
}
msg: "database setup failed",
error: stringifyError(error),
});
throw error;
}
const wrappedError = new Error(`Database setup failed: ${String(error)}`);
this.#rLog.error({
msg: "database setup failed with non-Error object",
error: String(error),
errorType: typeof error,
});
throw wrappedError;
}
}

Expand Down
Loading