mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-05 10:49:28 +02:00
First version with PostgreSQL
This commit is contained in:
parent
108626609e
commit
feb4bdfd9b
68 changed files with 1171 additions and 730 deletions
|
@ -1,36 +1,46 @@
|
|||
'use strict'
|
||||
|
||||
const mongoose = require('mongoose')
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const Sequelize = require('sequelize')
|
||||
|
||||
const constants = require('../initializers/constants')
|
||||
const logger = require('../helpers/logger')
|
||||
|
||||
// Bootstrap models
|
||||
require('../models/application')
|
||||
require('../models/oauth-token')
|
||||
require('../models/user')
|
||||
require('../models/oauth-client')
|
||||
require('../models/video')
|
||||
// Request model needs Video model
|
||||
require('../models/pods')
|
||||
// Request model needs Pod model
|
||||
require('../models/request')
|
||||
const database = {}
|
||||
|
||||
const database = {
|
||||
connect: connect
|
||||
}
|
||||
const sequelize = new Sequelize(constants.CONFIG.DATABASE.DBNAME, 'peertube', 'peertube', {
|
||||
dialect: 'postgres',
|
||||
host: constants.CONFIG.DATABASE.HOSTNAME,
|
||||
port: constants.CONFIG.DATABASE.PORT
|
||||
})
|
||||
|
||||
function connect () {
|
||||
mongoose.Promise = global.Promise
|
||||
mongoose.connect('mongodb://' + constants.CONFIG.DATABASE.HOSTNAME + ':' + constants.CONFIG.DATABASE.PORT + '/' + constants.CONFIG.DATABASE.DBNAME)
|
||||
mongoose.connection.on('error', function () {
|
||||
throw new Error('Mongodb connection error.')
|
||||
const modelDirectory = path.join(__dirname, '..', 'models')
|
||||
fs.readdir(modelDirectory, function (err, files) {
|
||||
if (err) throw err
|
||||
|
||||
files.filter(function (file) {
|
||||
if (file === 'utils.js') return false
|
||||
|
||||
return true
|
||||
})
|
||||
.forEach(function (file) {
|
||||
const model = sequelize.import(path.join(modelDirectory, file))
|
||||
|
||||
database[model.name] = model
|
||||
})
|
||||
|
||||
mongoose.connection.on('open', function () {
|
||||
logger.info('Connected to mongodb.')
|
||||
Object.keys(database).forEach(function (modelName) {
|
||||
if ('associate' in database[modelName]) {
|
||||
database[modelName].associate(database)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
logger.info('Database is ready.')
|
||||
})
|
||||
|
||||
database.sequelize = sequelize
|
||||
database.Sequelize = Sequelize
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue