0.0.13 • Published 3 years ago

pyniedb v0.0.13

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

PynieDB

thank you for use

Basics commands

  • pyniedb - connect(dbName: string, options: { path: string }) - Model(modelName: string, schema: SchemaObject)
  • Schema(schemaData: object)
  • Models - Model.create(data: object) - Model.findAll() - Model.find(param: string, value: any) - Model.delete(param: string, value: any)

Usages

  1. setting up

    const { default: pyniedb } = require('pyniedb');
    
    pyniedb.connect('pyniedb_usage', {
        path: `${__dirname}/database` // your preferred path
    });
    
    module.exports = pyniedb;
  2. creating model

    const { Schema } = require('pyniedb');
    const pyniedb = require('../config/database');
    
    const UserSchema = new Schema({
        name: {
            type: 'string',
            unique: false,
        },
        email: {
            type: 'string',
            unique: true,
        },
        active: {
            type: 'boolean',
            default: false, // setting 'default' and then the value
            unique: false,
        }
    });
    
    const User = pyniedb.Model('User', UserSchema);
    
    module.exports = User;
  3. consuming Model by controller

    const User = require('../models/User'); // the created model
    
    module.exports = {
        index: async (req, res) => { // list all Users
            const users = await User.findAll();
            res.json(users);
        },
    
        create: async (req, res) => { // create User
            const user = await User.create(req.body);
            res.status(201).json(user);
        },
    }
  4. adding Relation on Schema

    Address: {
        type: 'number',
        relation: {
            modelName: 'Addresses'
        },
    }

    create the Address Model (like item 2.) and then add data to it.

    Address.create({ city: 'LAS VEGAS' });

    then just create an user with new Relation

    User.create({ name: 'Test User', email: "test@example.com", Address: 1 });
  5. fetching User results:

    {
        "id": 1,
        "name": "Test User",
        "email": "test@example.com",
        "Address": { "id": 1, "city": "LAS VEGAS" },
        "active": true
    }
  6. Adding name in Schema relation fetch:
    address_id: {
        type: 'number',
        relation: {
            name: 'address',
            modelName: 'Addresses'
        },
    }
    User.create({ name: 'Test User', email: "test@example.com", address_id: 1 });
    {
        "id": 1,
        "name": "Test User",
        "email": "test@example.com",
        "address": { "id": 1, "city": "LAS VEGAS" }, // name "address" on schema
        "active": true
    }
0.0.13

3 years ago

0.0.10

3 years ago

0.0.11

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

1.0.0

3 years ago