Skip to content
Open
Show file tree
Hide file tree
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
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,30 @@ API gateway, usage metering, and billing services for the Callora API marketplac
| `npm run build`| Compile TypeScript to `dist/` |
| `npm start` | Run compiled `dist/index.js` |

## Database Migrations

This project uses [Knex.js](https://knexjs.org/) for database migrations. By default, it is configured to use a local SQLite3 database for development.

### Running Migrations

To apply pending migrations and update your database schema, run:
```bash
npm run migrate:up
```

To rollback the last batch of migrations, run:
```bash
npm run migrate:down
```

### Creating a New Migration

To generate a new migration file, run:
```bash
npm run migrate:make migration_name
```
This will create a new TypeScript file in the `migrations/` directory where you can define `up` and `down` schema changes.

## Project layout

```
Expand Down
Binary file added dev.sqlite3
Binary file not shown.
28 changes: 28 additions & 0 deletions knexfile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { Knex } from "knex";

const config: { [key: string]: Knex.Config } = {
development: {
client: "sqlite3",
connection: {
filename: "./dev.sqlite3"
},
useNullAsDefault: true,
migrations: {
directory: "./migrations",
extension: "ts"
}
},
production: {
client: "sqlite3",
connection: {
filename: "./prod.sqlite3"
},
useNullAsDefault: true,
migrations: {
directory: "./migrations",
extension: "ts"
}
}
};

export default config;
22 changes: 22 additions & 0 deletions migrations/20260225164108_init_users_developers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { Knex } from "knex";

export async function up(knex: Knex): Promise<void> {
await knex.schema.createTable("users", (table) => {
table.increments("id").primary();
table.string("stellar_address").notNullable().unique();
table.timestamps(true, true); // creates created_at and updated_at
});

await knex.schema.createTable("developers", (table) => {
table.increments("id").primary();
table.string("user_id").notNullable();
table.string("name").notNullable();
table.timestamps(true, true);
table.foreign("user_id").references("stellar_address").inTable("users").onDelete("CASCADE");
});
}

export async function down(knex: Knex): Promise<void> {
await knex.schema.dropTableIfExists("developers");
await knex.schema.dropTableIfExists("users");
}
Loading