1.0.38 • Published 2 years ago

baby-orm v1.0.38

Weekly downloads
-
License
Apache 2
Repository
github
Last release
2 years ago

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(['test@testmail.com', 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`,
    ['test@testmail.com', 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: "m.scofield@prison.break"
    })
    .then(result => {
        console.log("User created", result);
    })
    .catch(err => {
        console.error(err);
    });
1.0.38

2 years ago

1.0.37

2 years ago

1.0.36

3 years ago

1.0.35

3 years ago

1.0.34

3 years ago

1.0.33

3 years ago

1.0.32

3 years ago

1.0.29

3 years ago

1.0.31

3 years ago

1.0.30

3 years ago

1.0.26

3 years ago

1.0.25

3 years ago

1.0.24

3 years ago

1.0.23

3 years ago

1.0.28

3 years ago

1.0.27

3 years ago

1.0.22

3 years ago

1.0.19

3 years ago

1.0.21

3 years ago

1.0.20

3 years ago

1.0.18

3 years ago

1.0.17

3 years ago

1.0.16

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.0

3 years ago