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

beer-sdk

v1.2.0

Published

Wrapper package Node.js app most used for backend services

Downloads

12

Readme

Beer-Sdk

Build Status codecov Codacy Badge CodeFactor Known Vulnerabilities PRs Welcome

Wrapper package Node.js app most used for backend services

This is just a collection list node js package most used. If you create node js from scratch you can avoid DRY (Don't Repeat Yourself) to configure like logger, cache, etc..

Features

Installation

npm install sdk@npm:beer-sdk --save

or

yarn add sdk@npm:beer-sdk

Usage Examples

Please attach this code to root/parent js code. See examples below on how to implement the Beer sdk.

Index.js or App.js

require('sdk');

*Note: this code just only called only once in root js project

only once import this code you can use sdk feature on all file js without confused with much config. It's Magic!!! :laughing:

Logger

//log example
sdkLog.trace('trace log')
sdkLog.debug('debug log')
sdkLog.info('info log')
sdkLog.warn('warn log')
sdkLog.error('error log')
sdkLog.fatal('fatal log')

Cache-Manager

// cache example
function getUser(id, cb) {
	setTimeout(function () {
		sdkLog.info('Returning user from slow database.');
		cb(null, { id: id, name: 'Bob' });
	}, 100);
}

var userId = 123;
var key = 'user_' + userId;

// Note: ttl is optional in wrap()
sdkCache.wrap(key, function (cb) {
	getUser(userId, cb);
}, { ttl: 5 }, function (err, user) {
	sdkLog.info(user);
})

// Second time fetches user from memoryCache
sdkCache.wrap(key,function (cb) {
	getUser(userId, cb);
}, function (err, user) {
	sdkLog.info(user);
});

Custom Mongoose Plugins

sdkMongplug is mongoose plugins that you can attach to any mongoose model schema.

const mongoose = require('mongoose');

const userSchema = mongoose.Schema(
{
    /* schema definition here */
},
{ timestamps: true }
);

userSchema.plugin(sdkMongplug.toJSON);
userSchema.plugin(sdkMongplug.paginate);

const User = mongoose.model('User', userSchema);

toJSON

The toJSON plugin applies transform to JSON format and removes field following __v, createdAt, updatedAt, and any schema path that has private: true replaces _id with id

paginate

The paginate plugin adds the paginate static method to the mongoose schema.

Adding this plugin to the User model schema will allow you to do the following:

const queryUsers = async (filter, options) => {
    const users = await User.paginate(filter, options);
    return users;
};

The filter param is a regular mongo filter.

The options param can have the following (optional) fields:

const options = {
    sortBy: 'name:desc', // sort order
    limit: 5, // maximum results per page
    page: 2, // page number
};

The plugin also supports sorting by multiple criteria (separated by a comma): sortBy: name:desc,role:asc

The paginate method returns a Promise, which fulfills with an object having the following properties:

{
    "results": [],
    "page": 2,
    "limit": 5,
    "totalPages": 10,
    "totalResults": 48
}

Environment Variables

Default Config Log

//full
SDK_LOG_RING_BUFFER_LIMIT=100
SDK_LOG_STDOUT_LEVEL=trace
SDK_LOG_DEBUG_STREAM_LEVEL=trace
SDK_LOG_RING_BUFFER_LEVEL=info
SDK_LOG_SLACK_LEVEL=trace
SDK_LOG_ROTATING_STREAM_LEVEL=trace
SDK_LOG_ROTATING_FILE_PATH=`${process.cwd()}/src/logger/${new  Date().toISOString()}.log`
SDK_LOG_ROTATING_FILE_PERIOD=1d
SDK_LOG_ROTATING_FILE_COUNT=3

//NODE_ENV = development
SDK_LOG_STDOUT_ENABLE=false
SDK_LOG_DEBUG_STREAM_ENABLE=true
SDK_LOG_ROTATING_FILE_ENABLE=false
SDK_LOG_SLACK_ENABLE=false
SDK_LOG_SLACK_WEBHOOK_URL=

//NODE_ENV = production
SDK_LOG_STDOUT_ENABLE=false
SDK_LOG_DEBUG_STREAM_ENABLE=true
SDK_LOG_ROTATING_FILE_ENABLE=false
SDK_LOG_SLACK_ENABLE=false
SDK_LOG_SLACK_WEBHOOK_URL=

Default Config Cache

// full
SDK_CACHE_MEMORY_MAX=1000
SDK_CACHE_MEMORY_TTL=60

SDK_CACHE_REDIS_HOST=127.0.0.1
SDK_CACHE_REDIS_PORT=6379
SDK_CACHE_REDIS_AUTHPASS=
SDK_CACHE_REDIS_DB=0
SDK_CACHE_REDIS_TTL=60
  
//NODE_ENV = development
SDK_CACHE_MEMORY_ENABLE=true
SDK_CACHE_REDIS_ENABLE=false
  
//NODE_ENV = production
SDK_CACHE_MEMORY_ENABLE=false
SDK_CACHE_REDIS_ENABLE=false
*Note: You can override this config with adding to .env your project using prefix SDK_

Contribution

Want to help improve this package? We take pull requests.

License

MIT

Free Software, Hell Yeah!