@mifistix-cloud/database
v2.0.5
Published
Realtime Database SDK for Mifistix Cloud - Store and sync data in realtime
Downloads
450
Maintainers
Readme
@mifistix-cloud/database
Realtime Database SDK for Mifistix Cloud - Store and sync data in realtime.
License
This module is licensed for internal use within Mifistix only. See LICENSE for details.
Installation
npm install @mifistix-cloud/databaseQuick Start
const { initializeApp } = require('@mifistix-cloud/app');
const { getDatabase, ref, set, get, push, update, remove } = require('@mifistix-cloud/database');
// Initialize app
const app = initializeApp({
apiKey: 'your-api-key',
projectId: 'your-project-id'
});
// Get database instance
const db = getDatabase(app);
// Write data
await set(ref(db, 'users/user1'), {
name: 'John Doe',
email: '[email protected]'
});
// Read data
const snapshot = await get(ref(db, 'users/user1'));
console.log(snapshot.val());
// Push with auto ID
const result = await push(ref(db, 'users'), { name: 'Jane' });
console.log('New key:', result.key);
// Update partial data
await update(ref(db, 'users/user1'), { email: '[email protected]' });
// Delete data
await remove(ref(db, 'users/user1'));API Reference
getDatabase(app)
Get database instance.
Parameters:
app(Object, required): Initialized app instance
Returns: Database service instance
ref(db, path?)
Create a database reference.
Parameters:
db(Object): Database instancepath(string, optional): Database path (validated for path traversal)
Returns: Database reference
Example:
const userRef = ref(db, 'users/user1');set(dbRef, data)
Write or overwrite data at path.
Parameters:
dbRef(Object): Database referencedata(any): Data to write
Example:
await set(ref(db, 'users/user1'), { name: 'John' });push(dbRef, data)
Push data with auto-generated ID.
Parameters:
dbRef(Object): Database referencedata(any): Data to push
Returns: Object with key, path, and ref
Example:
const result = await push(ref(db, 'messages'), { text: 'Hello' });
console.log('New key:', result.key);get(dbRef)
Read data from database.
Parameters:
dbRef(Object): Database reference
Returns: Snapshot with exists() and val() methods
Example:
const snapshot = await get(ref(db, 'users/user1'));
if (snapshot.exists()) {
console.log(snapshot.val());
}update(dbRef, partial)
Update specific fields without overwriting entire object.
Parameters:
dbRef(Object): Database referencepartial(Object): Partial data to update
Example:
await update(ref(db, 'users/user1'), { email: '[email protected]' });remove(dbRef)
Delete data at path.
Parameters:
dbRef(Object): Database reference
Example:
await remove(ref(db, 'users/user1'));smartPush(dbRef, storage, file, docData)
Upload file to storage and create database record.
Parameters:
dbRef(Object): Database referencestorage(Object): Storage instancefile(File/Blob): File to uploaddocData(Object): Additional document data
Returns: Promise with database record
Example:
const storage = getStorage(app);
const result = await smartPush(
ref(db, 'documents'),
storage,
fileObject,
{ title: 'My Document', category: 'docs' }
);Security Features
- Path Traversal Protection: Validates paths to prevent
../attacks - Data Validation: Validates required parameters
- Project Isolation: Uses project ID in all requests
- Error Handling: Throws appropriate error types
Data Snapshot
The get() method returns a snapshot object:
const snapshot = await get(ref(db, 'path'));
snapshot.exists(); // boolean - whether data exists
snapshot.val(); // any - the data value
snapshot.key(); // string - the key of the snapshotArchitecture
database/
├── src/
│ ├── core/
│ │ └── DatabaseClient.js # Main database client
│ ├── services/
│ │ └── DatabaseService.js # CRUD operations
│ ├── utils/
│ │ └── helpers.js # Helper functions
│ ├── types/
│ │ └── index.js # Type definitions
│ └── config/
│ └── constants.js # Configuration constants
└── index.jsLicense
MIT
