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

simplymongo

v2.1.2

Published

A super simple MongoDB wrapper. Built for quick database building.

Downloads

33

Readme

Simply Mongo is the simplest way to use MongoDB without knowing how to use MongoDB.

Store your client data in collections and fetch and modify data easily.

Originally created for the https://altv.mp/ community.

⌨️ Learn how to script for alt:V

💡 Need a Roleplay Script? Try Athena!

Installation

Prerequisites

Install

$ npm install simplymongo

Starting Usage

Here are some generalized steps for getting started.

  1. Add callback for ready statement.
  2. Create new sm.Database.
  3. Connection establishment will trigger ready statement.
  4. Ready statement imports rest of the files.
  5. Establish database instances where needed with sm.fetchDatabaseInstance().

Additional information can be found below. 👇🏻

🔽 Importing

I recommend importing the entire library of simplymongo.

import * as sm from 'simplymongo';

🔗 Establish Connection

After a connection is established. You may use sm.onReady to listen for the ready status.

import * as sm from 'simplymongo';

new sm.Database('mongodb://localhost:27017', 'databasename', ['accounts', 'vehicles', 'characters']);

// OR
new sm.Database('mongodb://localhost:27017', 'databasename', ['accounts', 'vehicles', 'characters'], 'user', 'pass');

🤝🏼 Handling Connection to Simply Mongo

There is an onReady callback register that will assist you establishing Database connection before loading other files.

Register callback with this function, then import the rest of your files.

main.js

import * as sm from 'simplymongo';

sm.onReady(loadRestOfCode);

async function loadRestOfCode() {
    await import('somefile');
}

somefile.js

import * as sm from 'simplymongo';

const db = sm.getDatabase();

// OR
// No Params Required if fetching established db.
const db = new sm.Database();

♻️ Fetching Database Instance after Connection

This no longer requires an wait.

Use sm first and load the rest of your files.

Rest of your files will automatically fetch the correct instance.

somefile.js

import * as sm from 'simplymongo';

const db = sm.getDatabase();

// OR
// No Params Required if fetching established db.
const db = new sm.Database();

📝 Regular Usage

These are just some general examples of deleting, updating, etc.

Think of this as your basic CRUD lesson.

This example is targetting usage with https://altv.mp/

Check ./example/example.js for more explanation.

import * as sm from 'simplymongo';

const db = sm.getDatabase();

async function someAsyncFunction(player) {
    // We can now use player.data.id to update any changes to this document.
    player.data.money = 50000;
    await db.updatePartialData(player.data.id, { money: player.data.money }, 'accounts');
}

// Let's assume we're looking for this player's id now.
async function someUsernamePassed(player) {
    // Returns a document with { _id, username, bank }
    player.data = await db.insertData({ username: 'johnny', bank: 0 }, 'accounts', true);

    // Find the username in the database
    const matches = await db.fetchAllByField('username', player.data.username, 'accounts');

    // Check if it exists. Create it if it doe snot.
    if (matches.length <= 0) {
        // Account does not exist. Create it.
        player.data = await db.insertData({ username: 'johnny2' }, 'accounts', true);
    } else {
        // Account exists. Assign to player object.
        player.data = matches[0];
    }

    // Create money element and update bank element.
    player.data.money = 50000;
    player.data.bank = 10;

    // Update two entries for a player.
    await db.updatePartialData(player.data.id, { money: player.data.money, bank: player.data.bank }, 'accounts');

    // Update all entries for a player.
    await db.updatePartialData(player.data.id, { ...player.data }, 'accounts');

    // Delete the account!
    await db.deleteById(player.data._id.toString(), 'accounts');

    // Fetch all accounts in the accounts collection.
    const accounts = await db.fetchAllData('accounts');

    for (let i = 0; i < accounts.length; i++) {
        const account = accounts[i];
        console.log(account);
    }
}