0.8.6 • Published 9 years ago

simple-typed-sql v0.8.6

Weekly downloads
4
License
MIT
Repository
github
Last release
9 years ago

Simple Typed SQL

JavaScript/TypeScript SQL query builder allowing one to write complex SQL queries without needing use strings to refer to columns or tables. 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 simple-typed-sql

Config

Create Mapper object

// Create knex connection
let knexClient = knex(...);

import * as sqlMapper from 'simple-typed-sql';

let mapper = new sqlMapper.Mapper(knexClient);

Define mapping to a SQL table

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:

await mapper.insertInto(fooMapping, {
    id: 1,
    name: "foo1",
    createdTime: new Date(),
    fooCount: 5
});

Select all columns from table:

let fooList = await mapper.selectAllFrom(fooMapping);

console.log(fooList[0].fooCount); // 5
/* Compile error in TypeScript:
console.log(fooList[0].nonExisting);
*/

Simple where clause

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
}]*/
0.8.6

9 years ago

0.8.5

9 years ago

0.8.4

9 years ago

0.8.3

9 years ago

0.8.2

9 years ago

0.8.1

9 years ago

0.8.0

9 years ago

0.7.0

9 years ago

0.6.7

9 years ago

0.6.6

9 years ago

0.6.5

9 years ago

0.6.3

9 years ago

0.6.2

9 years ago

0.6.0

9 years ago

0.5.2

9 years ago

0.5.1

9 years ago

0.5.0

9 years ago

0.4.3

9 years ago

0.4.2

9 years ago

0.4.1

9 years ago

0.4.0

9 years ago

0.3.0

9 years ago

0.2.5

9 years ago

0.2.4

9 years ago

0.2.3

9 years ago

0.2.2

9 years ago

0.2.1

9 years ago

0.2.0

9 years ago

0.1.6

10 years ago

0.1.5

10 years ago

0.1.4

10 years ago

0.1.3

10 years ago

0.1.2

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago