Connecting Avleon to a database with TypeORM or Knex.
Avleon ships with two database integrations: TypeORM for an entity/repository
model, and Knex for a query builder. Both are optional peer dependencies —
install the one you use, along with its driver.
Register the datasource on the app with useTypeORM. It is asynchronous, so
await it before the app runs.
app.ts
import { Avleon } from '@avleon/core';
import { User } from './models/user';
import { UsersController } from './controllers/users.controller';
const app = Avleon.createApplication();
export async function setup() {
await app.useTypeORM({
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'postgres',
password: 'postgres',
database: 'myapi',
entities: [User],
synchronize: true, // development only
});
app.useControllers([UsersController]);
return app;
}
export default app;
serve.ts
import { Environment, inject } from '@avleon/core';
import app, { setup } from './app';
async function serve() {
await setup();
const env = inject(Environment);
await app.run(env.get<number>('PORT') || 4000);
}
serve();
Note: The project generated by avleon new compiles to CommonJS, where top-level
await is not available. Wrap async setup in a function and await it from your
entry point, as above.