wowsql 1.4.0
wowsql: ^1.4.0 copied to clipboard
Official Flutter/Dart SDK for WowSQL - MySQL Backend-as-a-Service with S3 Storage support, type-safe queries and fluent API
example/main.dart
import 'dart:typed_data';
import 'package:wowsql/wowsql.dart';
void main() async {
// Initialize database client
final client = WOWSQLClient(
projectUrl: 'https://your-project.wowsql.com',
apiKey: 'your-api-key',
);
// Initialize storage client
final storage = WOWSQLStorage(
projectSlug: 'your-project',
apiKey: 'your-api-key',
baseUrl: 'https://api.wowsql.com',
);
try {
// ==================== DATABASE EXAMPLES ====================
print('=== DATABASE OPERATIONS ===\n');
// 1. List all tables
print('1. List all tables');
final tables = await client.listTables();
print('Tables: $tables\n');
// 2. Get table schema
print('2. Get table schema');
final schema = await client.getTableSchema('users');
print('Columns: ${schema.columns.length}');
print('Row count: ${schema.rowCount}\n');
// 3. Select all users
print('3. Select all users');
final allUsers = await client.table('users').select(['*']).get();
print('Found ${allUsers.count} users\n');
// 4. Select with filters
print('4. Select active users');
final activeUsers = await client
.table('users')
.select(['id', 'name', 'email'])
.eq('status', 'active')
.limit(10)
.get();
print('Active users: ${activeUsers.count}\n');
// 5. Insert new user
print('5. Insert new user');
final newUser = await client.table('users').insert({
'name': 'John Doe',
'email': 'john@example.com',
'age': 30,
'status': 'active',
});
print('New user ID: ${newUser.id}\n');
// 6. Update user
print('6. Update user');
final updated = await client.table('users').updateById(newUser.id, {
'name': 'John Smith',
});
print('Updated ${updated.affectedRows} row(s)\n');
// 7. Complex query
print('7. Complex query');
final results = await client
.table('users')
.select(['id', 'name', 'email'])
.gt('age', 18)
.like('email', '%@example.com')
.orderBy('created_at', SortDirection.desc)
.limit(5)
.get();
print('Results: ${results.count}\n');
// 8. Get first result
print('8. Get first user');
final firstUser = await client
.table('users')
.select(['*'])
.eq('email', 'john@example.com')
.first();
print('User: ${firstUser?['name']}\n');
// 9. Delete user
print('9. Delete user');
final deleted = await client.table('users').deleteById(newUser.id);
print('Deleted ${deleted.affectedRows} row(s)\n');
// 10. Raw SQL query
print('10. Raw SQL query');
final sqlResults = await client.query<Map<String, dynamic>>(
'SELECT COUNT(*) as count FROM users WHERE age > 18',
);
print('Count: ${sqlResults.first['count']}\n');
// ==================== STORAGE EXAMPLES ====================
print('=== STORAGE OPERATIONS ===\n');
// 1. Get storage quota
print('1. Get storage quota');
final quota = await storage.getQuota();
print('Used: ${quota.storageUsedGb.toStringAsFixed(2)} GB');
print('Available: ${quota.storageAvailableGb.toStringAsFixed(2)} GB');
print('Total: ${quota.storageQuotaGb.toStringAsFixed(2)} GB');
print('Usage: ${quota.usagePercentage.toStringAsFixed(1)}%\n');
// 2. Upload file
print('2. Upload file');
final fileBytes = Uint8List.fromList('Hello, WOWSQL!'.codeUnits);
final uploadResult = await storage.upload(
fileBytes,
'uploads/test.txt',
contentType: 'text/plain',
);
print('Uploaded: ${uploadResult.key}');
print('URL: ${uploadResult.url}\n');
// 3. List files
print('3. List files');
final files = await storage.listFiles(prefix: 'uploads/');
print('Found ${files.length} files:');
for (final file in files) {
print(' - ${file.key} (${file.size} bytes)');
}
print('');
// 4. Get presigned URL for file
print('4. Get presigned URL');
final presignedUrl = await storage.getPresignedUrl('uploads/test.txt');
print('Presigned URL: $presignedUrl\n');
// 5. Get file URL with metadata
print('5. Get file URL with metadata');
final fileUrlData = await storage.getFileUrl('uploads/test.txt');
print('File URL: ${fileUrlData['file_url']}');
print('Bucket: ${fileUrlData['bucket_name']}\n');
// 6. Get storage info
print('6. Get storage info');
final storageInfo = await storage.getStorageInfo();
print('Bucket: ${storageInfo['bucket_name']}');
print('Region: ${storageInfo['region']}\n');
// 7. Delete file
print('7. Delete file');
await storage.deleteFile('uploads/test.txt');
print('File deleted\n');
// 9. Check API health
print('9. Check API health');
final health = await client.health();
print('Status: ${health['status']}\n');
print('✅ All operations completed successfully!');
} catch (e) {
print('❌ Error: $e');
} finally {
// Clean up
client.dispose();
storage.dispose();
}
}