open-index/open-npm huggingface dataset:
npm package: @htblaleonie/leonie-mqtt
Description: Leonie Mqtt Client to send and receive Mqtt messages easier
Author: Jonas Dorfinger
Latest version: 1.0.5

# leonie-mqtt

The Leonie MQTT-Client. It has a type safe API for sending and receiving MQTT messages easier.

## Usage

### Install

```shell
npm i @htblaleonie/leonie-mqtt
```

### Create instance

```javascript
import MqttClient from '@htblaleonie/leonie-mqtt';

const client = new MqttClient(config.mqttUrl);
```

### Publish

```javascript
client.publish(token, topic, payload, err => {
    if (err) {
        console.error(err);
    }
});
```

#### Params:

- ``token``
- ``topic``
- ``payload``
- ``callback(err)``

### Subscribe

```javascript
client.subscribe(token, topic, (topic, message, err) => {
    if (err) {
        console.error(err);
    } else {
        console.log(message.toString());
    }
});
```

#### Params:

- ``token``
- ``topic``
- ``callback(topic, message, err)``

## Building and publishing

To easily make this code available to other teams, it's being published on `npm`. Effectively, this means that everything in this folder is public!

To publish a new version on `npm`

1. Increment the version in the `package.json` (we are using semantic versioning)
2. Run `npm publish`

<br>

<hr>

Copyright © 2020 - 2021, Jonas Dorfinger. This package is [MIT licensed](./LICENSE)
npm package: @htc-class/simple-migrations
Description: Simple MySQL Migrations
Author: Jared Henderson
Latest version: 1.0.1

# Simple Migrations

> A barebones MySQL migration runner, that only works with `@htc-class/simple-mysql`

## Installation

```bash
npm install @htc-class/simple-migrations
```

## Setting up Migrations

You must create a folder in your project directory with the path `./src/migrations`. Then,
in that folder, start adding migration files, like the one shown below:

```ts
// ./src/migrations/01_CreateUserTable.ts
import { Migration } from '@htc-class/simple-migrations';
import SimpleSQL from '@htc-class/simple-mysql';

export default class CreateUserTable implements Migration {
  async up(db: SimpleSQL): Promise<void> {
    await db.mutate(/* sql */ `
      CREATE TABLE users (
        name TEXT NOT NULL,
        age INT NOT NULL,
        has_beard BOOLEAN NOT NULL,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
      );
    `);
  }

  async down(db: SimpleSQL): Promise<void> {
    await db.mutate(/* sql */ `DROP TABLE users;`);
  }
}
```

The migrations will be **ordered** by the alphabetical filesystem order, so it's
recommended to _prefix_ your filenames with a number or date, to keep them in order, like
this:

```
./src/
  migrations/
    _db-provider.ts
    01_CreateUserTable.ts
    02_CreateCardsTable.ts
    03_AddUserForeignKey.ts
    ...etc...
```

## Creating the Database Provider

In the `./src/migrations/` folder, you also _must_ provide a file named _exactly_
`_db-provider.ts` that exports a single function which provides an authenticated DB
instance. Here's a template:

```ts
// ./src/migrations/_db-provider.ts
import { DbProvider } from '@htc-class/simple-migrations';
import SimpleMySQL from '@htc-class/simple-mysql';

const dbProvider: DbProvider = () => {
  // or, import a configured instance from another file (if you have one)
  // and just export that, to avoid duplication
  return new SimpleMySQL({
    database: process.env.DATABASE_NAME ?? ``,
    user: process.env.DATABASE_USER ?? ``,
    password: process.env.DATABASE_PASSWORD ?? ``,
  });
};

export default dbProvider;
```

## Running Migrations

The library exposes an npm binary called `simple-migrate`, allowing for 5 commands, as
shown here:

```bash
npx simple-migrate up:all   # run all pending migrations "up"
npx simple-migrate up:one   # run ONE pending migration "up"
npx simple-migrate down:all # run all pending migrations "down"
npx simple-migrate down:one # run ONE pending migration "down"
npx simple-migrate reset    # same as running `down:all` and then `up:all`
```
npm package: @htc-class/simple-mysql
Description: Simple MySQL Typescript wrapper, for learning/demonstration only
Author: Jared Henderson
Latest version: 1.0.4

# @htc-class/simple-mysql

> A very simple Typescript wrapper around `mysql2/promise` for demonstration, learning,
> and rudimentary applications.

## Installation

```bash
$ npm install @htc-class/simple-mysql
```

## Usage

```ts
import SimpleMySQL from '@htc-class/simple-mysql';

const db = new SimpleMySQL({
  database: `pets`,
  user: `jared`,
  password: `goatbanjorodeo`,
});

// for selecting, use the `query()` method:
const selectResult = await db.query(`SELECT * FROM pets`);

// for updating, altering, droping, inserting, use `mutate()`
const dropResult = await db.mutate(`DROP TABLE pets`);
```

## TypeScript Notes:

Both `query()` and `mutate()` returns a _promise of_ a type called `Result<T>` which
models the possibility of a successful statement, or an error, the type is this:

```ts
type Result<T> =
  | {
      ok: true;
      value: T;
    }
  | {
      ok: false;
      error: string;
    };
```

The `query()` method is generic, allowing you to pass a type that you will get back an
_array of_ if there is no error. But _please note_, the type is not validated at runtime
in any way, so make sure it is correct!

`query()` returns a _promise_ of a result which returns an **array of the generic type you
provide.** The TypeScript signature of `query()` is:

```ts
function query<RowType = unknown>(query: string): Promise<Result<RowType[]>>;
```

That means if your `users` table had 3 columns, an `id = INT`, `name = TEXT`, and
`is_admin = BOOL`, you could express that in TypeScript like so:

```ts
type UserRow = {
  id: number;
  name: string;
  is_admin: boolean;
};

const result = await db.query<UserRow>(`SELECT * FROM users`);
if (result.ok) {
  console.log(result.value);
  // result.value = UserRow[]
  // > [{ id: 1, name: "Bob", is_admin: true}]
} else {
  console.error(result.error);
}
```

The `mutate()` method is a bit simpler, it just returns the type:

```ts
Promise<Result<{ affectedItems: number }>>
```

Which means you can use it like this:

```ts
const result = await db.mutate(`UPDATE users SET first_name = "Jared" WHERE id = 32;`);
if (result.ok) {
  console.log(`Updated ${result.value.affectedItems} rows.`);
} else {
  console.error(result.error);
}
```