-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
266 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
node_modules/ | ||
config.json | ||
logs/*.log | ||
.nyc_output/ | ||
coverage/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Load Base Model | ||
const model = require('./model') | ||
|
||
// Define Database Model | ||
module.exports = [{ | ||
id: { | ||
type: model.INTEGER, | ||
primaryKey: true, | ||
autoIncrement: true | ||
}, | ||
hitokoto: model.STRING, | ||
type: model.STRING, | ||
from: model.STRING, | ||
from_who: model.STRING, | ||
creator: model.STRING, | ||
creator_uid: model.STRING, | ||
assessor: model.STRING, | ||
owner: model.STRING, | ||
created_at: model.STRING, | ||
}, { | ||
// Sequelize Model config | ||
timestamps: false | ||
}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
const Sequelize = require('sequelize') | ||
const nconf = require('nconf') | ||
const path = require('path') | ||
|
||
class db { | ||
constructor (model) { | ||
this.connect() | ||
return this.registerModel(model) | ||
} | ||
static async connect () { | ||
if (this.sequelize) { | ||
return this.sequelize | ||
} else { | ||
const type = nconf.get('database') | ||
const config = { | ||
password: nconf.get(type + ':password'), | ||
username: nconf.get(type + ':username'), | ||
database: nconf.get(type + ':database'), | ||
host: nconf.get(type + ':host'), | ||
port: nconf.get(type + ':port') | ||
} | ||
const sequelize = new Sequelize(config.database, config.username, config.password, { | ||
host: config.host, | ||
port: config.port, | ||
dialect: type, | ||
pool: { | ||
max: 5, | ||
min: 0, | ||
acquire: 30000, | ||
idle: 10000 | ||
}, | ||
operatorsAliases: false | ||
}) | ||
// Test Connection | ||
await sequelize.authenticate() | ||
.then(() => { | ||
// console.log('Database Connection has been established successfully.') | ||
}) | ||
.catch(err => { | ||
console.error(err.message) | ||
}) | ||
|
||
this.sequelize = sequelize | ||
return sequelize | ||
} | ||
} | ||
static async registerModel (model) { | ||
await this.connect() | ||
if (this[model]) { | ||
return this[model] | ||
} else { | ||
// Register Model | ||
const modelArray = require(path.join(__dirname, '../', './src/models/databases', model)) | ||
this[model] = this.sequelize.define(model, modelArray[0], modelArray[1]) | ||
return this[model] | ||
} | ||
} | ||
|
||
static get () { | ||
this.connect() | ||
return this | ||
} | ||
} | ||
|
||
module.exports = db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
'use strict' | ||
const winston = require('winston') | ||
const config = require('../config') | ||
const nconf = require('nconf') | ||
const pkg = require('../package.json') | ||
const path = require('path') | ||
const fs = require('fs') | ||
const dirname = path.join(__dirname, '../') | ||
|
||
function setupWinston () { | ||
const logFile = config.log_path || path.join(__dirname, '../', './logs/', pkg.name + '.log') | ||
fs.existsSync(logFile) || fs.writeFileSync(logFile, '') | ||
winston.remove(winston.transports.Console) | ||
winston.add(winston.transports.File, { | ||
filename: logFile, | ||
level: config.log_level || (global.env === 'production' ? 'info' : 'verbose'), | ||
handleExceptions: true, | ||
maxsize: 5242880, | ||
maxFiles: 10 | ||
}) | ||
winston.add(winston.transports.Console, { | ||
colorize: nconf.get('log-colorize') !== 'false', | ||
timestamp: function () { | ||
var date = new Date() | ||
return config.json_logging ? date.toJSON() | ||
: date.toISOString() + ' [' + global.process.pid + ']' | ||
}, | ||
level: config.log_level || (global.env === 'production' ? 'info' : 'verbose'), | ||
json: !!config.json_logging, | ||
stringify: !!config.json_logging | ||
}) | ||
} | ||
|
||
function loadConfig (configFile) { | ||
winston.verbose('* using configuration stored in: %s', configFile) | ||
|
||
nconf.file({ | ||
file: configFile | ||
}) | ||
|
||
nconf.defaults({ | ||
base_dir: dirname, | ||
version: pkg.version | ||
}) | ||
|
||
if (!nconf.get('isCluster')) { | ||
nconf.set('isPrimary', 'true') | ||
nconf.set('isCluster', 'false') | ||
} | ||
} | ||
|
||
function printCopyright () { | ||
const colors = require('colors/safe') | ||
const date = new Date() | ||
console.log(colors.bgBlue(colors.black(' ' + pkg.name + ' v' + pkg.version + ' © ' + date.getFullYear() + ' All Rights Reserved. ')) + ' ' + colors.bgRed(colors.black(' Powered by teng-koa '))) | ||
console.log('') | ||
console.log(colors.bgCyan(colors.black(' 我们一路奋战,不是为了改变世界,而是为了不让世界改变我们。 '))) | ||
} | ||
|
||
module.exports = { | ||
load: () => { | ||
loadConfig(path.join(__dirname, '../', 'config.json')) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Test Hitokoto Database Connection | ||
const expect = require('chai').expect | ||
const path = require('path') | ||
const srcDir = path.join(__dirname, '../../src/') | ||
const testDir = path.join(__dirname, '../') | ||
const prestart = require(testDir + 'prestart') | ||
prestart.load() | ||
describe('Hitokoto Datebase Test', () => { | ||
const db = require(testDir + 'db') | ||
it('DB Connection should be true', async () => { | ||
const result = !!await db.connect() | ||
expect(result).to.be.true | ||
}) | ||
it('Register Model should be true', async() => { | ||
const result = !!await db.registerModel('hitokoto') | ||
expect(result).to.be.true | ||
}) | ||
it('Try get data from database', async () => { | ||
const hitokoto = await db.registerModel('hitokoto') | ||
const data = await hitokoto.findOne() | ||
expect(!!data).to.be.true | ||
}) | ||
after(()=>{ | ||
process.exit() | ||
}) | ||
}) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters