# KpgService

`class` in `plugins` · v0.6.1

<https://docs.seedcord.org/packages/plugins/0.6.1/classes/kpg-service>

Base class for KyselyPg services.

Provides a small, typed shim around the shared Kysely instance and ensures that subclasses have been decorated with `@RegisterKpgService`.

```ts
abstract class KpgService<
    Database extends object,
    TTable extends LiteralUnion<keyof Database, string>
>
```

## Examples

```ts
\@RegisterKpgService('users')
export class UsersService extends KpgService<ImportedDatabaseInterface, 'users'> {
  public async findById(id: string) {
    return this.entity
      .selectFrom(this.table)
      .selectAll().where('id', '=', id)
      .executeTakeFirst();
  }
}

// Usage inside handlers:
const user = await this.core.db.services.users.findById('abc');
```

## Constructors

### constructor

```ts
KpgService(
    kysely: KyselyPg<Database>,
    core: Core
)
```

Constructs a new instance of the `KpgService` class

## Properties

### core

```ts
protected readonly core: Core
```

### db

```ts
public get db(): Kysely<Database>
```

Shared Kysely instance used to interact with the Postgres database.

### kysely

```ts
protected readonly kysely: KyselyPg<Database>
```

### table

```ts
public readonly table: TTable
```
