登录
首页 >  文章 >  前端

Injee - 为前端开发人员提供的无配置即时数据库

来源:dev.to

时间:2024-07-30 14:21:53 288浏览 收藏

哈喽!大家好,很高兴又见面了,我是golang学习网的一名作者,今天由我给大家带来一篇《Injee - 为前端开发人员提供的无配置即时数据库》,本文主要会讲到等等知识点,希望大家一起学习进步,也欢迎大家关注、点赞、收藏、转发! 下面就一起来看看吧!

Injee - 为前端开发人员提供的无配置即时数据库

作为前端开发人员,等待 api 的交付是一件痛苦的事情。如果有一个内置 api 的奇迹数据库会怎样?好吧,这不再是幻想了。 injee 是一个数据库,为前端开发人员提供了随时可用的 crud api。通过阅读本页,您将学习如何使用 injee,在 injee 中创建图书记录,并且您将学习如何操作和搜索数据。

入门

安装java

您只需执行一次此操作。访问 https://java.com 为您的机器下载 java。一旦安装在你的 cmd 或终端上,输入 java --varsion ,它就一定可以工作。

下载仁济

您可以点击这里下载injee。或者在你的终端使用:

$ wget https://codeberg.org/injee/injee/releases/download/0.2.0/injee-0.2.0.jar

使用仁济

导航到下载 injee jar 文件的目录,并使用以下命令运行它:

$ java -jar injee-0.2.0.jar

健康

让我们检查一下服务器是否正在运行。我们使用 api get http://localhost:4125/ops/health.

在你的终端尝试:

$ curl -x get http://localhost:4125/ops/health

输出应该是

{
  "health": "ok"
}

创建书籍

所以让我们创建一个书籍存储库,神奇的是,injee 有 api post http://localhost:4125/api/books 来创建一本书。如果您想创建汽车存储库,injee 有 api post http://localhost:4125/api/cars api。那么让我们创建一本书并将其存储在 injee 中:

$ curl -x post http://localhost:4125/api/books \
       -h "content-type: application/json" \
       -d '{"title": "treasure island", "author": "robert louis stevenson"}'

输出

{
  "title": "treasure island",
  "author": "robert louis stevenson",
  "id": "722e2b57-59cc-4254-85b5-562858264f75"
}

因此,injee 存储这本书,并给出一个 json,其中包含您发送给 injee 的所有值,以及一个 uuid,该 uuid 分配给名为 id 的 ney。

现在让我们创建另一本书:

$ curl -x post http://localhost:4125/api/books \
       -h "content-type: application/json" \
       -d '{"title": "adventures of huckleberry finn", "author": "mark twain"}'

输出

{
  "title": "adventures of huckleberry finn",
  "author": "mark twain",
  "id": "689976e3-082e-4943-9525-a21b47cba325"
}

而且它有效!

列出所有书籍

现在列出我们使用 get http://localhost:4125/api/books:
的所有书籍

$ curl -x get http://localhost:4125/api/books

输出

[
  {
    "title": "treasure island",
    "author": "robert louis stevenson",
    "id": "722e2b57-59cc-4254-85b5-562858264f75"
  },
  {
    "title": "adventures of huckleberry finn",
    "author": "mark twain",
    "id": "689976e3-082e-4943-9525-a21b47cba325"
  }
]

我们储存了一系列漂亮的书籍。

取一本书

现在让我们只获取一本书,为此我们使用 api get http://localhost:4125/api/books/:id:

$ curl -x get http://localhost:4125/api/books/689976e3-082e-4943-9525-a21b47cba325

输出

{
  "title": "adventures of huckleberry finn",
  "author": "mark twain",
  "id": "689976e3-082e-4943-9525-a21b47cba325"
}

因此,如果我在前面加上 id get http://localhost:4125/api/books/ 我会得到一本书的详细信息。

更新一本书

要更新书籍,请使用 put 和 http://localhost:4125/api/books/:id,后跟书籍的参数:

$ curl -x put http://localhost:4125/api/books/689976e3-082e-4943-9525-a21b47cba325 \
       -h "content-type: application/json" \
       -d '{"title": "adventures of tom sawyer"}'

