# Owning Alepha

Every piece of Alepha is a class you can replace, its source can live in your project, and
what you write on top of it is standard TypeScript. So the day the framework does not do what
you need, the answer is never "wait for a release".

| You want to                            | Use                                           |
| -------------------------------------- | --------------------------------------------- |
| Replace a piece of the framework       | `alepha.with({ provide, use })`               |
| Change a module you installed          | `override()`, substitution, the module's atom |
| Read the framework's source, or fix it | the vendor plugin                             |
| Know what you would carry out          | the last section of this page                 |

## Replace a provider

Everything the framework does goes through a provider: a class the container builds. The
HTTP server, the database driver, the queue, the cache, the mail transport and the clock are
all providers, and none is sealed. Name the one to replace and the class to use instead:

```typescript filename="src/main.server.ts"
import { Alepha, run } from "alepha";
import {
  EmailProvider,
  type EmailSendOptions,
  type EmailSendResult,
} from "alepha/email";

class AcmeEmailProvider extends EmailProvider {
  async send(options: EmailSendOptions): Promise<EmailSendResult> {
    const response = await fetch("https://mail.example.app/send", {
      method: "POST",
      headers: { "content-type": "application/json" },
      body: JSON.stringify(options),
    });
    const receipt = (await response.json()) as { id: string };
    return { messageId: receipt.id };
  }
}

const alepha = Alepha.create().with({
  provide: EmailProvider,
  use: AcmeEmailProvider,
});

run(alepha);
```

Every class that injects `EmailProvider` now sends through yours, the framework's own
modules included. Your business code does not change, and neither does theirs.

This is the mechanism tests use to swap the mail server for an array, and it is not a
testing trick: it is how the framework picks a provider for each runtime, and how an app
picks its own. The one rule is the order: a substitution comes before anything builds the
original. See [Swapping Implementations](https://alepha.dev/docs/guides-core-alepha-instance.md#swapping-implementations).

## Change a module you installed

A module is a group of ordinary classes, so an app changes one from its own code, without
forking the package:

- `primitive.override({ ... })` replaces any option of a primitive the module declared: a
  page's component, an action's handler, a job's schedule.
- `alepha.with({ provide, use })` replaces one of its services, as above.
- `alepha.set(atom, value)` changes a setting it exposes.

[Extending a Module](https://alepha.dev/docs/guides-core-extending-a-module.md) walks through each on one example.

## Read the source, and fix it

The [vendor plugin](https://alepha.dev/docs/cli-plugins-vendor.md) copies the framework's source into your
project:

```typescript filename=alepha.config.ts
import { defineConfig } from "alepha/cli/config";
import { vendor } from "alepha/cli/vendor";

export default defineConfig({
  plugins: [
    vendor({
      packages: ["alepha"],
    }),
  ],
});
```

```bash
npx alepha vendor sync   # copy the source into .vendor/
npx alepha vendor diff   # list the changes you made to it
```

From then on the framework is code in your repository. A coding agent reads it beside your
app, and a bug you hit is a local edit away from fixed, today. `vendor diff` lists the
patches you carry, and `vendor sync` refuses to overwrite them without `--force`.

A patch is yours to carry until it lands upstream, so keep the list short: substitute a
class when that is enough, and patch the source when it is not.

## What you would carry out

If you leave, what you wrote on top of Alepha is not in a private format:

- **The data.** PostgreSQL, SQLite or D1, with files in S3, R2 or on disk. Nothing is stored
  in a format only Alepha reads.
- **The migrations.** SQL files in `migrations/`, generated by Drizzle Kit.
- **The schemas.** Zod schemas, with a few extra formats.
- **The pages.** React components. A component takes props and renders them.
- **The business logic.** Plain classes with plain methods.

What you would rewrite is the declarations: each `$action`, `$page` and `$job` becomes a
route, a page and a job in whatever comes next. That is the real cost of leaving, and it is
the cost of leaving any framework. The two sections above exist so that you rarely have a
reason to pay it.
