An open API service indexing awesome lists of open source software.

https://github.com/vasyas/interpolated-sql


https://github.com/vasyas/interpolated-sql

javascript mysql nodejs orm sql typescript

Last synced: 4 months ago
JSON representation

Awesome Lists containing this project

README

        

Make SQL queries using JavaScript tagged literals for parameters.

## Example

```
const id = 5
const user = await sql`select * from user where id = ${id}`
```

Best used with MySQL, where you can do
```
const id = await sql`
insert into user
set ${{ email: "[email protected]", role: Role.ADMIN }}
`.insert()
```

## Getting started

Get package
```
yarn install interpolated-sql
```

Create your own exec function, that will bind sql creation and connection handling

```
import {Sql} from "interpolated-sql"
import mysql from "mysql2/promise"

const connection = await mysql.createConnection({
host: "localhost",
user: "root",
database: "test",
})

function exec(parts, ...params) {
return new Sql(parts, params, () => connection)
}

async function getUsers() {
return await exec`select * from users`.all()
}
```

## API

see src/sql.ts