From e06b98ee0d71cf8bf806f196e1c830f418550154 Mon Sep 17 00:00:00 2001 From: Shigma <1700011071@pku.edu.cn> Date: Tue, 14 Jan 2020 20:15:19 +0800 Subject: [PATCH] feat: koishi-database-memory --- packages/database-memory/README.md | 11 ++++++++ packages/database-memory/package.json | 28 +++++++++++++++++++ .../src/index.ts} | 4 ++- .../tests/index.spec.ts} | 6 ++-- packages/database-memory/tsconfig.json | 10 +++++++ packages/koishi-core/package.json | 1 + packages/koishi-core/tests/suggestion.spec.ts | 7 ++--- packages/koishi-core/tests/validation.spec.ts | 7 ++--- packages/plugin-common/package.json | 1 + packages/plugin-common/tests/handler.spec.ts | 6 ++-- packages/plugin-common/tests/help.spec.ts | 8 ++---- packages/plugin-common/tests/repeater.spec.ts | 6 ++-- packages/plugin-common/tests/welcome.spec.ts | 7 ++--- packages/test-utils/src/index.ts | 1 - tsconfig.json | 1 + 15 files changed, 73 insertions(+), 31 deletions(-) create mode 100644 packages/database-memory/README.md create mode 100644 packages/database-memory/package.json rename packages/{test-utils/src/memory.ts => database-memory/src/index.ts} (96%) rename packages/{test-utils/tests/memory.spec.ts => database-memory/tests/index.spec.ts} (51%) create mode 100644 packages/database-memory/tsconfig.json diff --git a/packages/database-memory/README.md b/packages/database-memory/README.md new file mode 100644 index 0000000000..2fd03bcd46 --- /dev/null +++ b/packages/database-memory/README.md @@ -0,0 +1,11 @@ +# koishi-database-memory + +[![Status](https://img.shields.io/github/workflow/status/koishijs/koishi/CI/master?style=flat-square)](https://github.com/koishijs/koishi/actions?query=workflow:CI) +[![npm](https://img.shields.io/npm/v/koishi-database-memory?style=flat-square)](https://www.npmjs.com/package/koishi-database-memory) + +An in-memory database for Koishi. + +## Usage + +- [Using Database with Koishi](https://koishi.js.org/guide/using-database.html) +- [Full API Reference](https://koishi.js.org/api/database.html) diff --git a/packages/database-memory/package.json b/packages/database-memory/package.json new file mode 100644 index 0000000000..3709454ce3 --- /dev/null +++ b/packages/database-memory/package.json @@ -0,0 +1,28 @@ +{ + "name": "koishi-database-memory", + "description": "An in-memory database implementation for Koishi", + "version": "1.0.0", + "main": "dist/index.js", + "typings": "dist/index.d.ts", + "files": [ + "dist" + ], + "author": "Shigma <1700011071@pku.edu.cn>", + "license": "MIT", + "scripts": { + "lint": "eslint src --ext .ts", + "prepack": "tsc -b" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/koishijs/koishi.git" + }, + "bugs": { + "url": "https://github.com/koishijs/koishi/issues" + }, + "homepage": "https://github.com/koishijs/koishi/tree/master/packages/database-memory#readme", + "dependencies": { + "koishi-core": "^1.3.0", + "koishi-utils": "^1.0.2" + } +} diff --git a/packages/test-utils/src/memory.ts b/packages/database-memory/src/index.ts similarity index 96% rename from packages/test-utils/src/memory.ts rename to packages/database-memory/src/index.ts index 1619e00d47..b7603a92e8 100644 --- a/packages/test-utils/src/memory.ts +++ b/packages/database-memory/src/index.ts @@ -1,4 +1,4 @@ -import { injectMethods, TableType, TableData, createUser, User, createGroup, getSelfIds, Group, InjectConfig } from 'koishi-core' +import { injectMethods, TableType, TableData, createUser, User, createGroup, getSelfIds, Group, InjectConfig, registerDatabase } from 'koishi-core' import { observe } from 'koishi-utils' declare module 'koishi-core/dist/database' { @@ -25,6 +25,8 @@ export class MemoryDatabase { } } +registerDatabase('memory', MemoryDatabase) + function clone (source: T): T { return JSON.parse(JSON.stringify(source)) } diff --git a/packages/test-utils/tests/memory.spec.ts b/packages/database-memory/tests/index.spec.ts similarity index 51% rename from packages/test-utils/tests/memory.spec.ts rename to packages/database-memory/tests/index.spec.ts index 191bc44a42..3d758a3f72 100644 --- a/packages/test-utils/tests/memory.spec.ts +++ b/packages/database-memory/tests/index.spec.ts @@ -1,7 +1,5 @@ -import { testDatabase, MemoryDatabase } from 'koishi-test-utils' -import { registerDatabase } from 'koishi-core' - -registerDatabase('memory', MemoryDatabase) +import { testDatabase } from 'koishi-test-utils' +import '../src' testDatabase({ memory: {}, diff --git a/packages/database-memory/tsconfig.json b/packages/database-memory/tsconfig.json new file mode 100644 index 0000000000..3132edfc8f --- /dev/null +++ b/packages/database-memory/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "../tsconfig.base", + "compilerOptions": { + "outDir": "dist", + "rootDir": "src", + }, + "include": [ + "src", + ], +} \ No newline at end of file diff --git a/packages/koishi-core/package.json b/packages/koishi-core/package.json index 71e5aee236..7a6ce1de52 100644 --- a/packages/koishi-core/package.json +++ b/packages/koishi-core/package.json @@ -35,6 +35,7 @@ "@types/debug": "^4.1.5", "@types/ws": "^6.0.4", "get-port": "^5.1.0", + "koishi-database-memory": "^1.0.0", "koishi-test-utils": "^2.0.0" }, "dependencies": { diff --git a/packages/koishi-core/tests/suggestion.spec.ts b/packages/koishi-core/tests/suggestion.spec.ts index 84f9272778..16b0fae438 100644 --- a/packages/koishi-core/tests/suggestion.spec.ts +++ b/packages/koishi-core/tests/suggestion.spec.ts @@ -1,8 +1,7 @@ -import { MockedApp, MemoryDatabase } from 'koishi-test-utils' -import { messages, showSuggestions, registerDatabase } from 'koishi-core' +import { MockedApp } from 'koishi-test-utils' +import { messages, showSuggestions } from 'koishi-core' import { format } from 'util' - -registerDatabase('memory', MemoryDatabase) +import 'koishi-database-memory' describe('Command Suggestions', () => { const app = new MockedApp() diff --git a/packages/koishi-core/tests/validation.spec.ts b/packages/koishi-core/tests/validation.spec.ts index 438dfe742b..e012efe5fa 100644 --- a/packages/koishi-core/tests/validation.spec.ts +++ b/packages/koishi-core/tests/validation.spec.ts @@ -1,7 +1,6 @@ -import { MockedApp, MemoryDatabase } from 'koishi-test-utils' -import { UserFlag, GroupFlag, messages, registerDatabase } from 'koishi-core' - -registerDatabase('memory', MemoryDatabase) +import { MockedApp } from 'koishi-test-utils' +import { UserFlag, GroupFlag, messages } from 'koishi-core' +import 'koishi-database-memory' const app = new MockedApp({ database: { memory: {} } }) const session1 = app.createSession('user', 123) diff --git a/packages/plugin-common/package.json b/packages/plugin-common/package.json index 3f0fad6bd6..6928bdba01 100644 --- a/packages/plugin-common/package.json +++ b/packages/plugin-common/package.json @@ -28,6 +28,7 @@ "plugin" ], "devDependencies": { + "koishi-database-memory": "^1.0.0", "koishi-test-utils": "^2.0.0" }, "dependencies": { diff --git a/packages/plugin-common/tests/handler.spec.ts b/packages/plugin-common/tests/handler.spec.ts index 98edeefca4..3e2faed666 100644 --- a/packages/plugin-common/tests/handler.spec.ts +++ b/packages/plugin-common/tests/handler.spec.ts @@ -1,9 +1,7 @@ -import { MockedApp, MemoryDatabase } from 'koishi-test-utils' -import { registerDatabase } from 'koishi-core' +import { MockedApp } from 'koishi-test-utils' import { sleep } from 'koishi-utils' import requestHandler, { HandlerConfig } from '../src/request-handler' - -registerDatabase('memory', MemoryDatabase) +import 'koishi-database-memory' let app: MockedApp diff --git a/packages/plugin-common/tests/help.spec.ts b/packages/plugin-common/tests/help.spec.ts index 5beef61e8b..040a642b00 100644 --- a/packages/plugin-common/tests/help.spec.ts +++ b/packages/plugin-common/tests/help.spec.ts @@ -1,13 +1,11 @@ -import { Session, MemoryDatabase, MockedApp } from 'koishi-test-utils' -import { App, registerDatabase } from 'koishi-core' +import { Session, MockedApp } from 'koishi-test-utils' import { noop } from 'koishi-utils' import help from '../src/help' - -registerDatabase('memory', MemoryDatabase) +import 'koishi-database-memory' const MESSAGE_COMMAND_CALLED = 'command called' -function prepare (app: App) { +function prepare (app: MockedApp) { app.plugin(help) app.command('foo', 'command with options', { maxUsage: 100, minInterval: 1000 }) .option('-o [value]', 'option', { authority: 2, notUsage: true }) diff --git a/packages/plugin-common/tests/repeater.spec.ts b/packages/plugin-common/tests/repeater.spec.ts index 056188ce87..b731576f09 100644 --- a/packages/plugin-common/tests/repeater.spec.ts +++ b/packages/plugin-common/tests/repeater.spec.ts @@ -1,8 +1,6 @@ -import { MockedApp, MemoryDatabase } from 'koishi-test-utils' -import { registerDatabase } from 'koishi-core' +import { MockedApp } from 'koishi-test-utils' import repeater, { RepeaterOptions } from '../src/repeater' - -registerDatabase('memory', MemoryDatabase) +import 'koishi-database-memory' test('repeat', async () => { const app = new MockedApp() diff --git a/packages/plugin-common/tests/welcome.spec.ts b/packages/plugin-common/tests/welcome.spec.ts index cd10d0e629..24bc7b956a 100644 --- a/packages/plugin-common/tests/welcome.spec.ts +++ b/packages/plugin-common/tests/welcome.spec.ts @@ -1,9 +1,8 @@ -import { MockedApp, MemoryDatabase } from 'koishi-test-utils' -import { Meta, registerDatabase } from 'koishi-core' +import { MockedApp } from 'koishi-test-utils' +import { Meta } from 'koishi-core' import { sleep } from 'koishi-utils' import welcome from '../src/welcome' - -registerDatabase('memory', MemoryDatabase) +import 'koishi-database-memory' const shared: Meta = { postType: 'notice', diff --git a/packages/test-utils/src/index.ts b/packages/test-utils/src/index.ts index f16436a8bd..587281cd00 100644 --- a/packages/test-utils/src/index.ts +++ b/packages/test-utils/src/index.ts @@ -1,5 +1,4 @@ export * from './database' -export * from './memory' export * from './mocks' export * from './utils' export * from './server' diff --git a/tsconfig.json b/tsconfig.json index ed68a73def..7f040abae0 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -13,6 +13,7 @@ { "path": "./packages/database-level" }, { "path": "./packages/database-mysql" }, { "path": "./packages/database-sqlite" }, + { "path": "./packages/database-memory" }, { "path": "./packages/plugin-common" }, { "path": "./packages/plugin-schedule" }, { "path": "./packages/plugin-teach" },