# 安装

ts-grm 由两个包组成，都需要安装：

- `@ts-grm/core`——模型、视图（View）、Input DTO、查询 DSL 的类型与运行时，**与数据库无关**；
- `@ts-grm/sql`——SQL 执行、驱动、事务，真正的数据库后端。

任选一个与你项目一致的包管理器：

```bash
# npm
npm install @ts-grm/core @ts-grm/sql
```

```bash
# pnpm
pnpm add @ts-grm/core @ts-grm/sql
```

```bash
# yarn
yarn add @ts-grm/core @ts-grm/sql
```

```bash
# bun
bun add @ts-grm/core @ts-grm/sql
```

要求 Node.js ≥ 18（bun 等兼容运行时亦可）。ts-grm 零代码生成、无运行时反射，安装后不需要任何生成步骤或配置文件。

## 冒烟测试：安装好了吗

把下面的脚本加入工作区并运行（示例所需的 `sqlClient` 已准备好，详见[「认识沙盒」](../about/sandbox)页）。**跑通就说明环境就绪**——代码里的模型、视图、查询，接下来三页会一行行拆解，现在不用完全看懂：

```ts workspace
// 冒烟测试：输出种子数据即安装成功
// ① 导入：model/prop 建模型、dto 建视图；sqlClient 是示例提供的客户端
import { model, prop, dto } from '@ts-grm/core'
import { sqlClient } from './infra/sql-client'

// ② 声明模型：Book 表（id/name/edition/price 四列）
//    model(表名, 主键属性名, class)，字段即列
const Book = model("Book", "id", class {
    id = prop.i64()          // 主键，64 位整数
    name = prop.str(50)      // 定长字符串(50)
    edition = prop.i32()     // 32 位整数
    price = prop.num(10, 2)  // 定点数 num(总长, 小数位)
})

// ③ 声明视图：本次查询只取 id/name/price 三列
const BookView = dto.view(Book, c => [
    c.id,
    c.name,
    c.price,
])

// ④ 执行查询：全表取回（无 where/orderBy 时即 SELECT 全部）
const rows = await sqlClient.createQuery(Book, (q, book) => {
    return q.select(book.fetch(BookView))  // 按视图取形
}).fetchList()                              // 收尾：返回数组

console.log(rows)  // 结果面板输出种子数据
```

在沙盒里点「运行」，右侧结果面板应显示 `book` 表的 3 行种子数据。

## 下一步

环境就绪。下一节从零声明第一个**模型**。
