From 5efb4dabcf863574c55bb6aa4e5d4eaac34cf017 Mon Sep 17 00:00:00 2001 From: minottic Date: Tue, 20 Aug 2024 11:45:14 +0200 Subject: [PATCH] Use mongoimport to process json mongo types Otherwise syntaxes like {$date: ...} are misinterpreted --- services/mongodb/README.md | 6 +++--- services/mongodb/compose.yaml | 2 +- services/mongodb/config/init.js | 8 -------- services/mongodb/config/init.sh | 10 ++++++++++ 4 files changed, 14 insertions(+), 12 deletions(-) delete mode 100644 services/mongodb/config/init.js create mode 100755 services/mongodb/config/init.sh diff --git a/services/mongodb/README.md b/services/mongodb/README.md index 1d5a045d..8a62cc6e 100644 --- a/services/mongodb/README.md +++ b/services/mongodb/README.md @@ -4,7 +4,7 @@ The `mongodb` container is responsible of creating a mongodb container with init ## Configuration options -All files collection created with relative data are in the [seed folder](./config/seed/) and the init script [here](./config/init.js) +All files collection created with relative data are in the [seed folder](./config/seed/) and the init script [here](./config/init.sh) To add more collections during the creation of the database: 1. add the corresponding file(s) [here](./config/seed/), keeping the convention: `filename := collectionname.json`. @@ -14,10 +14,10 @@ These files are ingested into the database using mongo funcionalities and bypass ## Default configuration -In the default configuration [init.js](./config/init.js), the seeding creates data in the mongodb database used by the `backend` service (either [v4](../backend/services/v4/), by default, or [v3](../backend/services/v3/) if specified otherwise by setting `BE_VERSION`). +In the default configuration [init.sh](./config/init.sh), the seeding creates data in the mongodb database used by the `backend` service (either [v4](../backend/services/v4/), by default, or [v3](../backend/services/v3/) if specified otherwise by setting `BE_VERSION`). For an explanation of how setting `BE_VERSION` changes the environment creation see [here](../../README.md#docker-compose-profiles-and-env-variables-configuration-options). ## Dependency on `BE_VERSION` -Since [v3](../backend/services/v3/) and [v4](../backend/services/v4/) connect to two different DBs, the [BE_VERSION](./compose.yaml#L9) environment variable controls [which DB](./config/init.js#L1) should be seeded (`dacat` for [v3](../backend/services/v3/) and `dacat-next` for [v4](../backend/services/v4/)). +Since [v3](../backend/services/v3/) and [v4](../backend/services/v4/) connect to two different DBs, the [BE_VERSION](./compose.yaml#L9) environment variable controls [which DB](./config/init.sh#L5) should be seeded (`dacat` for [v3](../backend/services/v3/) and `dacat-next` for [v4](../backend/services/v4/)). diff --git a/services/mongodb/compose.yaml b/services/mongodb/compose.yaml index e1d31e4b..016201e9 100644 --- a/services/mongodb/compose.yaml +++ b/services/mongodb/compose.yaml @@ -3,7 +3,7 @@ services: image: mongo:7.0 volumes: - backend${BE_VERSION:-v4}_mongodb_data:/data/db - - ./config/init.js:/docker-entrypoint-initdb.d/init.js + - ./config/init.sh:/docker-entrypoint-initdb.d/init.sh - ./config/seed:/seed environment: BE_VERSION: ${BE_VERSION:-v4} diff --git a/services/mongodb/config/init.js b/services/mongodb/config/init.js deleted file mode 100644 index ce6a0982..00000000 --- a/services/mongodb/config/init.js +++ /dev/null @@ -1,8 +0,0 @@ -db = connect(`mongodb://localhost/${process.env.BE_VERSION === "v4"? "dacat-next": "dacat"}`); -seedFiles = fs.readdirSync("/seed"); -seedFiles.forEach((filename) => { - collectionName = filename.replace(/\.json$/, ""); - content = JSON.parse(fs.readFileSync("/seed/" + filename, "utf8")); - db.createCollection(collectionName); - db[collectionName].insertMany(content); -}); diff --git a/services/mongodb/config/init.sh b/services/mongodb/config/init.sh new file mode 100755 index 00000000..045000ea --- /dev/null +++ b/services/mongodb/config/init.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +cd /seed || exit + +[ "${BE_VERSION}" = "v4" ] && DB="dacat-next" || DB="dacat" + +for FILE_NAME in *.json +do + mongoimport --db "${DB}" --collection "${FILE_NAME%.*}" --file "${FILE_NAME}" --jsonArray +done