Skip to content

Commit

Permalink
started data management
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Fiscaletti authored and Nathan Fiscaletti committed Jan 9, 2022
1 parent 69fc847 commit 371b3ac
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 23 deletions.
6 changes: 0 additions & 6 deletions config/database.yaml

This file was deleted.

14 changes: 14 additions & 0 deletions config/development.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
server:
host: '127.0.0.1'
port: 1234
https:
enabled: false
certificate: '/absolute/path/to.crt'
private_key: '/absolute/path/to.key'

database:
host: 'localhost'
port: 3306
user: 'root'
password: 'aoc1080p'
database: 'coattail'
14 changes: 14 additions & 0 deletions config/production.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
server:
host: '127.0.0.1'
port: 1234
https:
enabled: false
certificate: '/absolute/path/to.crt'
private_key: '/absolute/path/to.key'

database:
host: 'localhost'
port: 3306
user: 'root'
password: 'aoc1080p'
database: 'coattail'
6 changes: 0 additions & 6 deletions config/server.yaml

This file was deleted.

14 changes: 14 additions & 0 deletions config/staging.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
server:
host: '127.0.0.1'
port: 1234
https:
enabled: false
certificate: '/absolute/path/to.crt'
private_key: '/absolute/path/to.key'

database:
host: 'localhost'
port: 3306
user: 'root'
password: 'aoc1080p'
database: 'coattail'
48 changes: 48 additions & 0 deletions lib/cli/commands/database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const environment = require(`../../environment`);
const config = require(`../../config`);
const connection = require(`../../core/data/connection`);

module.exports = {
description: 'Basic actions pertaining to database management.',
actions: {
"migrate": {
description: 'Manages migrations',
flags: {
"latest": "Run all migrations that have not yet been run.",
"rollback": "Rollback the last batch of migrations performed."
}
},
},

migrate: async (flags) => {
const conf = config.load();
const conn = connection.connect(conf);

if (flags.includes('latest')) {
console.log(`Using environment: ${environment.getEnvironmentName()}`)
const [batchNo, log] = await conn.migrate.latest({
directory: './lib/core/data/migrations'
});
if (log.length === 0) {
console.log('Already up to date');
}
console.log(`Batch ${batchNo} run: ${log.length} migrations`);
console.log(log.join('\n'));
process.exit();
}

if (flags.includes('rollback')) {
console.log(`Using environment: ${environment.getEnvironmentName()}`)
const [batchNo, log] = await conn.migrate.rollback({
directory: './lib/core/data/migrations'
});
if (log.length === 0) {
console.log('Already at the base migration');
}
console.log(`Batch ${batchNo} rolled back: ${log.length} migrations`);
console.log(log.join('\n'));
process.exit();
}
}
}

5 changes: 4 additions & 1 deletion lib/cli/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const yargs = require(`yargs`);

const COMM_MODULES = { api: require(`./commands/api`) };
const COMM_MODULES = {
api: require(`./commands/api`),
database: require(`./commands/database`)
};

async function execute() {
const compiled = yargs
Expand Down
16 changes: 6 additions & 10 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
const fs = require(`fs`);
const path = require(`path`);
const yaml = require(`js-yaml`);
const { getEnvironmentName } = require(`./environment`);

module.exports = {
load: () => {
const dir = `${__dirname}/../config`;
const configs = fs.readdirSync(dir).filter(element => element.endsWith(`.yml`) || element.endsWith(`.yaml`));
const results = [];
load: (env = 'development') => {
env = getEnvironmentName(env);

for(const configFile of configs) {
const contents = fs.readFileSync(path.join(dir, configFile), 'utf8');
results[path.parse(configFile).name] = yaml.load(contents);
}

return results;
const location = path.join(__dirname, '../config/', `${env}.yaml`);
const contents = fs.readFileSync(location, 'utf8');
return yaml.load(contents);
}
};
41 changes: 41 additions & 0 deletions lib/core/data/knexfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Update with your config settings.

module.exports = {

development: {
client: 'mysql',
connection: require(`../../config`).load(`development`).database,
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}
},

staging: {
client: 'mysql',
connection: require(`../../config`).load(`staging`).database,
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}
},

production: {
client: 'mysql',
connection: require(`../../config`).load(`production`).database,
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}
}

};
11 changes: 11 additions & 0 deletions lib/core/data/migrations/20220109183353_create_subscriptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const tableName = 'subscriptions';

exports.up = function(knex) {
return knex.schema.createTable(tableName, function (table) {
table.increments('id');
});
};

exports.down = function(knex) {
return knex.schema.dropTable(tableName);
};
16 changes: 16 additions & 0 deletions lib/environment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const VALID_ENVS = [ 'development', 'production', 'staging' ];

function getEnvironmentName(env = 'development') {
if (process.env.NODE_ENV) {
if (!VALID_ENVS.includes(process.env.NODE_ENV)) {
throw new Error(`Invalid value ${process.env.NODE_ENV} for NODE_ENV envirionment variable. Must be one of [ ${VALID_ENVS.join(', ')} ].`);
}
env = process.env.NODE_ENV;
}

return env;
}

module.exports = {
getEnvironmentName
};

0 comments on commit 371b3ac

Please sign in to comment.