Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions docs/memory/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 储存

Memory

## 需求:

用一套接口,像数据库一样高效读写数据

## 设计:

每个集合储存特定类型。
每个类型有自己的储存
方便地获取/管理每个储存的数据库
可以进行index来进行方便的查询
例如:
```ts
@Data
class SomeData {
@Index
index: number;

value: string;
}

@CollectionFor(SomeData)
class DataCollection extends Collection<SomeData> {

}

dataCollection = Container.get(DataCollection);list((v) => v.index === 0);
dataCollection.list({index: 1});
dataCollection.list((v) => v.index === 1);
```

45 changes: 45 additions & 0 deletions src/memory/DataService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
export abstract class DataService {
/**
* create a collection with given name and type
* @param name name of the collection
* @throws Error if the collection cannot be created
*/
abstract createCollection(name: string): void;

/**
* gets a collection with given name and type.
* @param name name of the collection
* @return the collection
* @throws Error if the collection does not exist
*/
abstract getCollection<T>(name: string): Collection<T>;

abstract exists(name: string): boolean;
}

export abstract class Collection<T> {
/**
* get name of the collection
* @return name of collection
*/
abstract getName(): string;

/**
* rename the collection
* @param name new name for the collection
* @throws Error if the collection name already exists
*/
abstract renameCollection(name: string): void;

abstract count(condition?: Predicate<T>): number;

abstract createIndex(...key: (keyof T)[]): void;

abstract dropIndex(...key: (keyof T)[]): void;

abstract list(condition?: Predicate<T>): T[];

abstract delete(condition?: Predicate<T>): void;

abstract update(condition: Predicate<T>, replacement: UnaryOperator<T>): void;
}
5 changes: 5 additions & 0 deletions src/memory/Decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

export const Data = function<T extends Constructor>(target: T): void {
const className = target.name;

};
13 changes: 13 additions & 0 deletions test/unit/memory/DataService.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {assert} from "chai";
import { DataService } from "../../../src/memory/DataService";
import { Container } from "typescript-ioc";

describe("data service", function() {
it("should create a collection", function() {
const dataService = Container.get(DataService);
const collectionName = "test";
dataService.createCollection(collectionName);
assert(dataService.exists(collectionName));
assert.exists(dataService.getCollection(collectionName));
});
});