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

baby-orm

v1.0.38

Published

BabyORM is a tiny NodeJS ORM for Postgresql

Downloads

86

Readme

Baby ORM - Tiny NodeJS ORM for Postgresql

Installation

Install node module

npm i -S baby-orm

Scripts

If you want to use migrations scripts, add this lines in your package.json

"scripts": {
    "babyorm:create": "node ./node_modules/baby-orm/bin/create",
    "babyorm:migrate": "node ./node_modules/baby-orm/bin/migrate"
}

To use scripts :

npm run babyorm:create filename [table_name] [create|alter]
npm run babyorm:create help

npm run babyorm:migrate

Env variables

You can create a .env file if you are not in Production mode. Add the following lines in it :

## DATABASE
PGUSER=my_user
PGHOST=127.0.0.1
PGPASSWORD=secretpassword
PGDATABASE=my_database
PGPORT=5432

BABYORM_BASE_PATH="/path/to/your/project"
BABYORM_DATABASE_DIR=database
BABYORM_MODELS_DIR=src/models

Make query

const { Query } = require("baby-orm");
let query = new Query();
query.setQuery(`SELECT * FROM users WHERE email = $1 AND valid = $2`);
query.setParams(['[email protected]', true]);
query.execute()
    .then(result => {
        console.log(result)
    })
    .catch(err => {
        console.error(err)
    });

It is possible to create directly in constructor

const { Query } = require("baby-orm");
let query = new Query(
    `SELECT * FROM users WHERE email = $1 AND valid = $2`,
    ['[email protected]', true]
);
query.execute()
    .then(result => {
        console.log(result)
    })
    .catch(err => {
        console.error(err)
    });

Create Model

Create file Model in correct directory (default src/models).

Example user.js for a User model :

const { Helpers } = require("baby-orm");
const UserModel = {
    config: {
        table: "users",
        use_autoincrement: false, // default true to have numeric ID, otherwise unique string
        timestamps: true, // if exists created_at and updated_at fields in your table
        soft_delete: true, // not really delete in DB, just fill deleted_at field if exist in your table
        fillable_fields: ["firstname", "lastname", "email"],
        hidden_fields: [], // fields not returns when you load a row
        validations: {
            firstname: ["string", "maxLength:64"],
            firstname: ["string", "maxLength:128"],
            email: ["email", "maxLength:128"],
        },
        relations: [] // load another model linked with this one
    },
    fields: {
        id: null,
        firstname: "John",
        lastname: "DOE",
        email: null,
    },
    methods: {
        getFullname: () => {
            return (
            Helpers.ucfirst(UserModel.fields.firstname) +
            " " +
            UserModel.fields.lastname.toUpperCase()
            );
        },
    },
};

module.exports = UserModel;

Use ORM

const { ORM } = require("baby-orm");
let User = ORM.model('user')
User.create({
        firstname: "Mickael",
        lastname: "Scofield",
        email: "[email protected]"
    })
    .then(result => {
        console.log("User created", result);
    })
    .catch(err => {
        console.error(err);
    });