Konjection — ORM Helper using Knex and Objection

What is Konjection?

Setup

DB Connection

const konject = require("konjection");const dbconfig = {
knex: {
client: "pg",
connection: {
host: "localhost",
port: "5432",
user: "test5",
password: "test5",
database: "test5",
},
},
};
const [knex, Model, konModel, maker] = konject(dbconfig);module.exports = {
knex,
Model,
konModel,
maker
};

Creating a Model

const {konModel} = require("./db.js")const Pets = konModel("pets")module.exports = Pets
const {konModel} = require("./db.js")const Owners = konModel("owners", {
relationships: () => {
const pets = require("./pets")
return {
pets: {
from: "owners.pet_id",
to: "pets.id",
model: pets
}
}
}
})
module.exports = Owners

Model.all

console.log(await Owners.all())
console.log(await Pets.all())

Model.one

console.log(await Owners.one(1))
console.log(await Pets.one(1)

Model.one

console.log(await Owners.one(1))
console.log(await Pets.one(1))

Model.create

console.log(await Owners.create({name: "Bob", age: 55}))
console.log(await Pets.create({name: "Spot", age: 5}))

Model.update

console.log(await Owners.update(1, {name: "Bob II", age: 55}))
console.log(await Pets.update(1, {name: "Spot II", age: 5}))

Model.destroy

console.log(await Owners.destroy(1))
console.log(await Pets.destroy(1))

Model.relate

console.log(await Owners.relate(1,1,'pets'))

Model.related

console.log(await Owners.related(1,'pets'))

Bottom Line

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store