5.1.3 • Published 22 days ago

redi.db.js v5.1.3

Weekly downloads
-
License
ISC
Repository
github
Last release
22 days ago

🚪 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}`);
});
5.1.3

22 days ago

5.1.2

1 month ago

5.1.1

1 month ago

5.1.0

1 month ago

5.0.5

1 month ago

5.0.4

4 months ago

5.0.3

4 months ago

5.0.2

4 months ago

5.0.1

4 months ago

5.0.0

5 months ago

4.1.8

7 months ago

4.1.9

7 months ago

4.1.7

8 months ago

4.0.8

11 months ago

4.1.4

11 months ago

4.0.5

12 months ago

4.1.3

11 months ago

4.1.6

9 months ago

4.0.7

12 months ago

4.1.5

10 months ago

4.0.6

12 months ago

4.1.0

11 months ago

4.1.2

11 months ago

4.1.1

11 months ago

3.0.2

1 year ago

3.1.0

1 year ago

3.0.1

1 year ago

3.0.0

1 year ago

2.0.3

1 year ago

4.0.4

12 months ago

2.0.2

1 year ago

4.0.1

1 year ago

4.0.0

1 year ago

4.0.3

12 months ago

4.0.2

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

1.0.0

1 year ago