monarisa
monarisa
Explore posts from servers
DTDrizzle Team
Created by monarisa on 7/31/2024 in #help
The type-check of partial-select doesn't work well
Oh...!!! Thank you so much...!!! It works!! And what you care about storing the global db is amazing.
19 replies
DTDrizzle Team
Created by monarisa on 7/31/2024 in #help
The type-check of partial-select doesn't work well
Actually I switch driver according to whether the env is local or production.(local: drizzleProxy, production: drizzleTiDB) And I use global variable because I don't want to make many db instances. What should I define the type of the global variable?? Or are there any other best practices?
19 replies
DTDrizzle Team
Created by monarisa on 7/31/2024 in #help
The type-check of partial-select doesn't work well
Thank you for trying...!!! And when I use your code, it works. But my code is this. And I found out the global definition make it broken. (please try it again..??)
import { drizzle as drizzleTiDB } from "drizzle-orm/tidb-serverless";
import { drizzle as drizzleProxy } from "drizzle-orm/mysql-proxy";
import { connect } from "@tidbcloud/serverless";

declare global {
var _db: ReturnType<typeof drizzleTiDB> | ReturnType<typeof drizzleProxy> | undefined;
}

const getDb = () => {
if (process.env.NODE_ENV === "production") {
const client = connect({
url: "",
});
return drizzleTiDB(client);
} else {
return drizzleProxy(async (sql, params, method) => {
try {
const response = await fetch("http://localhost:8000/query", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ sql, params, method }),
});

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

const data: any[] = await response.json();
return { rows: data };
} catch (e: any) {
console.error("Error from mysql proxy server: ", e.message);
return { rows: [] };
}
});
}
};

const db = globalThis._db || getDb()

if (process.env.NODE_ENV !== "production") {
globalThis._db = db;
}

export { db };

// normally I use the following code in other file.
export const getUsers = async () => {
const result = await db.select({
field1: users.id,
field2: users.name,
}).from(users);
const { field1, field2 } = result[0];
return result;
}
import { drizzle as drizzleTiDB } from "drizzle-orm/tidb-serverless";
import { drizzle as drizzleProxy } from "drizzle-orm/mysql-proxy";
import { connect } from "@tidbcloud/serverless";

declare global {
var _db: ReturnType<typeof drizzleTiDB> | ReturnType<typeof drizzleProxy> | undefined;
}

const getDb = () => {
if (process.env.NODE_ENV === "production") {
const client = connect({
url: "",
});
return drizzleTiDB(client);
} else {
return drizzleProxy(async (sql, params, method) => {
try {
const response = await fetch("http://localhost:8000/query", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ sql, params, method }),
});

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

const data: any[] = await response.json();
return { rows: data };
} catch (e: any) {
console.error("Error from mysql proxy server: ", e.message);
return { rows: [] };
}
});
}
};

const db = globalThis._db || getDb()

if (process.env.NODE_ENV !== "production") {
globalThis._db = db;
}

export { db };

// normally I use the following code in other file.
export const getUsers = async () => {
const result = await db.select({
field1: users.id,
field2: users.name,
}).from(users);
const { field1, field2 } = result[0];
return result;
}
19 replies
DTDrizzle Team
Created by monarisa on 7/31/2024 in #help
The type-check of partial-select doesn't work well
Thank you for your comment. This is my tsconfig. And strict mode set to true, but even when set to false, I cannot solve the problem.
tsconfig.json
{
"compilerOptions": {
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"plugins": [
{
"name": "next"
}
],
"paths": {
"@/*": ["./src/*"]
},
"types": [
"@cloudflare/workers-types/2023-07-01"
]
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
}
tsconfig.json
{
"compilerOptions": {
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"plugins": [
{
"name": "next"
}
],
"paths": {
"@/*": ["./src/*"]
},
"types": [
"@cloudflare/workers-types/2023-07-01"
]
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
}
19 replies