From 0f58d5d0a9b58f673030d5aa0e254e32cb9dd90a Mon Sep 17 00:00:00 2001 From: jia wei Date: Sat, 19 Jun 2021 15:54:02 +0800 Subject: [PATCH] =?UTF-8?q?feat(create):=20=E5=88=9B=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 27 ++++++++++++++++++++++----- examples/conn.ts | 36 +++++++++++++++++++++++++++++++++--- src/client.ts | 29 +++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 5bc21d2..37b0f54 100644 --- a/README.md +++ b/README.md @@ -9,13 +9,29 @@ import { Client } from "../mod.ts"; const client = new Client(); await client.connect("http://localhost:9200/"); -const ajax = async () => { +const count = async () => { try { - const count = await client.count({ + const names = await client.count({ index: "myindex2", - // method: "post", + method: "post", }); - console.log(count); + console.log(names); + } catch (error) { + console.error(error); + } +}; + +const create = async () => { + try { + const names = await client.create({ + index: "myindex2", + id: 3, + body: { + title: "当时明月在", + id: "6058046316761d2e8752aa4c", + }, + }); + console.log(names); } catch (error) { console.error(error); } @@ -23,7 +39,8 @@ const ajax = async () => { console.time("ajax"); // await Array.from(new Array(100)).map(ajax); -await ajax(); +await count(); +await create(); console.timeEnd("ajax"); ``` diff --git a/examples/conn.ts b/examples/conn.ts index 05a6585..12063bc 100644 --- a/examples/conn.ts +++ b/examples/conn.ts @@ -1,16 +1,41 @@ import { Client } from "../mod.ts"; +import { v4 } from "https://deno.land/std@0.99.0/uuid/mod.ts"; const client = new Client(); - await client.connect("http://localhost:9200/"); -const ajax = async () => { +const count = async () => { try { const names = await client.count({ index: "myindex2", method: "post", }); + console.log(names); + } catch (error) { + console.error(error); + } +}; +const create = async () => { + try { + const id = v4.generate(); + const names = await client.create({ + index: "myindex2", + id, + body: { + deleted: false, + // _id: '6058046316761d2e8752aa4c44', + _name: "hahs", + title: "倚天屠龙记4", + content: "剑心通明4", + userId: "41", + isSecret: false, + group: "中国", + contentText: "剑心通明4", + titleText: "倚天屠龙记4", + id: "6058046316761d2e8752aa4c", + }, + }); console.log(names); } catch (error) { console.error(error); @@ -19,5 +44,10 @@ const ajax = async () => { console.time("ajax"); // await Array.from(new Array(100)).map(ajax); -await ajax(); +// await create(); +await count(); + +setTimeout(async () => { + await create(); +}, 1000); console.timeEnd("ajax"); diff --git a/src/client.ts b/src/client.ts index 414b1fd..3c1db14 100644 --- a/src/client.ts +++ b/src/client.ts @@ -67,6 +67,35 @@ export class Client { }); } + create(params: { + method?: Method; + body: any; + id: string | number; + index: string; + }) { + let { + method = "PUT", + body, + id, + index, + } = params; + let path = ""; + + if (index != null && id != null) { + const type = "_doc"; + path = "/" + encodeURIComponent(index) + "/" + encodeURIComponent(type) + + "/" + encodeURIComponent(id) + "/" + "_create"; + } else { + path = "/" + encodeURIComponent(index) + "/" + "_create" + "/" + + encodeURIComponent(id); + } // build request object + return ajax({ + url: path, + method, + data: body, + }); + } + close() { if (this.conn) { this.conn.close();