# @theredhead/data

> Easy Asynchronous Data Access for mysql and sqlite

Latest version **0.0.8** (published 2021-12-11) · WTFPL license · 0 weekly downloads

## Install

```sh
npm install @theredhead/data
pnpm add @theredhead/data
yarn add @theredhead/data
bun add @theredhead/data
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.8 |
| Published | 2021-12-11 |
| First published | 2021-12-09 |
| Weekly downloads | 0 |
| License | WTFPL |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 73.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | kris@theredhead.nl |
| Maintainers | theredhead-kris |
| Keywords | data access, database, db, query, mysql, mysql2, sqlite, sqlite3 |

## Links

- npm: https://www.npmjs.com/package/@theredhead/data
- Repository: https://github.com/theredhead/theredhead-data
- Homepage: https://github.com/theredhead/theredhead-data#readme
- Issues: https://github.com/theredhead/theredhead-data/issues
- npm.io page: https://npm.io/package/@theredhead/data

## Alternatives

- [gamedig](https://npm.io/package/gamedig.md) — 29.3K weekly downloads
- [join-monster](https://npm.io/package/join-monster.md) — 12.8K weekly downloads
- [masked](https://npm.io/package/masked.md) — 5.5K weekly downloads
- [@comunica/actor-query-process-explain-logical](https://npm.io/package/@comunica/actor-query-process-explain-logical.md) — 4.7K weekly downloads
- [@veracity/vui](https://npm.io/package/@veracity/vui.md) — 4.6K weekly downloads

## Recent versions

- 0.0.8 (latest) — 2021-12-11
- 0.0.7 — 2021-12-11
- 0.0.6 — 2021-12-11
- 0.0.5 — 2021-12-10
- 0.0.4 — 2021-12-10
- 0.0.3 — 2021-12-09
- 0.0.2 — 2021-12-09

## README

# @theredhead/data

## Purpose

This package provides interfaces for use with @theredhead/data-access-mysql and @theredhead/data-access-sqlite

## Features

- Easy to use full featured
- Fully built with typescript
- Optional peer dependencies on [mysql2](https://www.npmjs.com/package/mysql2) and/or [sqlite3](https://www.npmjs.com/package/sqlite3). (only grab the engine you use)


### Some quick examples using FetchRequest fluently through FetchRequestBuilder

```typescript
const db = new SqliteDatabase('starwars.sqlite3');
const carrie = await db.from('actor')
  .where( 'surname = ? AND name = ?', 'Carrie','Fischer')
  .orderBy('surname', 'DESC')
  .fetch();

const mark = await db.from('actor')
  .whereAnd(
    ['surname = ?', 'Hamill'],
    ['name = ?', 'Mark']
  )
  .fetch();

const otiginalTrilogyMainCast = await db.from('actor')
  .whereOr(
    ['name = ?', 'Carrie'],
    ['name = ?', 'Mark']
    ['name = ?', 'Harrison'],
    ['name = ?', 'Peter'],
    ['name = ?', 'Kenny'],
    ['name = ?', 'Anthony'],
  )
  .fetch();
```

The interfaces making this possible:

```typescript

export interface IDbConnection {

  // straight sql execution
  executeScalar<T>(text: string, params: DbParams): Promise<T>;
  executeSingle<T extends PartialRecord>(text: string, params: DbParams): Promise<T>;
  executeArray<T extends Record>(text: string, params: DbParams): Promise<T[]>;
  executeNonQuery(text: string, params: DbParams): Promise<number>;

  // schema inspection
  tableExists(table: string): Promise<boolean>;
  columnExists(table: string, column: string): Promise<boolean>;

  // CRUD
  insert<T extends PartialRecord>(table: string, obj: T): Promise<T>;
  update<T extends Record>(table: string, obj: T): Promise<T>;
  delete<T extends Record>(table: string, id: number): Promise<T>;

  // FetchRequest support
  fetch<T extends Record>(request: FetchRequest): Promise<T[]>;
  from(table: string): FetchRequestBuilder;
}

```

### Connecting to mysql

```typescript
const connection = new MySqlConnection({
  host: "127.0.0.1",
  user: "user",
  password: "password",
  database: "database",
})
```

### Connecting to sqlite:

```typescript
const connection = new SqliteConnection('/path/to/file');
```


### Conventions that avoid problems.
#### For the `insert`, `update` and `delete` methods:

We expect every table to have a unique, numeric identity column. This means that for mysql tables, you must have a rowid column probably declared as `rowid BIGINT NOT NULL UNIQUE AUTO_INCREMENT`. Note that this is for the database, not for the data, so it does not need to be a primary key, but it must be unique. If you don't want it to be named `rowid`, you can set the `rowIdColumn` property on your `MySqlConnection` to something else.

sqlite does not need special consideration. but you will get the rowid column along with your records from `FetchRequest`s and the `insert`/`update` methods.

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