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

mongoose-recommended-plugin

v1.0.2

Published

a mongoose plugin to easy implent a recommended system with only the mongoose schema

Downloads

2

Readme

mongoose-recommended-plugin

install

npm install mongoose-recommended-plugin

what is this library about? the scope of this library is allow mongoose users to implement in a simple way a content-based recommended system with mongoose schema, is pretty simple and in future i want to introduce a collaborative-filter method.

how work calculate similarities between mongoose entities on a single text field using tfidf and vector distance, for more search content-based systems descriptions

how to use this library after install in your project add the plugin in entity schema in wich you want similar entities:

import { RecommendedPlugin } from 'mongoose-recommended-plugin';

const mongooseSchema = {

    YOUR SCHEMA DEFINITION
    
    };


    // before generating the model 
    mongooseSchema.plugin(RecommendedPlugin);

after add the plugin to schema you can put in schema types two new field:

  • similar = indicate the text field to calculate similarity like name or description
  • minSimilarity = indicate the min percentage to mark another entity similar (es 0.1 is 10%)

an example:

{
        offerCode: {
            type: String,
            odinQFilter: true
        },
        discountCode: {
            type: String,
        },
        // make sure flace similar on a String field!
        discountDescription: {
            type: String,
            odinQFilter: true,
            similar: true,
            minSimilarity: 0.1
        },
        originalPrice: {
            type: Number
        },
        discountedPrice: {
            type: Number
        },
        discountPercentage: {
            type: Number
        },
        startDate: {
            type: Date
        },
        endDate: {
            type: Date
        },
        neverExpire: {
            type: Boolean,
            default: false
        },
        offerLink: {
            type: String
        },
}

after this on the basic schema you have 2 new methods that allow you to calculate similars and get it:

  • calculateSimilars
  • getSimilar

important before calling getSimilar you have to call calculateSimilars to calculate and save in the db the similars. we will see it now

now we have to call calculateSimilars to get and save into db the results (plugin will save results in a collection name: BASIC_COLLECTION_NAME+similarresults).

for using it i suggest using a schedulr like it:

import schedule from 'node-schedule';
import Offers from '../../api/offers/model';

const log = logger.child({ section: '\x1B[0;35mScheduler:\x1B[0m' });

export const start = function () {
    log.info('Starting...');

    schedule.scheduleJob('*/10 * * * * *',calculateSimilarsResult);

    log.info('Starting...', 'DONE');
};

async function calculateSimilarsResult(){
    await Offers.calculateSimilars();
}

this is an example of how calculate similars every 10 seconds, ut you can call it when you want and how you want.

after this we can call seconds method passing the _id of entity for wich we want similars:


await Offers.getSimilar('619d2d91eac832002d2f36de')

and thats all!

db format of plugin save


{ 
    "_id" : ObjectId("61a25cae646804e510d84f92"), 
    "relatedScore" : [
        {
            "id" : ObjectId("619d2d91eac832002d2f36de"), 
            "score" : 0.45293266622972733
        }
    ], 
    "entityId" : "619ac77c39dd6b002d1bd3bb", 
    "__v" : NumberInt(0)
}

for questions or contribute write at [email protected]