# typed-sql

> SQL query builder for Typescript

Latest version **0.10.0** (published 2016-12-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install typed-sql
pnpm add typed-sql
yarn add typed-sql
bun add typed-sql
```

## 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.10.0 |
| Published | 2016-12-11 |
| First published | 2016-12-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 (+2 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Samuli Holopainen |
| Maintainers | smphhh |
| Keywords | typescript, sql, node |

## Links

- npm: https://www.npmjs.com/package/typed-sql
- Repository: https://github.com/smphhh/simple-typed-sql
- Homepage: https://github.com/smphhh/simple-typed-sql#readme
- Issues: https://github.com/smphhh/simple-typed-sql/issues
- npm.io page: https://npm.io/package/typed-sql

## Dependencies (3)

- [md5](https://npm.io/package/md5.md) ^2.2.1
- [knex](https://npm.io/package/knex.md) ^0.12.6
- [bluebird](https://npm.io/package/bluebird.md) ^3.4.6

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.10.0 (latest) — 2016-12-11
- 0.9.3 — 2016-12-10
- 0.9.2 — 2016-12-10
- 0.9.1 — 2016-12-10
- 0.9.0 — 2016-12-10

## README

# Typed SQL

SQL query builder for TypeScript. Write complex SQL queries without needing to use
strings to refer to column or table names. Provides partial type safety and attribute auto-completion when
used with TypeScript. Powered by Knex.js.

Supports:
- Select, insert and update queries
- Complex where clauses
- Joins with complex join conditions
- Transactions
- Aggregation functions and group by

Upcoming:
- Select expressions

## Installation

```
npm install typed-sql
```

## Config

Create Mapper object

```typescript
// Create knex connection
let knexClient = knex(...);

import * as sqlMapper from 'typed-sql';

let mapper = new sqlMapper.Mapper(knexClient);
```

Define mapping to a SQL table

```typescript
let fooMapping = sqlMapper.defineMapping(
    'foo_table_name',
    {
        id: sqlMapper.defineNumber(),
        name: sqlMapper.defineString(),
        createdTime: sqlMapper.defineDatetime({ fieldName: 'created_time' }),
        fooCount: sqlMapper.defineNumber({ fieldName: 'foo_count' })
    }
);
```

## Quick usage

Insert data:

```typescript
await mapper.insertInto(fooMapping, {
    id: 1,
    name: "foo1",
    createdTime: new Date(),
    fooCount: 5
});
```

Select all columns from table:

```typescript
let fooList = await mapper.selectAllFrom(fooMapping);

console.log(fooList[0].fooCount); // 5
/* Compile error in TypeScript:
console.log(fooList[0].nonExisting);
*/
```

Simple where clause

```typescript
await mapper.insertInto(fooMapping, {
    id: 2,
    name: "foo2",
    createdTime: new Date(),
    fooCount: 2
});

let littleFoos = await mapper
    .selectAllFrom(fooMapping)
    .whereLessThan(fooMapping.fooCount, 3);

console.log(littleFoos); /*
[{
    id: 2,
    name: "foo2",
    createdTime: "Sun Sep 25 2016 22:18:53 GMT+0300 (FLE Daylight Time)",
    fooCount: 2
}]*/
```

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