# @team_seki/postgres-plugin

> This plugin is based on the [knex](https://knexjs.org/) library

Latest version **1.5.0** (published 2024-08-21) · 0 weekly downloads

## Install

```sh
npm install @team_seki/postgres-plugin
pnpm add @team_seki/postgres-plugin
yarn add @team_seki/postgres-plugin
bun add @team_seki/postgres-plugin
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.5.0 |
| Published | 2024-08-21 |
| First published | 2023-01-03 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 16.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | moisesugarcia, kmilo8346, dmunozgaete |

## Links

- npm: https://www.npmjs.com/package/@team_seki/postgres-plugin
- npm.io page: https://npm.io/package/@team_seki/postgres-plugin

## Recent versions

- 1.5.0 (latest) — 2024-08-21
- 1.5.0-next.20240821-0719314-7273595abbef9d3946feb337648e73e8 (next) — 2024-08-21
- 1.4.1 — 2024-08-19
- 1.4.1-next.20240819-2e6af06-f56ce37017aedafe90277e082e357d42 — 2024-08-19
- 1.4.0 — 2024-08-18
- 1.4.0-next.20240818-d434bac-5ee617d5a3f894db11f2869456073b9c — 2024-08-18
- 1.2.0 — 2024-08-18
- 1.1.0 — 2024-08-08
- 1.1.0-next.20240808-939976e-e377736dd6211da1005c206cbecd4376 — 2024-08-08
- 1.0.6 — 2023-01-27
- 1.0.6-next.20230127-456753b-1ddfb5665c649865c251447732f94408 — 2023-01-27
- 1.0.5 — 2023-01-25
- 1.0.5-next.20230123-8f3321c-6f2a484f7150316d116ddb98a8a8aeb4 — 2023-01-23
- 1.0.4 — 2023-01-16
- 1.0.4-next.20230116-69a3015-174a7dc23827f29fc6cfacb3943ac466 — 2023-01-16
- … 9 more at https://npm.io/package/@team_seki/postgres-plugin/versions

## README

# postgres-plugin

This plugin is based on the [knex](https://knexjs.org/) library

## Building

Run `nx build postgres-plugin` to build the library.

## Environment variables

Environment variables are supported since `v1.4.1`.

- `SK_GOOGLE_POSTGRES_<identifier>_CONNECTION_STRING`. The connection string. E.g.: `postgresql://localhost:5432`

- `SK_GOOGLE_POSTGRES_<identifier>_PRIVATE_IP_ADDRESS`. The ip address where the service is running. E.g.: `localhost`

- `SK_GOOGLE_POSTGRES_<identifier>_USERNAME`. The username used to connect to the service. E.g.: `postgres`

- `SK_GOOGLE_POSTGRES_<identifier>_PASSWORD`. The password used to connect to the service. E.g.: `my-password`

## How to use it

```
const postgresPlugin = await new PostgresPlugin({ database: "db1" });
const knex = postgresPlugin.getClient();
const result = await knex.raw("select 1;");
postgresPlugin.close();
```

## Migration

Create new migration

```
import { Knex, IMigration } from '@team_seki/postgres-plugin';

const migrations: IMigration[] = [
  {
    name: '000_define_users',
    async up(knex: Knex) {
      await knex.schema.createTable(tableName, (table) => {
        table.bigIncrements('id').unsigned().primary();
        table.string('name', 512).notNullable();
        table.integer('age').notNullable();
      });
    },
    async down(knex: Knex) {
      console.log('Removing users table...');
      await knex.schema.dropTableIfExists(tableName);
      console.log('The users table was deleted successfully.');
    },
  }
];
```

### runMigration

Runs all migrations that have not yet been run.

```
import { PostgresPlugin } from '@team_seki/postgres-plugin';
import migrations from './migrations'

const postgresPlugin = new PostgresPlugin({
  database: 'db1'
});
await postgresPlugin.runMigration(migrations);
postgresPlugin.close();
```

### rollbackMigration

Rolls back the latest migration group. If the all parameter is truthy, all applied migrations will be rolled back instead of just the last batch. The default value for this parameter is false.

```
import { PostgresPlugin } from '@team_seki/postgres-plugin';
import migrations from './migrations'

const postgresPlugin = new PostgresPlugin({
  database: 'db1'
});
await postgresPlugin.rollbackMigration(migrations);
postgresPlugin.close();
```

### upMigration

Runs the specified (by name parameter) or the next chronological migration that has not yet be run.

```
import { PostgresPlugin } from '@team_seki/postgres-plugin';
import migrations from './migrations'

const postgresPlugin = new PostgresPlugin({
  database: 'db1'
});
await postgresPlugin.upMigration(migrations, '000_define_users');
postgresPlugin.close();
```

### downMigration

Will undo the specified (by name parameter) or the last migration that was run.

```
import { PostgresPlugin } from '@team_seki/postgres-plugin';
import migrations from './migrations'

const postgresPlugin = new PostgresPlugin({
  database: 'db1'
});
await postgresPlugin.downMigration(migrations, '000_define_users');
postgresPlugin.close();
```

---
_Source: https://npm.io/package/@team_seki/postgres-plugin · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
