Jim
Jim
Explore posts from servers
DTDrizzle Team
Created by Ghostman on 3/8/2024 in #help
How to remove the space between timestamp and precision?
8 replies
DTDrizzle Team
Created by Ghostman on 3/8/2024 in #help
How to remove the space between timestamp and precision?
Unfortunalty not, I will let you know if I find a workaround without converting to string
8 replies
DTDrizzle Team
Created by Ghostman on 3/8/2024 in #help
How to remove the space between timestamp and precision?
@Ghostman hey ghostman sorry i missed this!
8 replies
DTDrizzle Team
Created by Ghostman on 3/8/2024 in #help
How to remove the space between timestamp and precision?
fixed by setting the timestamp to a sting e.g.
createdAt: timestamp("created_at", {precision: 3, mode: 'string' }).defaultNow().notNull()
createdAt: timestamp("created_at", {precision: 3, mode: 'string' }).defaultNow().notNull()
8 replies
DTDrizzle Team
Created by Ghostman on 3/8/2024 in #help
How to remove the space between timestamp and precision?
hey @Ghostman facing the same issue, did you find a workaround?
8 replies
DTDrizzle Team
Created by Jim on 3/19/2024 in #help
Defining websocket for NeonDB connection
The Singleton pattern for the neonConfig threw me through a loop
4 replies
DTDrizzle Team
Created by Jim on 3/19/2024 in #help
Defining websocket for NeonDB connection
should of just followed the instructions:
import { drizzle } from 'drizzle-orm/neon-serverless';
import { Pool, neonConfig } from '@neondatabase/serverless';
import dotenv from "dotenv";
import * as schema from "./schema";
import ws from 'ws';

dotenv.config({ path: ".env.local" });
neonConfig.webSocketConstructor = ws; // here
const pool = new Pool({ connectionString: process.env.DB_URL });

export const db = drizzle(pool, { schema })
import { drizzle } from 'drizzle-orm/neon-serverless';
import { Pool, neonConfig } from '@neondatabase/serverless';
import dotenv from "dotenv";
import * as schema from "./schema";
import ws from 'ws';

dotenv.config({ path: ".env.local" });
neonConfig.webSocketConstructor = ws; // here
const pool = new Pool({ connectionString: process.env.DB_URL });

export const db = drizzle(pool, { schema })
4 replies
DTDrizzle Team
Created by Jim on 3/19/2024 in #help
Defining websocket for NeonDB connection
Fixed it! 🥴
4 replies
DTDrizzle Team
Created by Jim on 12/3/2023 in #help
avoiding conflicts in mySQL
in fact gpt recommended making 'temporary tables' but i think thats overkill, plus i'm not 100% sure it solves the issue
33 replies
DTDrizzle Team
Created by Jim on 12/3/2023 in #help
avoiding conflicts in mySQL
gpt only gets you so far 🤖
33 replies
DTDrizzle Team
Created by Jim on 12/3/2023 in #help
avoiding conflicts in mySQL
good to have someone who knows what theyre doing look over it though so thanks!
33 replies
DTDrizzle Team
Created by Jim on 12/3/2023 in #help
avoiding conflicts in mySQL
yeah, i've been trying to find a solution all weekend haha
33 replies
DTDrizzle Team
Created by Jim on 12/3/2023 in #help
avoiding conflicts in mySQL
Ahh I see, thats a handy return object
33 replies
DTDrizzle Team
Created by Jim on 12/3/2023 in #help
avoiding conflicts in mySQL
I should add one right, since im using the column in a WHERE clause?
33 replies
DTDrizzle Team
Created by Jim on 12/3/2023 in #help
avoiding conflicts in mySQL
no i dont think so
33 replies
DTDrizzle Team
Created by Jim on 12/3/2023 in #help
avoiding conflicts in mySQL
Yeah i suspected so! Also planetscale doesnt do procedures so thats another avenue blocked
33 replies
DTDrizzle Team
Created by Jim on 12/3/2023 in #help
avoiding conflicts in mySQL
yeahhh i think i used that elsewhere in my codebase, to get the auto increment id of an insert
33 replies
DTDrizzle Team
Created by Jim on 12/3/2023 in #help
avoiding conflicts in mySQL
thanks! yeah im new to db stuff an on the drizzle docs there is no built in way to do update returns: https://orm.drizzle.team/docs/update#update-with-returning
33 replies
DTDrizzle Team
Created by Jim on 12/3/2023 in #help
avoiding conflicts in mySQL
would be a lot simpler if mysql returned ids 😂
33 replies
DTDrizzle Team
Created by Jim on 12/3/2023 in #help
avoiding conflicts in mySQL
// add txid for each record in this transaction
records.forEach((r) => {
r.idTx = createId();
});

