@ladc/sql-bricks-modifier
TypeScript icon, indicating that this package has built-in type declarations

0.22.7 • Public • Published

@ladc/sql-bricks-modifier

Build Status npm Type definitions GitHub

LADC is a common API on top of relational database (SQL) connectors. It can connect to Postgresql, MariaDB / MySQL, SQLite. The API is inspired from PDO and JDBC. It’s named LADC for “a Layer Above Database Connectors”.

This package is a plugin for LADC. It integrates the query builder SQL Bricks to LADC.

Additional API

This package overloads the following methods of the LADC objects MainConnection and TransactionConnection:

  • prepare(sqlBricksQuery)
  • exec(sqlBricksQuery)
  • all(sqlBricksSelectStatement)
  • singleRow(sqlBricksSelectStatement)
  • singleValue(sqlBricksSelectStatement)
  • cursor(sqlBricksSelectStatement)

How to use SQL Bricks with LADC (example with SQLite)

Install with LADC and a connector (here is an example with SQLite):

npm install ladc @ladc/sqlite3-adapter sql-bricks @ladc/sql-bricks-modifier

Here is how to create a connection:

import ladc from "ladc";
import sqlite3Adapter from "@ladc/sqlite3-adapter";
import sqlBricksModifier from "@ladc/sql-bricks-modifier";

const cn = ladc({
  adapter: sqlite3Adapter({ fileName: `${__dirname}/mydb.sqlite` }),
  modifier: sqlBricksModifier(),
});

Then, use it:

import { select } from "sql-bricks";

async function test(cn) {
  const q = select("col1, col2").from("table1");
  const rows = await cn.all(q);
  console.log(rows);
}

How to use SQL Bricks with LADC: The case of MySQL

MySQL requires a specific placeholder option for SQL Bricks:

import ladc from "ladc";
import mysql2Adapter from "@ladc/mysql2-adapter";
import sqlBricksModifier from "@ladc/sql-bricks-modifier";

const cn = ladc({
  adapter: mysql2Adapter({
    mysql2Config: {
      host: "-my-server-",
      database: "-my-database-",
      user: "-my-user-",
      password: "-my-password-",
    },
  }),
  modifier: sqlBricksModifier({
    toParamsOptions: { placeholder: "?" }, // ← Specific to MySQL
  }),
});

Now, the API can be used as usual.

Contribute

With VS Code, our recommanded plugin is:

  • TSLint from Microsoft (ms-vscode.vscode-typescript-tslint-plugin)

Readme

Keywords

Package Sidebar

Install

npm i @ladc/sql-bricks-modifier

Weekly Downloads

1

Version

0.22.7

License

CC0-1.0

Unpacked Size

18.4 kB

Total Files

10

Last publish

Collaborators

  • paleo