model enum pluggable

This commit is contained in:
jysperm
2014-10-20 11:22:42 +08:00
parent 52c873a26c
commit 4e1091fb83
10 changed files with 89 additions and 40 deletions

View File

@@ -7,6 +7,7 @@ app.libs =
async: require 'async'
bodyParser: require 'body-parser'
cookieParser: require 'cookie-parser'
copy: require 'copy-to'
crypto: require 'crypto'
depd: require 'depd'
express: require 'express'
@@ -23,7 +24,7 @@ app.libs =
ObjectId: (require 'mongoose').Schema.Types.ObjectId
{cookieParser, crypto, bodyParser, depd, express, fs, harp, middlewareInjector, mongoose} = exports.libs
{cookieParser, copy, crypto, bodyParser, depd, express, fs, harp, middlewareInjector, mongoose} = exports.libs
{morgan, nodemailer, path, redis, redisStore, expressSession} = exports.libs
RedisStore = redisStore expressSession
@@ -66,14 +67,35 @@ do ->
app.config = config
app.db = require './core/db'
app.pluggable = require './core/pluggable'
app.schemas = {}
app.models =
Account: {}
BalanceLog: {}
CouponCode: {}
Notification: {}
SecurityLog: {}
Ticket: {}
app.pluggable.initializePlugins()
require './core/model/account'
require './core/model/balance_log'
require './core/model/coupon_code'
require './core/model/notification'
require './core/model/security_log'
require './core/model/ticket'
for name, schema of app.schemas
copy(mongoose.model(name, schema)).to app.models[name]
app.templates = require './core/templates'
app.i18n = require './core/i18n'
app.utils = require './core/utils'
app.cache = require './core/cache'
app.config = require './config'
app.package = require './package.json'
app.billing = require './core/billing'
app.pluggable = require './core/pluggable'
app.middleware = require './core/middleware'
app.notification = require './core/notification'
app.authenticator = require './core/authenticator'
@@ -84,16 +106,6 @@ app.express = express()
app.redis = redis.createClient 6379, '127.0.0.1',
auth_pass: config.redis.password
app.schemas = {}
app.models =
Account: require './core/model/account'
BalanceLog: require './core/model/balance_log'
CouponCode: require './core/model/coupon_code'
Notification: require './core/model/notification'
SecurityLog: require './core/model/security_log'
Ticket: require './core/model/ticket'
app.express.use bodyParser.json()
app.express.use morgan 'dev'
app.express.use cookieParser()
@@ -164,8 +176,6 @@ app.express.use '/ticket', require './core/router/ticket'
app.express.use '/admin', require './core/router/admin'
app.express.use '/panel', require './core/router/panel'
app.pluggable.initializePlugins()
app.express.get '/', (req, res) ->
unless res.headerSent
res.redirect '/panel/'

View File

@@ -11,3 +11,6 @@ else
module.exports = mongoose.createConnection mongodb_uri
exports.mongodb_uri = mongodb_uri
mongoose.connection.on 'error', (err) ->
console.error err if err

View File

@@ -1,13 +1,12 @@
async = require 'async'
_ = require 'underscore'
{pluggable} = app
{selectModelEnum} = pluggable
{_, async, mongoose} = app.libs
Token = mongoose.Schema
type:
required: true
type: String
enum: ['full_access']
enum: ['full_access'].concat selectModelEnum 'Token', 'type'
token:
required: true
@@ -81,7 +80,9 @@ Account = mongoose.Schema
type: Object
default: {}
module.exports = mongoose.model 'Account', Account
_.extend app.schemas,
Account: Account
Token: Token
# @param account: username, email, password
# @param callback(account)

View File

@@ -1,3 +1,5 @@
{pluggable} = app
{selectModelEnum} = pluggable
{_, ObjectId, mongoose} = app.libs
BalanceLog = mongoose.Schema
@@ -9,7 +11,7 @@ BalanceLog = mongoose.Schema
type:
required: true
type: String
enum: ['deposit']
enum: ['deposit'].concat selectModelEnum 'BalanceLog', 'type'
amount:
required: true
@@ -23,4 +25,5 @@ BalanceLog = mongoose.Schema
type: Object
default: {}
module.exports = mongoose.model 'BalanceLog', BalanceLog
_.extend app.schemas,
BalanceLog: BalanceLog

View File

