fix: seed.js не завершает процесс при запуске сервера
process.exit(0) при существующих товарах убивал Node до app.listen — 502 от Caddy. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+128
-112
@@ -1,119 +1,135 @@
|
|||||||
const { db } = require('./db');
|
const { db } = require('./db');
|
||||||
|
|
||||||
const count = db.prepare('SELECT COUNT(*) AS n FROM products').get().n;
|
function runSeed() {
|
||||||
if (count > 0) {
|
const count = db.prepare('SELECT COUNT(*) AS n FROM products').get().n;
|
||||||
console.log('База уже содержит товары, пропуск seed.');
|
if (count > 0) {
|
||||||
process.exit(0);
|
console.log('База уже содержит товары, пропуск seed.');
|
||||||
}
|
return;
|
||||||
|
|
||||||
const insertCategory = db.prepare(
|
|
||||||
'INSERT INTO categories (slug, name) VALUES (?, ?)'
|
|
||||||
);
|
|
||||||
const insertProduct = db.prepare(`
|
|
||||||
INSERT INTO products (category_id, slug, name, description, price_cents, stock, image_url)
|
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
||||||
`);
|
|
||||||
|
|
||||||
const categories = [
|
|
||||||
{ slug: 'electronics', name: 'Электроника' },
|
|
||||||
{ slug: 'clothing', name: 'Одежда' },
|
|
||||||
{ slug: 'home', name: 'Дом и быт' },
|
|
||||||
];
|
|
||||||
|
|
||||||
const categoryIds = {};
|
|
||||||
const seed = db.transaction(() => {
|
|
||||||
for (const c of categories) {
|
|
||||||
const r = insertCategory.run(c.slug, c.name);
|
|
||||||
categoryIds[c.slug] = r.lastInsertRowid;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const products = [
|
const insertCategory = db.prepare(
|
||||||
{
|
'INSERT INTO categories (slug, name) VALUES (?, ?)'
|
||||||
cat: 'electronics',
|
);
|
||||||
slug: 'wireless-headphones',
|
const insertProduct = db.prepare(`
|
||||||
name: 'Беспроводные наушники',
|
INSERT INTO products (category_id, slug, name, description, price_cents, stock, image_url)
|
||||||
description: 'Шумоподавление, 30 ч автономности, Bluetooth 5.3.',
|
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||||
price: 499000,
|
`);
|
||||||
stock: 24,
|
|
||||||
image: 'https://images.unsplash.com/photo-1505740420928-5e560c06d30e?w=400&h=400&fit=crop',
|
const categories = [
|
||||||
},
|
{ slug: 'electronics', name: 'Электроника' },
|
||||||
{
|
{ slug: 'clothing', name: 'Одежда' },
|
||||||
cat: 'electronics',
|
{ slug: 'home', name: 'Дом и быт' },
|
||||||
slug: 'smart-watch',
|
|
||||||
name: 'Умные часы',
|
|
||||||
description: 'Пульс, GPS, водозащита IP68.',
|
|
||||||
price: 1299000,
|
|
||||||
stock: 15,
|
|
||||||
image: 'https://images.unsplash.com/photo-1523275335684-37898b6baf30?w=400&h=400&fit=crop',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
cat: 'electronics',
|
|
||||||
slug: 'mechanical-keyboard',
|
|
||||||
name: 'Механическая клавиатура',
|
|
||||||
description: 'Hot-swap, RGB подсветка, переключатели Brown.',
|
|
||||||
price: 749000,
|
|
||||||
stock: 18,
|
|
||||||
image: 'https://images.unsplash.com/photo-1587829741301-dc798b83add3?w=400&h=400&fit=crop',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
cat: 'clothing',
|
|
||||||
slug: 'cotton-tshirt',
|
|
||||||
name: 'Хлопковая футболка',
|
|
||||||
description: '100% хлопок, унисекс, размеры S–XL.',
|
|
||||||
price: 199000,
|
|
||||||
stock: 50,
|
|
||||||
image: 'https://images.unsplash.com/photo-1521572163474-6864f9cf17ab?w=400&h=400&fit=crop',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
cat: 'clothing',
|
|
||||||
slug: 'denim-jacket',
|
|
||||||
name: 'Джинсовая куртка',
|
|
||||||
description: 'Классический крой, прочный деним.',
|
|
||||||
price: 459000,
|
|
||||||
stock: 12,
|
|
||||||
image: 'https://images.unsplash.com/photo-1551028711-00167b16eac5?w=400&h=400&fit=crop',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
cat: 'home',
|
|
||||||
slug: 'ceramic-mug',
|
|
||||||
name: 'Керамическая кружка',
|
|
||||||
description: 'Объём 350 мл, подходит для посудомойки.',
|
|
||||||
price: 89000,
|
|
||||||
stock: 40,
|
|
||||||
image: 'https://images.unsplash.com/photo-1514228742587-6b1558fcca13?w=400&h=400&fit=crop',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
cat: 'home',
|
|
||||||
slug: 'desk-lamp',
|
|
||||||
name: 'Настольная лампа',
|
|
||||||
description: 'LED, регулировка яркости и цветовой температуры.',
|
|
||||||
price: 329000,
|
|
||||||
stock: 20,
|
|
||||||
image: 'https://images.unsplash.com/photo-1507473885765-e6ed057f782c?w=400&h=400&fit=crop',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
cat: 'home',
|
|
||||||
slug: 'throw-blanket',
|
|
||||||
name: 'Плед',
|
|
||||||
description: 'Мягкий флис, 150×200 см.',
|
|
||||||
price: 249000,
|
|
||||||
stock: 30,
|
|
||||||
image: 'https://images.unsplash.com/photo-1555041469-a586c12e1940?w=400&h=400&fit=crop',
|
|
||||||
},
|
|
||||||
];
|
];
|
||||||
|
|
||||||
for (const p of products) {
|
const categoryIds = {};
|
||||||
insertProduct.run(
|
const seed = db.transaction(() => {
|
||||||
categoryIds[p.cat],
|
for (const c of categories) {
|
||||||
p.slug,
|
const r = insertCategory.run(c.slug, c.name);
|
||||||
p.name,
|
categoryIds[c.slug] = r.lastInsertRowid;
|
||||||
p.description,
|
}
|
||||||
p.price,
|
|
||||||
p.stock,
|
|
||||||
p.image
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
seed();
|
const products = [
|
||||||
console.log('Добавлено категорий:', categories.length, ', товаров: 8');
|
{
|
||||||
|
cat: 'electronics',
|
||||||
|
slug: 'wireless-headphones',
|
||||||
|
name: 'Беспроводные наушники',
|
||||||
|
description: 'Шумоподавление, 30 ч автономности, Bluetooth 5.3.',
|
||||||
|
price: 499000,
|
||||||
|
stock: 24,
|
||||||
|
image:
|
||||||
|
'https://images.unsplash.com/photo-1505740420928-5e560c06d30e?w=400&h=400&fit=crop',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
cat: 'electronics',
|
||||||
|
slug: 'smart-watch',
|
||||||
|
name: 'Умные часы',
|
||||||
|
description: 'Пульс, GPS, водозащита IP68.',
|
||||||
|
price: 1299000,
|
||||||
|
stock: 15,
|
||||||
|
image:
|
||||||
|
'https://images.unsplash.com/photo-1523275335684-37898b6baf30?w=400&h=400&fit=crop',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
cat: 'electronics',
|
||||||
|
slug: 'mechanical-keyboard',
|
||||||
|
name: 'Механическая клавиатура',
|
||||||
|
description: 'Hot-swap, RGB подсветка, переключатели Brown.',
|
||||||
|
price: 749000,
|
||||||
|
stock: 18,
|
||||||
|
image:
|
||||||
|
'https://images.unsplash.com/photo-1587829741301-dc798b83add3?w=400&h=400&fit=crop',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
cat: 'clothing',
|
||||||
|
slug: 'cotton-tshirt',
|
||||||
|
name: 'Хлопковая футболка',
|
||||||
|
description: '100% хлопок, унисекс, размеры S–XL.',
|
||||||
|
price: 199000,
|
||||||
|
stock: 50,
|
||||||
|
image:
|
||||||
|
'https://images.unsplash.com/photo-1521572163474-6864f9cf17ab?w=400&h=400&fit=crop',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
cat: 'clothing',
|
||||||
|
slug: 'denim-jacket',
|
||||||
|
name: 'Джинсовая куртка',
|
||||||
|
description: 'Классический крой, прочный деним.',
|
||||||
|
price: 459000,
|
||||||
|
stock: 12,
|
||||||
|
image:
|
||||||
|
'https://images.unsplash.com/photo-1551028711-00167b16eac5?w=400&h=400&fit=crop',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
cat: 'home',
|
||||||
|
slug: 'ceramic-mug',
|
||||||
|
name: 'Керамическая кружка',
|
||||||
|
description: 'Объём 350 мл, подходит для посудомойки.',
|
||||||
|
price: 89000,
|
||||||
|
stock: 40,
|
||||||
|
image:
|
||||||
|
'https://images.unsplash.com/photo-1514228742587-6b1558fcca13?w=400&h=400&fit=crop',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
cat: 'home',
|
||||||
|
slug: 'desk-lamp',
|
||||||
|
name: 'Настольная лампа',
|
||||||
|
description: 'LED, регулировка яркости и цветовой температуры.',
|
||||||
|
price: 329000,
|
||||||
|
stock: 20,
|
||||||
|
image:
|
||||||
|
'https://images.unsplash.com/photo-1507473885765-e6ed057f782c?w=400&h=400&fit=crop',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
cat: 'home',
|
||||||
|
slug: 'throw-blanket',
|
||||||
|
name: 'Плед',
|
||||||
|
description: 'Мягкий флис, 150×200 см.',
|
||||||
|
price: 249000,
|
||||||
|
stock: 30,
|
||||||
|
image:
|
||||||
|
'https://images.unsplash.com/photo-1555041469-a586c12e1940?w=400&h=400&fit=crop',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
for (const p of products) {
|
||||||
|
insertProduct.run(
|
||||||
|
categoryIds[p.cat],
|
||||||
|
p.slug,
|
||||||
|
p.name,
|
||||||
|
p.description,
|
||||||
|
p.price,
|
||||||
|
p.stock,
|
||||||
|
p.image
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
seed();
|
||||||
|
console.log('Добавлено категорий:', categories.length, ', товаров: 8');
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { runSeed };
|
||||||
|
|
||||||
|
if (require.main === module) {
|
||||||
|
runSeed();
|
||||||
|
}
|
||||||
|
|||||||
+1
-1
@@ -4,7 +4,7 @@ const session = require('express-session');
|
|||||||
const SQLiteStore = require('connect-sqlite3')(session);
|
const SQLiteStore = require('connect-sqlite3')(session);
|
||||||
|
|
||||||
require('./db');
|
require('./db');
|
||||||
require('./seed');
|
require('./seed').runSeed();
|
||||||
|
|
||||||
const { loadUser } = require('./middleware/auth');
|
const { loadUser } = require('./middleware/auth');
|
||||||
const healthRoutes = require('./routes/health');
|
const healthRoutes = require('./routes/health');
|
||||||
|
|||||||
Reference in New Issue
Block a user