Building and Migrating
Building a Cloesce project generally consists of three steps:
- Compilation
- Running database migrations
- Building your frontend code.
Compiling
In your project directory, run the following command to compile your Cloesce Models:
$ npx cloesce compile
This command looks for a cloesce.config.json file in your project root, which contains configuration settings for the Cloesce compiler. If the file is not found, or settings are omitted, default values will be used.
After compilation, a .generated folder is created in your project root. This should not be committed to source control, as it is regenerated on each build. The folder contains:
-
cidl.jsonThe Cloesce Interface Definition Language file, representing your data Models and their relationships. This file is used internally by Cloesce for generating client code, migrations, and running the Cloudflare Worker.
-
client.ts:The generated client code for accessing your Models from the frontend. Import this file in your frontend code to interact with your Cloesce Models over HTTP.
-
workers.ts:The generated Cloudflare Worker code with all linked dependencies (including your custom
mainfunction if defined). This file is the entry point for your Cloudflare Worker and is referenced in the generatedwrangler.toml.
Alpha Note:
wrangler.jsoncis not fully supported. Please usewrangler.tomlfor now.
Generating Migrations
To generate database migration files based on changes to your Cloesce Models, run the following command:
$ npx cloesce migrate <migration-name>
This command compares your current Cloesce Models against the last applied migration and generates a new migration file in the migrations/ folder with the specified <migration-name>. The migration file contains SQL statements to update your D1 database schema to match your Models.
You must apply the generated migrations to your D1 database using the Wrangler CLI:
$ npx wrangler d1 migrations apply <database-binding-name>
Running
After compiling and applying migrations, you can build and run your Cloudflare Worker locally using Wrangler:
$ npx wrangler dev --port <port-number>