Tauri Plugin Mongoose
A Tauri v2 plugin for MongoDB/Mongoose-like database operations.
Installation
Rust (src-tauri/Cargo.toml)
[]
= "0.2.1"
JavaScript/TypeScript
Setup
1. Register the plugin in your Tauri app
In your src-tauri/src/lib.rs:
2. Add permissions in tauri.conf.json
In your src-tauri/tauri.conf.json, add the plugin permissions:
Or use the default permissions:
Usage
import { connect, Model } from 'tauri-plugin-mongoose';
// Connect to MongoDB
await connect('mongodb://localhost:27017', 'myDatabase');
// Or with options object
await connect({ url: 'mongodb://localhost:27017', dbName: 'myDatabase' });
// Define a schema
const userSchema = {
name: { type: 'string', required: true },
email: { type: 'string', required: true, unique: true },
age: { type: 'number' }
};
// Create a model
const User = new Model('users', userSchema);
// Create a document
const newUser = await User.create({
name: 'John Doe',
email: 'john@example.com',
age: 30
});
// Get document by ID
const user = await User.getById('60f1b5d6e4b0e...');
Store files (GridFS)
import { connect, Files } from 'tauri-plugin-mongoose';
await connect('mongodb://localhost:27017', 'myDatabase');
const FilesBucket = new Files('assets');
// Save an existing file from disk (path must be accessible to the Tauri backend)
await FilesBucket.savePath('/absolute/path/to/image.png', { filename: 'image.png' });
// Save raw bytes (Uint8Array, ArrayBuffer, or number[])
const bytes = new Uint8Array([1, 2, 3, 4]);
const saved = await FilesBucket.save(bytes, { filename: 'data.bin', metadata: { kind: 'example' } });
console.log(saved._id);
// Get a usable file:// URL from GridFS id
const url = await FilesBucket.getUrl(saved._id);
console.log(url); // file:///tmp/tauri-plugin-mongoose/gridfs-cache/...
API
connect(url: string, dbName?: string): Promise<void>
connect(options: ConnectOptions): Promise<void>
Connect to a MongoDB database.
Model
A class for defining and interacting with MongoDB collections.
new Model(name: string, schema: Schema)- Create a new modelmodel.create(doc)- Create a new documentmodel.getById(id)- Get a document by its ObjectId
Files
A helper for storing binary files using MongoDB GridFS.
new Files(bucket: string)- Target the GridFS bucket (e.g.,"assets"→assets.files/assets.chunks).files.savePath(path, options?)- Read a file from disk and store it.files.save(data, options?)- Store in-memory bytes (Uint8Array,ArrayBuffer, ornumber[]).files.getUrl(id, options?)- Resolve a GridFS file id and return a localfile://URL pointing to a cached copy.
License
MIT