Skip to content

Commit

Permalink
Use mongoimport to process json mongo types
Browse files Browse the repository at this point in the history
Otherwise syntaxes like {$date: ...} are misinterpreted
  • Loading branch information
minottic committed Aug 20, 2024
1 parent ab631af commit 5efb4da
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
6 changes: 3 additions & 3 deletions services/mongodb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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/)).
2 changes: 1 addition & 1 deletion services/mongodb/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
8 changes: 0 additions & 8 deletions services/mongodb/config/init.js

This file was deleted.

10 changes: 10 additions & 0 deletions services/mongodb/config/init.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5efb4da

Please sign in to comment.