# Database

## Permission

{% hint style="warning" %}
`database` permission is required.
{% endhint %}

<pre class="language-json" data-title="my-plugin.json"><code class="lang-json">{
    //...
    "plugin": {
        "permissions": {
<strong>            "scopes": ["database"]
</strong>        }
    }
}
</code></pre>

## Invalidate queries

After some database operations you might want to cause the client to automatically refetch certain queries. This is possible using `$app.invalidateClientQuery`  - [helpers](https://seanime.gitbook.io/seanime-extensions/plugins/apis/helpers "mention")

## Local files

You can interact with the scanned file entries (also called local files).

### Get all

```typescript
const localFiles = $database.localFiles.getAll()
```

### Edit

```typescript
// Get all 'One Piece' files
const onePieceLocalFiles = $database
    .localFiles.findBy((lf) => {
        return lf.mediaId === 21
    })
    
// Lock all 'One piece' files
for (const lf of onePieceLocalFiles) {
    lf.locked = true
}

$database.localFiles.save(onePieceLocalFiles)
```

Note that `save` only works for existing entries.

### Insert

```typescript
// Inserts a new collection of local files
// This is equivalent to doing a scan
const localFiles = $database.localFiles.insert([
    //...
])
```

## AniList

### Get Token

{% hint style="warning" %}
`anilist-token` permission is required
{% endhint %}

```typescript
$database.anilist.getToken()
```

### Get Username

```typescript
$database.anilist.getUsername()
```

## Auto Downloader Rules

```typescript
// Get all rules
$database.autoDownloaderRules.getAll()

// Get rules by media ID
const rules = $database.autoDownloaderRules.getByMediaId(21)

for (const rule of rules) {
    rule.enabled = false
    
    // Update a rule
    $database.autoDownloaderRules.update(rule.dbId, rule)
}

// Remove a rule
$database.autoDownloaderRules.remove(ruleDbId)

// Insert a rule
$database.autoDownloaderRules.insert({ 
    //...
})
```

## Auto Downloader Items

In Seanime, an item is usually added by the auto downloader when the user has chosen not to immediately download torrents. It is shown in the queue and lets the user download that torrent later.

```typescript
// Get all items
$database.autoDownloaderItems.getAll()

// Get items by media ID
const items = $database.autoDownloaderItems.getByMediaId(21)

for (const item of items) {
    // Update an item
    $database.autoDownloaderItems.update(item.dbId, item)
}

// Remove an item
$database.autoDownloaderItems.remove(itemDbId)

// Insert an item
$database.autoDownloaderItems.insert({ 
    //...
})
```

## Silenced media entries

```typescript
const silencedAnimeIds = $database.silencedMediaEntries.getAllIds()

// Silence an anime
$database.silencedMediaEntries.setSilenced(21, true)

$database.silencedMediaEntries.isSilenced(21) // true
```

## Media fillers

```typescript
const fillerData = $database.mediaFillers.getAll()

$database.mediaFillers.get(21)
$database.mediaFillers.insert("provider", 21, "slug", ["600"])
$database.mediaFillers.remove(21)
```
