fix: seed.js не завершает процесс при запуске сервера
process.exit(0) при существующих товарах убивал Node до app.listen — 502 от Caddy. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+25
-9
@@ -1,9 +1,10 @@
|
|||||||
const { db } = require('./db');
|
const { db } = require('./db');
|
||||||
|
|
||||||
|
function runSeed() {
|
||||||
const count = db.prepare('SELECT COUNT(*) AS n FROM products').get().n;
|
const count = db.prepare('SELECT COUNT(*) AS n FROM products').get().n;
|
||||||
if (count > 0) {
|
if (count > 0) {
|
||||||
console.log('База уже содержит товары, пропуск seed.');
|
console.log('База уже содержит товары, пропуск seed.');
|
||||||
process.exit(0);
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const insertCategory = db.prepare(
|
const insertCategory = db.prepare(
|
||||||
@@ -35,7 +36,8 @@ const seed = db.transaction(() => {
|
|||||||
description: 'Шумоподавление, 30 ч автономности, Bluetooth 5.3.',
|
description: 'Шумоподавление, 30 ч автономности, Bluetooth 5.3.',
|
||||||
price: 499000,
|
price: 499000,
|
||||||
stock: 24,
|
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',
|
cat: 'electronics',
|
||||||
@@ -44,7 +46,8 @@ const seed = db.transaction(() => {
|
|||||||
description: 'Пульс, GPS, водозащита IP68.',
|
description: 'Пульс, GPS, водозащита IP68.',
|
||||||
price: 1299000,
|
price: 1299000,
|
||||||
stock: 15,
|
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',
|
cat: 'electronics',
|
||||||
@@ -53,7 +56,8 @@ const seed = db.transaction(() => {
|
|||||||
description: 'Hot-swap, RGB подсветка, переключатели Brown.',
|
description: 'Hot-swap, RGB подсветка, переключатели Brown.',
|
||||||
price: 749000,
|
price: 749000,
|
||||||
stock: 18,
|
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',
|
cat: 'clothing',
|
||||||
@@ -62,7 +66,8 @@ const seed = db.transaction(() => {
|
|||||||
description: '100% хлопок, унисекс, размеры S–XL.',
|
description: '100% хлопок, унисекс, размеры S–XL.',
|
||||||
price: 199000,
|
price: 199000,
|
||||||
stock: 50,
|
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',
|
cat: 'clothing',
|
||||||
@@ -71,7 +76,8 @@ const seed = db.transaction(() => {
|
|||||||
description: 'Классический крой, прочный деним.',
|
description: 'Классический крой, прочный деним.',
|
||||||
price: 459000,
|
price: 459000,
|
||||||
stock: 12,
|
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',
|
cat: 'home',
|
||||||
@@ -80,7 +86,8 @@ const seed = db.transaction(() => {
|
|||||||
description: 'Объём 350 мл, подходит для посудомойки.',
|
description: 'Объём 350 мл, подходит для посудомойки.',
|
||||||
price: 89000,
|
price: 89000,
|
||||||
stock: 40,
|
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',
|
cat: 'home',
|
||||||
@@ -89,7 +96,8 @@ const seed = db.transaction(() => {
|
|||||||
description: 'LED, регулировка яркости и цветовой температуры.',
|
description: 'LED, регулировка яркости и цветовой температуры.',
|
||||||
price: 329000,
|
price: 329000,
|
||||||
stock: 20,
|
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',
|
cat: 'home',
|
||||||
@@ -98,7 +106,8 @@ const seed = db.transaction(() => {
|
|||||||
description: 'Мягкий флис, 150×200 см.',
|
description: 'Мягкий флис, 150×200 см.',
|
||||||
price: 249000,
|
price: 249000,
|
||||||
stock: 30,
|
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',
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
@@ -117,3 +126,10 @@ const seed = db.transaction(() => {
|
|||||||
|
|
||||||
seed();
|
seed();
|
||||||
console.log('Добавлено категорий:', categories.length, ', товаров: 8');
|
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