MongoDB 和 Redis 的查询都基于 Typescript。当前的自动完成也基于 Monaco Editor 的 Typescript 功能。
MongoDB
查询示例:
typescript
mongoshell((db) => {
db.collection("user").find().limit(12).skip(1);
});
INFO
mongoshell(db => {})
会在打开编辑器时自动填充,不需要手动输入。
INFO
函数也可以是异步的。
可以声明多条命令。命令中也可以定义参数。
typescript
mongoshell((db) => {
db.collection("user").find().limit(params.int("limit")).skip(1);
});
Redis
查询示例:
typescript
redisshell((cli) => {
cli.scan(0).stream();
});
可以声明多条命令。命令中也可以定义参数。
对于各种 Scan 命令,都有额外的stream()
方法,可以根据表格滚动自动获取。
一些命令也有其他扩展参数,例如,下面的命令会扫描所有内存占用>= 56 的 key:
typescript
redisshell((cli) => {
cli.scan(0, { memory: 56 }).stream();
});
参数
参数可以在 MongoDB/Redis 命令中定义,例如:
typescript
mongoshell((db) => {
db.collection("user").find(
{name: { $regexp: params.string("name regexp") }}
).limit(
params.int("limit")
);
});
params
的类型定义为:
typescript
declare const params: Params;
interface Param { name: string; }
declare class Params {
int(name: string, opts?: {
max?: number;
min?: number;
default?: number;
nullable?: boolean;
}): number;
number(name: string, opts?: {
max?: number;
min?: number;
default?: number;
nullable?: boolean;
}): number;
float(name: string, opts?: {
max?: number;
min?: number;
default?: number;
nullable?: boolean;
}): number;
string(name: string, opts?: {
default?: number;
nullable?: boolean;
regexp?: RegExp;
}): string;
datetime(name: string, opts?: {
min?: string | Date;
max?: string | Date;
default?: number;
nullable?: boolean;
layout?: string;
timezone?: string;
}): Date;
}
其他 API
其他可以使用的对象有:
typescript
interface IObjectId {
toString(): string;
toHexString(): string;
}
// 可以用来编码和解码 Base64:
declare class Base64 {
static encode(input: Uint8Array): string;
static decode(input: string): Uint8Array;
}
// 可以用来生成 ObjectId,例如:
// ObjectId();
// ObjectId("650555814b47771608479999");
// ObjectId(1234567890);
declare function ObjectId(inputId?: string | number | IObjectId): IObjectId;
// 可以用来执行 HTTP 请求。
// 至于作用,我想可以通过自定义api来动态生成查询命令。
declare function http(method: string, url: string, opts?: { headers?: { [k: string]: string[] }; timeouts?: number; proxy?: string; body?: ["file", string] | Uint8Array }): Promise<{
code: number;
message: string;
headers: { [k: string]: string[] };
body: Uint8Array;
}>;