Nickolaki
Nickolaki
Explore posts from servers
DTDrizzle Team
Created by Nickolaki on 8/16/2023 in #help
Db race condition
True, thank you bro
38 replies
DTDrizzle Team
Created by Nickolaki on 8/16/2023 in #help
Db race condition
Yeah the unique thing seems to have solved it. Will try without transaction too to see if its even needed
38 replies
DTDrizzle Team
Created by Nickolaki on 8/16/2023 in #help
Db race condition
I guess you can't, ill do some testing
38 replies
DTDrizzle Team
Created by Nickolaki on 8/16/2023 in #help
Db race condition
The intresting thing about changing the col to unique. I wonder if i will still run into the issues
38 replies
DTDrizzle Team
Created by Nickolaki on 8/16/2023 in #help
Db race condition
Yes exactly
38 replies
DTDrizzle Team
Created by Nickolaki on 8/16/2023 in #help
Db race condition
I currently don't have a user table 😛
38 replies
DTDrizzle Team
Created by Nickolaki on 8/16/2023 in #help
Db race condition
So would a unique col sort this?
38 replies
DTDrizzle Team
Created by Nickolaki on 8/16/2023 in #help
Db race condition
only 1
38 replies
DTDrizzle Team
Created by Nickolaki on 8/16/2023 in #help
Db race condition
Na I don't want them to
38 replies
DTDrizzle Team
Created by Nickolaki on 8/16/2023 in #help
Db race condition
(I don't want that)
38 replies
DTDrizzle Team
Created by Nickolaki on 8/16/2023 in #help
Db race condition
Nothing stopping a user with email "[email protected]" to have 2 journeys atm
38 replies
DTDrizzle Team
Created by Nickolaki on 8/16/2023 in #help
Db race condition
export const journeys = pgTable("journey", {
id: uuid("id").defaultRandom().primaryKey(),
customer_firstName: text("customer_firstName").notNull(),
customer_lastName: text("customer_lastName").notNull(),
customer_emailAddress: text("customer_emailAddress").notNull(),
currentstep: text("currentstep").notNull(),
});
export const journeys = pgTable("journey", {
id: uuid("id").defaultRandom().primaryKey(),
customer_firstName: text("customer_firstName").notNull(),
customer_lastName: text("customer_lastName").notNull(),
customer_emailAddress: text("customer_emailAddress").notNull(),
currentstep: text("currentstep").notNull(),
});
38 replies
DTDrizzle Team
Created by Nickolaki on 8/16/2023 in #help
Db race condition
I do not its simply a text property in the table, so maybe that's what I should do
38 replies
DTDrizzle Team
Created by Nickolaki on 8/16/2023 in #help
Db race condition
@Raphaël M (@rphlmr) ⚡ Boss, can you help me with this? 😘
38 replies
DTDrizzle Team
Created by Nickolaki on 8/16/2023 in #help
Db race condition
Has that given you context? 🙂
38 replies
DTDrizzle Team
Created by Nickolaki on 8/16/2023 in #help
Db race condition
I don't know if I'm remotely using db transactions for what they are supposed to be for.
38 replies
DTDrizzle Team
Created by Nickolaki on 8/16/2023 in #help
Db race condition
I have an endpoint that create a db journey for a customer. A customer should only ever have 1 journey.
export async function POST(req: Request) {
const body = (await req.json()) as CreateJourneyRequest;

const existingJourney = await db.transaction(async (tx) => {
const result = await tx.query.journeys.findFirst({
with: {
products: {
with: {
product: true,
},
},
},
where: (journeys, { eq }) =>
eq(journeys.customer_emailAddress, body.emailAddress),
});

return result;
});

if (existingJourney)
return NextResponse.json(existingJourney);
}

const journeyId = uuidv4();

const newJourney = {
id: journeyId,
customer_firstName: body.firstName,
customer_lastName: body.lastName,
customer_emailAddress: body.emailAddress,
completed: false,
currentstep: "review",
};

await db.transaction(async (tx) => {
console.log(Date.now(), "Inserting New Journey");
await tx.insert(journeys).values(newJourney);
console.log(Date.now(), "Finished Inserting New Journey");

const productsToLink = await tx.query.products.findMany({
where: (products) => inArray(products.name, body.initialProducts),
});

const journeysProductsToInsert = productsToLink.map((product) => ({
journeyId,
productId: product.id,
}));

await tx.insert(journeysToProducts).values(journeysProductsToInsert);
});
}
export async function POST(req: Request) {
const body = (await req.json()) as CreateJourneyRequest;

const existingJourney = await db.transaction(async (tx) => {
const result = await tx.query.journeys.findFirst({
with: {
products: {
with: {
product: true,
},
},
},
where: (journeys, { eq }) =>
eq(journeys.customer_emailAddress, body.emailAddress),
});

return result;
});

if (existingJourney)
return NextResponse.json(existingJourney);
}

const journeyId = uuidv4();

const newJourney = {
id: journeyId,
customer_firstName: body.firstName,
customer_lastName: body.lastName,
customer_emailAddress: body.emailAddress,
completed: false,
currentstep: "review",
};

await db.transaction(async (tx) => {
console.log(Date.now(), "Inserting New Journey");
await tx.insert(journeys).values(newJourney);
console.log(Date.now(), "Finished Inserting New Journey");

const productsToLink = await tx.query.products.findMany({
where: (products) => inArray(products.name, body.initialProducts),
});

const journeysProductsToInsert = productsToLink.map((product) => ({
journeyId,
productId: product.id,
}));

await tx.insert(journeysToProducts).values(journeysProductsToInsert);
});
}
What I'm seeing is that If I call this endpoint twice in very fast succession. 1st request will not get an existingJourney and proceeed to insert one. 2nd request will query existingJourney but will still be undefined as the inserted one from request 1 is not complete or committed (not sure how it works).
38 replies
DTDrizzle Team
Created by Nickolaki on 8/13/2023 in #help
Query where clause with array.
Perfect thank you, just seen all the possible operators on the docs. ❤️
4 replies
DTDrizzle Team
Created by Nickolaki on 8/11/2023 in #help
Insert One to Many
You and me both, god knows why Im using mysql
52 replies
DTDrizzle Team
Created by Nickolaki on 8/11/2023 in #help
Insert One to Many
We shall see I guess, if that doesn't work its for another ticket i guess 😛 Thank you for your help bro
52 replies