0.8.0 • Published 10 months ago

blazend v0.8.0

Weekly downloads
6
License
ISC
Repository
-
Last release
10 months ago

Blazend

Blazend is a backend framework to build CRUD applications faster and easily in NodeJS/Typescript.

It provides a simple and sweet, yet flexible abstraction over knex to increase developer productivity.

Install

$ npm i blazend

# Then add one of the following:
$ npm install pg
$ npm install sqlite3
$ npm install mysql
$ npm install mysql2

Initialization

const { App } = require('blazend');

const appConfig = {
  db: {
    config: {
      dbType: "postgres|mysql|sqlite",
      conn: "<connection-string>",
      schema: "<schema>", // Applicable for `postgres` only
      debug: true|false // To turn the debugging of knex 
    },
    onConnectionSuccess: () => {},
    onConnectionError: (error) => {}
  }
}

const app = new App(appConfig)

Example: Initializing to use postgres:

import { App } from "blazend";

const appConfig = {
  db: {
    config: {
      dbType: "postgres",
      conn: "postgres://postgres:mysecretpassword@localhost:5432/postgres",
      schema: "public",
      debug: true
    },
    onConnectionSuccess: () => console.log("Successfully connected to database!"),
    onConnectionError: (error) => console.log("Error connecting to database", error)
  }
}

const app = new App(appConfig)

Performing CRUD operations

The app.modules.db object is a simple wrapper over knex. You can use it to perform CRUD operations with it inside your services.

Below are examples for various operations

Inserting one record

const user = { id: 1, name: "John" }
try {
  await app.modules.db.insert("users").doc(user).apply()
} catch (error) {
  console.log("Error", error)
}

Inserting multiple records

const users = [
  { id: 1, name: "John" },
  { id: 2, name: "Jack" }
]

try {
  await app.modules.db.insert("users").docs(users).apply()
} catch (error) {
  console.log("Error", error)
}

Reading one record

import { cond } from "blazend";

try {
  const user = await app.modules.db.get("users").where(cond("id", "==", 1)).apply()
} catch (error) {
  console.log("Error", error)
}

Reading multiple record

import { cond, and, or } from "blazend";

try {
  const whereClause = or(
    cond("role", "==", "admin"),
    and(
      cond("country", "==", "India"),
      cond("billing_enabled", "==", true)
    )
  )

  const sortOptions = [{ column: "age", order: "desc" }, { column: "name", order: "asc" }]

  const users = await app.modules.db.get("users")
    .select("id", "name", "age")
    .where(whereClause)
    .sort(sortOptions)
    .limit(10)
    .offset(50)
    .apply()

  console.log("Users", users)
} catch (error) {
  console.log("Error", error)
}

Updating records

import { cond } from "blazend";

try {
  await app.modules.db.update("users").where(cond("id", "==", 1)).set({ name: "Jack" }).apply()
} catch (error) {
  console.log("Error", error)
}

Deleting records

import { cond } from "blazend";

try {
  await app.modules.db.delete("users").where(cond("id", "==", 1)).apply()
} catch (error) {
  console.log("Error", error)
}

Transactions

import { cond } from "blazend";

try {
  await app.modules.db.transaction(trx => {
    const user = { id: 1, name: "John" }
    await trx.insert("users").doc(user).apply()
    await trx.update("users").where(cond("id", "==", 2)).set({ name: "Jack" }).apply()
  })
} catch (error) {
  console.log("Error", error)
}

Executing prepared queries

try {
  await app.modules.db.rawExec("select * from users where id = :userId", { userId: 1 }).apply()
} catch (error) {
  console.log("Error", error)
}
0.8.0

10 months ago

0.5.0

2 years ago

0.7.0

2 years ago

0.6.0

2 years ago

0.4.0

3 years ago

0.3.0

3 years ago

0.2.0

3 years ago

0.1.9

3 years ago

0.1.8

3 years ago

0.1.7

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago