> For the complete documentation index, see [llms.txt](https://db.coolstone.dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://db.coolstone.dev/4/templates/collections.md).

# Collections

### Setup

{% code lineNumbers="true" %}

```js
const Collection = require('dubnium/collection');
const collection = new Collection(db, name, schema)
```

{% endcode %}

| Parameter | Type             | About                                                                                                                                                                                                                               |
| --------- | ---------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `db`      | Object           | The database instance this collection is associated with.                                                                                                                                                                           |
| `name`    | String           | The prefix or name of the collection.                                                                                                                                                                                               |
| `schema`  | Object \| String | Schema definition for validating data. Can be: - A string: any non-null value is valid. - An object: keys are field names and values are either expected types (`"string"`, `"number"`, etc.) or `"required"` for mandatory fields. |

## Validate a Record

* If schema is a string, validation passes if `data` is not `undefined` or `null`.
* If schema is an object:
  * Each key in the schema must exist in the data.
  * `"required"` fields must be non-empty.
  * Each field must match the expected type.

{% code lineNumbers="true" %}

```javascript
collection.validate(data)
```

{% endcode %}

**Parameters**

| Name   | Type   | About                 |
| ------ | ------ | --------------------- |
| `data` | Object | The data to validate. |

**Returns**

| Type    | About                                                     |
| ------- | --------------------------------------------------------- |
| Boolean | `true` if the data matches the schema, `false` otherwise. |

Creates a new record in the database after validating it against the schema.

{% code lineNumbers="true" %}

```javascript
collection.create('tag', data)
```

{% endcode %}

**Parameters**

| Name   | Type            | About                             |
| ------ | --------------- | --------------------------------- |
| `tag`  | String          | Unique identifier for the record. |
| `data` | Object \|String | Data to be stored.                |

**Returns**

* A promise resolving to the result of `db.create(tag, data)`

## Full Example

{% code lineNumbers="true" %}

```js
const Collection = require('./Collection');
const db = require('./myDatabase'); // Must implement create(tag, data)

const userSchema = {
  username: 'string',
  email: 'string',
  password: 'required'
};

const users = new Collection(db, 'users', userSchema);

async function addUser() {
  const userData = { username: 'alice', email: 'alice@example.com', password: 'secret' };
  try {
    const result = await users.create('user_1', userData);
    console.log('User created:', result);
  } catch (err) {
    console.error(err);
  }
}

addUser();
```

{% endcode %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://db.coolstone.dev/4/templates/collections.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