输出

{
  "title": "adventures of tom sawyer",
  "author": "mark twain",
  "id": "689976e3-082e-4943-9525-a21b47cba325"
}

如上所示,书名已从《哈克贝利·费恩历险记》改为《汤姆·索亚历险记》。

现在让我们列出所有书籍:

$ curl -x get http://localhost:4125/api/books

输出

[
  {
    "title": "treasure island",
    "author": "robert louis stevenson",
    "id": "722e2b57-59cc-4254-85b5-562858264f75"
  },
  {
    "title": "adventures of tom sawyer",
    "author": "mark twain",
    "id": "689976e3-082e-4943-9525-a21b47cba325"
  }
]

确认我们的更新。

删除一本书

现在让我们删除一本书。为此,请使用 delete 和 http://localhost:4125/api/books/:id:

$ curl -x delete http://localhost:4125/api/books/689976e3-082e-4943-9525-a21b47cba325

输出

不会有任何输出,如果您在代码中尝试并收到响应对象,您应该得到状态 204。

现在我们列出所有书籍,并确认《汤姆索亚历险记》已被删除:

$ curl -x get http://localhost:4125/api/books

输出

[
  {
    "title": "treasure island",
    "author": "robert louis stevenson",
    "id": "722e2b57-59cc-4254-85b5-562858264f75"
  }
]


列表表

现在让我们创建一个用户:

$ curl -x post http://localhost:4125/api/users \
       -h "content-type: application/json" \
       -d '{"name": "karthik"}'

输出

{
  "name": "karthik",
  "created_at": "2024-07-22t11:18:42z",
  "updated_at": "2024-07-22t11:18:42z",
  "id": "ad100ab0-7893-421d-9233-353cc8899aa9"
}

现在我们的数据库中必须有两个表,即 books 和 users,让我们使用以下 api 列出它们:

$ curl -x get http://localhost:4125/ops/tables

输出

[
  "books",
  "users"
]

检索记录

让我们在用户表中添加另一条用户记录:

$ curl -x post http://localhost:4125/api/users \
       -h "content-type: application/json" \
       -d '{"name": "pari"}'

现在让我们获取所有用户并确认我们的添加

$ curl -x get http://localhost:4125/api/users

[
  {
    "name": "karthik",
    "created_at": "2024-07-22t11:18:42z",
    "updated_at": "2024-07-22t11:18:42z",
    "id": "ad100ab0-7893-421d-9233-353cc8899aa9"
  },
  {
    "name": "pari",
    "created_at": "2024-07-22t11:23:27z",
    "updated_at": "2024-07-22t11:23:27z",
    "id": "1f06bb65-1f2d-4980-9cfc-cf3d38c9db7e"
  }
]

现在让我们在 users 中搜索一个字符串:

$ curl -x get http://localhost:4125/api/users?q=pari

[
  {
    "name": "pari",
    "created_at": "2024-07-22t11:23:27z",
    "updated_at": "2024-07-22t11:23:27z",
    "id": "1f06bb65-1f2d-4980-9cfc-cf3d38c9db7e"
  }
]

支持仁济

现在让我们将数据库备份到名为 backup.json 的文件中:

$ curl -x get http://localhost:4125/ops/save?file=backup.json

输出

{
  "message": "saved to file backup.json"
}

阻止仁济

最后,要停止 injee,请在运行 injee 的终端中按 ctrl+c 来停止它。

加载备份

让我们重新开始仁济吧:

$ java -jar injee-0.2.0.jar

$ curl -x get http://localhost:4125/ops/load?file=backup.json

输出

{
  "message": "loaded from file backup.json"
}

所以你已经恢复了原来的数据库并运行了。恭喜.

不断更新

了解 injee 最新动态的最佳方法之一是关注其页面 https://injee.codeberg.page/ ,或关注其 rss https://codeberg.org/injee.rss

到这里,我们也就讲完了《Injee - 为前端开发人员提供的无配置即时数据库》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:dev.to 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>