Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ jobs:
- name: Begin release...
uses: actions/checkout@v4

- name: Use Node 18
- name: Use Node 20
uses: actions/setup-node@v4
with:
node-version: 18.x
node-version: 20.x

- name: Install dependencies
run: npm install
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ node_modules/
*.swp
*.swo
*.env
*.env.*
*.env.*
test.js
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
18
20
23 changes: 18 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,30 +70,43 @@ const tasks = [
];

module.exports = {
tasks: tasks,
tasks: tasks
settings: {
mysql: {
prodtime: {
host: "localhost",
user: "root",
password: "root",
port: "3306"
port: "3306",
// for secrets, rely on environment variables (see Settings)
}
},
google: {
gcred: {
"type": "service_account",
// ...
// for secrets, rely on environment variables for each key, or rather set an env var with the credential
// json, see Settings below
}
}
}
};

```

#### MySql Connections and Google Credential Settings

Note how in the above example, `connection` and `googleCredential` refer to keys defined in the `settings` object.
## Settings

The configuration settings for MySql + Google Sheets API are pulled from the environment, using conventions. Environment
variables are also loaded from any local `.env` file relative to location application is launched from.

With a named MySql connection, it will look for:

- host: `{name}_mysql_host`
- user: `{name}_mysql_user`
- password: `{name}_mysql_password`
- port: `{name}_mysql_port`

With a named Google Sheet credential, it will expect an environment variable called `{name}_google` which contains the credential JSON. Alternatively, set individual keys using `{name}_google_{property}`.

To set up your Google Credentials, see https://www.npmjs.com/package/googleapis#service-account-credentials
Note: Make sure you share the sheet (with Editor rights) with the email address noted in the `client_email` param of the service account JSON.
Expand Down
3 changes: 3 additions & 0 deletions index.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#!/usr/bin/env node
const dotenv = require('dotenv');
const yargs = require('yargs/yargs');
const {uploadDataToGoogleSheets} = require("./sheet-uploader");
const {hideBin} = require('yargs/helpers')
const argv = yargs(hideBin(process.argv)).argv
const {getConfig} = require('./config-provider');
const {executeTasks} = require('./mysql-service');

dotenv.config();

const configPath = argv.path;
if (!configPath) {
console.error(`error: param --path is required\n`);
Expand Down
15 changes: 9 additions & 6 deletions mysql-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ function getConnection(
if (!settings.mysql) {
throw new Error(`expected mysql to be defined on settings object but wasn't`);
}
const config = settings.mysql[name];
if (config.host
&& config.user
&& config.password
&& config.port) {
const config = settings.mysql[name] ?? {};
const keys = ["host", "user", "password", "port"];
keys.forEach(key => {
config[key] = config[key] ?? process.env[`${name}_mysql_${key}`];
});
const missingKeys = keys.filter(key => !config[key]);
if (missingKeys.length === 0) {
return config;
}
throw new Error(`One or more mysql settings are missing for connection named '${name}'`);
console.error(`missing keys for mysql connection '${name}': ${missingKeys.join(", ")}`);
process.exit(-1);
}

function getKnexConnection(
Expand Down
94 changes: 66 additions & 28 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mysql2sheet",
"version": "1.1.0",
"version": "2.0.1",
"description": "Does what it says on the tin. Takes data in your MySql database and uploads it to a Google Sheet.",
"main": "index.js",
"bin": {
Expand Down Expand Up @@ -32,13 +32,14 @@
"url": "https://github.com/rohland/mysql2sheet"
},
"engines": {
"node": ">=18"
"node": ">=20"
},
"homepage": "https://github.com/rohland/mysql2sheet#readme",
"dependencies": {
"googleapis": "^135.0.0",
"dotenv": "^16.4.7",
"googleapis": "^148.0.0",
"knex": "^3.1.0",
"mysql2": "^3.9.7",
"mysql2": "^3.14.0",
"yargs": "^17.7.2"
}
}
39 changes: 35 additions & 4 deletions sheet-uploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ function getGoogleAuth(
if (cachedAuthToken) {
return cachedAuthToken;
}
const credential = credentials[name];
if (!credential) {
throw new Error(`could not find google credential with name '${name}`);
}
const credential = getGoogleCredentials(name, credentials);
const auth = new google.auth.JWT(
credential.client_email,
null,
Expand All @@ -25,6 +22,40 @@ function getGoogleAuth(
return auth;
}

function getGoogleCredentials(name, credentials) {
try {
let credential = (credentials ?? {})[name];
if (credential) {
const keys = [
"type",
"project_id",
"private_key_id",
"private_key",
"client_email",
"client_id",
"auth_uri",
"token_uri",
"auth_provider_x509_cert_url",
"client_x509_cert_url"];
keys.forEach(key => {
credential[key] = credential[key] ?? process.env[`${name}_google_${key}`];
});
} else {
credential = process.env[`${name}_google`]
? JSON.parse(process.env[`${name}_google`])
: null;
}
if (!credential) {
console.error(`missing env keys for google credential '${name}'`);
process.exit(-1);
}
return credential;
} catch (err) {
console.error(`could not parse google credential with name '${name}'`, err);
process.exit(-1);
}
}

function getResultType(task) {
const resultType = task.type;
if (!resultType) {
Expand Down