mnik01
mnik01
Explore posts from servers
CDCloudflare Developers
Created by mnik01 on 1/19/2024 in #workers-help
Batch request to D1 fails for some reason.
What am i doing wrong? Maybe there are better ways? Logs an error: Failed. Drivers count: 799, meta: undefined. Workers code:
const drivers = getDriversFromOrders(allOrders);
// array with ~800 items like {id, name, work_rules}[]
const currentDate = new Date().toISOString().split('T')[0];

if (drivers.length === 0) return console.log('No drivers found');
// Prepare the data for bulk insert
let driversData = drivers.map(driver => `('${driver.driver_id}', '${driver.driver_name}', '${driver.work_rules}')`);
let ordersData = drivers.map(driver => `('${driver.driver_id}', '${currentDate}', ${driver.orders_count})`);

// SQL for bulk insert with replace in `drivers` table
let sqlDrivers = `INSERT OR REPLACE INTO drivers (driver_id, driver_name, city) VALUES ${driversData.join(", ")}`;

// SQL for bulk insert with replace in `daily_orders` table
// Assuming driver_id and date form a unique combination
let sqlOrders = `INSERT OR REPLACE INTO daily_orders (driver_id, date, orders_count) VALUES ${ordersData.join(", ")}`;

// Execute the queries
const { meta, success } = await env.DB.batch([
env.DB.prepare(sqlDrivers),
env.DB.prepare(sqlOrders),
]);

if (success) {
console.log(`Sucessfully Completed. meta: ${JSON.stringify(meta)}. Drivers count: ${drivers?.length}`);
} else {
console.log(`Failed. Drivers count: ${drivers?.length}, meta: ${JSON.stringify(meta)}`);
}
const drivers = getDriversFromOrders(allOrders);
// array with ~800 items like {id, name, work_rules}[]
const currentDate = new Date().toISOString().split('T')[0];

if (drivers.length === 0) return console.log('No drivers found');
// Prepare the data for bulk insert
let driversData = drivers.map(driver => `('${driver.driver_id}', '${driver.driver_name}', '${driver.work_rules}')`);
let ordersData = drivers.map(driver => `('${driver.driver_id}', '${currentDate}', ${driver.orders_count})`);

// SQL for bulk insert with replace in `drivers` table
let sqlDrivers = `INSERT OR REPLACE INTO drivers (driver_id, driver_name, city) VALUES ${driversData.join(", ")}`;

// SQL for bulk insert with replace in `daily_orders` table
// Assuming driver_id and date form a unique combination
let sqlOrders = `INSERT OR REPLACE INTO daily_orders (driver_id, date, orders_count) VALUES ${ordersData.join(", ")}`;

// Execute the queries
const { meta, success } = await env.DB.batch([
env.DB.prepare(sqlDrivers),
env.DB.prepare(sqlOrders),
]);

if (success) {
console.log(`Sucessfully Completed. meta: ${JSON.stringify(meta)}. Drivers count: ${drivers?.length}`);
} else {
console.log(`Failed. Drivers count: ${drivers?.length}, meta: ${JSON.stringify(meta)}`);
}
This way on DB creation works:
-- Insert initial data into the 'drivers' table
INSERT INTO drivers (driver_id, driver_name, city) VALUES
('D001', 'John Doe', 'Almaty'),
('D002', 'Jane Smith', 'Nur-Sultan'),
('D003', 'Mike Brown', 'Shymkent');

-- Insert initial data into the 'daily_orders' table
INSERT INTO daily_orders (driver_id, date, orders_count) VALUES
('D001', '2024-01-15', 10),
('D002', '2024-01-15', 8),
('D003', '2024-01-15', 7),
('D001', '2024-01-16', 12),
('D002', '2024-01-16', 14),
('D002', '2024-01-17', 9),
('D003', '2024-01-17', 10);
-- Insert initial data into the 'drivers' table
INSERT INTO drivers (driver_id, driver_name, city) VALUES
('D001', 'John Doe', 'Almaty'),
('D002', 'Jane Smith', 'Nur-Sultan'),
('D003', 'Mike Brown', 'Shymkent');

-- Insert initial data into the 'daily_orders' table
INSERT INTO daily_orders (driver_id, date, orders_count) VALUES
('D001', '2024-01-15', 10),
('D002', '2024-01-15', 8),
('D003', '2024-01-15', 7),
('D001', '2024-01-16', 12),
('D002', '2024-01-16', 14),
('D002', '2024-01-17', 9),
('D003', '2024-01-17', 10);
3 replies
RRailway
Created by mnik01 on 8/2/2023 in #✋|help
Can't find any github repo. App stays on "Configure GitHub App"
10 replies
RRailway
Created by mnik01 on 3/12/2023 in #✋|help
Application URL disappeared in dashboard after deployment of backend server app
18 replies