encore-getting-started

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Getting Started with Encore.ts

Encore.ts 入门指南

Instructions

操作步骤

Install Encore CLI

安装 Encore CLI

bash
undefined
bash
undefined

macOS

macOS

brew install encoredev/tap/encore
brew install encoredev/tap/encore

Linux/WSL

Linux/WSL

Windows (PowerShell)

Windows (PowerShell)

undefined
undefined

Create a New App

创建新应用

bash
undefined
bash
undefined

Interactive - choose from templates

交互式 - 从模板中选择

encore app create my-app
encore app create my-app

Or start with a blank app

或者从空白应用开始

encore app create my-app --example=ts/hello-world
undefined
encore app create my-app --example=ts/hello-world
undefined

Project Structure

项目结构

A minimal Encore.ts app:
my-app/
├── encore.app           # App configuration
├── package.json         # Dependencies
├── tsconfig.json        # TypeScript config
├── encore.service.ts    # Service definition
└── api.ts               # API endpoints
一个最简版 Encore.ts 应用结构:
my-app/
├── encore.app           # 应用配置文件
├── package.json         # 依赖文件
├── tsconfig.json        # TypeScript 配置
├── encore.service.ts    # 服务定义文件
└── api.ts               # API 端点文件

The encore.app File

encore.app 文件

cue
// encore.app
{
    "id": "my-app"
}
This file marks the root of your Encore app. The
id
is your app's unique identifier.
cue
// encore.app
{
    "id": "my-app"
}
该文件标记你的 Encore 应用的根目录。
id
是应用的唯一标识符。

Define a Service

定义服务

Create
encore.service.ts
to define a service:
typescript
// encore.service.ts
import { Service } from "encore.dev/service";

export default new Service("my-service");
创建
encore.service.ts
文件来定义服务:
typescript
// encore.service.ts
import { Service } from "encore.dev/service";

export default new Service("my-service");

Create Your First API

创建你的第一个 API

typescript
// api.ts
import { api } from "encore.dev/api";

interface HelloResponse {
  message: string;
}

export const hello = api(
  { method: "GET", path: "/hello", expose: true },
  async (): Promise<HelloResponse> => {
    return { message: "Hello, World!" };
  }
);
typescript
// api.ts
import { api } from "encore.dev/api";

interface HelloResponse {
  message: string;
}

export const hello = api(
  { method: "GET", path: "/hello", expose: true },
  async (): Promise<HelloResponse> => {
    return { message: "Hello, World!" };
  }
);

Run Your App

运行你的应用

bash
undefined
bash
undefined

Start the development server

启动本地开发服务器

encore run
encore run

Your API is now available at http://localhost:4000

你的 API 现在可以通过 http://localhost:4000 访问

undefined
undefined

Open the Local Dashboard

打开本地控制台

bash
undefined
bash
undefined

Opens the local development dashboard

打开本地开发控制台

encore run
encore run

The dashboard shows:
- All your services and endpoints
- Request/response logs
- Database queries
- Traces and spans

控制台展示以下内容:
- 所有服务和端点
- 请求/响应日志
- 数据库查询记录
- 追踪信息和调用链

Common CLI Commands

常用 CLI 命令

CommandDescription
encore run
Start the local development server
encore test
Run tests
encore db shell <db>
Open a psql shell to a database
encore gen client
Generate API client code
encore app link
Link to an existing Encore Cloud app
命令描述
encore run
启动本地开发服务器
encore test
运行测试
encore db shell <db>
打开数据库的 psql 命令行界面
encore gen client
生成 API 客户端代码
encore app link
关联到已有的 Encore Cloud 应用

Add a Database

添加数据库

typescript
// db.ts
import { SQLDatabase } from "encore.dev/storage/sqldb";

const db = new SQLDatabase("mydb", {
  migrations: "./migrations",
});
Create a migration:
sql
-- migrations/1_create_table.up.sql
CREATE TABLE items (
    id SERIAL PRIMARY KEY,
    name TEXT NOT NULL
);
typescript
// db.ts
import { SQLDatabase } from "encore.dev/storage/sqldb";

const db = new SQLDatabase("mydb", {
  migrations: "./migrations",
});
创建迁移文件:
sql
-- migrations/1_create_table.up.sql
CREATE TABLE items (
    id SERIAL PRIMARY KEY,
    name TEXT NOT NULL
);

Next Steps

下一步

  • Add more endpoints (see
    encore-api
    skill)
  • Add authentication (see
    encore-auth
    skill)
  • Add infrastructure like Pub/Sub, cron jobs (see
    encore-infrastructure
    skill)
  • Deploy to Encore Cloud:
    encore app link
    then
    git push encore
  • 添加更多端点(参考
    encore-api
    技能文档)
  • 添加认证功能(参考
    encore-auth
    技能文档)
  • 添加 Pub/Sub、定时任务等基础设施(参考
    encore-infrastructure
    技能文档)
  • 部署到 Encore Cloud:先执行
    encore app link
    ,再执行
    git push encore