# KyselyService

`class` in `plugin-kysely-postgres` · v0.1.4

<https://docs.seedcord.org/packages/plugin-kysely-postgres/0.1.4/classes/kysely-service>

Base class for KyselyPostgres services.

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

```ts
abstract class KyselyService<
    TTable extends LiteralUnion<KyselyTable, string> =
        KyselyTable
>
```

## Examples

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

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

## Constructors

### constructor

```ts
KyselyService(kysely: KyselyPostgres, core: CoreBase)
```

Constructs a new instance of the `KyselyService` class

## Properties

### core

```ts
protected readonly core: CoreBase
```

### db

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

Shared Kysely instance used to interact with the Postgres database.

### kysely

```ts
protected readonly kysely: KyselyPostgres
```

### table

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