[ PROMPT_NODE_24078 ]
Do Storage 配置说明
[ SKILL_DOCUMENTATION ]
# DO 存储配置
## SQLite 后端 (推荐)
**wrangler.jsonc:**
c
{
"migrations": [
{
"tag": "v1",
"new_sqlite_classes": ["Counter", "Session", "RateLimiter"]
}
]
}
**迁移生命周期:** 迁移在每次部署时运行一次。现有的 DO 实例在下一次调用时会获取新的存储后端。重命名/删除类需要使用 `renamed_classes` 或 `deleted_classes` 条目。
## KV 后端 (旧版)
**wrangler.jsonc:**
c
{
"migrations": [
{
"tag": "v1",
"new_classes": ["OldCounter"]
}
]
}
## TypeScript 设置
typescript
export class MyDurableObject extends DurableObject {
sql: SqlStorage;
constructor(ctx: DurableObjectState, env: Env) {
super(ctx, env);
this.sql = ctx.storage.sql;
// 初始化模式
this.sql.exec(`
CREATE TABLE IF NOT EXISTS users(
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email TEXT UNIQUE
);
`);
}
}
// 绑定
interface Env {
MY_DO: DurableObjectNamespace;
}
export default {
async fetch(request: Request, env: Env): Promise {
const id = env.MY_DO.idFromName('singleton');
const stub = env.MY_DO.get(id);
// 现代 RPC: 直接调用方法 (推荐)
const result = await stub.someMethod();
return Response.json(result);
// 旧版: 转发请求 (仍然有效)
// return stub.fetch(request);
}
}
## CPU 限制
c
{
"limits": {
"cpu_ms": 300000 // 5 分钟 (默认 30 秒)
}
}
## 位置控制
typescript
// 管辖区 (GDPR/FedRAMP)
const euNamespace = env.MY_DO.jurisdiction("eu");
const id = euNamespace.newUniqueId();
const stub = euNamespace.get(id);
// 位置提示 (尽力而为)
const stub = env.MY_DO.get(id, { locationHint: "enam" });
// 提示: wnam, enam, sam, weur, eeur, apac, oc, afr, me
## 初始化
typescript
export class Counter extends DurableObject {
value: number;
constructor(ctx: DurableObjectState, env: Env) {
super(ctx, env);
// 初始化期间阻塞并发请求
ctx.blockConcurrencyWhile(async () => {
this.value = (await ctx.storage.get("value")) || 0;
});
}
}