npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

konjection

v1.0.1

Published

Konjection is an abstraction and helper library for working Knex Query Build and the Objection ORM.

Downloads

4

Readme

Konjection

by Alex Merced

Konjection is an abstraction and helper library for working Knex Query Build and the Objection ORM.

Konject

If all starts with the Konjection function. It takes a config object and in that config object there is a property called 'knex' which should have the configurations for your database connection to pass to Knex. It will return the Db object (Knex Object), Model object (Objection Model Object with the Knex Object in it), and konModel function (Function for creating model with crud functions for a table). Make sure the appropriate database drivers are installed for you project.

const konject = require('konjection')

const config = {
    knex: {
  client: "pg",
  connection: {
    host: "localhost",
    port: "5432",
    user: "test5",
    password: "test5",
    database: "test5",
  },
  log: {
    warn(message) {
      console.log(message);
    },
    error(message) {
      console.log(message);
    },
    deprecate(message) {
      console.log(message);
    },
    debug(message) {
      console.log(message);
    },
  },
}
}

const [DB, Model, konModel] = konject(config)

konModel function

Takes a string that is the name of the table in your database and returns the model with the following starter functions, you can always add more.

Item.all() => return all items

Item.one(id) => return one item based on ID

Item.create(newItem) => takes in an object and creates a new record

Item.update(id, updatedIem) => takes an ID and updates it based on the updatedItem

Item.destroy(id) => destroys the particular record

Item.related(id, relationship) => pull the data from a particular relationship

Item.relate(sourceID, targetID, relationship) => make an item from source model relate to target item in related model

In the below code we use the konModel function and add a special function for non-id based queries.

relationships

You can pass in a second object as an argument, keys in this object include.

relationships: function that returns object with relationship mapping


const konject = require("konjection");

try {
  // CONFIG OBJECT WITH KNEX CONNECTION INFO
  const config = {
    knex: {
      client: "pg",
      connection: {
        host: "localhost",
        port: "5432",
        user: "test5",
        password: "test5",
        database: "test5",
      },
      log: {
        warn(message) {
          console.log(message);
        },
        error(message) {
          console.log(message);
        },
        deprecate(message) {
          console.log(message);
        },
        debug(message) {
          console.log(message);
        },
      },
    },
  };

  //RUNNING konject
  const [DB, Model, konModel, maker] = konject(config);

  // Create Models, Pet model has a belongs to relationship with owner
  const Owner = konModel("owners");
  const Pet = konModel("pets", {
    relationships: () => {
      //in this part of the function require any models to avoid circular imports
      return {
        owner: {
          relation: Model.BelongsToOneRelation,
          modelClass: Owner,
          join: {
            from: "owners.id",
            to: "pets.owner_id",
          },
        },
      };
    },
  });

  //Executing database commands in async function
  const stuff = async () => {
    //Create Owners Table
    await maker.createTable("owners", function (t) {
      t.increments("id");
      t.string("name");
      t.integer("age");
      t.integer("pet_id").references("pets.id");
    });

    //Create Pets table
    await maker.createTable("pets", function (t) {
      t.increments("id");
      t.string("name");
      t.integer("age");
      t.integer("owner_id").references("owners.id");
    });

    //Create Some Owners and Pets
    await Owner.create({ name: "Bob", age: 55 });
    await Owner.create({ name: "Steve", age: 55 });
    await Owner.create({ name: "Josie", age: 55 });

    await Pet.create({ name: "Spot", age: 5 });
    await Pet.create({ name: "Mittens", age: 5 });
    await Pet.create({ name: "Butch", age: 5 });

    //Grab pet with id one and log it
    const pet = await Pet.one(1);
    console.log(pet);

    //Relate pet with id 1 with owner with id 1
    await Pet.relate(1, 1, "owner");
    //Log the owner related to pet 1
    console.log(await Pet.related(1, "owner"));
    //log all pets
    console.log(await Pet.all());
    //log all pets, populate owner data
    console.log(await Pet.query().withGraphFetched("owner"));
  };

  stuff();
} catch (error) {
  console.log(error);
}