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

normaly

v0.0.38

Published

Network distributed Object Relation Model

Downloads

1,222

Readme

Normaly

Network distributed Object Relation Model. Do query and sql operations across network multiple database and integrate REST API as SQL interface.

Purpose

In data management and normalization projects, we often met the need of using model meta data either in backend, when etracting the data from a source, and in front end, when presenting data to user and handling diffent columns type with appropriate functions. Traditional Object Relation Model often works in backend, leaving perhaps few freedom in data query, and produce structured objects (with no meta data) to send across the network response; then this object is received by client which should interpret data with another frontend-model: this cause to repeat structures of metadata and to increase the probability of errors. the purpose of normal is to have a single Data Relation and Definition Model across tiers.

How to install

npm install normaly

Examples

Initialize the store

/**File: models/store.js
 * Creates a store, a collection of all models.
 * In this example a Knex db store is used.
 */

// includes the normaly store and the knex store
const { store, KdbStoreHost } = require('normaly');
// includes your knex connection definition 
const knex = require('../db/knex');

// Initializes store with your knex db connection
store.setup(new KdbStoreHost(knex));

// exports the store object
module.exports = store;

Creates the User model

/**File: models/User.js
 * Creates a simple model of User based on a db table 
 * called user_t.
 */
const store = require('./store');

/**User model is a simple table with 
 * domain name and name
 * 
 */
var User = store.entity('User', (model) => {

    // there is a table called 'user_t' on DB
    model.source('user_t');

    // defines the columns/fields of this model
    model.id('id');
    model.label('domainName')    // domainName is used as 'label' for this entity
        .source('domain_name');     // domain_name is the name of the column on the table

    model.string('name');          // name of user
});

module.exports = User;

Creates the Project model

/**File: models/Project.js
 * Creates a model for Projects.
 * There is a foreign key on project_t on column id_user to user_t table.
 */
const store = require('./store');
const User = require('./User');

var Project = store.entity('Project', model => {
  // there is a table called 'project_t' on DB
  model.source('project_t');

  // model.id( 'id' );      // optional, primary key is 'id' by default
  model.label('name');      // the label field can be used to briefly stringify the project

  model.string('name');         // specifies the name field (optional)
  model.string('description');  // description

  model.objectLink(User)      // Links to User model
    .source('id_user');     // specifies the foreign key column on table project_t to user_t
});

module.exports = Project;

Defines a route in node express

/**File: routes/projectRoute.js
 * queries the project table, 
 * filtering for those project of the given user passed to url
 */
const Project = require('../models/Project');
const User = require('../models/User');
const projectRoute = require('express').Router();

projectRoute.route('/project/:username?')
    .get((req, res) => {

        // queries the project table, 
        // filtering for those project of the given user passed to url.
        // if no username is specified, using .where( false ) all project will be 
        // selected and returned.
        return Project
            .select('*')      // selects all fields
            .joinAllRelated()   // all related table are join in order to filtering
            .where(req.params.username && User.domainName.equals(req.params.username))
            .then((data) => {

                return res.send(data);
            })
            .catch((err) => {

                console.error(err);
                res.status(500).json({ error: true, data: { message: err.message } });
            });

    });

module.exports = projectRoute;