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

shop-manager

v0.0.61

Published

Shop manager to manage items in your project with MySQL

Downloads

4

Readme

Shop-manager

Shop manager to manage items in your project with MySQL

Installation

To install it, run npm install shop-manager

Documentation

| Content | Section | |:-------:|:-------:| |Initiation| start | | Propreties | propreties list | | All methods | methods list | | Types | types list |

Initiation

Here is how to start the manager

const { ShopManager } = require('shop-manager');
const { createConnection } = require('mysql')

const database = createConnection({
    // Database credits
})

const manager = new ShopManager(database);

Propreties

Here are the propreties you can access

Items

You can get all the items cached

manager.items

Is is a Collection of items by their id

Inventories

You can get all the inventories

manager.inventories

It is a Collection of Collection of inventories by user id by guild id (the main collection is by guild id and the sub collections are users id)

Methods

guildItems

Get all the items of a guild

manager.guildItems(guild);
manager.guildItems(interaction);
manager.guildItems(member);
manager.guildItems('1234');

Parameter :

Returns a collection of items by the id of the guild, containing only guild's items

addItem

Add an item to the guild's items

manager.addItem({
    guild: '1234',
    type: 'string',
    name: "name of the item",
    content: "super string content to show",
    quantity: 0, // The item will be infinite
    price: 10000
});

manager.addItem({
    guild: message.guild,
    type: 'role',
    name: "Admin role",
    content: "141592653589713238",
    price: 10000,
    quantity: 2 // Only 2 will be available
});

Returns a promise, either string typed, with "no reply from database" as content, when the database didn't reply, and the item, when the request is done and the item is added to the list ( Promise<'no reply from database' | item> )

Parameter :

removeItem

Remove an item from the manager, using its ID

manager.removeItem(17); // Remove the item with the ID 17

Returns nothing ( void )

Parameters :

  • id : number, corresponding to an ID of the items

addInventory

Add one or more items to a user's inventory

const item = manager.items.first();

manager.addItem(message.guild, message.author, item, 3); // Add 3 items "item"
manager.addItem(message.guild, message.author, item); // Add 1 item "item"

Returns a true Promise ( Promise<true> )

Parameters :

removeInventory

Remove one or more items from a user's inventory

const item = manager.items.first();

manager.removeInventory(message.guild, message.author, item, 2); // Remove 2 items "item"
manager.removeInvetory(message.guild, message.author, item); // Remove 1 item "item"

Returns a true Promise ( Promise<true> )

Parameters :

getInventory

Get the inventory of an user in a guild

manager.getInventory(message.guild, message.author);

Return an inventory object

Parameters :

buyItem

Removes an item from the shop and add it in the inventory of the user

manager.buyItem(message.guild, message.author, 16); // Buy the item with the id 16
manager.buyItem(message.guild, message.author, 18, 6); // Buy 6 items with ID 18

Returns a promise, returning either 'no item' if no item is found in the cache. Returns nothing buyable if there is no item remaining ( remaining proprety of items equals 0). Returns true when everything is done

Parameters :

updateQuantity

Update the quantity of an item ( quantity proprety of items )

manager.updateQuantity(message.guild, 14, 8); // Set the quantity of item with 14 ID to 8

Returns a promise with no item, item is infinite or true

When no item is found with the ID, no item is the value of the promise

When the item to edit is infinite ( when its quantity is 0 ), the value is item is infinite

Returns true when everything is done

Promise<'no item' | 'item is infinite' | true>;

Parameters :

updateRemaining

Update the remaining quantity of an item ( remaining proprety of items )

manager.updateRemaining(message.guild, 9, 2); // Set the remaining quantity of item with 9 ID to 2

Returns a promise with no item, item is infinite or true

When no item is found with the ID, no item is the value of the promise

When the item to edit is infinite ( when its quantity is 0 ), the value is item is infinite

Returns true when everything is done

Promise<'no item' | 'item is infinite' | true>;

