secure-mysql-sdk
v1.3.1
Published
Official MySQL SDK for JavaScript/Node.js - Zero-dependency client optimized for MySQL databases with PHP backend integration
Maintainers
Readme
Secure MySQL SDK
The official MySQL-optimized JavaScript/Node.js SDK for multi-tenant MySQL environments. Built specifically for MySQL + PHP architectures with zero dependencies and shared hosting compatibility.
🚀 Installation
npm install secure-mysql-sdk📋 Quick Start
const { SecureDbClient } = require('secure-mysql-sdk');
// Initialize client
const client = new SecureDbClient(
'https://your-api-endpoint.com/api.php',
'your-api-key',
{ database: 'your-database-name' }
);
// Query data
const users = await client.from('users').select('*');
console.log(users.data);🏗️ Server Integration
This SDK is designed to work with Secure Database API servers. Whether you're running:
- Local Development:
http://localhost/secure-database-api/backend/public/api.php - Production Server:
https://your-domain.com/api.php - Cloud Deployment: Any hosted Secure Database API instance
The SDK automatically handles authentication, request formatting, and response parsing, making it much easier than direct HTTP calls.
Why Use the SDK vs Direct API Calls?
✅ Zero Dependencies - No external libraries required
✅ Type Safety - Full TypeScript support included
✅ Method Chaining - Fluent, readable query syntax
✅ Auto Authentication - Handles API keys and JWT tokens
✅ Error Handling - Comprehensive error management
✅ Query Builder - No need to manually construct API requests
🔧 Usage
Initialize Client
const { SecureDbClient } = require('secure-mysql-sdk');
const client = new SecureDbClient(baseUrl, apiKey, options);Parameters:
baseUrl(string): Your API endpoint URLapiKey(string): Your API keyoptions(object): Configuration optionsdatabase(string): Database nameprojectId(string): Optional project IDtimeout(number): Request timeout in milliseconds (default: 30000)
Select Data
// Get all records
const result = await client.from('users').select('*');
// Select specific columns
const result = await client.from('users').select('id, name, email');
// With conditions
const result = await client
.from('users')
.select('*')
.eq('status', 'active')
.gt('age', 18)
.limit(10);
// Get single record
const user = await client
.from('users')
.select('*')
.eq('id', 1)
.single();Insert Data
const result = await client
.from('users')
.insert({
name: 'John Doe',
email: '[email protected]',
age: 30
});Update Data
const result = await client
.from('users')
.update({ name: 'Jane Doe' })
.eq('id', 1);Delete Data
const result = await client
.from('users')
.delete()
.eq('id', 1);🔍 Query Methods
Filters
.eq('column', value) // Equal to
.neq('column', value) // Not equal to
.gt('column', value) // Greater than
.lt('column', value) // Less than
.like('column', pattern) // LIKE pattern matching
.in('column', [values]) // IN array of valuesModifiers
.order('column', true) // Order by column (true = ASC, false = DESC)
.limit(10) // Limit results
.offset(20) // Offset results📊 Response Format
All methods return a response object with the following structure:
{
data: [...], // Query results or null
error: { // Error object or null
message: "Error description",
details: {...} // Additional error details
},
status: 200, // HTTP status code
statusText: "OK", // HTTP status text
count: 5 // Number of records (for select queries)
}Success Response
{
data: [
{ id: 1, name: 'John', email: '[email protected]' },
{ id: 2, name: 'Jane', email: '[email protected]' }
],
error: null,
status: 200,
statusText: "OK",
count: 2
}Error Response
{
data: null,
error: {
message: "Table 'users' not found",
details: {...}
},
status: 404,
statusText: "Not Found"
}🌐 Browser Support
The SDK works in both Node.js and modern browsers:
<!-- Browser usage -->
<script src="https://unpkg.com/secure-mysql-sdk"></script>
<script>
const client = new SecureDbClient(apiUrl, apiKey, options);
</script>🔒 Error Handling
try {
const result = await client.from('users').select('*');
if (result.error) {
console.error('API Error:', result.error.message);
} else {
console.log('Data:', result.data);
}
} catch (error) {
console.error('Network Error:', error.message);
}🛠️ Advanced Usage
Raw SQL Queries
const result = await client.sql(
'SELECT * FROM users WHERE age > ?',
[18]
);Database Schema
const schema = await client.getSchema();
console.log('Available tables:', schema.data);Multiple Databases
const client1 = client.database('database1');
const client2 = client.database('database2');
const users1 = await client1.from('users').select('*');
const users2 = await client2.from('users').select('*');📝 TypeScript Support
The package includes TypeScript definitions:
import { SecureDbClient, ApiResponse } from 'secure-mysql-sdk';
const client = new SecureDbClient(baseUrl, apiKey, { database: 'mydb' });
const result: ApiResponse = await client
.from('users')
.select('*')
.eq('active', true);🔧 Configuration Examples
Basic Setup
const client = new SecureDbClient(
'https://api.example.com/secure-db',
'sdk_abc123...',
{ database: 'production_db' }
);With Timeout
const client = new SecureDbClient(
'https://api.example.com/secure-db',
'sdk_abc123...',
{
database: 'production_db',
timeout: 60000 // 60 seconds
}
);📄 License
MIT
🤝 Contributing
Issues and pull requests are welcome at the GitHub repository.
📚 Documentation
For more detailed documentation, visit: Full Documentation
Made with ❤️ for developers who need a simple, powerful database SDK
