RUNNb
RUNNb
Explore posts from servers
DTDrizzle Team
Created by RUNNb on 11/10/2024 in #help
Rollback not happening on tx.rollback() [PG DRIVER]
export async function createRestaurantMenuMutation({
label,
type,
availabilityDate,
categories,
}: CreateRestaurantMenuProps) {
let error: string | undefined;

try {
await db.transaction(async (tx) => {
const createMenu = await db
.insert(menus)
.values(restaurantMenuParse.data)
.returning()
.execute();

if (!createMenu[0]) {
error = "Não foi possível criar o menu.";
tx.rollback();
}

categories.forEach(async (category) => {
const categoryParse =
await createMenuCategorySchema.safeParseAsync({
...category,
});

if (!categoryParse.success) {
console.error(
"Error parsing category: ",
categoryParse.error,
);
error = "Existe um erro nos dados inseridos.";
tx.rollback();
}

const createCategory = await tx
.insert(menuCategories)
.values({
name: category.name,
menuId: createMenu[0]!.id,
})
.returning()
.execute();

if (!createCategory[0]) {
error = `Ocorreu um erro a criar a categoria ${category.name}. `;
tx.rollback();
}
});
return {
...createMenu[0],
categories,
};
});
} catch (e) {
console.log("Exception creating menu: ", { e });
throw new Error(error);
}
}
export async function createRestaurantMenuMutation({
label,
type,
availabilityDate,
categories,
}: CreateRestaurantMenuProps) {
let error: string | undefined;

try {
await db.transaction(async (tx) => {
const createMenu = await db
.insert(menus)
.values(restaurantMenuParse.data)
.returning()
.execute();

if (!createMenu[0]) {
error = "Não foi possível criar o menu.";
tx.rollback();
}

categories.forEach(async (category) => {
const categoryParse =
await createMenuCategorySchema.safeParseAsync({
...category,
});

if (!categoryParse.success) {
console.error(
"Error parsing category: ",
categoryParse.error,
);
error = "Existe um erro nos dados inseridos.";
tx.rollback();
}

const createCategory = await tx
.insert(menuCategories)
.values({
name: category.name,
menuId: createMenu[0]!.id,
})
.returning()
.execute();

if (!createCategory[0]) {
error = `Ocorreu um erro a criar a categoria ${category.name}. `;
tx.rollback();
}
});
return {
...createMenu[0],
categories,
};
});
} catch (e) {
console.log("Exception creating menu: ", { e });
throw new Error(error);
}
}
I've tried a couple different approachs (throwing errors etc, keeps the same issue)
7 replies
RRailway
Created by RUNNb on 10/27/2023 in #✋|help
How to use volume with nextjs?
I am trying to persist image on volume. RAILWAY_VOLUME_MOUNT_PATH =/images I've tried to implement the change for the railway volume but without success. This is the api route for local storage on /public/upload_images
import { NextApiHandler, NextApiRequest } from "next";
import formidable from "formidable";
import path from "path";
import fs from "fs/promises";
import {randomUUID} from "crypto";

export const config = {
api: {
bodyParser: false,
},
};

const readFile = (
req: NextApiRequest,
filename: string,
saveLocally?: boolean
): Promise<{ fields: formidable.Fields; files: formidable.Files }> => {
const options: formidable.Options = {};
if (saveLocally) {
options.uploadDir = path.join(process.cwd(), "/public/upload_images");
options.filename = (name, ext, path, form) => {
const extension = path.originalFilename?.split(".")[1];
return `${filename}.${extension}`;
};
}
options.maxFileSize = 4000 * 1024 * 1024;
const form = formidable(options);
return new Promise((resolve, reject) => {
form.parse(req, (err, fields, files) => {
if (err) reject(err);
resolve({ fields, files });
});
});
};
const handler: NextApiHandler = async (req, res) => {
try {
// await fs.readdir(path.join(process.cwd() + "/public", "/upload_images"));
await fs.readdir(path.join(process.cwd() + "/public", "/upload_images"));
} catch (error) {
// await fs.mkdir(path.join(process.cwd() + "/public", "/upload_images"));
await fs.mkdir(path.join(process.cwd() + "/public", "/upload_images"));
}

const filename = randomUUID();
const { files } = await readFile(req, filename, true);
// @ts-ignore
const extension = files.image[0]?.newFilename.split('.')[1];
res.json({ done: "ok", filename: `${filename}.${extension}` });
};
export default handler;
import { NextApiHandler, NextApiRequest } from "next";
import formidable from "formidable";
import path from "path";
import fs from "fs/promises";
import {randomUUID} from "crypto";

export const config = {
api: {
bodyParser: false,
},
};

const readFile = (
req: NextApiRequest,
filename: string,
saveLocally?: boolean
): Promise<{ fields: formidable.Fields; files: formidable.Files }> => {
const options: formidable.Options = {};
if (saveLocally) {
options.uploadDir = path.join(process.cwd(), "/public/upload_images");
options.filename = (name, ext, path, form) => {
const extension = path.originalFilename?.split(".")[1];
return `${filename}.${extension}`;
};
}
options.maxFileSize = 4000 * 1024 * 1024;
const form = formidable(options);
return new Promise((resolve, reject) => {
form.parse(req, (err, fields, files) => {
if (err) reject(err);
resolve({ fields, files });
});
});
};
const handler: NextApiHandler = async (req, res) => {
try {
// await fs.readdir(path.join(process.cwd() + "/public", "/upload_images"));
await fs.readdir(path.join(process.cwd() + "/public", "/upload_images"));
} catch (error) {
// await fs.mkdir(path.join(process.cwd() + "/public", "/upload_images"));
await fs.mkdir(path.join(process.cwd() + "/public", "/upload_images"));
}

const filename = randomUUID();
const { files } = await readFile(req, filename, true);
// @ts-ignore
const extension = files.image[0]?.newFilename.split('.')[1];
res.json({ done: "ok", filename: `${filename}.${extension}` });
};
export default handler;
38 replies
RRailway
Created by RUNNb on 5/22/2023 in #✋|help
Can't access api url (nestjs project)
So on my local env if I access localhost:port/api I can see api swagger documentation. Also I can do any request. When deployed on railway I get no errors, I see app running logs but can't access api documentation page or do any request. Any ideas on what is possibly going on?
17 replies