-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit-data.js
More file actions
207 lines (181 loc) · 7.34 KB
/
init-data.js
File metadata and controls
207 lines (181 loc) · 7.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import { insertFilamentSchema } from './shared/schema.js';
import { db } from './server/db.js';
import { filaments, manufacturers, materials, diameters, colors, storageLocations } from './shared/schema.js';
import fs from 'fs';
import path from 'path';
async function insertInitialData() {
try {
// Check lock file to prevent repeated initialization
const lockFile = path.resolve('./init-data.lock');
if (fs.existsSync(lockFile)) {
console.log("Database has already been initialized (lock file found). Skipping data insertion.");
process.exit(0);
return;
}
console.log("Checking for existing data...");
// Check if all required tables exist
try {
await db.select().from(manufacturers).limit(1);
await db.select().from(materials).limit(1);
await db.select().from(colors).limit(1);
await db.select().from(diameters).limit(1);
await db.select().from(storageLocations).limit(1);
await db.select().from(filaments).limit(1);
console.log("All tables exist and are accessible.");
} catch (error) {
console.error("Error checking tables:", error);
console.log("Trying to create tables...");
try {
// Create tables manually if they don't exist
await db.execute(`
CREATE TABLE IF NOT EXISTS manufacturers (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL
);
CREATE TABLE IF NOT EXISTS materials (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL
);
CREATE TABLE IF NOT EXISTS colors (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
code TEXT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL
);
CREATE TABLE IF NOT EXISTS diameters (
id SERIAL PRIMARY KEY,
value NUMERIC NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL
);
CREATE TABLE IF NOT EXISTS storage_locations (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL
);
CREATE TABLE IF NOT EXISTS filaments (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
manufacturer TEXT,
material TEXT NOT NULL,
color_name TEXT,
color_code TEXT,
diameter NUMERIC,
print_temp TEXT,
total_weight NUMERIC NOT NULL,
remaining_percentage NUMERIC NOT NULL,
purchase_date DATE,
purchase_price NUMERIC,
status TEXT,
spool_type TEXT,
dryer_count INTEGER DEFAULT 0 NOT NULL,
last_drying_date DATE,
storage_location TEXT
);
`);
console.log("Tables successfully created.");
} catch (createError) {
console.error("Error creating tables:", createError);
process.exit(1);
}
}
// Check if data already exists
const existingData = await db.select().from(manufacturers);
if (existingData.length > 0) {
console.log("Data already exists in the database, skipping initialization.");
// Create lock file to prevent future initializations
fs.writeFileSync(lockFile, new Date().toISOString());
process.exit(0);
return;
}
console.log("Checking if sample data should be initialized...");
// Only add sample data if INIT_SAMPLE_DATA environment variable is set to "true"
if (process.env.INIT_SAMPLE_DATA === "true") {
console.log("INIT_SAMPLE_DATA is set to true. Adding sample data...");
// Add basic selection options
console.log("Adding basic materials, manufacturers, etc...");
try {
await db.insert(manufacturers).values({ name: "Bambu Lab" }).onConflictDoNothing();
await db.insert(manufacturers).values({ name: "Prusament" }).onConflictDoNothing();
await db.insert(manufacturers).values({ name: "Filamentworld" }).onConflictDoNothing();
await db.insert(manufacturers).values({ name: "Ninjatek" }).onConflictDoNothing();
await db.insert(materials).values({ name: "PLA" }).onConflictDoNothing();
await db.insert(materials).values({ name: "PETG" }).onConflictDoNothing();
await db.insert(materials).values({ name: "ABS" }).onConflictDoNothing();
await db.insert(materials).values({ name: "TPU" }).onConflictDoNothing();
await db.insert(diameters).values({ value: 1.75 }).onConflictDoNothing();
await db.insert(colors).values({ name: "Schwarz (Bambu Lab)", code: "#000000" }).onConflictDoNothing();
await db.insert(colors).values({ name: "Weiß (Bambu Lab)", code: "#FFFFFF" }).onConflictDoNothing();
await db.insert(colors).values({ name: "Transparent", code: "#FFFFFF" }).onConflictDoNothing();
await db.insert(colors).values({ name: "Rot", code: "#F44336" }).onConflictDoNothing();
await db.insert(colors).values({ name: "Grau", code: "#9E9E9E" }).onConflictDoNothing();
await db.insert(storageLocations).values({ name: "Keller" }).onConflictDoNothing();
console.log("Basic selection options inserted.");
} catch (insertError) {
console.error("Error inserting basic selection options:", insertError);
}
const initialFilaments = [
{
name: "PLA Schwarz Bambu Lab",
manufacturer: "Bambu Lab",
material: "PLA",
colorName: "Schwarz",
colorCode: "#000000",
diameter: "1.75",
printTemp: "200-220°C",
totalWeight: "1",
remainingPercentage: "65"
},
{
name: "PETG Transparent",
manufacturer: "Prusament",
material: "PETG",
colorName: "Transparent",
colorCode: "#FFFFFF",
diameter: "1.75",
printTemp: "230-250°C",
totalWeight: "1",
remainingPercentage: "15"
},
{
name: "ABS Rot",
manufacturer: "Filamentworld",
material: "ABS",
colorName: "Rot",
colorCode: "#F44336",
diameter: "1.75",
printTemp: "240-260°C",
totalWeight: "1",
remainingPercentage: "0"
},
{
name: "TPU Flexibel Grau",
manufacturer: "Ninjatek",
material: "TPU",
colorName: "Grau",
colorCode: "#9E9E9E",
diameter: "1.75",
printTemp: "210-230°C",
totalWeight: "0.5",
remainingPercentage: "75"
}
];
for (const filamentData of initialFilaments) {
const parsedData = insertFilamentSchema.parse(filamentData);
await db.insert(filaments).values(parsedData);
}
console.log("Sample filaments inserted.");
} else {
console.log("INIT_SAMPLE_DATA is not set to true. Skipping sample data insertion.");
}
// Create lock file to prevent future initializations
fs.writeFileSync(lockFile, new Date().toISOString());
console.log("Initial data successfully inserted and lock file created!");
} catch (error) {
console.error("Error inserting initial data:", error);
process.exit(1);
}
process.exit(0);
}
insertInitialData();