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

query-orm

v1.2.0

Published

Loopback like ORM built to work with any node.js framework.

Downloads

11

Readme

query-orm

Loopback like ORM built to work with any node.js framework.

npm version

Installation

npm i query-orm

Usage

app.js

const QueryORM = require('query-orm');

const app = new QueryORM({
  appRoot: DIRNAME,
  modelConfig: './model-config.json',
  dataSources: './datasource.json'
});

app.on('model-init', (data) => {
  console.log(data);
});

app.on('error', (error) => {
  console.log(error);
});

async function createUser (data) {
  const createdUser = await app.models.User.create(data);
  return createdUser;
}

createUser({
  firstname: 'Bharath',
  lastname: 'Reddy',
  username: 'bharathreddy',
  password: 'Test@1234'
}).then((createdUser) => {
  console.log(createdUser)
}).catch((error) => {
  console.log(error);
});

app.models.User.find({
  where: {
    username: 'bharathreddy'
  }
}).then((result) => {
  console.log(result)
}).catch((error) => {
  console.log(error);
});

datasources.json

{
  "testdatasource": {
    "name": "testdatasource",
    "settings": {
      "indexName": "testindex",
      "isAlias": false,
      "isPattern": false,
      "mappings": {
        "properties": {
          "docType": {
            "type": "keyword"
          },
          "userId": {
            "type": "keyword"
          },
          "username": {
            "type": "keyword"
          },
          "firstname": {
            "type": "keyword"
          },
          "lastname": {
            "type": "keyword"
          },
          "password": {
            "type": "keyword"
          },
          "created": {
            "type": "date"
          },
          "updated": {
            "type": "date"
          }
        }
      },
      "aliases": {},
      "indexSettings": {
        "number_of_shards": 1,
        "number_of_replicas": 2
      },
      "createIndex": true,
      "updateMapping": true,
      "version": 7,
      "defaultLimit": 200,
      "configuration": {
        "nodes": [
          "https://localhost:9200"
        ],
        "requestTimeout": 30000,
        "pingTimeout": 3000,
        "auth": {
          "username": "admin",
          "password": "admin"
        },
        "agent": {
          "maxSockets": 1,
          "keepAlive": false
        },
        "ssl": {
          "rejectUnauthorized": false
        },
        "sniffInterval": 10000
      }
    },
    "connector": "query-orm-connector-elastic",
    "localConnector": false
  }
}

model-config.json

{
  "directory": "./models",
  "models": {
    "User": {
      "dataSource": "testdatasource"
    }
  }
}

models/user.json

{
  "name": "User",
  "plural": "users",
  "validations": {},
  "id": {
    "property": "userId",
    "generated": true
  },
  "schema": {
    "type": "object",
    "properties": {
      "userId": {
        "type": "string"
      },
      "username": {
        "type": "string"
      },
      "password": {
        "type": "string"
      },
      "firstname": {
        "type": "string"
      },
      "lastname": {
        "type": "string"
      },
      "created": {
        "type": "string",
        "format": "date-time"
      },
      "updated": {
        "type": "string",
        "format": "date-time"
      }
    },
    "required": [
      "firstname",
      "lastname",
      "userId",
      "username",
      "password"
    ]
  },
  "relations": {}
}

Model Methods

find

This method is for fetching data from Model's dataSource based on given query filter.

Example:

await app.models.User.find({
  where: {
    firstname: 'bharath'
  },
  limit: 100,
  skip: 10
})

findOne

This method is for fetching only one matched record from Model's dataSource based on given query filter.

limit and skip options will not work here.

Example:

await app.models.User.findOne({
  where: {
    firstname: 'bharath'
  }
})

findById

This method is for fetching the record from Model's dataSource based on given id parameter.

limit and skip options will not work here.

Example:

await app.models.User.findById('id123');

count

This method is for fetching the count from Model's dataSource based on given query filter.

limit and skip options will not work here.

Example:

await app.models.User.count({
  firstname: 'bharath'
});

create

This method is for create a record in Model.

Example:

await app.models.User.create({
  firstname: 'bharath'
});

updateById

This method is for updating a record matching the given id with attributes object provided.

Example:

await app.models.User.updateById('id123', {
  firstname: 'bharath'
});

updateByQuery

This method is for updating multiple records matched for given query with attributes object provided.

Example:

await app.models.User.updateByQuery({
  and: [{
    lastname: 'reddy'
  }, {
    firstname: 'test'
  }]
}, {
  firstname: 'bharath'
});

deleteById

This method is for deleting a record that matches the given id.

Example:

await app.models.User.deleteById('id123');

deleteByQuery

This method is for deleting multiple records that matches the given query.

Example:

await app.models.User.updateByQuery({
  and: [{
    lastname: 'reddy'
  }, {
    firstname: 'test'
  }]
});

Querying Data

Base Filters

{
  "where": {},
  "limit": 100,
  "skip": 2, // offset
  "searchafter": [], // optional for pagination
  "order": [], // [] or ""
  "fields": [], // [] or ""
  "include": [], // [] or "" for relations
}

where is the main query attribute. It supports and and or queries.

Relations

Connectors

OperationalHooks

before save

This hook will be triggered before execute of methods (create, updateById and updateByQuery).

Example:

  //create
  Model.observe('before save', async (instance) => {
    instance.foo = 'bar1';
    return Promise.resolve();
  })
  
    //update
  Model.observe('before save', async (data) => {
    // data.id, data.attributes, data.where are available w.r.t method
    return Promise.resolve();
  })

after save

This hook will be triggered after execute of methods (create, updateById and updateByQuery).

Example:

  Model.observe('after save', async (ctx) => {
    //ctx.instance will be result and isNewInstance is also available on ctx
    return Promise.resolve();
  })

before delete

This hook will be triggered before execute of methods (deleteById and deleteByQuery).

Example:

  Model.observe('before delete', async (data) => {
    // data.id, data.attributes, data.where are available w.r.t method
    return Promise.resolve();
  })

after delete

This hook will be triggered after execute of methods (deleteById and deleteByQuery).

Example:

  Model.observe('after delete', (ctx) => {
    //ctx.instance will be result
    return Promise.resolve();
  })