// sql for setting idtx
const sqlSetChunks: SQL[] = [];
sqlSetChunks.push(sql`CASE`);
for (const record of records) {
sqlSetChunks.push(sql` WHEN id = ${record.id} THEN ${record.idTx}`);
}
sqlSetChunks.push(sql`END`);
const sqlSet = sql.join(sqlSetChunks, sql.raw(" "));

// sql for match id and current lastModified date
const sqlWhereChunks: SQL[] = [];
records.forEach((r, i) => {
sqlWhereChunks.push(
sql`(${recordTable.id} = ${r.id} AND ${recordTable.lastModified} = ${r.lastModified})`
);
if (i < records.length - 1) {
sqlWhereChunks.push(sql` OR `);
}
});
const sqlWhere = sql.join(sqlWhereChunks, sql.raw(" "));

try {
await db.transaction(async (tx) => {
await tx
.update(recordTable)
.set({ statusId: status, idTx: sqlSet })
.where(sqlWhere);

// select records that were attempted to be updated
const selectedRecordsPostUpdate = await tx
.select()
.from(recordTable)
.where(
inArray(
recordTable.id,
records.map((r) => r.id)
)
);

// filter out records that were not updated by matching idTx with server idTx
const updatedRecords = selectedRecordsPostUpdate.filter((r) =>
records.find((r2) => r2.id === r.id && r2.idTx === r.idTx)
);
// if no records were updated, throw error
if (updatedRecords.length === 0) {
throw new Error("No records updated");
}
await tx.insert(statusLog).values(
updatedRecords.map((r) => ({
recordId: r.id,
statusId: status,
message: "bulk",
}))
);

// if any records were not updated, throw error
if (updatedRecords.length !== records.length) {
throw new Error("Not all records updated");
}
});
// add txid for each record in this transaction
records.forEach((r) => {
r.idTx = createId();
});

// sql for setting idtx
const sqlSetChunks: SQL[] = [];
sqlSetChunks.push(sql`CASE`);
for (const record of records) {
sqlSetChunks.push(sql` WHEN id = ${record.id} THEN ${record.idTx}`);
}
sqlSetChunks.push(sql`END`);
const sqlSet = sql.join(sqlSetChunks, sql.raw(" "));

// sql for match id and current lastModified date
const sqlWhereChunks: SQL[] = [];
records.forEach((r, i) => {
sqlWhereChunks.push(
sql`(${recordTable.id} = ${r.id} AND ${recordTable.lastModified} = ${r.lastModified})`
);
if (i < records.length - 1) {
sqlWhereChunks.push(sql` OR `);
}
});
const sqlWhere = sql.join(sqlWhereChunks, sql.raw(" "));

try {
await db.transaction(async (tx) => {
await tx
.update(recordTable)
.set({ statusId: status, idTx: sqlSet })
.where(sqlWhere);

// select records that were attempted to be updated
const selectedRecordsPostUpdate = await tx
.select()
.from(recordTable)
.where(
inArray(
recordTable.id,
records.map((r) => r.id)
)
);

// filter out records that were not updated by matching idTx with server idTx
const updatedRecords = selectedRecordsPostUpdate.filter((r) =>
records.find((r2) => r2.id === r.id && r2.idTx === r.idTx)
);
// if no records were updated, throw error
if (updatedRecords.length === 0) {
throw new Error("No records updated");
}
await tx.insert(statusLog).values(
updatedRecords.map((r) => ({
recordId: r.id,
statusId: status,
message: "bulk",
}))
);

// if any records were not updated, throw error
if (updatedRecords.length !== records.length) {
throw new Error("Not all records updated");
}
});
33 replies