Skip to content

Commit

Permalink
Add includes;
Browse files Browse the repository at this point in the history
Correct bugs;
Update doc;
  • Loading branch information
Maxime Magné committed Jan 15, 2020
1 parent ab87071 commit 7973c95
Show file tree
Hide file tree
Showing 34 changed files with 827 additions and 222 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
node_modules
package-lock.json
package-lock.json.*
.*
#*#
_docpress
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# HearthJS

### v2.1.0
- Add the possibility to include SQL file in another SQL file
- Add genericQueue in helpers
- Log errors which are generated by the query key (SQL filename) of a schema
- Send `req` instead of `req.body` to SQL files
- Correct the `put` request in testClient

### v2.0.1
- Can access express from hearthjs module

Expand Down
1 change: 1 addition & 0 deletions docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* [Translations](/translations/#translations)
* [Cron](/cron/#cron)
* [Logger](/logger/#logger)
* [Helpers](/helpers/#helpers)
* [Cli](/cli/#cli)
* [Database](/database/#database)
* [Tests](/tests/#tests)
1 change: 1 addition & 0 deletions docs/addons/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* [Translations](/translations/#translations)
* [Cron](/cron/#cron)
* [Logger](/logger/#logger)
* [Helpers](/helpers/#helpers)
* [Cli](/cli/#cli)
* [Database](/database/#database)
* [Tests](/tests/#tests)
1 change: 1 addition & 0 deletions docs/api/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* [Translations](/translations/#translations)
* [Cron](/cron/#cron)
* [Logger](/logger/#logger)
* [Helpers](/helpers/#helpers)
* [Cli](/cli/#cli)
* [Database](/database/#database)
* [Tests](/tests/#tests)
1 change: 1 addition & 0 deletions docs/cli/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* [Translations](/translations/#translations)
* [Cron](/cron/#cron)
* [Logger](/logger/#logger)
* [Helpers](/helpers/#helpers)
* [Cli](/cli/#cli)
* [Database](/database/#database)
* [Tests](/tests/#tests)
1 change: 1 addition & 0 deletions docs/converter/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* [Translations](/translations/#translations)
* [Cron](/cron/#cron)
* [Logger](/logger/#logger)
* [Helpers](/helpers/#helpers)
* [Cli](/cli/#cli)
* [Database](/database/#database)
* [Tests](/tests/#tests)
1 change: 1 addition & 0 deletions docs/cron/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* [Add a cron](/cron/#add-a-cron)
* [Manage crons](/cron/#manages-cron)
* [Logger](/logger/#logger)
* [Helpers](/helpers/#helpers)
* [Cli](/cli/#cli)
* [Database](/database/#database)
* [Tests](/tests/#tests)
1 change: 1 addition & 0 deletions docs/database/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* [Translations](/translations/#translations)
* [Cron](/cron/#cron)
* [Logger](/logger/#logger)
* [Helpers](/helpers/#helpers)
* [Cli](/cli/#cli)
* [Database](/database/#database)
* [Connection](/database/#connection)
Expand Down
1 change: 1 addition & 0 deletions docs/extern-api/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* [Translations](/translations/#translations)
* [Cron](/cron/#cron)
* [Logger](/logger/#logger)
* [Helpers](/helpers/#helpers)
* [Cli](/cli/#cli)
* [Database](/database/#database)
* [Tests](/tests/#tests)
55 changes: 55 additions & 0 deletions docs/helpers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
## Helpers

### Generic queue

You know calling a callback in a loop is not a good idea and you have to create recursive function to loop on your array. This time is done. Now you can use a `genericQueue`!

#### How it works?

It's simple, hearthjs will create an object which will call one of your callback for each item.

Here is the genericQueue prototype

```js
hearthjs.helpers.genericQueue(items, itemHandler, errorHandler, callback)
```

An example will be clearer than text:

```js
const hearthjs = require('hearthjs')

let myArray = ['value1', 'value2', 'value3']

let genericQueue = hearthjs.helpers.genericQueue(myArray, (item, next) => {
// You will receive one by one each item of your array (value1, value2, value3)
// You must call next when you finish processing your item
// You can pass an error to next callback
}, (error) => {
// Handle errors here
}, () => {
// This callback is called when we stop looping on the array
})
```

**WARNING: The genericQueue shift element of the array one by one. Take care of cloning your array before sending it to the genericQueue if you want to reuse it after.**

```js
// Clone an array
let clonedArray = JSON.parse(JSON.stringify(arrayToClone))
```

```js
const hearthjs = require('hearthjs')

let myArray = ['value1', 'value2', 'value3']
let concatenateValues = ''

let genericQueue = hearthjs.helpers.genericQueue(myArray, (item, next) => {
concatenateValues += item + ' '
}, (error) => {
// Handle errors here
}, () => {
console.log(concatenateValues) // display: value1 value2 value3
})
```
15 changes: 15 additions & 0 deletions docs/helpers/_sidebar.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
* [Getting started](/#hearthjs)
* [Server](/server/#server)
* [Api](/api/#api)
* [Converter](/converter/#converter)
* [Data validation](/validation/#data-validation)
* [Templating](/templating/#templating)
* [Addons](/addons/#addons)
* [Extern API](/extern-api/#extern-api)
* [Translations](/translations/#translations)
* [Cron](/cron/#cron)
* [Logger](/logger/#logger)
* [Helpers](/helpers/#helpers)
* [Cli](/cli/#cli)
* [Database](/database/#database)
* [Tests](/tests/#tests)
1 change: 1 addition & 0 deletions docs/logger/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* [Logger](/logger/#logger)
* [Log message](/logger/#log-message)
* [Debug message](/logger/#debug-message)
* [Helpers](/helpers/#helpers)
* [Cli](/cli/#cli)
* [Database](/database/#database)
* [Tests](/tests/#tests)
1 change: 1 addition & 0 deletions docs/server/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* [Translations](/translations/#translations)
* [Cron](/cron/#cron)
* [Logger](/logger/#logger)
* [Helpers](/helpers/#helpers)
* [Cli](/cli/#cli)
* [Database](/database/#database)
* [Tests](/tests/#tests)
16 changes: 16 additions & 0 deletions docs/templating/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,22 @@ Users:
* Tom Benoit
```

### Include

You can include SQL files in another SQL file by using the following marker: `{->` and `<-}`.

You have to passe the name of a SQL file which is registered when your server is starting.

```sql
WITH insert_value {
# Execute your insert here
}

{-> getValue <-}
```

This will execute the insert and then the SQL request which is in the `getValue.sql` file.

### Constant

You can add pre-defined constant in your template. If you add an unknown constant, hearthjs will ignore it. You must use `{$` and `$}` to add a constant.
Expand Down
1 change: 1 addition & 0 deletions docs/templating/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* [Translations](/translations/#translations)
* [Cron](/cron/#cron)
* [Logger](/logger/#logger)
* [Helpers](/helpers/#helpers)
* [Cli](/cli/#cli)
* [Database](/database/#database)
* [Tests](/tests/#tests)
1 change: 1 addition & 0 deletions docs/tests/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* [Translations](/translations/#translations)
* [Cron](/cron/#cron)
* [Logger](/logger/#logger)
* [Helpers](/helpers/#helpers)
* [Cli](/cli/#cli)
* [Database](/database/#database)
* [Tests](/tests/#tests)
Expand Down
1 change: 1 addition & 0 deletions docs/translations/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* [CLI](/translations/#cli)
* [Cron](/cron/#cron)
* [Logger](/logger/#logger)
* [Helpers](/helpers/#helpers)
* [Cli](/cli/#cli)
* [Database](/database/#database)
* [Tests](/tests/#tests)
1 change: 1 addition & 0 deletions docs/validation/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* [Translations](/translations/#translations)
* [Cron](/cron/#cron)
* [Logger](/logger/#logger)
* [Helpers](/helpers/#helpers)
* [Cli](/cli/#cli)
* [Database](/database/#database)
* [Tests](/tests/#tests)
8 changes: 6 additions & 2 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ const api = {
// Exec addonc exec function
addon.execAddon(server._addons[addonIndex], schema[key], database, req.originalUrl, schema, req, res, (err) => {
if (err) {
logger.log(err.toString(), 'error')
return callback(err)
}

Expand All @@ -256,16 +257,18 @@ const api = {
// Execute before
this._mayExec(req, res, schema.before, 'before', (err) => {
if (err) {
logger.log(err.toString(), 'error')
return res.json(this.createResponse(false, {}, err.toString()))
}

if (schema.query !== undefined) {
// Execute user query
let model = (schema.out === undefined) ? null : schema.out

database.exec(schema.query, req.body, model, (err, result, formatted) => {
database.exec(schema.query, req, model, (err, result, formatted) => {
if (err) {
return res.json(this.createResponse(false, {}, err.toString()))
logger.log(err.toString(), 'error')
return res.json(this.createResponse(false, {}, 'An error occured'))
}

// Execute after
Expand All @@ -288,6 +291,7 @@ const api = {
_execAfter: function (req, res, data, schema) {
this._mayExec(req, res, schema.after, 'after', data, (err, newData) => {
if (err) {
logger.log(err.toString(), 'error')
return res.json(this.createResponse(false, {}, err.toString()))
}
data = (newData !== undefined) ? newData : data
Expand Down
2 changes: 1 addition & 1 deletion lib/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ const converter = {
// model is an object like {}
if (columnName === null) {
// Receive an object => fill value and recall fill current if
// we found an object or an arrau
// we found an object or an array
for (var k = 0; k < Object.keys(model).length; k++) {
let objKey = Object.keys(model)[k]

Expand Down
46 changes: 22 additions & 24 deletions lib/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,35 +124,33 @@ const database = {
return callback(err)
}

let obj = null

try {
obj = mustache.render(content, data, model)
} catch (e) {
return callback(e)
}

// Print for debug
if (obj.print !== undefined && obj.print === true) {
console.log('-- SQL Request --')
console.log(obj.string)
console.log('-- req --')
console.log(data)
console.log('-- Transform data --')
console.log(obj.data)
}

this.query(obj.string, obj.data, (err, res, rows) => {
mustache.render(content, data, model, this._sqlFiles, (err, obj) => {
if (err) {
return callback(new Error(`Error while executing query for file ${name}.sql. ${err.toString()}`))
return callback(err)
}

let formattedObject = rows
if (model !== undefined && model !== null) {
formattedObject = converter.sqlToJson(model, rows)
// Print for debug
if (obj.print !== undefined && obj.print === true) {
console.log('-- SQL Request --')
console.log(obj.string)
console.log('-- req --')
console.log(data)
console.log('-- Transform data --')
console.log(obj.data)
}

return callback(null, res, formattedObject)
this.query(obj.string, obj.data, (err, res, rows) => {
if (err) {
return callback(new Error(`Error while executing query for file ${name}.sql. ${err.toString()}`))
}

let formattedObject = rows
if (model !== undefined && model !== null) {
formattedObject = converter.sqlToJson(model, rows)
}

return callback(null, res, formattedObject)
})
})
})
},
Expand Down
41 changes: 41 additions & 0 deletions lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,47 @@ const helper = {
APP_SERVER_PORT: 8080,
APP_SERVER_LANG: 'en'
}, null, 2), callback)
},

/**
* Loop on an array to process all items
* @param {Array} items List of item to process
* @param {Function} itemHandler Function which will process every item
* @param {Function} errorHandler Function which will be called in case of error
* @param {Function} callback
*/
genericQueue: function (items, itemHandler, errorHandler, callback) {
return {
items: items,
currentItem: null,
isRunning: false,

processNextItem: function (err) {
if (errorHandler && err) {
return errorHandler(err)
}

if (this.items.length === 0) {
this.isRunning = false

if (callback) {
return callback()
}

return
}

this.currentItem = this.items.shift()
itemHandler.call(this, this.currentItem, this.processNextItem.bind(this))
},

start: function () {
if (this.isRunning === false) {
this.isRunning = true
this.processNextItem()
}
}
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const t = require('./translate').t
const logger = require('./logger')
const testClient = require('./testClient')
const express = require('express')
const helper = require('./helper')

const hearth = {
server: server,
Expand All @@ -20,6 +21,7 @@ const hearth = {
logger: logger,
cron: cron,
httpClient: testClient,
helpers: helper,
useAddon: function (addon, schemaKeyName) {
server.useAddon(addon, schemaKeyName)
},
Expand Down
Loading

0 comments on commit 7973c95

Please sign in to comment.