Skip to content

Commit

Permalink
Merge pull request #29 from moevm/kharitonov_json_data
Browse files Browse the repository at this point in the history
Add auto adding json data to DB
  • Loading branch information
necitboss authored Dec 12, 2024
2 parents a6c8b48 + 940fceb commit 9a11265
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
update
} from "./controllers/ComponentsController.js";
import path from 'path';
import ComponentsModel from "./models/Components.js";
import * as fs from "node:fs";
const app = express();

const isProduction = true;
Expand All @@ -21,7 +23,29 @@ const folder = isProduction? "dist": "_public";
const PORT = 4444;
const __dirname = import.meta.dirname;
mongoose.connect("mongodb://127.0.0.1:27017/build_pc")
.then(() => console.log('DB ok'))
.then(() => {
console.log('DB ok')
const components = ComponentsModel.find()
ComponentsModel.find().limit(1)
.then(data => {
if (data.length === 0){
const jsonData = JSON.parse(fs.readFileSync('./utils/data.json', 'utf-8'));
const components = jsonData.components;
for (const component of components){
const doc = new ComponentsModel({
name: component.name,
type: component.type,
price: component.price,
count: component.count,
main_properties: component.main_properties,
other_properties: component.other_properties
})
doc.save()
.then(() => {})
}
}
})
})
.catch((err) => console.warn('DB error: ', err));

app.use(cors())
Expand Down

0 comments on commit 9a11265

Please sign in to comment.