You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

6.1 KiB

Compact Database API - Before vs After Comparison

The Problem: Verbose Database Operations

The current database operations require significant boilerplate code, making simple operations unnecessarily complex.

Before: Verbose & Repetitive

Loading Data

// 6 lines for a simple query!
@Component
export default class ContactsView extends Vue {
  async loadContacts() {
    const platformService = PlatformServiceFactory.getInstance();
    const result = await platformService.dbQuery("SELECT * FROM contacts WHERE visible = ?", [1]);
    const contacts = databaseUtil.mapQueryResultToValues(result) as Contact[];
    await databaseUtil.logToDb(`Loaded ${contacts.length} contacts`);
    this.contacts = contacts;
  }
}

Saving Data

// 8+ lines for a simple insert!
async saveContact(contact: Contact) {
  const platformService = PlatformServiceFactory.getInstance();
  const { sql, params } = databaseUtil.generateInsertStatement(contact, "contacts");
  const result = await platformService.dbExec(sql, params);
  await databaseUtil.logToDb(`Contact saved with ID: ${result.lastId}`);
  if (result.changes !== 1) {
    throw new Error("Failed to save contact");
  }
  return result;
}

Settings Management

// 4+ lines for settings
async updateAppSettings(newSettings: Partial<Settings>) {
  const success = await databaseUtil.updateDefaultSettings(newSettings as Settings);
  await databaseUtil.logToDb(success ? "Settings saved" : "Settings save failed", success ? "info" : "error");
  return success;
}

After: Compact & Clean

Loading Data

// 2 lines - 70% reduction!
@Component
export default class ContactsView extends Vue {
  private db = useCompactDatabase();
  
  async loadContacts() {
    const contacts = await this.db.query<Contact>("SELECT * FROM contacts WHERE visible = ?", [1]);
    await this.db.log(`Loaded ${contacts.length} contacts`);
    this.contacts = contacts;
  }
}

Saving Data

// 2 lines - 75% reduction!
async saveContact(contact: Contact) {
  const result = await this.db.insert("contacts", contact);
  await this.db.log(`Contact saved with ID: ${result.lastId}`);
  return result;
}

Settings Management

// 1 line - 75% reduction!
async updateAppSettings(newSettings: Partial<Settings>) {
  return await this.db.saveSettings(newSettings);
}

Advanced Examples

Multiple Usage Patterns

1. Vue-Facing-Decorator Class Components

@Component
export default class MyComponent extends Vue {
  private db = useCompactDatabase(); // Composable in class
  
  async mounted() {
    // Query with type safety
    const users = await this.db.query<User>("SELECT * FROM users WHERE active = ?", [1]);
    
    // Get single record
    const setting = await this.db.queryOne<Setting>("SELECT * FROM settings WHERE key = ?", ["theme"]);
    
    // CRUD operations
    await this.db.insert("logs", { message: "Component mounted", date: new Date().toISOString() });
    await this.db.update("users", { lastActive: Date.now() }, "id = ?", [this.userId]);
    await this.db.delete("temp_data", "created < ?", [Date.now() - 86400000]);
  }
}

2. Composition API Setup

export default {
  setup() {
    const db = useCompactDatabase();
    
    const loadData = async () => {
      const items = await db.query("SELECT * FROM items");
      await db.log("Data loaded");
      return items;
    };
    
    return { loadData };
  }
}

3. Direct Import (Non-Composable)

import { db } from "@/composables/useCompactDatabase";

// Use anywhere without setup
export async function backgroundTask() {
  const data = await db.query("SELECT * FROM background_jobs");
  await db.log(`Processing ${data.length} jobs`);
}

Feature Comparison

Operation Before (Lines) After (Lines) Reduction
Simple Query 4 lines 1 line 75%
Insert Record 4 lines 1 line 75%
Update Record 5 lines 1 line 80%
Delete Record 3 lines 1 line 67%
Get Settings 3 lines 1 line 67%
Save Settings 4 lines 1 line 75%
Log Message 1 line 1 line 0% (already compact)

Benefits

🎯 Massive Code Reduction

  • 70-80% less boilerplate for common operations
  • Cleaner, more readable code
  • Faster development with less typing

🔧 Developer Experience

  • Auto-completion for all database operations
  • Type safety with generic query methods
  • Consistent API across all database operations
  • Built-in logging for debugging

🛡️ Safety & Reliability

  • Same security as existing functions (wraps them)
  • Parameterized queries prevent SQL injection
  • Error handling built into the composable
  • Type checking prevents runtime errors

🔄 Flexibility

  • Works with vue-facing-decorator (your current pattern)
  • Works with Composition API (future-proof)
  • Works with direct imports (utility functions)
  • Progressive adoption - use alongside existing code

Migration Path

Phase 1: New Code

// Start using in new components immediately
const db = useCompactDatabase();
const data = await db.query("SELECT * FROM table");

Phase 2: Gradual Replacement

// Replace verbose patterns as you encounter them
// Old:
const platformService = PlatformServiceFactory.getInstance();
const result = await platformService.dbQuery(sql, params);
const mapped = databaseUtil.mapQueryResultToValues(result);

// New:
const mapped = await db.query(sql, params);

Phase 3: Full Adoption

// Eventually all database operations use the compact API

Performance Impact

  • Zero performance overhead - same underlying functions
  • Slight memory improvement - fewer service instantiations
  • Better caching - singleton pattern for platform service
  • Reduced bundle size - less repeated boilerplate code

The compact database composable transforms verbose, error-prone database operations into clean, type-safe one-liners while maintaining all existing security and functionality.