# 创建集合

Canonical URL: https://developers.iagenify.com/zh/sdk/ref/connected-storage/run-write/create-collection/
Language: zh

`storage.run({ action: "createCollection", collection })`

创建空收藏。 需要的非常少 — 写入名称创建它, 但当结构是点时却明确了 。

Status: document database · free · refuses a duplicate

```javascript
await storage.run({ action: "createCollection", collection: "invoices" });
// { ok: true, created: "invoices" }
```

## 当你真正需要的时候 {#needed}

A 音 A [插入插入](/zh/sdk/ref/connected-storage/run-write/insert) 将没有收藏的收藏创建为名称。 因此, 这是对于在写入任何文件之前必须存在结构的案例来说 : 设置步骤、 迁移、 界面, 将收藏的收藏在数据出现之前列出 。

## 拒绝拒绝现有名称, 并附有可读信件 {#exists}

> **拒绝声明收藏已经存在**
>
> 而不是数据库自己的代码, 数据库的代码给出了一个条件, 而不说明该怎么做。 如果创建两次对您无害, 请将拒绝视为成功 。

```javascript
async function ensureCollection(storage, collection) {
  const existing = await storage.run({ action: "collections" });
  if (existing.some((c) => c.name === collection)) return "already there";
  await storage.run({ action: "createCollection", collection });
  return "created";
}
```

## 这是写作,是被要求的 {#permission}

结构变化与数据结构结构的允许类别相同——在创建收藏之前,在每种模式下,只要最开放,都先询问一个代理。 [储存类别](/zh/sdk/agent/permissions/categories/storage).

- [浮点数](/zh/sdk/ref/connected-storage/run-write/drop-collection): 和 equ'il envoorte。 和 cequ'il envoorte。
- [run()](/zh/sdk/ref/connected-storage/run): 行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动,行动

```python
storage.run(action="createCollection", collection="invoices")
# {"ok": True, "created": "invoices"}
```

```python
def ensure_collection(storage, collection):
    existing = storage.run(action="collections")
    if any(c["name"] == collection for c in existing):
        return "already there"
    storage.run(action="createCollection", collection=collection)
    return "created"
```
