Skip to content

Commit

Permalink
feat: Add releasing/executing multi-query support (#14)
Browse files Browse the repository at this point in the history
* Rework query-face-template
* Tried to make it more readable and effective
* Rename as validate-query-template
* Add centralized errors management

* Rework query-face
* Tried to make it more readable and effective
* Move some functionst to separate helper files
* Update errors
* Add whitelistRoutes for devMode

* feat: Add anonymous routes to allow testing query
* Any logged-in user can test query

* feat: Add parameter validations for queries

* fix: Remove unnecessary packages

* feat: Add releasing/executing multi-query support

Add initial data

Update qf migrations

feat: Add dependent param default value
  • Loading branch information
enginustun authored Jul 26, 2020
1 parent 8f8e01c commit e5d9920
Show file tree
Hide file tree
Showing 26 changed files with 1,136 additions and 916 deletions.
2 changes: 1 addition & 1 deletion .jsdoc/node/find-versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = function() {
fs.readFileSync(path.join(__dirname, '..', '..', 'package.json'), 'utf8')
);

require('child_process').exec('git tag -l', (err, stdOut) => {
require('child_process').execSync('git tag -l', (err, stdOut) => {
let versions = [pkg.version];
if (err) {
console.log(err);
Expand Down
69 changes: 69 additions & 0 deletions docs/query-face-node/1.0/queryFaceNode.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ <h3></h3>
<li class="doc-function">
<a href="queryFaceNode.html#.setDatabase">setDatabase</a>
</li>
<li class="doc-function">
<a href="queryFaceNode.html#.setMainDatabase">setMainDatabase</a>
</li>
<li class="doc-function">
<a href="queryFaceNode.html#.upMigrations">upMigrations</a>
</li>
Expand Down Expand Up @@ -921,6 +924,72 @@ <h5>Example</h5>
const databases = require('./databases');
queryFaceNode.setDatabase('todoDB', databases.todoDB);</code></pre>

<h4 class="name" id=".setMainDatabase">
<span class="type-signature">(static) </span>setMainDatabase<span
class="signature"
>(database)</span
><span class="type-signature"></span>
</h4>

<h5>Parameters:</h5>

<table class="params">
<thead>
<tr>
<th>Name</th>

<th>Type</th>

<th class="last">Description</th>
</tr>
</thead>

<tbody>
<tr>
<td class="name"><code>database</code></td>

<td class="type">
<span class="param-type">knex</span>
</td>

<td class="description last">
knex database instance you configured to use qf db operations
</td>
</tr>
</tbody>
</table>

<dl class="details"></dl>

<h5>Example</h5>

<pre class="prettyprint"><code>// somewhere in your project
// databases.js
require('dotenv').config();
module.exports = {
mainDB: require('knex')({
client: process.env.QF_DB_CLIENT,
connection: {
host: process.env.QF_DB_HOST,
port: process.env.QF_DB_PORT,
user: process.env.QF_DB_USER,
password: process.env.QF_DB_PASSWORD,
database: process.env.QF_DB_DATABASE
},
migrations: {
tableName: process.env.QF_DB_MIGRATION_TABLE,
directory: process.env.QF_DB_MIGRATION_DIRECTORY
},
seeds: {
directory: process.env.QF_DB_SEEDS_DIRECTORY
}
})
};

// app.js
const databases = require('./databases');
queryFaceNode.setMainDatabase(databases.mainDB);</code></pre>

<h4 class="name" id=".upMigrations">
<span class="type-signature">(static) </span>upMigrations<span
class="signature"
Expand Down
36 changes: 35 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const router = require('./lib/middleware/router');
const setDatabase = require('./lib/databases').setDatabase;
const { setDatabase, setMainDatabase } = require('./lib/databases');
const {
runMigrations,
listMigrations,
Expand Down Expand Up @@ -66,6 +66,40 @@ module.exports = router;
*/
module.exports.setDatabase = setDatabase;

/**
* @memberof queryFaceNode.
* @function setMainDatabase
* @param {knex} database knex database instance you configured to use qf db operations
* @example
* // somewhere in your project
* // databases.js
* require('dotenv').config();
* module.exports = {
* mainDB: require('knex')({
* client: process.env.QF_DB_CLIENT,
* connection: {
* host: process.env.QF_DB_HOST,
* port: process.env.QF_DB_PORT,
* user: process.env.QF_DB_USER,
* password: process.env.QF_DB_PASSWORD,
* database: process.env.QF_DB_DATABASE
* },
* migrations: {
* tableName: process.env.QF_DB_MIGRATION_TABLE,
* directory: process.env.QF_DB_MIGRATION_DIRECTORY
* },
* seeds: {
* directory: process.env.QF_DB_SEEDS_DIRECTORY
* }
* })
* };
*
* // app.js
* const databases = require('./databases');
* queryFaceNode.setMainDatabase(databases.mainDB);
*/
module.exports.setMainDatabase = setMainDatabase;

/**
* @memberof queryFaceNode.
* @function runMigrations
Expand Down
26 changes: 26 additions & 0 deletions lib/common/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const MAIN_DB_NAME = 'qfdb';
const aliasRegex = /\{\{([a-zA-Z_$][0-9a-zA-Z_$]*)\.([a-zA-Z_$][0-9a-zA-Z_$]*)(:(.*))?\}\}/;
const aliasRegexG = new RegExp(aliasRegex, 'g');
const paramRegex = /\$\{([_a-zA-Z\s]+)\}/;
const paramPrefixMap = {
QF_CONST: 'qfc',
};
const queryMap = {
delete: 'from',
update: 'from',
set: 'update',
};

const whitelistRoutes = ['get-tables'];
const anonymousRoutes = ['test-query'];

module.exports = {
MAIN_DB_NAME,
aliasRegexG,
aliasRegex,
anonymousRoutes,
paramRegex,
paramPrefixMap,
queryMap,
whitelistRoutes,
};
29 changes: 26 additions & 3 deletions lib/databases.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
const path = require('path');
const databaseMap = {};
const setDatabase = (dbName, knexDB) => {
databaseMap[dbName] = knexDB;
};
const setMainDatabase = knexDB => {
databaseMap.qfdb = knexDB;
};
// try to parse consumer's databases file
try {
const databases = require(path.join(process.cwd(), 'src', 'databases.js'));
if (databases) {
Object.entries(databases).forEach(([name, db]) => {
if (name && db) {
if (name === 'qfdb') {
setMainDatabase(db);
console.log(`[qfdb] MAIN database is set automatically`);
} else {
setDatabase(name, db);
console.log(`[${name}] database is set automatically`);
}
}
});
}
} catch (error) {}

module.exports = {
getDatabase: dbName => databaseMap[dbName],
setDatabase: (dbName, knexDB) => {
databaseMap[dbName] = knexDB;
},
setDatabase,
setMainDatabase,
};
Loading

0 comments on commit e5d9920

Please sign in to comment.