-
Notifications
You must be signed in to change notification settings - Fork 0
/
BrandedFoodSqlExtraction.js
402 lines (353 loc) · 17.5 KB
/
BrandedFoodSqlExtraction.js
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
const sqlite3 = require('sqlite3').verbose();
const fs = require('fs');
const { chain } = require('stream-chain');
const { parser } = require('stream-json');
const { streamArray } = require('stream-json/streamers/StreamArray');
// Connect to SQLite database
const db = new sqlite3.Database('brandedFoods.db');
// Create tables
const createTables = () => {
db.serialize(() => {
db.run(`CREATE TABLE IF NOT EXISTS BrandedFoods (
id INTEGER PRIMARY KEY AUTOINCREMENT,
fdcId INTEGER NOT NULL,
foodClass TEXT,
description TEXT,
modifiedDate TEXT,
availableDate TEXT,
marketCountry TEXT,
brandOwner TEXT,
gtinUpc TEXT,
dataSource TEXT,
ingredients TEXT,
servingSize REAL,
servingSizeUnit TEXT,
householdServingFullText TEXT,
brandedFoodCategory TEXT,
dataType TEXT,
publicationDate TEXT
)`);
db.run(`CREATE TABLE IF NOT EXISTS FoodNutrients (
id INTEGER PRIMARY KEY AUTOINCREMENT,
foundationalFoodId INTEGER,
brandedFoodId INTEGER,
nutrientId INTEGER,
foodNutrientDerivationId INTEGER,
max REAL,
min REAL,
median REAL,
amount REAL,
dataPoints INTEGER,
FOREIGN KEY (brandedFoodId) REFERENCES BrandedFoods(id),
FOREIGN KEY (foundationalFoodId) REFERENCES FoundationFoods(id),
FOREIGN KEY (nutrientId) REFERENCES Nutrients(id),
FOREIGN KEY (foodNutrientDerivationId) REFERENCES FoodNutrientDerivations(id)
)`);
db.run(`CREATE TABLE IF NOT EXISTS Nutrients (
id INTEGER PRIMARY KEY,
number TEXT,
name TEXT,
rank INTEGER,
unitName TEXT
)`);
db.run(`CREATE TABLE IF NOT EXISTS FoodNutrientSources (
id INTEGER PRIMARY KEY,
code TEXT,
description TEXT
)`);
db.run(`CREATE TABLE IF NOT EXISTS FoodNutrientDerivations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
code TEXT,
description TEXT,
foodNutrientSourceId INTEGER,
FOREIGN KEY (foodNutrientSourceId) REFERENCES FoodNutrientSources(id)
)`);
db.run(`CREATE TABLE IF NOT EXISTS FoodAttributeTypes (
id INTEGER PRIMARY KEY,
name TEXT,
description TEXT
)`);
db.run(`CREATE TABLE IF NOT EXISTS FoodAttributes (
id INTEGER PRIMARY KEY,
brandedFoodId INTEGER,
name TEXT,
value TEXT,
foodAttributeTypeId INTEGER,
FOREIGN KEY (brandedFoodId) REFERENCES BrandedFoods(id),
FOREIGN KEY (foodAttributeTypeId) REFERENCES FoodAttributeTypes(id)
)`);
db.run(`CREATE TABLE IF NOT EXISTS LabelNutrients (
brandedFoodId INTEGER,
nutrientName TEXT,
value REAL,
PRIMARY KEY (brandedFoodId, nutrientName),
FOREIGN KEY (brandedFoodId) REFERENCES BrandedFoods(id)
)`);
db.run(`CREATE TABLE IF NOT EXISTS FoodUpdateLogs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
brandedFoodId INTEGER,
foodClass TEXT,
description TEXT,
dataType TEXT,
fdcId INTEGER,
publicationDate TEXT,
FOREIGN KEY (brandedFoodId) REFERENCES BrandedFoods(id)
)`);
db.run(`CREATE TABLE IF NOT EXISTS FoodUpdateLogAttributes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
foodUpdateLogId INTEGER,
attributeId INTEGER,
FOREIGN KEY (foodUpdateLogId) REFERENCES FoodUpdateLogs(id),
FOREIGN KEY (attributeId) REFERENCES FoodAttributes(id)
)`);
db.run(`CREATE TABLE IF NOT EXISTS FoundationFoods (
id INTEGER PRIMARY KEY AUTOINCREMENT,
fdcId INTEGER NOT NULL,
foodClass TEXT,
description TEXT,
publicationDate TEXT,
isHistoricalReference BOOLEAN
)`);
db.run(`CREATE TABLE IF NOT EXISTS FoodPortions (
id INTEGER PRIMARY KEY,
foundationFoodId INTEGER,
value REAL,
measureUnitId INTEGER,
modifier TEXT,
gramWeight REAL,
sequenceNumber INTEGER,
minYearAcquired INTEGER,
amount REAL,
FOREIGN KEY (foundationFoodId) REFERENCES FoundationFoods(id),
FOREIGN KEY (measureUnitId) REFERENCES MeasureUnits(id)
)`);
db.run(`CREATE TABLE IF NOT EXISTS MeasureUnits (
id INTEGER PRIMARY KEY,
name TEXT,
abbreviation TEXT
)`);
db.run(`CREATE TABLE IF NOT EXISTS FoodCategories (
id INTEGER PRIMARY KEY,
code TEXT,
description TEXT
)`);
db.run(`CREATE TABLE IF NOT EXISTS InputFoods (
id INTEGER PRIMARY KEY,
foundationFoodId INTEGER,
foodDescription TEXT,
inputFoodFdcId INTEGER,
inputFoodDescription TEXT,
inputFoodPublicationDate TEXT,
inputFoodCategoryId INTEGER,
FOREIGN KEY (foundationFoodId) REFERENCES FoundationFoods(id),
FOREIGN KEY (inputFoodCategoryId) REFERENCES FoodCategories(id)
)`);
// Create indexes for faster search with joins and where clauses
db.run("CREATE INDEX IF NOT EXISTS idx_foodnutrients_nutrientid ON FoodNutrients(nutrientId);");
db.run("CREATE INDEX IF NOT EXISTS idx_nutrients_id ON Nutrients(id);");
db.run("CREATE INDEX IF NOT EXISTS idx_foodnutrients_brandedfoodid ON FoodNutrients(brandedFoodId);");
});
};
// Insert data into tables
const insertData = (data, type) => {
if (type === 'Branded') {
const food = data.value;
db.run(`INSERT INTO BrandedFoods (fdcId, foodClass, description, modifiedDate, availableDate, marketCountry, brandOwner, gtinUpc, dataSource, ingredients, servingSize, servingSizeUnit, householdServingFullText, brandedFoodCategory, dataType, publicationDate)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
[food.fdcId, food.foodClass, food.description, food.modifiedDate, food.availableDate, food.marketCountry, food.brandOwner, food.gtinUpc, food.dataSource, food.ingredients, food.servingSize, food.servingSizeUnit, food.householdServingFullText, food.brandedFoodCategory, food.dataType, food.publicationDate],
function (err) {
if (err) {
return console.log(err.message);
}
const brandedFoodId = this.lastID;
// Insert food attributes
food.foodAttributes.forEach(attr => {
db.run(`INSERT INTO FoodAttributes (brandedFoodId, name, value, foodAttributeTypeId) VALUES (?, ?, ?, ?)`,
[brandedFoodId, attr.name, attr.value, attr.foodAttributeType.id], function (err) {
if (err) {
return console.log(err.message);
}
});
});
// Insert label nutrients
const labelNutrients = food.labelNutrients;
for (const key in labelNutrients) {
if (labelNutrients.hasOwnProperty(key)) {
db.run(`INSERT INTO LabelNutrients (brandedFoodId, nutrientName, value) VALUES (?, ?, ?)`,
[brandedFoodId, key, labelNutrients[key].value], function (err) {
if (err) {
return console.log(err.message);
}
});
}
}
// Insert food nutrients and related tables
food.foodNutrients.forEach(nutrient => {
db.run(`INSERT INTO Nutrients (id, number, name, rank, unitName) VALUES (?, ?, ?, ?, ?)
ON CONFLICT(id) DO NOTHING`,
[nutrient.nutrient.id, nutrient.nutrient.number, nutrient.nutrient.name, nutrient.nutrient.rank, nutrient.nutrient.unitName], function (err) {
if (err) {
return console.log(err.message);
}
});
db.run(`INSERT INTO FoodNutrientSources (id, code, description) VALUES (?, ?, ?)
ON CONFLICT(id) DO NOTHING`,
[nutrient.foodNutrientDerivation.foodNutrientSource.id, nutrient.foodNutrientDerivation.foodNutrientSource.code, nutrient.foodNutrientDerivation.foodNutrientSource.description], function (err) {
if (err) {
return console.log(err.message);
}
});
db.run(`INSERT INTO FoodNutrientDerivations (code, description, foodNutrientSourceId) VALUES (?, ?, ?)`,
[nutrient.foodNutrientDerivation.code, nutrient.foodNutrientDerivation.description, nutrient.foodNutrientDerivation.foodNutrientSource.id], function (err) {
if (err) {
return console.log(err.message);
}
const derivationId = this.lastID;
db.run(`INSERT INTO FoodNutrients (brandedFoodId, nutrientId, foodNutrientDerivationId, amount) VALUES (?, ?, ?, ?)`,
[brandedFoodId, nutrient.nutrient.id, derivationId, nutrient.amount], function (err) {
if (err) {
return console.log(err.message);
}
});
});
});
// Insert food update logs
food.foodUpdateLog.forEach(log => {
db.run(`INSERT INTO FoodUpdateLogs (brandedFoodId, foodClass, description, dataType, fdcId, publicationDate) VALUES (?, ?, ?, ?, ?, ?)`,
[brandedFoodId, log.foodClass, log.description, log.dataType, log.fdcId, log.publicationDate], function (err) {
if (err) {
return console.log(err.message);
}
const logId = this.lastID;
// Insert food update log attributes
log.foodAttributes.forEach(attr => {
db.run(`INSERT INTO FoodUpdateLogAttributes (foodUpdateLogId, attributeId) VALUES (?, ?)`,
[logId, attr.id], function (err) {
if (err) {
return console.log(err.message);
}
});
});
});
});
});
} else if (type === 'Foundation') {
const food = data;
db.run(`INSERT INTO FoundationFoods (fdcId, foodClass, description, publicationDate, isHistoricalReference)
VALUES (?, ?, ?, ?, ?)`,
[food.fdcId, food.foodClass, food.description, food.publicationDate, food.isHistoricalReference],
function (err) {
if (err) {
return console.log(err.message);
}
const foundationFoodId = this.lastID;
// Insert food nutrients
food.foodNutrients.forEach(nutrient => {
db.run(`INSERT INTO Nutrients (id, number, name, rank, unitName) VALUES (?, ?, ?, ?, ?)
ON CONFLICT(id) DO NOTHING`,
[nutrient.nutrient.id, nutrient.nutrient.number, nutrient.nutrient.name, nutrient.nutrient.rank, nutrient.nutrient.unitName], function (err) {
if (err) {
return console.log(err.message);
}
});
db.run(`INSERT INTO FoodNutrientSources (id, code, description) VALUES (?, ?, ?)
ON CONFLICT(id) DO NOTHING`,
[nutrient.foodNutrientDerivation.foodNutrientSource.id, nutrient.foodNutrientDerivation.foodNutrientSource.code, nutrient.foodNutrientDerivation.foodNutrientSource.description], function (err) {
if (err) {
return console.log(err.message);
}
});
db.run(`INSERT INTO FoodNutrientDerivations (code, description, foodNutrientSourceId) VALUES (?, ?, ?)`,
[nutrient.foodNutrientDerivation.code, nutrient.foodNutrientDerivation.description, nutrient.foodNutrientDerivation.foodNutrientSource.id], function (err) {
if (err) {
return console.log(err.message);
}
const derivationId = this.lastID;
db.run(`INSERT INTO FoodNutrients (foundationalFoodId, nutrientId, foodNutrientDerivationId, amount, max, min, median, dataPoints) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
[foundationFoodId, nutrient.nutrient.id, derivationId, nutrient.amount, nutrient.max, nutrient.min, nutrient.median, nutrient.dataPoints], function (err) {
if (err) {
return console.log(err.message);
}
});
});
});
// Insert food portions
if(food && food.foodPortions)
food.foodPortions.forEach(portion => {
db.run(`INSERT INTO MeasureUnits (id, name, abbreviation) VALUES (?, ?, ?)
ON CONFLICT(id) DO NOTHING`,
[portion.measureUnit.id, portion.measureUnit.name, portion.measureUnit.abbreviation], function (err) {
if (err) {
return console.log(err.message);
}
});
db.run(`INSERT INTO FoodPortions (id, foundationFoodId, value, measureUnitId, modifier, gramWeight, sequenceNumber, minYearAcquired, amount) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
[portion.id, foundationFoodId, portion.value, portion.measureUnit.id, portion.modifier, portion.gramWeight, portion.sequenceNumber, portion.minYearAcquired, portion.amount], function (err) {
if (err) {
return console.log(err.message);
}
});
});
// Insert input foods
if(food && food.inputFoods)
food.inputFoods.forEach(input => {
db.run(`INSERT INTO FoodCategories (id, code, description) VALUES (?, ?, ?)
ON CONFLICT(id) DO NOTHING`,
[input.inputFood.foodCategory.id, input.inputFood.foodCategory.code, input.inputFood.foodCategory.description], function (err) {
if (err) {
return console.log(err.message);
}
});
db.run(`INSERT INTO InputFoods (id, foundationFoodId, foodDescription, inputFoodFdcId, inputFoodDescription, inputFoodPublicationDate, inputFoodCategoryId) VALUES (?, ?, ?, ?, ?, ?, ?)`,
[input.id, foundationFoodId, input.foodDescription, input.inputFood.fdcId, input.inputFood.description, input.inputFood.publicationDate, input.inputFood.foodCategory.id], function (err) {
if (err) {
return console.log(err.message);
}
});
});
});
}
};
// Execute the script
createTables();
let dataSaved = 0;
// const brandedPipeline = chain([
// fs.createReadStream('brandedDownload.json'),
// parser(),
// streamArray()
// ]);
// brandedPipeline.on('data', (data) => {
// insertData(data, 'Branded');
// dataSaved++;
// if (dataSaved % 20 === 0) {
// console.log('Branded data saved:', dataSaved, data.value.description);
// }
// });
const foundationPipeline = chain([
fs.createReadStream('foundationDownload.json'),
parser(),
streamArray()
]);
foundationPipeline.on('data', (data) => {
insertData(data.value, 'Foundation');
dataSaved++;
if (dataSaved % 20 === 0) {
console.log('Foundation data saved:', dataSaved, data.value.description);
}
});
foundationPipeline.on('end', () => {
console.log('All foundation data inserted.');
db.close();
});
foundationPipeline.on('error', (err) => {
console.error('Error:', err);
});
// brandedPipeline.on('end', () => {
// console.log('All branded data inserted.');
// foundationPipeline.resume(); // Start the foundation pipeline
// });
// brandedPipeline.on('error', (err) => {
// console.error('Error:', err);
// });
// Start by running the branded pipeline
//brandedPipeline.resume();