Skip to content

Commit

Permalink
feat: koishi-database-memory
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jan 14, 2020
1 parent 6d26711 commit e06b98e
Show file tree
Hide file tree
Showing 15 changed files with 73 additions and 31 deletions.
11 changes: 11 additions & 0 deletions packages/database-memory/README.md
Original file line number Diff line number Diff line change
@@ -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)
28 changes: 28 additions & 0 deletions packages/database-memory/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
Original file line number Diff line number Diff line change
@@ -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' {
Expand All @@ -25,6 +25,8 @@ export class MemoryDatabase {
}
}

registerDatabase('memory', MemoryDatabase)

function clone <T> (source: T): T {
return JSON.parse(JSON.stringify(source))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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: {},
Expand Down
10 changes: 10 additions & 0 deletions packages/database-memory/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../tsconfig.base",
"compilerOptions": {
"outDir": "dist",
"rootDir": "src",
},
"include": [
"src",
],
}
1 change: 1 addition & 0 deletions packages/koishi-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
7 changes: 3 additions & 4 deletions packages/koishi-core/tests/suggestion.spec.ts
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
7 changes: 3 additions & 4 deletions packages/koishi-core/tests/validation.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"plugin"
],
"devDependencies": {
"koishi-database-memory": "^1.0.0",
"koishi-test-utils": "^2.0.0"
},
"dependencies": {
Expand Down
6 changes: 2 additions & 4 deletions packages/plugin-common/tests/handler.spec.ts
Original file line number Diff line number Diff line change
@@ -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

Expand Down
8 changes: 3 additions & 5 deletions packages/plugin-common/tests/help.spec.ts
Original file line number Diff line number Diff line change
@@ -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 })
Expand Down
6 changes: 2 additions & 4 deletions packages/plugin-common/tests/repeater.spec.ts
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
7 changes: 3 additions & 4 deletions packages/plugin-common/tests/welcome.spec.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
1 change: 0 additions & 1 deletion packages/test-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from './database'
export * from './memory'
export * from './mocks'
export * from './utils'
export * from './server'
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand Down

0 comments on commit e06b98e

Please sign in to comment.