POST json-服务器中的对象集合

POST collection of objects in json-server

我正在使用 json-server 为前端团队伪造 Api。

我们希望有一个功能可以在一次调用中创建多个对象(例如产品)。

在 WebApi2 或实际 RestApis 中,可以像下面这样完成:

POST api/products //For Single Creation
POST api/productCollections  //For Multiple Creation

我不知道如何使用 json-server 来实现它。我尝试使用邮递员 POST 将以下数据 api/products ,但它不会拆分数组并单独创建项目。

[
    {
        "id": "ff00feb6-b1f7-4bb0-b09c-7b88d984625d",
        "code": "MM",
        "name": "Product 2"
    },
    {
        "id": "1f4492ab-85eb-4b2f-897a-a2a2b69b43a5",
        "code": "MK",
        "name": "Product 3"
    }
]

它将整个数组视为单个项目并附加到现有的 json。

你能建议我如何在 json-server 中模拟批量插入吗?或者 Restful Api 应该始终用于单个对象操作?

据我所知,这不是 json-server 本身支持的功能,但可以通过解决方法来实现。

我假设您对 node.js

有一些先验知识

您必须创建一个 server.js 文件,然后您将使用 node.js 运行。 server.js 文件将使用 json-server 模块。

我在下面的代码片段中包含了 server.js 文件的代码。

我使用 lodash 进行重复检查。因此,您需要安装 lodash。如果你不想使用 lodash,你也可以用你自己的代码替换它,但我认为 lodash 工作得很好。

server.js 文件包含一个自定义 post 请求函数,它访问 json-server 实例中使用的 lowdb 实例。来自 POST 请求的数据被检查是否重复,并且只有新记录被添加到 id 尚不存在的数据库中。 lowdb的write()函数将数据持久化到db.json文件中。因此,内存中的数据和文件中的数据将始终匹配。

请注意,由 json-server 生成的 API 端点(或重写的端点)将仍然存在。因此,您可以将自定义函数与默认端点结合使用。

随意在需要的地方添加错误处理。

const jsonServer = require('json-server');
const server = jsonServer.create();
const _ = require('lodash')
const router = jsonServer.router('./db.json');
const middlewares = jsonServer.defaults();
const port = process.env.PORT || 3000;
    
server.use(middlewares);
server.use(jsonServer.bodyParser)
server.use(jsonServer.rewriter({
    '/api/products': '/products'
}));
    
server.post('/api/productcollection', (req, res) => {
    const db = router.db; // Assign the lowdb instance
    
    if (Array.isArray(req.body)) {
        req.body.forEach(element => {
            insert(db, 'products', element); // Add a post
        });
    }
    else {
        insert(db, 'products', req.body); // Add a post
    }
    res.sendStatus(200)
    
    /**
     * Checks whether the id of the new data already exists in the DB
     * @param {*} db - DB object
     * @param {String} collection - Name of the array / collection in the DB / JSON file
     * @param {*} data - New record
     */
    function insert(db, collection, data) {
        const table = db.get(collection);
        if (_.isEmpty(table.find(data).value())) {
            table.push(data).write();
        }
    }
});
    
server.use(router);
server.listen(port);

如有任何问题,欢迎随时提问。

标记为正确的答案实际上对我不起作用。由于插入函数的编写方式,它将始终生成新文档而不是更新现有文档。 “重写”对我也不起作用(也许我做错了什么),但创建一个完全独立的端点有帮助。

这是我的代码,以防它帮助其他人尝试进行批量插入(并修改现有数据(如果存在))。

const jsonServer = require('json-server');
const server = jsonServer.create()
const _ = require('lodash');
const router = jsonServer.router('./db.json');
const middlewares = jsonServer.defaults()

server.use(middlewares)
server.use(jsonServer.bodyParser)


server.post('/addtasks', (req, res) => {
    const db = router.db; // Assign the lowdb instance
    if (Array.isArray(req.body)) {
        req.body.forEach(element => {
            insert(db, 'tasks', element);
        });
    }
    else {
        insert(db, 'tasks', req.body);
    }
    res.sendStatus(200)

    function insert(db, collection, data) {
        const table = db.get(collection);

        // Create a new doc if this ID does not exist
        if (_.isEmpty(table.find({_id: data._id}).value())) {
            table.push(data).write();
        }
        else{
            // Update the existing data
            table.find({_id: data._id})
            .assign(_.omit(data, ['_id']))
            .write();
        }
    }
});

server.use(router)
server.listen(3100, () => {
  console.log('JSON Server is running')
})

在前端,调用看起来像这样: axios.post('http://localhost:3100/addtasks', tasks)

在发布此问题时它可能不起作用,但现在它起作用了,调用 /products 端点上的数组以进行批量插入。