> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/feathersjs/feathers/llms.txt
> Use this file to discover all available pages before exploring further.

# Database Adapters Overview

> Learn about Feathers database adapters and how to connect your application to various databases including MongoDB, SQL, and in-memory storage.

Feathers provides a powerful adapter system that allows you to connect your services to different databases using a consistent API. All adapters share common patterns and querying capabilities while supporting database-specific features.

## Available Adapters

Feathers supports multiple database adapters out of the box:

<CardGroup cols={2}>
  <Card title="MongoDB" icon="leaf" href="/guides/databases/mongodb">
    Native MongoDB adapter for document-based storage
  </Card>

  <Card title="Knex (SQL)" icon="database" href="/guides/databases/knex">
    SQL adapter supporting PostgreSQL, MySQL, SQLite, and more
  </Card>

  <Card title="Memory" icon="memory">
    In-memory storage for testing and prototyping
  </Card>
</CardGroup>

## Common Features

All Feathers database adapters share these core capabilities:

### Standard Service Methods

Every adapter implements the standard Feathers service interface:

```typescript theme={null}
interface ServiceMethods {
  find(params?: Params): Promise<Paginated<Result> | Result[]>
  get(id: Id, params?: Params): Promise<Result>
  create(data: Data | Data[], params?: Params): Promise<Result | Result[]>
  update(id: Id, data: Data, params?: Params): Promise<Result>
  patch(id: Id | null, data: PatchData, params?: Params): Promise<Result | Result[]>
  remove(id: Id | null, params?: Params): Promise<Result | Result[]>
}
```

### Query Syntax

All adapters support a common query syntax for filtering, sorting, and pagination:

<CodeGroup>
  ```typescript Filtering theme={null}
  // Find users with age greater than 18
  await app.service('users').find({
    query: {
      age: { $gt: 18 }
    }
  })

  // Find users with specific names
  await app.service('users').find({
    query: {
      name: { $in: ['Alice', 'Bob'] }
    }
  })
  ```

  ```typescript Sorting theme={null}
  // Sort by name ascending
  await app.service('users').find({
    query: {
      $sort: { name: 1 }
    }
  })

  // Sort by age descending
  await app.service('users').find({
    query: {
      $sort: { age: -1 }
    }
  })
  ```

  ```typescript Pagination theme={null}
  // Get first 10 results
  await app.service('users').find({
    query: {
      $limit: 10,
      $skip: 0
    }
  })

  // Get next 10 results
  await app.service('users').find({
    query: {
      $limit: 10,
      $skip: 10
    }
  })
  ```

  ```typescript Selection theme={null}
  // Select specific fields
  await app.service('users').find({
    query: {
      $select: ['name', 'email']
    }
  })
  ```
</CodeGroup>

### Supported Query Operators

| Operator | Description           | Example                                                 |
| -------- | --------------------- | ------------------------------------------------------- |
| `$eq`    | Equal (implicit)      | `{ age: 25 }`                                           |
| `$ne`    | Not equal             | `{ status: { $ne: 'deleted' } }`                        |
| `$lt`    | Less than             | `{ age: { $lt: 30 } }`                                  |
| `$lte`   | Less than or equal    | `{ age: { $lte: 30 } }`                                 |
| `$gt`    | Greater than          | `{ age: { $gt: 18 } }`                                  |
| `$gte`   | Greater than or equal | `{ age: { $gte: 18 } }`                                 |
| `$in`    | In array              | `{ role: { $in: ['admin', 'user'] } }`                  |
| `$nin`   | Not in array          | `{ role: { $nin: ['guest'] } }`                         |
| `$or`    | Logical OR            | `{ $or: [{ age: { $lt: 18 } }, { age: { $gt: 65 } }] }` |
| `$and`   | Logical AND           | `{ $and: [{ age: { $gte: 18 } }, { verified: true }] }` |

### Pagination

Enable pagination in your service options:

```typescript theme={null}
const service = new MongoDBService({
  Model: collection,
  paginate: {
    default: 10,  // Default page size
    max: 50       // Maximum page size
  }
})
```

Paginated responses include:

```typescript theme={null}
interface Paginated<T> {
  total: number    // Total count of matching records
  limit: number    // Page size
  skip: number     // Number of records skipped
  data: T[]        // Array of results
}
```

## Adapter Options

All adapters support these common options:

```typescript theme={null}
interface AdapterServiceOptions {
  id: string              // Primary key field name (default: 'id')
  events: string[]        // Custom events to emit
  paginate?: PaginationOptions  // Pagination configuration
  multi?: boolean | string[]    // Allow multi updates/deletes
  filters?: FilterSettings      // Custom query filters
  operators?: string[]          // Custom query operators
}
```

### Multi Operations

By default, `patch` and `remove` without an ID will throw an error. Enable multi operations:

```typescript theme={null}
const service = new MongoDBService({
  Model: collection,
  multi: true  // Allow all multi operations
})

// Or enable for specific methods only
const service = new MongoDBService({
  Model: collection,
  multi: ['patch', 'remove']
})

// Now you can patch/remove multiple records
await service.patch(null, { status: 'archived' }, {
  query: { active: false }
})
```

## Internal Methods

Adapters provide internal `_method` versions that bypass hooks and validation:

```typescript theme={null}
// These skip hooks and sanitization
await service._find(params)
await service._get(id, params)
await service._create(data, params)
await service._update(id, data, params)
await service._patch(id, data, params)
await service._remove(id, params)
```

<Warning>
  Internal methods should only be used within hooks or when you need to bypass the normal service flow. They don't run hooks or emit events.
</Warning>

## Type Safety

All adapters support full TypeScript typing:

```typescript theme={null}
interface User {
  _id: ObjectId
  name: string
  email: string
  age: number
}

interface UserData {
  name: string
  email: string
  age: number
}

interface UserParams extends Params {
  user?: User
}

class UserService extends MongoDBService<User, UserData, UserParams> {
  // Fully typed service methods
}
```

## Next Steps

<CardGroup cols={2}>
  <Card title="MongoDB Adapter" icon="leaf" href="/guides/databases/mongodb">
    Learn how to use the MongoDB adapter
  </Card>

  <Card title="Knex Adapter" icon="database" href="/guides/databases/knex">
    Connect to SQL databases with Knex
  </Card>

  <Card title="Common Patterns" icon="code" href="/guides/databases/adapters">
    Explore adapter patterns and best practices
  </Card>
</CardGroup>
