Database

An ORM is at times too much to get data out of the database. Kretes provides an thin layer of integration with various RDMBS systems using Sqorn. Thanks to this library you can write usual SQL queries, yet fully integrated with the regular JavaScript data structures.

Configuration

The database configuration is stored config/database.json as a JSON document.

In order to start using the database integration you only need to require kretes/db:

const db = require('kretes/db');

Basic Queries

Let's see how we can perform some basic and frequent SQL queries in Kretes

Select

Get all elements with all columns from widgets table; equivalent to select * from widgets:

const results = await db`widgets`;

Get all elements with all some columns from widgets table; equivalent to select id, name from widgets:

const results = await db`widgets`.return('id', 'name');

Get a single element from widgets table by id:

const result = await db`widgets`.where({ id })

Insert/Update

Insert a single element into widgets table:

await db`widgets`.insert({ name: 'Widget 1', amount: 2 })

Insert few elements at once into widgets table:

await db`widgets`.insert([
  { name: 'Widget 1', amount: 2 },
  { name: 'Widget 2', amount: 7 },
  { name: 'Widget 3', amount: 4 }
])

Update an existing element (identified by id) in widgets table:

await db`widgets`.where({ id: 2 }).set({ name: 'Widget 22' })

Found a mistake?