fix: seed.js не завершает процесс при запуске сервера

process.exit(0) при существующих товарах убивал Node до app.listen — 502 от Caddy.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
shop
2026-05-16 21:12:18 +03:00
parent d3cc243686
commit 1ce40fd40a
2 changed files with 129 additions and 113 deletions
+39 -23
View File
@@ -1,27 +1,28 @@
const { db } = require('./db');
const count = db.prepare('SELECT COUNT(*) AS n FROM products').get().n;
if (count > 0) {
function runSeed() {
const count = db.prepare('SELECT COUNT(*) AS n FROM products').get().n;
if (count > 0) {
console.log('База уже содержит товары, пропуск seed.');
process.exit(0);
}
return;
}
const insertCategory = db.prepare(
const insertCategory = db.prepare(
'INSERT INTO categories (slug, name) VALUES (?, ?)'
);
const insertProduct = db.prepare(`
);
const insertProduct = db.prepare(`
INSERT INTO products (category_id, slug, name, description, price_cents, stock, image_url)
VALUES (?, ?, ?, ?, ?, ?, ?)
`);
`);
const categories = [
const categories = [
{ slug: 'electronics', name: 'Электроника' },
{ slug: 'clothing', name: 'Одежда' },
{ slug: 'home', name: 'Дом и быт' },
];
];
const categoryIds = {};
const seed = db.transaction(() => {
const categoryIds = {};
const seed = db.transaction(() => {
for (const c of categories) {
const r = insertCategory.run(c.slug, c.name);
categoryIds[c.slug] = r.lastInsertRowid;
@@ -35,7 +36,8 @@ const seed = db.transaction(() => {
description: 'Шумоподавление, 30 ч автономности, Bluetooth 5.3.',
price: 499000,
stock: 24,
image: 'https://images.unsplash.com/photo-1505740420928-5e560c06d30e?w=400&h=400&fit=crop',
image:
'https://images.unsplash.com/photo-1505740420928-5e560c06d30e?w=400&h=400&fit=crop',
},
{
cat: 'electronics',
@@ -44,7 +46,8 @@ const seed = db.transaction(() => {
description: 'Пульс, GPS, водозащита IP68.',
price: 1299000,
stock: 15,
image: 'https://images.unsplash.com/photo-1523275335684-37898b6baf30?w=400&h=400&fit=crop',
image:
'https://images.unsplash.com/photo-1523275335684-37898b6baf30?w=400&h=400&fit=crop',
},
{
cat: 'electronics',
@@ -53,7 +56,8 @@ const seed = db.transaction(() => {
description: 'Hot-swap, RGB подсветка, переключатели Brown.',
price: 749000,
stock: 18,
image: 'https://images.unsplash.com/photo-1587829741301-dc798b83add3?w=400&h=400&fit=crop',
image:
'https://images.unsplash.com/photo-1587829741301-dc798b83add3?w=400&h=400&fit=crop',
},
{
cat: 'clothing',
@@ -62,7 +66,8 @@ const seed = db.transaction(() => {
description: '100% хлопок, унисекс, размеры S–XL.',
price: 199000,
stock: 50,
image: 'https://images.unsplash.com/photo-1521572163474-6864f9cf17ab?w=400&h=400&fit=crop',
image:
'https://images.unsplash.com/photo-1521572163474-6864f9cf17ab?w=400&h=400&fit=crop',
},
{
cat: 'clothing',
@@ -71,7 +76,8 @@ const seed = db.transaction(() => {
description: 'Классический крой, прочный деним.',
price: 459000,
stock: 12,
image: 'https://images.unsplash.com/photo-1551028711-00167b16eac5?w=400&h=400&fit=crop',
image:
'https://images.unsplash.com/photo-1551028711-00167b16eac5?w=400&h=400&fit=crop',
},
{
cat: 'home',
@@ -80,7 +86,8 @@ const seed = db.transaction(() => {
description: 'Объём 350 мл, подходит для посудомойки.',
price: 89000,
stock: 40,
image: 'https://images.unsplash.com/photo-1514228742587-6b1558fcca13?w=400&h=400&fit=crop',
image:
'https://images.unsplash.com/photo-1514228742587-6b1558fcca13?w=400&h=400&fit=crop',
},
{
cat: 'home',
@@ -89,7 +96,8 @@ const seed = db.transaction(() => {
description: 'LED, регулировка яркости и цветовой температуры.',
price: 329000,
stock: 20,
image: 'https://images.unsplash.com/photo-1507473885765-e6ed057f782c?w=400&h=400&fit=crop',
image:
'https://images.unsplash.com/photo-1507473885765-e6ed057f782c?w=400&h=400&fit=crop',
},
{
cat: 'home',
@@ -98,7 +106,8 @@ const seed = db.transaction(() => {
description: 'Мягкий флис, 150×200 см.',
price: 249000,
stock: 30,
image: 'https://images.unsplash.com/photo-1555041469-a586c12e1940?w=400&h=400&fit=crop',
image:
'https://images.unsplash.com/photo-1555041469-a586c12e1940?w=400&h=400&fit=crop',
},
];
@@ -113,7 +122,14 @@ const seed = db.transaction(() => {
p.image
);
}
});
});
seed();
console.log('Добавлено категорий:', categories.length, ', товаров: 8');
seed();
console.log('Добавлено категорий:', categories.length, ', товаров: 8');
}
module.exports = { runSeed };
if (require.main === module) {
runSeed();
}
+1 -1
View File
@@ -4,7 +4,7 @@ const session = require('express-session');
const SQLiteStore = require('connect-sqlite3')(session);
require('./db');
require('./seed');
require('./seed').runSeed();
const { loadUser } = require('./middleware/auth');
const healthRoutes = require('./routes/health');