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

@myorm/myorm

v0.4.15

Published

Provides an easy way to interact with a database by creating contexts connected to tables in your databases.

Downloads

46

Readme

myorm-logo-text-description-640x283

MyORM

The syntax-friendly, type-safe, and easy to use object relational mapping model for your database needs.

Documentation

Below are a few brief non-descriptive examples of some of the features MyORM has to offer using the mysql-adapter.

If you'd like to read about all of the features and what MyORM has to offer, you can read more documentation here

Query

import { MyORMContext } from '@myorm/myorm';
import { adapter, createMySql2Pool } from '@myorm/mysql-adapter';

const pool = createMySql2Pool({ 
    database: "vehicles",
    host: "localhost", 
    user: "root",
    password: "root" 
});
const ctx: MyORMContext<Car> = new MyORMContext(adapter(pool), "Car");

const cars = await ctx
    .where(m => m.Make.equals("Ford")
        .and(m => m.Color.equals("Red")
            .or(m => m.Make.equals("Toyota"))
            .and(m => m.Color.equals("Blue"))))
    .groupBy((m, { avg }) => [
        m.Make, 
        m.Model, 
        m.Color, 
        avg(m.Mileage)
    ])
    .sortBy(m => [
        m.Make,
        m.Model.desc()
    ])
    .skip(2)
    .take(2)
    .select();

console.log(cars);
/** prints (results are not from a legitimate database)
 * {
 *   Make: "Ford",
 *   Model: "Focus",
 *   Color: "Red",
 *   $avg_Mileage: 49999
 * },
 * {
 *   Make: "Toyota",
 *   Model: "Tacoma",
 *   Color: "Blue",
 *   $avg_Mileage: 30000
 * }
 */ 

Insert

Inserting a row when the table has a defined identity key. (A primary key that automatically increments)

import { MyORMContext } from '@myorm/myorm';
import { adapter, createMySql2Pool } from '@myorm/mysql-adapter';

const pool = createMySql2Pool({ 
    database: "vehicles",
    host: "localhost", 
    user: "root",
    password: "root" 
});
const ctx: MyORMContext<Car> = new MyORMContext(adapter(pool), "Car");

let newCar = {
    Make: "Chevy",
    Model: "Malibu",
    Color: "Yellow",
    Mileage: 15,
    MPGHwy: 38.2,
    MPGCity: 29.9,
    Year: 2020
};

[newCar] = await ctx.insert(newCar);

console.log(newCar);

/** prints (results are not from a legitimate database)
 * {
 *   Id: 1000, // automatically assigned
 *   Make: "Chevy",
 *   Model: "Malibu",
 *   Color: "Yellow",
 *   Mileage: 15,
 *   MPGHwy: 38.2,
 *   MPGCity: 29.9,
 *   Year: 2020
 * }
 */ 

Update

Updating a row implicitly when a primary key is already defined in the record being updated.

import { MyORMContext } from '@myorm/myorm';
import { adapter, createMySql2Pool } from '@myorm/mysql-adapter';

const pool = createMySql2Pool({ 
    database: "vehicles",
    host: "localhost", 
    user: "root",
    password: "root" 
});
const ctx: MyORMContext<Car> = new MyORMContext(adapter(pool), "Car");

let newCar = {
    Make: "Chevy",
    Model: "Malibu",
    Color: "Yellow",
    Mileage: 15,
    MPGHwy: 38.2,
    MPGCity: 29.9,
    Year: 2020
};

[newCar] = await ctx.insert(newCar);
console.log(newCar.Id); // prints '1000'
newCar.MPGCity = 30.1;

// update by Primary Key, if one exists on object and table.
let n = await ctx.update(newCar);

console.assert(n > 0); // should pass

Updating a row explicitly using a WHERE clause.

// update by WHERE clause and callback using a proxy set intercept.
n = await ctx
    .where(m => m.Id.equals(1000))
    .update(m => {
        m.MPGCity = 30.3;
    });

console.assert(n > 0);

// alternatively and similarly, an object can be returned from the callback. 
// NOTE: (if both are specified, then the returned object takes precedence)
n = await ctx
    .where(m => m.Id.equals(1000))
    .update(m => ({
        MPGCity: 30.3
    }));

