Skip to content

Commit

Permalink
adjust database
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jan 9, 2020
1 parent e8fff6a commit 9540892
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
2 changes: 1 addition & 1 deletion packages/koishi-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@
"devDependencies": {
"@types/debug": "^4.1.5",
"@types/ws": "^6.0.4",
"get-port": "^5.1.0",
"koishi-database-level": "^1.0.3",
"koishi-test-utils": "^1.0.3"
},
"dependencies": {
"axios": "^0.19.0",
"debug": "^4.1.1",
"escape-string-regexp": "^2.0.0",
"get-port": "^5.1.0",
"koishi-utils": "^1.0.2",
"leven": "^3.1.0",
"ws": "^7.2.1"
Expand Down
38 changes: 21 additions & 17 deletions packages/koishi-core/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,15 @@ export interface AbstractDatabase {
stop? (): void | Promise<void>
}

const unknownMethods: { [K in SubdatabaseType]?: DatabaseInjections<K> } = {}
const unknownOptions: { [K in SubdatabaseType]?: TableConfig<K> } = {}
const subdatabases: { [K in SubdatabaseType]?: Subdatabase<K> } = {}
const existingDatabases: { [K in SubdatabaseType]?: DatabaseMap } = {}

export function registerDatabase <K extends SubdatabaseType> (name: K, subdatabase: Subdatabase<K, {}>) {
subdatabases[name] = subdatabase as any
subdatabase._methods = {}
subdatabase._options = {}
subdatabase._methods = unknownMethods[name] ?? {}
subdatabase._options = unknownOptions[name] ?? {}
}

export type DatabaseInjections <K extends SubdatabaseType, T extends TableType = TableType> = {
Expand All @@ -175,24 +177,26 @@ export function injectMethods <K extends SubdatabaseType, T extends TableType> (
options?: TableConfig<K>,
) {
const Subdatabase = subdatabases[name] as Subdatabase<K>
if (!Subdatabase) return
if (Subdatabase._manager) {
const config = Subdatabase._manager.config[name]
if (!config) return
if (!Subdatabase._manager.database[name]) {
Subdatabase._manager.createSubdatabase(name, config)
}
Subdatabase._manager.injectMethods(name, table, methods)
let methodMap: Partial<Record<TableType, DatabaseInjections<K, T>>>
let optionMap: Partial<Record<TableType, TableConfig<K>>>

// inject before subdatabase was registered
if (!Subdatabase) {
methodMap = unknownMethods[name] || (unknownMethods[name] = {} as never)
optionMap = unknownOptions[name] || (unknownOptions[name] = {} as never)
} else {
Subdatabase._methods[table] = {
...Subdatabase._methods[table] as any,
...methods as any,
optionMap = Subdatabase._options
if (Subdatabase._manager) {
// inject after application was created
Subdatabase._manager.injectMethods(name, table, methods)
methodMap = {} as any
} else {
methodMap = Subdatabase._methods
}
}
Subdatabase._options[table] = {
...Subdatabase._options[table] as any,
...options as any,
}

methodMap[table] = { ...methodMap[table], ...methods }
optionMap[table] = { ...optionMap[table] as any, ...options as any }
}

class DatabaseManager {
Expand Down
9 changes: 5 additions & 4 deletions packages/koishi-core/tests/database.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ class BarDatabase {
constructor (public options: BarOptions) {}
}

registerDatabase('foo', FooDatabase)
registerDatabase('bar', BarDatabase)

injectMethods('foo', 'user', {
myUserFunc1 () {
return 'my-foo-user-func'
Expand All @@ -68,6 +65,9 @@ injectMethods('foo', 'user', {
},
})

registerDatabase('foo', FooDatabase)
registerDatabase('bar', BarDatabase)

injectMethods('bar', 'user', {
myUserFunc1 () {
return 'my-bar-user-func'
Expand Down Expand Up @@ -117,7 +117,8 @@ describe('multiple databases', () => {
expect(() => new App({
database: {
foo: { value: 1 },
bar: {},
// make coverage happy
bar: { identifier: 'id' },
},
})).toThrow()
})
Expand Down

0 comments on commit 9540892

Please sign in to comment.