Configure sequelize and generate the first (and likely only) model: Event

This commit is contained in:
R. Tyler Croy 2018-09-09 18:30:25 -07:00
parent 918ab6d5a4
commit 13bd07e810
No known key found for this signature in database
GPG Key ID: 1426C7DC3F51E16F
9 changed files with 152 additions and 0 deletions

8
.sequelizerc Normal file
View File

@ -0,0 +1,8 @@
const path = require('path')
module.exports = {
'config': path.resolve('config', 'database.js'),
'models-path': path.resolve('src', 'models')
}
// vim: ft=javascript

25
config/database.js Normal file
View File

@ -0,0 +1,25 @@
/*
* This module exists to dynamically construct the right configuration for the
* sequelize command line tool for migrations.
*/
const fs = require('fs');
const path = require('path');
const nodeEnv = process.env.NODE_ENV || 'development';
let connectorConfig = null;
let connectorConfigFile = path.join(__dirname, `${nodeEnv}.json`);
if (fs.existsSync(connectorConfigFile)) {
connectorConfig = JSON.parse(fs.readFileSync(connectorConfigFile));
}
else {
connectorConfig = JSON.parse(fs.readFileSync(path.join(__dirname, 'default.json')));
}
module.exports = {};
module.exports[nodeEnv] = {
'url' : process.env.DB_CONNECTION_STRING || connectorConfig['postgres'],
'dialect' : 'postgresql',
};

0
migrations/.gitignore vendored Normal file
View File

View File

@ -0,0 +1,34 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Events', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
type: {
allowNull: false,
type: Sequelize.STRING
},
payload: {
allowNull: false,
type: Sequelize.JSON
},
createdAt: {
allowNull: false,
defaultValue: Sequelize.literal('NOW()'),
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
defaultValue: Sequelize.literal('NOW()'),
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Events');
}
};

39
package-lock.json generated
View File

@ -274,6 +274,12 @@
"integrity": "sha512-yprFYuno9FtNsSHVlSWd+nRlmGoAbqbeCwOryP6sC/zoCjhpArcRMYp19EvpSUSizJAlsXEwJv+wcWS9XaXdMw==",
"dev": true
},
"@types/bluebird": {
"version": "3.5.24",
"resolved": "https://registry.npmjs.org/@types/bluebird/-/bluebird-3.5.24.tgz",
"integrity": "sha512-YeQoDpq4Lm8ppSBqAnAeF/xy1cYp/dMTif2JFcvmAbETMRlvKHT2iLcWu+WyYiJO3b3Ivokwo7EQca/xfLVJmg==",
"dev": true
},
"@types/body-parser": {
"version": "1.17.0",
"resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.17.0.tgz",
@ -302,6 +308,15 @@
"@types/node": "*"
}
},
"@types/continuation-local-storage": {
"version": "3.2.1",
"resolved": "https://registry.npmjs.org/@types/continuation-local-storage/-/continuation-local-storage-3.2.1.tgz",
"integrity": "sha1-oz4N+dzptCTRyY/E/evYV43O7H4=",
"dev": true,
"requires": {
"@types/node": "*"
}
},
"@types/events": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/@types/events/-/events-1.2.0.tgz",
@ -366,6 +381,12 @@
"integrity": "sha512-D1xlXHZpDonVX+VJ28XtcD5xlu8ex6Fc4cQNnrm2wJvlQnbec9RedhCrhQr6kRAE9XWHSec+JPuTmqJ9jC0qsA==",
"dev": true
},
"@types/lodash": {
"version": "4.14.116",
"resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.116.tgz",
"integrity": "sha512-lRnAtKnxMXcYYXqOiotTmJd74uawNWuPnsnPrrO7HiFuE3npE2iQhfABatbYDyxTNqZNuXzcKGhw37R7RjBFLg==",
"dev": true
},
"@types/mime": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/@types/mime/-/mime-2.0.0.tgz",
@ -389,6 +410,18 @@
"integrity": "sha512-41qEJgBH/TWgo5NFSvBCJ1qkoi3Q6ONSF2avrHq1LVEZfYpdHmj0y9SuTK+u9ZhG1sYQKBL1AWXKyLWP4RaUoQ==",
"dev": true
},
"@types/sequelize": {
"version": "4.27.25",
"resolved": "https://registry.npmjs.org/@types/sequelize/-/sequelize-4.27.25.tgz",
"integrity": "sha512-wAoS4tz8MIS+4QfNWZKvO8gXBbwDhrI5PEpTUyTgwnR74M36/rGEVyugdK8KFj5IqamUJfFbAgnJ2Tw/16hKqw==",
"dev": true,
"requires": {
"@types/bluebird": "*",
"@types/continuation-local-storage": "*",
"@types/lodash": "*",
"@types/validator": "*"
}
},
"@types/serve-static": {
"version": "1.13.2",
"resolved": "https://registry.npmjs.org/@types/serve-static/-/serve-static-1.13.2.tgz",
@ -399,6 +432,12 @@
"@types/mime": "*"
}
},
"@types/validator": {
"version": "9.4.1",
"resolved": "https://registry.npmjs.org/@types/validator/-/validator-9.4.1.tgz",
"integrity": "sha512-Y8UyLZvBPgckGhEIFCGBPj1tsRbpcZn4rbAp7lUxC3EW/nDR2V6t9LltE+mvDJxQQ+Bg3saE3UAwn6lsG5O1yQ==",
"dev": true
},
"@types/winston": {
"version": "2.4.4",
"resolved": "https://registry.npmjs.org/@types/winston/-/winston-2.4.4.tgz",

View File

@ -26,6 +26,7 @@
"@types/feathersjs__express": "^1.1.4",
"@types/feathersjs__feathers": "^3.0.4",
"@types/jest": "^23.3.2",
"@types/sequelize": "^4.27.25",
"@types/winston": "^2.4.4",
"jest": "^23.5.0",
"nodemon": "^1.18.4",

0
seeders/.gitignore vendored Normal file
View File

11
src/models/event.ts Normal file
View File

@ -0,0 +1,11 @@
'use strict';
export default (sequelize, DataTypes) => {
const Event = sequelize.define('Event', {
type: DataTypes.STRING,
payload: DataTypes.JSON
}, {});
Event.associate = function(models) {
};
return Event;
};

34
src/models/index.ts Normal file
View File

@ -0,0 +1,34 @@
'use strict';
import fs from 'fs';
import path from 'path';
import Sequelize from 'sequelize';
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/database')[env];
const db : any = {};
const sequelize = new Sequelize(config.database, config.username, config.password, config);
fs
.readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
.forEach(file => {
const model = sequelize['import'](path.join(__dirname, file));
db[model.name] = model;
});
Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
export default db;