Skip to content

MongoDB 和 Redis 的查询都基于 Typescript。自动完成也基于 Monaco Editor 的 Typescript 功能。

MongoDB

查询示例:

typescript
mongoshell((db) => {
  db.collection("user").find().limit(12).skip(1);
});

TIP

mongoshell(db => {})会在打开编辑器时自动填充,不需要手动输入。

TIP

函数也可以是异步的。

TIP

打包了 tsc,所以可以输入 Typescript。

可以声明多条命令。命令中也可以定义参数。

typescript
mongoshell((db) => {
  // 声明带有参数的命令
  db.collection("user")
    .find()
    .limit(params.int("limit"))
    .skip(params.int("skip", { default: 1 }));
});

Redis

查询示例:

typescript
redisshell((cli) => {
  // 根据滚动自动加载的扫描命令
  cli.scan(0).stream();

  // 声明带有参数的命令
  cli.get(params.string("key", { default: "hello" }));
});

可以声明多条命令。命令中也可以定义参数。

对于各种 Scan 命令,都有额外的stream()方法,可以根据表格滚动自动获取。

一些命令也有其他扩展参数,例如,下面的命令会扫描所有内存占用>= 56 的 key:

typescript
redisshell((cli) => {
  cli.scan(0, { memory: 56 }).stream();
});

参数说明

参数 Api 由全局变量params提供。具体请参见Params 说明

其他的 JavaScript 接口

typescript
declare class Base64 {
  static encode(input: Uint8Array): string;
  static decode(input: string): Uint8Array;
}

declare interface IHTTPOptions {
  headers?: { [k: string]: string[] };
  timeouts?: number;
  proxy?: string;
  body?: ["file", string] | Uint8Array;
}

declare interface IHttpResponse {
  code: number;
  message: string;
  headers: { [k: string]: string[] };
  body: Uint8Array;
}

declare function http(
  method: "get" | "post" | "put" | "head" | "delete" | "options" | "patch",
  url: string,
  opts?: IHTTPOptions
): Promise<IHttpResponse>;

declare const params: Params;

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?: string;
      nullable?: boolean;
      regexp?: RegExp;
      kind?: "json" | "textarea";
    }
  ): string;
  datetime(
    name: string,
    opts?: {
      min?: string | Date;
      max?: string | Date;
      default?: Date;
      nullable?: boolean;
      layout?: string;
      timezone?: string;
    }
  ): Date;
}

另外的也可以直接使用ObjectId, Decimal128, Long, UUID这些 BSON 类型。