{"id":20135540,"url":"https://github.com/raisely/datastore-env","last_synced_at":"2025-04-09T17:32:23.833Z","repository":{"id":31169212,"uuid":"127095385","full_name":"raisely/datastore-env","owner":"raisely","description":"Store .env variables in google datastore","archived":false,"fork":false,"pushed_at":"2022-03-25T21:42:04.000Z","size":328,"stargazers_count":8,"open_issues_count":4,"forks_count":1,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-03-23T19:39:10.015Z","etag":null,"topics":["dotenv","environment-variables","google-datastore"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/raisely.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-03-28T06:38:02.000Z","updated_at":"2024-07-03T02:50:56.000Z","dependencies_parsed_at":"2022-08-07T16:15:27.645Z","dependency_job_id":null,"html_url":"https://github.com/raisely/datastore-env","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/raisely%2Fdatastore-env","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/raisely%2Fdatastore-env/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/raisely%2Fdatastore-env/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/raisely%2Fdatastore-env/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/raisely","download_url":"https://codeload.github.com/raisely/datastore-env/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248077492,"owners_count":21043972,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dotenv","environment-variables","google-datastore"],"created_at":"2024-11-13T21:15:25.035Z","updated_at":"2025-04-09T17:32:23.448Z","avatar_url":"https://github.com/raisely.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# datastore-env\n\nSave your environment variables in Google Cloud Datastore\n\n## Features\n\n* Loads variables from your Datastore into an object storing them as key value pairs\n* (Optionally) Merges the variables into `process.env` to save rewriting code\n* Can automatically generate a list of required variables from your `.env` file\n* Fails fast when one or more required environment variables is missing from your configuration\n* Creates placeholders in your datastore for missing variables so you just need fill them with the correct value\n* Initialise the datastore config from an existing `.env` file\n\nBased on [this Python Gist](https://gist.github.com/SpainTrain/6bf5896e6046a5d9e7e765d0defc8aa8)\n\nThis is useful for migrating projects that make heavy use of `.env` to Google Cloud\nif you don't want to refactor code and don't want API keys, etc stored in your app.yaml\n\nNB: If you use a `.env` file, you must still use a tool like [dotenv](https://github.com/motdotla/dotenv) to load those variables into `process.env`.\n\n# Why?\n\nUp until recently, google cloud didn't support a convenient way of storing environment variables except \nhardcoding them in files that were likely to go into source control.\n\ndatastore-env was created as a simple way to bootstrap applications that were already heavily dependent on \n`process.env` into the google cloud environment.\n\nEven though environment variables are now supported, this provides some nifty conveniences.\n\n# Getting Started\n\n```sh\nnpm install --save datastore-env\n\n# Generate a list of required environment variables\nnpm run env -- datastore-env generate:required -i .env -o config/requiredEnv.js\n```\nThen in your application\n\n```js\nconst DatastoreEnvironment = require('datastore-env');\n\n// File generated above\nconst envVars = require('./config/requiredEnv.js');\n\nconst options = {\n  namespace, // Namespace for datastore\n  projectId, // Defaults to the value of process.env.PROJECT_ID\n  path // Path to prepend to variable names when reading/writing to datastore\n\n  optional: envVars.optional, // Load these variables from the datastore\n  required: envVars.required, // Load these variables and raise exception if not present\n\n  // Default settings if a variable is missing\n  defaults: {},\n  requireDatastore: false, // Should an exception be raised if cannot connect to datastore (default: false)\n};\n\nconst env = new DatastoreEnvironment(options);\n\n// Load all required keys into process.env\n// If some keys are not set, will throw an exception indicating which ones\n// and suggest a URL to visit to set them\nawait env.loadEnvironment();\n\n// OR if you only want to load the settings but not merge them with process.env\nconst settings = env.verifyEnvironment()\n```\n\n## A note on migrating from dotenv\ndotenv runs synchronously, loadEnvironment, as it's querying a database is asynchronous.\nIf you wish to wait on this without restructuring a lot of code, you could make use\nof something like [deasync](https://github.com/abbr/deasync).\nNOTE: The original author of this generally [advises against](https://github.com/vkurchatkin/deasync) using it, so don't make it a habbit ;-)\n\nExample:\n\n```javascript\nconst deasync = require('deasync');\n\n// Wrap loadEnvironment in a node callback style\nfunction asyncLoad(cb) {\n\tenv.loadEnvironment().then(() =\u003e cb()).catch(cb);\n}\n\n// This will block until loadEnvironment has finished\ndeasync(asyncLoad)();\n\nconsole.log(process.env['FROM_DATASTORE']);\n```\n\n# Generating Required Variables\nThe command `npm run env -- datastore-env gen:required -i .env -o config/requiredEnv.js`\nwill generate your list of required variables that will be loaded from the\ndatastore settings.\nThis script can be used as part of a build process to automatically add new keys.\nThe script generates the list from a .env file\n\nSome variables may be optional, you can mark them by placing a comment above that starts\nwith \"optional\"\n\n``` sh\n# optional Define the color for the background\nBG_COLOR=emerald\n```\n\nBG_COLOR will be added to the list of optional variables\n\nIf you wish to omit some variables entirely in .env you can put a comment on\nthe line before that starts with\n\n``` sh\n# datastore-env-ignore For development only\nDEBUG_MODE=trace\n```\n\nTo ignore all entries after a certain line, use\n\n``` sh\n# datastore-env-ignore-all\nDEBUG_MODE=really_really_verbose\nDANCE_MODE=break\n```\n\n# Initialising your Datastore\nYou can initialise your datastore from a `.env` file by calling\n\n```sh\nnpm run env -- datastore-env upload:env -i .env\n```\n\nThis script will upload the contents of the selected `.env` file to your datastore\n\nBy default the script will NOT overwrite existing keys, and will skip upload any required\nor optional keys in your `.env` file (using the same indicators in comments as when Generating\nrequired variables (see above).\n\nYou can alter the behaviour by using the command line arguments `--overwrite` or\n`--include=all|required|optional` respectively.\n\n# Running Locally\nThere are 3 ways you can use and test this library when running locally.\n\n1) No datastore (Simplest, but doesn't mirror production)\n2) Datastore emulator (Fast, and mirrors production)\n3) Connect to cloud datastore (Slow)\n\n### No Datastore\n\nIf it is not possible to connect to datastore, datastore-env will continue without\n(ie it will just use environment variables and defaults).\n\n### Datastore emulator\n\nYou can run the Google Cloud Datastore Emulator locally:\n\u003chttps://cloud.google.com/datastore/docs/tools/datastore-emulator\u003e\n\n### Connect to Cloud Datastore\n\nConnect to one of your Cloud Datastore's by setting the environment variable\n`GOOGLE_APPLICATION_CREDENTIALS` to the path of a json credentials file with access\nto the service.\n\nLearn more about getting a credentials file at:\n\u003chttps://developers.google.com/accounts/docs/application-default-credentials\u003e\n\n# Contributing\n\nContributions are welcome. Please submit a pull request and include tests.\n\nPlease follow the coding style in `.editorconfig` and `.eslintrc`.\n\nContributions should pass `npm test \u0026\u0026 npm run lint` (see below on testing)\n\n## Testing\n\n1. Install the [Google Datastore Emulator](https://cloud.google.com/datastore/docs/tools/datastore-emulator)\n2. In one terminal run `npm run datastore`\n3. Copy the export command to another terminal window, eg `export DATASTORE_EMULATOR_HOST=localhost:8081`\n4. Run `npm test`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fraisely%2Fdatastore-env","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fraisely%2Fdatastore-env","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fraisely%2Fdatastore-env/lists"}