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

orange-dragonfly-orm

v0.7.42

Published

Database connection and SQL queries object models

Downloads

427

Readme

Orange Dragonfly ORM

One day Orange Dragonfly will become a NodeJS framework. For now I'm starting to publish its components.

This library is created to work with database and it also implements Active record pattern.

Installation

npm i orange-dragonfly-orm
npm i orange-dragonfly-orm-mysql # Or some other driver

How it works

Simple example which explains the idea

Include libraries

// This is how you include the library
const ORM = require("orange-dragonfly-orm")
// This is how you include MySQL driver 
const MySQLDriver = require("orange-dragonfly-orm-mysql")
// Just loading config
const config = require('./your-config.json')

Active records classes should be defined:

// Extend class for having generic features for records of the table "brand"
class Brand extends ORM.ActiveRecord {
  // Specifying relations
  static get available_relations () {
    return {
      // This makes records from table "car_models" with "brand_id" available for calling as "await brand_object.rel('models')" 
      'models': ORM.Relation.children(this, this.model('CarModel'))
    }
  }
}

// Extend class for having generic features for records of the table "car_model" 
class CarModel extends ORM.ActiveRecord {}

Database connector should be registered:

// This is the way how you provide information about DB to ORM library
ORM.AbstractQuery.registerDB(new MySQLDriver(config))
// Models should be registered, so they can be accessed via ActiveRecord.model(class_name_string)
// The main case when you need it is structure with separate file per model class (it can be some issues with dependencies)   
ORM.ActiveRecord
  .registerModel(Brand)
  .registerModel(CarModel)

This is how we work with the data:

const run = async () => {
  // Loading all records from table "brand"
  const brands = await Brand.all()
  // Iterate brands and load related models
  for (let brand of brands) {
    console.log(`Models of ${brand.data['brand_name']}: ${await brand.rel('models')}`)
  }
  return true
}

Library is supposed to be used inside async functions:

run()
  .then(res => console.log(`Done: ${res}`))
  .catch(e => console.error(e))
  .finally(() => ORM.AbstractQuery.releaseDB()) // If you need to close connection, close it

You can also find a bit more in example/example.js and in tests folder.