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

redi.db.js

v5.1.3

Published

Library for working with RediDB in Node.js

Downloads

53

Readme

🚪 Connecting to redi.db server

const { Document, RediClient } = require('redi.db.js');

const client = new RediClient({
	ip: '0.0.0.0',
	port: 5000,

	login: 'root',
	password: 'root',

	websocket: true, // Recommended for faster processing
	useSSL: false, // Use "true" if your protocol uses https
});

client.on('connected', () => {
	console.log('Connected!');
});

client.on('reconnected', () => {
	console.log('Reconnected!');
});

client.on('disconnect', () => {
	console.log('Disconnected!');
});

client.on('error', ({ error }) => {
	console.log(`Handled error: ${error.message}`);
});

👨‍🦳 Create a separate base with the collection.

const MyDocuments = new (class MyDocuments extends Document {
	constructor() {
		super(client, 'MyDatabase', 'MyCollection', MyDocuments.getDefaultModel(), false);
	}

	static getDefaultModel() {
		return {
			id: [Number, String],
			data: {
				status: Boolean,
			},
		};
	}

	async searchWithStatus() {
		return this.search({ data: { status: true } });
	}
})();

// By using your class, you are not violating the DRY principle by using "searchWithStatus" in different parts of the code. However, you can do without such deep nesting:
const MyDocuments = new Document(client, 'MyDatabase', 'MyCollection', { id: [Number, String], data: { status: Boolean } }, false);

👕 Create a user in the corresponding collection

await MyDocuments.create({ id: 1, data: { status: false } }, { id: 2, data: { status: true } });

👖 search the created user by ID

await MyDocuments.searchOne({ id: 1 });

// { _id: ..., id: 1, data: {...} }
// If the data does not exist - returns null

// You can also get multiple datas at once.
await MyDocuments.search({ data: { status: true } });
// or use custom methods from MyDocuments:
await MyDocuments.searchWithStatus();

🩰 searchOrCreate method

// If you don't know if you have a data with a certain ID, you can use this method.

await MyDocuments.searchOrCreate({ id: 3 }, { id: 3, data: { status: true } });

// {
//   created: true, // If created - true, else false
//   data: { id: 3, data: { status: true } }
// }

💿 Updating the data model

await MyDocuments.update({ id: 3 }, { data: { status: false } }); // Return [ { _id: ..., updated: true / false } ]

🔟 Get count of data in collection

await MyDocuments.count({ data: { status: false } }); // Returns number

🗑 Deleting a user model

await MyDocuments.delete({ id: 3 }); // Returns [ { _id: ..., deleted: true / false } ]
//                          ^ If no argument is specified, all models in the collection will be deleted.

🤞 You can use .$save() or .$delete

let found = await MyDocuments.searchOne();
if (!found) return;

found.data.status = true;

await found.$save(); // Return { _id: ..., updated: true / false }
await found.$delete(); // Return { _id: ..., deleted: true / false }

⌨️ TypeScript support

import { Document, RediClient, type DatabaseDocument, type Model } from 'redi.db.js';

const client = new RediClient({
	ip: '0.0.0.0',
	port: 5000,

	login: 'root',
	password: 'root',

	websocket: false,
	useSSL: false,
});

type UserModel = {
	id: string | number;
	data: {
		messages: string[];
		status: boolean;
	};
};

const UserDocuments = new (class UserDocuments extends Document<UserModel> {
	constructor() {
		super(client, 'MyProject', 'Users', UserDocuments.getDefaultModel(), false);
	}

	static getDefaultModel(): Model<UserModel> {
		return {
			id: [Number, String],
			data: {
				messages: Array,
				status: Boolean,
			},
		};
	}

	// Type your function with returning documents
	async getByStatus(): Promise<DatabaseDocument<UserModel>[]> {
		return await this.search({ data: { status: true } });
	}
})();

client.on('connected', async () => {
	console.log('Connected to redidb!');

	const user = await UserDocuments.searchOne({ id: 1 });
	if (!user) return console.log('User not found');

	user.data.status = true;
	await user.$save();

	console.log('User saved.');
});

client.on('reconnected', () => {
	console.log('Reconnected to redidb!');
});

client.on('disconnect', () => {
	console.log('Connection to redidb is closed');
});

client.on('error', ({ error }) => {
	console.log(`Handled error: ${error.message}`);
});