[**Documentation**](../../../README)

***

[Documentation](../../../README) / [@zerotal/notifications](../README) / DatabaseChannel

# Class: DatabaseChannel

Defined in: [notifications/src/DatabaseChannel.ts:63](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/DatabaseChannel.ts#L63)

Stores notifications in the database.
The table and its indexes are created automatically on first use.

Schema:
  id              TEXT PRIMARY KEY       — UUID
  notifiable_type TEXT NOT NULL          — recipient's model class name
  notifiable_id   TEXT NOT NULL          — stringified notifiable id
  type            TEXT NOT NULL          — notification class name
  data            TEXT NOT NULL          — JSON-encoded toDatabase() result
  read_at         TEXT                   — ISO timestamp when read; NULL = unread
  created_at      TEXT NOT NULL          — ISO timestamp

Every read is scoped by `(notifiable_type, notifiable_id)`, not by id alone:
ids are only unique within a model, so a `User#1` and a `Team#1` would
otherwise share one inbox.

## Constructors

### Constructor

> **new DatabaseChannel**(`table`, `_sql`): `DatabaseChannel`

Defined in: [notifications/src/DatabaseChannel.ts:67](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/DatabaseChannel.ts#L67)

#### Parameters

##### table

`string`

##### \_sql

[`SQLInstance`](../../orm/index/interfaces/SQLInstance)

#### Returns

`DatabaseChannel`

## Methods

### send()

> **send**(`notifiable`, `notification`): `Promise`\<`void`\>

Defined in: [notifications/src/DatabaseChannel.ts:103](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/DatabaseChannel.ts#L103)

#### Parameters

##### notifiable

`Notifiable`

##### notification

[`Notification`](Notification)

#### Returns

`Promise`\<`void`\>

***

### markAsRead()

> **markAsRead**(`id`): `Promise`\<`void`\>

Defined in: [notifications/src/DatabaseChannel.ts:134](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/DatabaseChannel.ts#L134)

Mark a notification as read by its id.

#### Parameters

##### id

`string`

#### Returns

`Promise`\<`void`\>

***

### markAsUnread()

> **markAsUnread**(`id`): `Promise`\<`void`\>

Defined in: [notifications/src/DatabaseChannel.ts:141](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/DatabaseChannel.ts#L141)

Mark a notification as unread by its id.

#### Parameters

##### id

`string`

#### Returns

`Promise`\<`void`\>

***

### unread()

> **unread**(`notifiable`, `query?`): `Promise`\<[`NotificationRecord`](../interfaces/NotificationRecord)[]\>

Defined in: [notifications/src/DatabaseChannel.ts:147](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/DatabaseChannel.ts#L147)

Return unread notifications for a notifiable, newest first.

#### Parameters

##### notifiable

`Notifiable`

##### query?

[`InboxQuery`](../interfaces/InboxQuery) = `{}`

#### Returns

`Promise`\<[`NotificationRecord`](../interfaces/NotificationRecord)[]\>

***

### all()

> **all**(`notifiable`, `query?`): `Promise`\<[`NotificationRecord`](../interfaces/NotificationRecord)[]\>

Defined in: [notifications/src/DatabaseChannel.ts:162](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/DatabaseChannel.ts#L162)

Return notifications for a notifiable, newest first.

#### Parameters

##### notifiable

`Notifiable`

##### query?

[`InboxQuery`](../interfaces/InboxQuery) = `{}`

#### Returns

`Promise`\<[`NotificationRecord`](../interfaces/NotificationRecord)[]\>

***

### unreadCount()

> **unreadCount**(`notifiable`): `Promise`\<`number`\>

Defined in: [notifications/src/DatabaseChannel.ts:177](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/DatabaseChannel.ts#L177)

How many unread notifications a notifiable has — for a badge, without loading rows.

#### Parameters

##### notifiable

`Notifiable`

#### Returns

`Promise`\<`number`\>

***

### markAllAsRead()

> **markAllAsRead**(`notifiable`): `Promise`\<`void`\>

Defined in: [notifications/src/DatabaseChannel.ts:193](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/DatabaseChannel.ts#L193)

Mark every unread notification for a notifiable as read.

#### Parameters

##### notifiable

`Notifiable`

#### Returns

`Promise`\<`void`\>

***

### recent()

> **recent**(`limit?`): `Promise`\<[`NotificationRecord`](../interfaces/NotificationRecord)[]\>

Defined in: [notifications/src/DatabaseChannel.ts:214](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/DatabaseChannel.ts#L214)

The most recent stored notifications across every notifiable, newest first.
Backs the admin console; use `all()` for one recipient's inbox.

#### Parameters

##### limit?

`number` = `100`

#### Returns

`Promise`\<[`NotificationRecord`](../interfaces/NotificationRecord)[]\>

***

### delete()

> **delete**(`id`): `Promise`\<`void`\>

Defined in: [notifications/src/DatabaseChannel.ts:223](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/DatabaseChannel.ts#L223)

Delete one stored notification by id.

#### Parameters

##### id

`string`

#### Returns

`Promise`\<`void`\>

***

### clear()

> **clear**(`notifiable`): `Promise`\<`void`\>

Defined in: [notifications/src/DatabaseChannel.ts:229](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/DatabaseChannel.ts#L229)

Delete every stored notification for a notifiable.

#### Parameters

##### notifiable

`Notifiable`

#### Returns

`Promise`\<`void`\>

***

### prune()

> **prune**(`days`, `includeUnread?`): `Promise`\<`number`\>

Defined in: [notifications/src/DatabaseChannel.ts:247](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/DatabaseChannel.ts#L247)

Delete read notifications older than `days`, across every notifiable.
Backs `notifications:prune` — an inbox table grows without bound otherwise.

#### Parameters

##### days

`number`

Age threshold in days.

##### includeUnread?

`boolean` = `false`

Also prune notifications never read. Default: false.

#### Returns

`Promise`\<`number`\>

How many rows were deleted.

***

### \_count()

> **\_count**(): `Promise`\<`number`\>

Defined in: [notifications/src/DatabaseChannel.ts:265](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/DatabaseChannel.ts#L265)

Total stored notifications, across every notifiable.

#### Returns

`Promise`\<`number`\>
