Skip to content

Commit 61c160a

Browse files
authored
Merge pull request #92 from paparomeo/classes-for-model-generators
Implement module generators with classes for code reuse.
2 parents 4ec59e5 + bd80e71 commit 61c160a

File tree

6 files changed

+104
-89
lines changed

6 files changed

+104
-89
lines changed

.jshintrc

Lines changed: 0 additions & 3 deletions
This file was deleted.

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ dist: trusty
22
sudo: false
33
language: node_js
44
node_js:
5-
- 4
65
- 6
76
- 8
87
services:

lib/modelGenerators/default.js

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,73 @@
1-
var DataTypes = require('sequelize').DataTypes
1+
const DataTypes = require('sequelize').DataTypes
22

3-
exports.joiSchemaToSequelizeModel = function (resourceName, joiSchema) {
4-
var model = {
5-
id: {
3+
module.exports = class {
4+
constructor (resourceName, joiSchema) {
5+
this.id = {
66
type: DataTypes.UUID,
77
defaultValue: DataTypes.UUIDV4,
88
primaryKey: true
9-
},
10-
type: {
9+
}
10+
this.type = {
1111
type: DataTypes.VIRTUAL, // We do not actually save this to DB, but API needs this
1212
set: function (val) {
1313
this.setDataValue('type', val)
1414
},
1515
get: function () {
1616
return resourceName
1717
}
18-
},
19-
meta: {
18+
}
19+
this.meta = {
2020
type: DataTypes.STRING,
2121
get: function () {
22-
var data = this.getDataValue('meta')
22+
const data = this.getDataValue('meta')
2323
if (!data) return undefined
2424
return JSON.parse(data)
2525
},
2626
set: function (val) {
2727
return this.setDataValue('meta', JSON.stringify(val))
2828
}
2929
}
30-
}
3130

32-
Object.keys(joiSchema).forEach(function (attributeName) {
33-
var attribute = joiSchema[attributeName]
34-
if (attribute._type === 'string') model[attributeName] = { type: DataTypes.TEXT, allowNull: true }
35-
if (attribute._type === 'date') model[attributeName] = { type: DataTypes.DATE, allowNull: true }
36-
if (attribute._type === 'number') model[attributeName] = { type: DataTypes.NUMERIC, allowNull: true }
37-
if (attribute._type === 'boolean') model[attributeName] = { type: DataTypes.BOOLEAN, allowNull: true }
38-
if (attribute._type === 'array') {
39-
// Serialize array to ';'-separated string for most SQL dbs.
40-
model[attributeName] = {
41-
type: DataTypes.STRING,
42-
allowNull: true,
43-
get: function () {
44-
var data = this.getDataValue(attributeName)
45-
return data ? data.split(';') : []
46-
},
47-
set: function (val) {
48-
this.setDataValue(attributeName, val.join(';'))
31+
for (let attributeName of Object.keys(joiSchema)) {
32+
const attribute = joiSchema[attributeName]
33+
if (attribute._type === 'string') {
34+
this[attributeName] = {
35+
type: DataTypes.TEXT,
36+
allowNull: true
37+
}
38+
}
39+
if (attribute._type === 'date') {
40+
this[attributeName] = {
41+
type: DataTypes.DATE,
42+
allowNull: true
43+
}
44+
}
45+
if (attribute._type === 'number') {
46+
this[attributeName] = {
47+
type: DataTypes.NUMERIC,
48+
allowNull: true
49+
}
50+
}
51+
if (attribute._type === 'boolean') {
52+
this[attributeName] = {
53+
type: DataTypes.BOOLEAN,
54+
allowNull: true
55+
}
56+
}
57+
if (attribute._type === 'array') {
58+
// Serialize array to ';'-separated string for most SQL dbs.
59+
this[attributeName] = {
60+
type: DataTypes.STRING,
61+
allowNull: true,
62+
get: function () {
63+
const data = this.getDataValue(attributeName)
64+
return data ? data.split(';') : []
65+
},
66+
set: function (val) {
67+
this.setDataValue(attributeName, val.join(';'))
68+
}
4969
}
5070
}
5171
}
52-
})
53-
54-
return model
72+
}
5573
}

lib/modelGenerators/postgres.js

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,65 @@
1-
var DataTypes = require('sequelize').DataTypes
1+
const DataTypes = require('sequelize').DataTypes
2+
const Default = require('./default')
23

3-
exports.joiSchemaToSequelizeModel = function (resourceName, joiSchema) {
4-
var model = {
5-
id: {
6-
type: DataTypes.UUID,
7-
defaultValue: DataTypes.UUIDV4,
8-
primaryKey: true
9-
},
10-
type: {
11-
type: DataTypes.VIRTUAL, // We do not actually save this to DB, but API needs this
12-
set: function (val) {
13-
this.setDataValue('type', val)
14-
},
15-
get: function () {
16-
return resourceName
17-
}
18-
},
19-
meta: {
4+
module.exports = class extends Default {
5+
constructor (resourceName, joiSchema) {
6+
super(resourceName, joiSchema)
7+
this.meta = {
208
type: DataTypes.JSONB,
219
get: function () {
22-
var data = this.getDataValue('meta')
10+
const data = this.getDataValue('meta')
2311
if (!data) return undefined
2412
return data
2513
},
2614
set: function (val) {
2715
return this.setDataValue('meta', val)
2816
}
2917
}
30-
}
3118

32-
Object.keys(joiSchema).forEach(function (attributeName) {
33-
var attribute = joiSchema[attributeName]
34-
if (attribute._type === 'string') model[attributeName] = { type: DataTypes.TEXT, allowNull: true }
35-
if (attribute._type === 'date') model[attributeName] = { type: DataTypes.DATE, allowNull: true }
36-
if (attribute._type === 'number') {
37-
if (typeof attribute._flags.precision !== 'undefined') {
38-
model[attributeName] = { type: DataTypes.NUMERIC(32, attribute._flags.precision), allowNull: true }
39-
} else {
40-
model[attributeName] = { type: DataTypes.NUMERIC, allowNull: true }
19+
for (let attributeName of Object.keys(joiSchema)) {
20+
const attribute = joiSchema[attributeName]
21+
if (attribute._type === 'number') {
22+
if (typeof attribute._flags.precision !== 'undefined') {
23+
this[attributeName] = {
24+
type: DataTypes.NUMERIC(32, attribute._flags.precision),
25+
allowNull: true
26+
}
27+
} else {
28+
this[attributeName] = {
29+
type: DataTypes.NUMERIC,
30+
allowNull: true
31+
}
32+
}
4133
}
42-
}
43-
if (attribute._type === 'boolean') model[attributeName] = { type: DataTypes.BOOLEAN, allowNull: true }
44-
if (attribute._type === 'array') {
45-
// PostgreSQL has proper array support, so lets use that
46-
switch (attribute._inner.items[0]._type) {
47-
case 'string': model[attributeName] = {type: DataTypes.ARRAY(DataTypes.STRING), allowNull: true}; break
48-
case 'number': model[attributeName] = {type: DataTypes.ARRAY(DataTypes.NUMERIC), allowNull: true}; break
49-
case 'boolean': model[attributeName] = {type: DataTypes.ARRAY(DataTypes.BOOLEAN), allowNull: true}; break
50-
}
51-
model[attributeName].get = function () {
52-
return this.getDataValue(attributeName) || []
53-
}
54-
model[attributeName].set = function (val) {
55-
this.setDataValue(attributeName, val || [])
34+
if (attribute._type === 'array') {
35+
// PostgreSQL has proper array support, so lets use that
36+
switch (attribute._inner.items[0]._type) {
37+
case 'string':
38+
this[attributeName] = {
39+
type: DataTypes.ARRAY(DataTypes.STRING),
40+
allowNull: true
41+
}
42+
break
43+
case 'number':
44+
this[attributeName] = {
45+
type: DataTypes.ARRAY(DataTypes.NUMERIC),
46+
allowNull: true
47+
}
48+
break
49+
case 'boolean':
50+
this[attributeName] = {
51+
type: DataTypes.ARRAY(DataTypes.BOOLEAN),
52+
allowNull: true
53+
}
54+
break
55+
}
56+
this[attributeName].get = function () {
57+
return this.getDataValue(attributeName) || []
58+
}
59+
this[attributeName].set = function (val) {
60+
this.setDataValue(attributeName, val || [])
61+
}
5662
}
5763
}
58-
})
59-
60-
return model
64+
}
6165
}

lib/sqlHandler.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ var semver = require('semver')
1010
var _ = require('lodash')
1111
var util = require('util')
1212
var modelGenerators = {
13-
postgres: require('./modelGenerators/postgres'),
14-
default: require('./modelGenerators/default')
13+
Postgres: require('./modelGenerators/postgres'),
14+
Default: require('./modelGenerators/default')
1515
}
1616

1717
var MIN_SERVER_VERSION = '1.10.0'
@@ -140,14 +140,11 @@ SqlStore.prototype._buildModels = function () {
140140
var modelAttributes = { }
141141
switch (self.config.dialect) {
142142
case 'postgres':
143-
modelAttributes = modelGenerators.postgres.joiSchemaToSequelizeModel(
144-
self.resourceConfig.resource,
145-
localAttributes)
143+
modelAttributes = new modelGenerators.Postgres(self.resourceConfig.resource, localAttributes)
146144
break
147145
default:
148-
modelAttributes = modelGenerators.default.joiSchemaToSequelizeModel(
149-
self.resourceConfig.resource,
150-
localAttributes)
146+
modelAttributes = new modelGenerators.Default(self.resourceConfig.resource, localAttributes)
147+
break
151148
}
152149

153150
self.baseModel = self.sequelize.define(self.resourceConfig.resource, modelAttributes, { timestamps: false })

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"url": "https://github.com/holidayextras/jsonapi-store-relationaldb.git"
2020
},
2121
"engines": {
22-
"node": ">=4.5"
22+
"node": ">=6"
2323
},
2424
"dependencies": {
2525
"async": "2.5.0",

0 commit comments

Comments
 (0)