@earth-app/collegedb
    Preparing search index...

    Function run

    • Executes a statement on the appropriate shard based on the primary key. The primary key is used to determine which shard should store the record, ensuring consistent routing for future queries.

      Type Parameters

      • T = Record<string, unknown>

        Type of the result records

      Parameters

      • key: string

        Primary key to route the query (should match the record's primary key)

      • sql: string

        SQL statement with parameter placeholders

      • bindings: any[] = []

        Parameter values to bind to the SQL statement

      Returns Promise<D1Result<T>>

      Promise that resolves when the statement is complete

      If statement fails or routing fails

      // Insert a new user
      await run('user-123',
      'INSERT INTO users (id, name, email) VALUES (?, ?, ?)',
      ['user-123', 'John Doe', 'john@example.com']
      );

      // Insert a post linked to a user
      await run('post-456',
      'INSERT INTO posts (id, user_id, title, content) VALUES (?, ?, ?, ?)',
      ['post-456', 'user-123', 'Hello World', 'My first post!']
      );
      // Update user information
      await run('user-123',
      'UPDATE users SET name = ?, email = ? WHERE id = ?',
      ['John Smith', 'johnsmith@example.com', 'user-123']
      );

      // Update post content
      await run('post-456',
      'UPDATE posts SET title = ?, content = ?, updated_at = strftime("%s", "now") WHERE id = ?',
      ['Updated Title', 'Updated content here', 'post-456']
      );
      // Delete a specific user
      await run('user-123',
      'DELETE FROM users WHERE id = ?',
      ['user-123']
      );

      // Delete user's posts (cascade delete)
      await run('user-123',
      'DELETE FROM posts WHERE user_id = ?',
      ['user-123']
      );

      // Delete with conditions
      await run('user-123',
      'DELETE FROM posts WHERE user_id = ? AND created_at < ?',
      ['user-123', Date.now() - 86400000] // Posts older than 1 day
      );