Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[BUG]: Weird output from SQL queries with d1-http #3775

Closed
1 task done
nicholasgriffintn opened this issue Dec 15, 2024 · 2 comments
Closed
1 task done

[BUG]: Weird output from SQL queries with d1-http #3775

nicholasgriffintn opened this issue Dec 15, 2024 · 2 comments
Labels
bug Something isn't working driver/d1 priority Will be worked on next rqb relational queries

Comments

@nicholasgriffintn
Copy link

Report hasn't been filed before.

  • I have verified that the bug I'm about to report hasn't been filed before.

What version of drizzle-orm are you using?

0.38.2

What version of drizzle-kit are you using?

0.30.1

Other packages

No response

Describe the Bug

I'm gettting weird output from SQL queries where all the data is coming through as id for some reason.

For example, with this query:

const existingUserEmail = await db.query.user.findFirst({
        where: eq(user.email, providerUser.email),
      });

      console.log("existingUserEmail", existingUserEmail);

I'm getting back:

existingUserEmail {
  id: {
    id: 1,
    name: 'Nicholas Griffin',
    avatar_url: '',
    email: '',
    github_username: '',
    company: '',
    site: '',
    location: '',
    bio: '',
    twitter_username: '',
    created_at: '2024-12-15 15:13:04',
    updated_at: '2024-12-15 15:13:04',
    setup_at: null,
    terms_accepted_at: null
  },
  name: undefined,
  avatar_url: undefined,
  email: undefined,
  github_username: undefined,
  company: undefined,
  site: undefined,
  location: undefined,
  bio: undefined,
  twitter_username: undefined,
  created_at: undefined,
  updated_at: undefined,
  setup_at: undefined,
  terms_accepted_at: undefined
}

I'm trying to use d1-http as I can't use a binding with the framework, my config is here:

https://github.com/nicholasgriffintn/fosdem-pwa/blob/main/drizzle.config.ts

Also configuration with a proxy here:

https://github.com/nicholasgriffintn/fosdem-pwa/blob/main/app/server/db/index.ts

I've been scratching my head for ages, any ideas? Much appreciated if you do!

@nicholasgriffintn nicholasgriffintn added the bug Something isn't working label Dec 15, 2024
@L-Mario564 L-Mario564 added rqb relational queries priority Will be worked on next driver/d1 labels Dec 23, 2024
@alvin0
Copy link

alvin0 commented Dec 24, 2024

@nicholasgriffintn Thank you for providing your reference source code. It seems I can resolve your issue. Please refer to the source code below.

import { drizzle } from "drizzle-orm/sqlite-proxy";
import * as schema from "./schema";

// Destructure and validate environment variables
const {
  CLOUDFLARE_D1_ACCOUNT_ID,
  CLOUDFLARE_D1_DATABASE_ID,
  CLOUDFLARE_D1_TOKEN,
} = process.env;

if (
  !CLOUDFLARE_D1_ACCOUNT_ID ||
  !CLOUDFLARE_D1_DATABASE_ID ||
  !CLOUDFLARE_D1_TOKEN
) {
  throw new Error("Missing required Cloudflare D1 environment variables.");
}

// Define TypeScript types for the API responses
type D1ResponseInfo = {
  code: number;
  message: string;
};

type D1Response = {
  result: {
    meta: {
      changed_db: boolean;
      changes: number;
      duration: number;
      last_row_id: number;
      rows_read: number;
      rows_written: number;
      size_after: number;
    };
    results: Array<unknown>;
    success: boolean;
  }[];
  errors: D1ResponseInfo[];
  messages: D1ResponseInfo[];
  success: boolean;
};

// Initialize the drizzle ORM with a custom fetch function
export const databaseD1 = drizzle(
  async (sql: string, params: any[], method: string) => {
    // console.log("method ", method);
    // Determine the endpoint based on the method
    const endpoint = method === "values" ? "raw" : "query";
    const url = `https://api.cloudflare.com/client/v4/accounts/${CLOUDFLARE_D1_ACCOUNT_ID}/d1/database/${CLOUDFLARE_D1_DATABASE_ID}/${endpoint}`;

    // Make the POST request to the Cloudflare D1 API
    const response = await fetch(url, {
      method: "POST",
      headers: {
        Authorization: `Bearer ${CLOUDFLARE_D1_TOKEN}`,
        "Content-Type": "application/json",
      },
      body: JSON.stringify({ sql, params }),
    });

if (response.status !== 200)
      throw new Error(
        `Error from sqlite proxy server: ${response.status} ${
          response.statusText
        }\n${JSON.stringify(await response.json())}`
      );


    const responseJson = (await response.json()) as D1Response;

    // Check for HTTP errors
    if (!responseJson.success) {
      throw new Error(
        `Error from Cloudflare D1: ${response.status} ${
          response.statusText
        }\n${JSON.stringify(responseJson)}`
      );
    }

    const qResult = responseJson.result[0];
    const rows = qResult.results.map((r: any) => Object.values(r));

    return { rows: method == "all" ? rows : rows[0] };
  },
  { schema, logger: true }
);

@nicholasgriffintn
Copy link
Author

Thanks, that's awesome! Did the trick looking at it, that was a real head scratch, you saved me.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working driver/d1 priority Will be worked on next rqb relational queries
Projects
None yet
Development

No branches or pull requests

3 participants