Generated Code
What lives in _generated/ and why it exists.
Last updated on
The _generated/ folder is created and maintained by npx typebase-io-cli codegen. Do not edit these files. They are overwritten every time you run the command.
_generated/server.ts
// ⚠️ AUTO-GENERATED FILE — DO NOT EDIT
import type { ActionBuilder, GetDBBuilder, InferRouterInputs, InferRouterOutputs } from 'typebase-io/server';
import type { auth as authConfig } from '../auth.ts';
import type { env as envSchema } from '../env.ts';
import type { publisher as publisherConfig } from '../publisher.ts';
import type { relations } from '../db/relations.ts';
import { filterActions } from 'typebase-io/server';
import * as ActionModule1 from '../actions/mutations/todos.ts';
import * as ActionModule2 from '../actions/queries/todos.ts';
export const router = {
mutations: {
todos: filterActions(ActionModule1),
},
queries: {
todos: filterActions(ActionModule2),
},
};
export type Router = typeof router;
export type RouterInputs = InferRouterInputs<typeof router>;
export type RouterOutputs = InferRouterOutputs<typeof router>;
export declare const action: ActionBuilder<typeof relations, typeof authConfig, typeof envSchema, typeof publisherConfig>;
export declare const getDB: GetDBBuilder<typeof relations>;The imports and the type arguments track what your project actually has. Every slot your project doesn't have is never, and its import is left out: no db/schema.ts gives never in the first slot, no auth.ts in the second, no env.ts in the third, and no publisher.ts in the fourth. Each never slot also drops the matching key from the action context.
router: an object that mirrors your actions/ folder structure. Every file becomes a namespace, every exported action becomes a method. Non-action exports from the same file are not added to the router. This is what your client imports as Router to get full type safety.
RouterInputs / RouterOutputs: type maps that mirror the router, giving you the input and output type of any action by path. Use them to type component props, helper functions, or anything else that handles action data without re-declaring the shape:
import type { RouterInputs, RouterOutputs } from '../typebase/_generated/server';
type Todo = RouterOutputs['queries']['todos']['getOne']; // what getOne returns
type CreateTodoInput = RouterInputs['mutations']['todos']['create']; // what create acceptsFor a handler, the output type comes from the action's .output() schema or is inferred from the value it returns. For a streaming action, it is an async iterator whose event type comes from .output() or the values the generator yields — the same type either way. To pull one event out of it, pass the entry to InferStreamEvent from typebase-io/server:
import type { InferStreamEvent } from 'typebase-io/server';
import type { RouterOutputs } from '../typebase/_generated/server';
type Todos = InferStreamEvent<RouterOutputs['queries']['todos']['getMany']>; // one event of the streamaction: the builder you import in every action file to define a handler or stream. It's typed with your schema relations, auth config, env schema, and publisher config, so the context object in .handler() and .stream() is always accurate. DATABASE_URL and BETTER_AUTH_SECRET are added to env by the builder itself whenever your project has a database or auth, so they're typed without appearing in your env.ts.
getDB: a function that returns the Drizzle database instance. Use it when you need database access from code that runs outside of an action handler or stream, such as a shared utility module:
import { getDB } from './_generated/server';
const db = getDB();
const todos = await db.query.todos.findMany();Inside action handlers and streams you should keep using the db passed into the context. It's the same client, just already wired up for you.
_generated/db.d.ts
This file is only generated when your project has a db/schema.ts file.
// ⚠️ AUTO-GENERATED FILE — DO NOT EDIT
import type { InferDB } from 'typebase-io/db';
import type * as schema from '../db/schema.ts';
import type { auth } from '../auth.ts';
export type DB = InferDB<typeof schema>;
export type AuthSession = typeof auth.$Infer.Session;DB: a map of all your table names to their inferred row types. Use it to type variables without repeating your schema inline:
import type { DB } from './_generated/db';
type Todo = DB['todos']['select'];
type NewTodo = DB['todos']['insert'];AuthSession: the inferred session type from better-auth. Only present when your project has an auth.ts file. Use it to type session objects in middleware or utility functions:
import type { AuthSession } from './_generated/db';
function requireSession(session: AuthSession | null) {
if (!session) throw new ServerError('UNAUTHORIZED');
return session;
}