Skip to content

Commit

Permalink
Added new helpers (eq, ne, etc...) and sequelize-typescript mappings (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgary authored Apr 18, 2019
1 parent 6be0274 commit 2441d08
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gatewayapps/cradle-template-emitter",
"version": "0.4.0",
"version": "0.5.0",
"description": "Cradle emitter for reading Handlebars based template files",
"main": "./dist/index.js",
"scripts": {
Expand All @@ -25,7 +25,7 @@
"@gatewayapps/cradle": ">=0.4.0"
},
"devDependencies": {
"@gatewayapps/cradle": "^0.4.0",
"@gatewayapps/cradle": "^0.4.2",
"@types/fs-extra": "^5.0.4",
"@types/handlebars": "^4.0.40",
"@types/node": "^10.9.4",
Expand Down
28 changes: 27 additions & 1 deletion src/TemplateEmitter/TemplateEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export class TemplateEmitter implements ICradleEmitter {

public mapDefaultValues(typeName: string, defaultValue: any) {
try {
if (!this.dataTypeMappings) {
if (!this.dataTypeMappings) {
return defaultValue
}
return this.dataTypeMappings.convertValue(typeName, defaultValue)
Expand Down Expand Up @@ -151,6 +151,32 @@ export class TemplateEmitter implements ICradleEmitter {
handlebars.registerHelper('toLowerCase', (str) => {
return str.toLowerCase()
})
handlebars.registerHelper({
eq: function (v1, v2) {
return v1 === v2
},
ne: function (v1, v2) {
return v1 !== v2
},
lt: function (v1, v2) {
return v1 < v2
},
gt: function (v1, v2) {
return v1 > v2
},
lte: function (v1, v2) {
return v1 <= v2
},
gte: function (v1, v2) {
return v1 >= v2
},
and: function () {
return Array.prototype.slice.call(arguments).every(Boolean)
},
or: function () {
return Array.prototype.slice.call(arguments, 0, -1).some(Boolean)
}
})

handlebars.registerHelper('getDistinctObjects', (context, options) => {
const got: any = []
Expand Down
35 changes: 35 additions & 0 deletions src/TemplateEmitter/mappings/sequelize-typescript/mapping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
export const values = {
Boolean: {

type: 'boolean',
},
DateTime: {

type: 'Date',
},
Decimal: {

type: 'number'
},
Integer: {

type: 'number',
},
String: {

type: 'string',
}
}

export function convertValue(typeName: string, input: any): any {
switch (typeName) {
case 'DateTime': {
if (input === 'DateTimeNow') {
return 'Sequelize.NOW'
} else {
return `new Date('${(input as Date).toISOString()}')`
}
}
default: return input
}
}

0 comments on commit 2441d08

Please sign in to comment.