@@ -1,3 +1,5 @@
{pluggable} = app
{selectModelEnum} = pluggable
{_, ObjectId, mongoose} = app.libs
CouponCode = mongoose.Schema
@@ -16,7 +18,7 @@ CouponCode = mongoose.Schema
type:
required: true
type: String
enum: ['amount']
enum: ['amount'].concat selectModelEnum 'CouponCode', 'type'
meta:
type: Object
@@ -33,7 +35,8 @@ CouponCode = mongoose.Schema
default: Date.now
]
module.exports = mongoose.model 'CouponCode', CouponCode
_.extend app.schemas,
CouponCode: CouponCode
exports.type_meta =
amount:

View File

@@ -1,7 +1,7 @@
{pluggable} = app
{selectModelEnum} = pluggable
{_, ObjectId, mongoose} = app.libs
module.exports = exports = app.db.collection 'notifications'
Notification = mongoose.Schema
account_id:
type: ObjectId
@@ -15,12 +15,12 @@ Notification = mongoose.Schema
type:
required: true
type: String
enum: ['payment_success']
enum: ['payment_success'].concat selectModelEnum 'Notification', 'type'
level:
required: true
type: String
enum: ['notice', 'event', 'log']
enum: ['notice', 'event', 'log'].concat selectModelEnum 'Notification', 'type'
created_at:
type: Date
@@ -30,7 +30,8 @@ Notification = mongoose.Schema
type: Object
default: {}
module.exports = mongoose.model 'Notification', Notification
_.extend app.schemas,
Notification: Notification
exports.createNotice = (account, group_name, type, level, meta, callback) ->
exports.insert

View File

@@ -1,3 +1,5 @@
{pluggable} = app
{selectModelEnum} = pluggable
{_, ObjectId, mongoose} = app.libs
SecurityLog = mongoose.Schema
@@ -9,7 +11,7 @@ SecurityLog = mongoose.Schema
type:
required: true
type: String
enum: ['update_password', 'update_setting', 'update_email']
enum: ['update_password', 'update_setting', 'update_email'].concat selectModelEnum 'SecurityLog', 'type'
created_at:
type: Date
@@ -24,7 +26,8 @@ SecurityLog = mongoose.Schema
type: ObjectId
ref: 'Token'
module.exports = mongoose.model 'SecurityLog', SecurityLog
_.extend app.schemas,
SecurityLog: SecurityLog
exports.create = (account, type, token, payload, callback) ->
matched_token = _.findWhere account.tokens,

View File

@@ -1,7 +1,7 @@
{pluggable} = app
{selectModelEnum} = pluggable
{_, ObjectId, mongoose} = app.libs
module.exports = exports = app.db.collection 'tickets'
Reply = mongoose.Schema
_id:
type: ObjectId
@@ -50,7 +50,7 @@ Ticket = mongoose.Schema
status:
type: String
enum: ['open', 'pending', 'finish', 'closed']
enum: ['open', 'pending', 'finish', 'closed'].concat selectModelEnum 'Ticket', 'type'
option:
type: Object
@@ -64,7 +64,8 @@ Ticket = mongoose.Schema
Reply
]
module.exports = mongoose.model 'Ticket', Ticket
_.extend app.schemas,
Ticket: Ticket
exports.createTicket = (account, title, content, members, status, options, callback) ->
exports.insert

View File

@@ -14,13 +14,27 @@ hookHelper = (options) ->
return _.extend [], options
exports.hooks =
app:
# action: function(callback)
started: hookHelper
global_event: true
model:
# model: string, field: string, type: string
type_enum: hookHelper
global_event: true
# model: string, action(schema, callback)
middleware: hookHelper
global_event: true
account:
# filter: function(username, callback(is_allow))
username_filter: hookHelper
always_notice: true
global_event: true
# filter: function(account, callback)
before_register: hookHelper
always_notice: true
global_event: true
# action: function(account, callback)
resources_limit_changed: []
@@ -105,7 +119,7 @@ exports.selectHook = (account, hook_name) ->
return _.filter pointer, (hook) ->
if hook.plugin_info.type == 'extension'
return true
else if pointer.always_notice or hook.always_notice
else if pointer.global_event or hook.always_notice
return true
else if !account
return false
@@ -114,6 +128,15 @@ exports.selectHook = (account, hook_name) ->
else
return false
exports.selectModelEnum = (model, field) ->
result = []
for hook in exports.selectHook null, 'model.type_enum'
if hook.model == model and hook.field == field
result.push hook.type
return result
exports.initializePlugins = (callback) ->
checkDependencies = ->
all_plugins = _.union config.plugin.available_extensions, config.plugin.available_services

View File

@@ -53,7 +53,8 @@
"morgan": "^1.3.2",
"mongoose": "^3.8.17",
"depd": "^1.0.0",
"body-parser": "^1.9.0"
"body-parser": "^1.9.0",
"copy-to": "^1.0.1"
},
"devDependencies": {
"mocha": "^1.21.5",