veddb-client
v0.2.1
Published
Official JavaScript/TypeScript client for VedDB v0.2.0 - MongoDB-like API with verified wire protocol
Maintainers
Readme
vedDB Client v0.2.0
Modern JavaScript/TypeScript client for VedDB v0.2.0 with Mongoose-like API
🚀 Features
- Mongoose-like API - Familiar Schema and Model pattern
- TypeScript Support - Full type definitions included
- Query Builder - Fluent, chainable queries
- Connection Pooling - Automatic connection management
- Validation - Schema-based validation
- Hooks - Pre/post middleware support
- Modern ES Modules - Clean import/export syntax
📦 Installation
npm install veddb
# or
yarn add veddb
# or
pnpm add veddb🏃 Quick Start
Basic Usage
import veddb, { Schema, model } from 'veddb';
// Connect to VedDB server
await veddb.connect('localhost', 50051);
// Define a schema
const userSchema = new Schema({
name: { type: String, required: true },
email: { type: String, required: true, unique: true },
age: { type: Number, min: 0, max: 120 },
createdAt: { type: Date, default: Date.now }
});
// Create a model
const User = model('User', userSchema);
// Create a document
const user = await User.create({
name: 'Alice',
email: '[email protected]',
age: 30
});
// Find documents
const users = await User.find({ age: { $gt: 25 } });
// Update
await User.updateOne({ name: 'Alice' }, { age: 31 });
// Delete
await User.deleteOne({ name: 'Alice' });TypeScript Usage
import veddb, { Schema, model, Document } from 'veddb';
interface IUser extends Document {
name: string;
email: string;
age: number;
createdAt: Date;
}
const userSchema = new Schema<IUser>({
name: { type: String, required: true },
email: { type: String, required: true },
age: { type: Number },
createdAt: { type: Date, default: Date.now }
});
const User = model<IUser>('User', userSchema);
const user: IUser = await User.findOne({ email: '[email protected]' });🔧 API Reference
Connection
// Connect with options
await veddb.connect('localhost', 50051, {
poolSize: 10,
auth: {
username: 'admin',
password: 'password'
},
tls: {
enabled: true
}
});
// Connection events
veddb.connection.on('connected', () => console.log('Connected!'));
veddb.connection.on('error', (err) => console.error(err));
// Disconnect
await veddb.disconnect();Schema Definition
const schema = new Schema({
// Basic types
name: String,
age: Number,
active: Boolean,
birthday: Date,
// With options
email: {
type: String,
required: true,
unique: true,
lowercase: true,
trim: true
},
// Nested objects
address: {
street: String,
city: String,
zip: String
},
// Arrays
tags: [String],
friends: [{ type: Schema.Types.ObjectId, ref: 'User' }],
// Default values
createdAt: { type: Date, default: Date.now },
// Custom validators
password: {
type: String,
validate: {
validator: (v) => v.length >= 8,
message: 'Password must be at least 8 characters'
}
}
});
// Add methods
schema.method('getFullName', function() {
return `${this.firstName} ${this.lastName}`;
});
// Add statics
schema.static('findByEmail', function(email) {
return this.findOne({ email });
});
// Add hooks
schema.pre('save', function(next) {
this.updatedAt = new Date();
next();
});
schema.post('save', function(doc) {
console.log('Document saved:', doc._id);
});Model Operations
// Create
const user = new User({ name: 'Bob', age: 25 });
await user.save();
// OR
await User.create({ name: 'Charlie', age: 35 });
await User.insertMany([{ name: 'Dave' }, { name: 'Eve' }]);
// Read
const all = await User.find();
const filtered = await User.find({ age: { $gte: 30 } });
const one = await User.findOne({ name: 'Alice' });
const byId = await User.findById('507f1f77bcf86cd799439011');
// Update
await User.updateOne({ name: 'Bob' }, { age: 26 });
await User.updateMany({ age: { $lt: 30 } }, { status: 'young' });
await User.findByIdAndUpdate(id, { age: 27 }, { new: true });
// Delete
await User.deleteOne({ name: 'Bob' });
await User.deleteMany({ age: { $lt: 18 } });
await User.findByIdAndDelete(id);
// Count
const count = await User.countDocuments({ age: { $gt: 30 } });
const exists = await User.exists({ name: 'Alice' });Query Builder
// Chainable queries
const users = await User
.find({ status: 'active' })
.where('age').gt(25).lt(50)
.where('city').equals('NYC')
.sort({ age: -1, name: 1 })
.limit(10)
.skip(5)
.select('name email age')
.exec();
// Query operators
query.where('age').gt(25); // Greater than
query.where('age').gte(25); // Greater than or equal
query.where('age').lt(50); // Less than
query.where('age').lte(50); // Less than or equal
query.where('name').equals('Alice');
query.where('status').in(['active', 'pending']);
query.where('city').ne('NYC'); // Not equal
query.where('tags').all(['javascript', 'nodejs']);
query.where('name').regex(/^A/);
// Sorting
query.sort({ age: -1 }); // Descending
query.sort('name'); // Ascending
query.sort('-age name'); // Multiple fields
// Pagination
query.limit(20);
query.skip(40);
// Field selection
query.select('name email'); // Include
query.select('-password -secret'); // ExcludeAggregation
const results = await User.aggregate([
{ $match: { age: { $gt: 25 } } },
{ $group: {
_id: '$city',
avgAge: { $avg: '$age' },
count: { $sum: 1 }
}},
{ $sort: { count: -1 } },
{ $limit: 10 }
]);🐳 Docker Support
VedDB Server is available as a Docker image:
docker pull mihirrabariii/veddb:latest
docker run -d -p 50051:50051 mihirrabariii/veddb:latestThen connect from your app:
await veddb.connect('localhost', 50051);📖 Examples
See the examples directory for more:
🔗 Related
- VedDB Server: https://github.com/Mihir-Rabari/ved-db-server
- Docker Hub: https://hub.docker.com/r/mihirrabariii/veddb
- Rust Client: https://github.com/Mihir-Rabari/ved-db-rust-client
📄 License
MIT © Mihir Rabari
🤝 Contributing
Contributions are welcome! Please open an issue or PR.
📧 Contact
- Email: [email protected]
- Instagram: @mihirrabariii
- GitHub: Mihir-Rabari