console.assert(n > 0);

Delete

Deleting a row implicitly when a primary key is already defined in the record being deleted.

import { MyORMContext } from '@myorm/myorm';
import { adapter, createMySql2Pool } from '@myorm/mysql-adapter';

const pool = createMySql2Pool({ 
    database: "vehicles",
    host: "localhost", 
    user: "root",
    password: "root" 
});
const ctx: MyORMContext<Car> = new MyORMContext(adapter(pool), "Car");

newCar = {
    Make: "Chevy",
    Model: "Malibu",
    Color: "Yellow",
    Mileage: 15,
    MPGHwy: 38.2,
    MPGCity: 29.9,
    Year: 2020
};

[newCar] = await ctx.insert(newCar);

// delete by Primary Key, if one exists on object and table.
let n = await ctx.delete(newCar);

console.assert(n > 0);

Deleting a row explicitly using a WHERE clause.

// delete by WHERE clause.
let n = await ctx
    .where(m => m.Id.equals(1000))
    .delete();

console.assert(n > 0);

Other

Saving a state of a context as a view-like variable context.

import { MyORMContext } from '@myorm/myorm';
import { adapter, createMySql2Pool } from '@myorm/mysql-adapter';

const pool = createMySql2Pool({ 
    database: "vehicles",
    host: "localhost", 
    user: "root",
    password: "root" 
});
const ctx: MyORMContext<Car> = new MyORMContext(adapter(pool), "Car");

// duplicate context, that has no effect on `ctx`, but is in a state with a `WHERE` clause being applied to always filter for the `Make` column being equal to "Ford" 
const fords = ctx.where(m => m.Make.equals("Ford"));

const fordsWithMoreThan10kMileage = await fords.where(m => m.Mileage.greaterThan(10000)).select();
const fordsWithLessThan10kMileage = await fords.where(m => m.Mileage.lessThan(10000)).select();

Programmatic defaults of columns when their key does not exist in their javascript object being inserted.

import { MyORMContext } from '@myorm/myorm';
import { adapter, createMySql2Pool } from '@myorm/mysql-adapter';

const pool = createMySql2Pool({ 
    database: "vehicles",
    host: "localhost", 
    user: "root",
    password: "root" 
});
const ctx: MyORMContext<Car> = new MyORMContext(adapter(pool), "Car");

// upon insertion, a proxy will detect if the property already exists on `m`, if so, then the value will not be set.
// Otherwise, the set variables in the callback will be defaulted to the property.
ctx.default(m => {
    m.Mileage = 0;
    m.MPGCity = 25;
    m.MPGHwy = 35;
    m.Year = 2023;
});

let newCar = {
    Make: "Chevy",
    Model: "Malibu",
    Color: "Yellow",
};

let usedCar = { 
    Make: "Toyota", 
    Model: "Tacoma", 
    Color: "Grey", 
    Mileage: 32000, 
    Year: 2021 
};

const [chevyMalibu, toyotaTacoma] = await ctx.insert([newCar, usedCar]);

console.assert(chevyMalibu.Color === "Yellow" && chevyMalibu.Mileage === 0 && chevyMalibu.MPGCity === 25 && chevyMalibu.MPGHwy === 35 && chevyMalibu.Year === 2023);

console.assert(toyotaTacoma.Color === "Grey" && toyotaTacoma.Mileage === 32000 && toyotaTacoma.MPGCity === 25 && toyotaTacoma.MPGHwy === 35 && toyotaTacoma.Year === 2021);

Adapters

Below is a list of MyORM supported adapters

MySQL

Connect MyORM to your MySQL database.

MySQL Adapter

MSSQL (Microsoft SQL)

work in progress

Connect MyORM to your MSSQL database.

MSSQL Adapter

SQLite

work in progress

Connect MyORM to a SQLite file database.

SQLite Adapter

POSTgres

work in progress

Connect MyORM to a POSTgres database.

POSTgres Adapter

JSON

Connect MyORM to a JavaScript object that mimics a database.

POSTgres Adapter

Plugins and other supported material

Below is a list of supported plugins that can be used with various applications.

GraphQL

Generate Root Query and Mutation types through MyORM for instant use in your GraphQL API.

GraphQL Plugin