Parameters :

setInfinite

Make an item infinite or not

manager.setInfinite(message.guild, 7, true); // Make item with ID 7 infinite
manager.setInfinite(message.guild, 4, false, 10); // Make item with ID 4 not infinite, with quantity 10

When you set an item to not infinite, you have to specify value parameter

Returns a Promise with no item, set as not infinite, but no value or true

Returns Promise<'no item'> when no item is found with the id

Returns Promise<'set as not infinite'> when you set an item to not infinite, but you don't specify the value parameter

Returns Promise<true> when everything is done

Parameters :

  • guild : guild resolvable
  • itemId : number, corredponding to an item ID
  • infinite : boolean
  • value : number. This is optional, but must be specified when infinite is false

updatePrice

Update the price of an item

manager.updatePrice(message.guild, 5, 10000); // Set the price of the item with id 5 to 10000

Parameters :

  • guild : guild resolvable
  • itemId : number, corresponding to an item ID
  • price : number, this is the new price

Returns Promise<'no item' | true>.

The promise returns 'no item' when no item is found

The promise returns true when everything is done

updateName

Update the name of an item

manager.updateName(message.guild, 6, "Sword"); // Set the name of the item with id 6 to 'Sword'

Parameters :

  • guild : guild resolvable
  • itemId : number, corresponding to an item ID
  • name : string, this is the new name

Returns Promise<'no item' | true>.

The promise returns 'no item' when no item is found

The promise returns true when everything is done

updateContent

Update the content of an item

manager.updatePrice(message.guild, 3, 'The best item you can find on the marketplace.'); // Update the content of the item with id 3

Parameters :

  • guild : guild resolvable
  • itemId : number, corresponding to an item ID
  • content : string, this is the new content

Returns Promise<'no item' | true>.

The promise returns 'no item' when no item is found

The promise returns true when everything is done

Types

Types of the manager

Item

type item<Raw extends boolean = false> = {
    guild_id: string;
    name: string;
    content: string;
    type: itemType; // 'role' | 'string'
    id: number;
    price: If<Raw, string, number>;
    remaining: If<Raw, string, number>;
    quantity: If<Raw, string, number>; // 0 if infinite
}

ItemType

Type of an item

type itemType = 'string' | 'role'

Inventory

type inventory<Raw extends boolean = false> = {
    guild_id: string;
    user_id: string;
    items: If<Raw, string, inventoryItem[]>; // { name: string; content: string; price: number; id: number; type: itemType; }[]
}

Inventory item

type inventoryItem = {
    name: string;
    content: string;
    quantity: number;
    price: number;
    id: number;
    type: itemType;
}

Types :

guild resolvable

Anything that contains a guild

import { Guild, BaseInteraction, GuildMember, Message } from 'discord.js';

type guildResolvable = Guild | BaseInteraction | GuildMember | Message | string;

addOptions

Options to provide when adding a new item

export type addOptions = {
    guild: guildResolvable; // Anything that can give a guild
    name: string; // Name of the item
    content: string; // Value of the item : string or role id
    type: itemType; // Type of the item
    quantity?: number; // Optional quantity (default 0, infinite)
    price: number; // Price of the item
};

The content is either the ID of the role, in case of #addOptions.type = 'role', or the content of the string item, when #addOptions.type = 'string'

Types :

user resolvable

Any thing that contains a user

import { User, GuildMember, BaseInteraction, Message } from 'discord.js';

type userResolvable = User | GuildMember | BaseInteraction | Message | string;

DefaultQueryResult

Default result of a MySQL request

export type DefaultQueryResult = {
    fieldCount: number;
    affectedRows: number;
    insertId: number;
    serverStatus: number;
    warningCount: number;
    message: string;
    protocol41: boolean;
    changedRows: number;
};

QueryResult

Result of a MySQL query

export type QueryResult<T> = T extends DefaultQueryResult ? DefaultQueryResult : T[];

Support

Get support on the support server