1.0.1 • Published 4 years ago

knex-db-access v1.0.1

Weekly downloads
1
License
ISC
Repository
-
Last release
4 years ago

knex-db-access

An easy way to access your SQL database without queries

Usage


const dbConnection = require("./directoryName");
const DBAccess = require("knex-db-access");
const db = new DBAccess(dbConnection, "tableName");

Your dbConnection file could look like this:

Your db connection file has to be a connection to your knex init file.

const knex = require("knex");

require("dotenv").config();

const knexConfig = require("../knexfile");

const env = process.env.NODE_ENV;

module.exports = knex(knexConfig[env]);

Methods Available At Default

Tip: All methods return a promise!

find

Returns all records in table

findBy

returns the first record where query is present

  • takes an object with the key and value you want to match Example:
db.findBy({ username: req.body.username });

create

returns the data created on success

db.create(dataObject).then((result) => {
  // result === dataObject
});

delete

Required: id passed to delete method

returns true on success, false on failure an error message printed to the console

const deleted = await db.delete(idNumber);
if (deleted) {
  // success!
}

deleteBy

Required: object passed to delete method

returns true on success, false on failure an error message

const deleted = await db.deleteBy({ username: req.body.username });
if (deleted) {
  // success!
}