Skip to content
Merged
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
42 changes: 18 additions & 24 deletions rivetkit-typescript/packages/rivetkit/src/registry/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,15 @@ function databaseNotConfiguredError(): RivetError {
);
}

function databaseClientNotReadyError(): RivetError {
return new RivetError(
"actor",
"database_client_not_ready",
"actor database client was not initialized before user code ran. this is an internal lifecycle error; the migration callback should have pre-warmed the client. file an issue if you can reproduce.",
{ public: true },
);
}

function stateNotEnabledError(): RivetError {
return new RivetError(
"actor",
Expand Down Expand Up @@ -2325,7 +2334,6 @@ export class ActorContextHandleAdapter {
#clientFactory?: () => AnyClient;
#databaseProvider?: Exclude<AnyDatabaseProvider, undefined>;
#db?: unknown;
#dbProxy?: unknown;
#dispatchCancelToken?: CancellationTokenHandle;
#kv?: NativeKvAdapter;
#queue?: NativeQueueAdapter;
Expand Down Expand Up @@ -2388,32 +2396,18 @@ export class ActorContextHandleAdapter {
throw databaseNotConfiguredError();
}

if (!this.#dbProxy) {
this.#dbProxy = new Proxy(
{},
{
get: (_target, property) => {
if (property === "then") {
return undefined;
}
if (this.#db) {
return this.#db;
}

return async (...args: Array<unknown>) => {
const client = await this.ensureDatabaseClient();
const value = Reflect.get(
client as object,
property,
);
if (typeof value !== "function") {
return value;
}
return await value.apply(client, args);
};
},
},
);
const runtimeState = getNativeRuntimeState(this.#runtime, this.#ctx);
const cachedClient = runtimeState.databaseClient;
if (cachedClient) {
this.#db = cachedClient.client;
return this.#db;
}

return this.#dbProxy;
throw databaseClientNotReadyError();
}

get state(): unknown {
Expand Down
Loading