-
-
Notifications
You must be signed in to change notification settings - Fork 676
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: 将 db accessor 并到 Globals 管理;
- Loading branch information
Showing
21 changed files
with
169 additions
and
69 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,5 +1,6 @@ | ||
import { db } from "../lib/db" | ||
import { Globals } from "../lib/globals" | ||
|
||
const db = Globals.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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { db } from "../lib/db" | ||
import { Globals } from "../lib/globals" | ||
|
||
const db = Globals.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
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { db } from "../lib/db" | ||
import { Globals } from "../lib/globals" | ||
|
||
const db = Globals.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
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
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,84 @@ | ||
|
||
|
||
import { MongoAccessor, getDb, LoggerInterface } from 'less-api' | ||
import { Db } from 'less-api-database' | ||
import Config from '../config' | ||
import { createLogger } from './logger' | ||
|
||
/** | ||
* 管理应用的全局对象 | ||
*/ | ||
export class Globals { | ||
private static _accessor: MongoAccessor = null | ||
private static _db: Db = null | ||
private static _logger: LoggerInterface = null | ||
|
||
static get accessor() { | ||
return this._accessor | ||
} | ||
|
||
static get db() { | ||
return this._db | ||
} | ||
|
||
static get logger() { | ||
return this._logger | ||
} | ||
|
||
/** | ||
* 初始化全局对象 | ||
*/ | ||
static init() { | ||
// 创建全局日志对象 | ||
if (null === this._logger) { | ||
this._logger = createLogger('server') | ||
} | ||
|
||
// 创建 app db accessor | ||
if (null === this._accessor) { | ||
this._accessor = this._createAccessor() | ||
} | ||
|
||
// 创建 Db ORM 实例 | ||
if (null === this._db) { | ||
this._db = this.createDb() | ||
} | ||
|
||
Object.freeze(Globals) | ||
} | ||
|
||
|
||
/** | ||
* 创建 Db 实例 | ||
* @returns | ||
*/ | ||
static createDb() { | ||
if (null === this._accessor) { | ||
throw new Error('Globals.accessor is empty, please run Globals.init() before!') | ||
} | ||
return getDb(this._accessor) | ||
} | ||
|
||
|
||
/** | ||
* 创建 MongoAccessor 对象 | ||
* @returns | ||
*/ | ||
private static _createAccessor() { | ||
const accessor = new MongoAccessor(Config.db.database, Config.db.uri, { | ||
poolSize: Config.db.poolSize, | ||
useNewUrlParser: true, | ||
useUnifiedTopology: true | ||
}) | ||
|
||
accessor.setLogger(createLogger('db', 'warning')) | ||
accessor.init() | ||
|
||
return accessor | ||
} | ||
} | ||
|
||
/** | ||
* 初始化全局资源对象 | ||
*/ | ||
Globals.init() |
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 @@ | ||
|
||
|
||
/** | ||
* 递归深冻结对象 | ||
* @param object | ||
* @returns | ||
*/ | ||
export function deepFreeze(object: Object) { | ||
// Retrieve the property names defined on object | ||
const propNames = Object.getOwnPropertyNames(object) | ||
|
||
// Freeze properties before freezing self | ||
|
||
for (const name of propNames) { | ||
const value = object[name] | ||
|
||
if (value && typeof value === "object") { | ||
deepFreeze(value) | ||
} | ||
} | ||
|
||
return Object.freeze(object) | ||
} |
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
Oops, something went wrong.