Javascript 返回到lodash链接中的父对象。链();
我正在使用一个库(),它在引擎盖下使用lodash在json文件中创建一个本地数据库。我想知道的是,在修改子对象以修改另一个对象之后,如何在lodash链接中访问父对象,例如Javascript 返回到lodash链接中的父对象。链();,javascript,node.js,lodash,lowdb,Javascript,Node.js,Lodash,Lowdb,我正在使用一个库(),它在引擎盖下使用lodash在json文件中创建一个本地数据库。我想知道的是,在修改子对象以修改另一个对象之后,如何在lodash链接中访问父对象,例如 const low=require('lowdb'); const FileSync=require('lowdb/adapters/FileSync'); constadapter=newfilesync('db.json'); 常数db=低(适配器); /* 例如,数据库结构: { “员额”:[ { “id”:1, “
const low=require('lowdb');
const FileSync=require('lowdb/adapters/FileSync');
constadapter=newfilesync('db.json');
常数db=低(适配器);
/*
例如,数据库结构:
{
“员额”:[
{
“id”:1,
“标题”:“post1”
},
{
“id”:2,
“标题”:“post2”
},
{
“id”:3,
“标题”:“post3”
}
]
}
*/
分贝
.get('posts'))
.find({id:2})
.assign({title:'edited title'})
//之后,我想回到帖子编辑同一链中的另一个帖子
.write();
我知道这可以在多个电话中完成,但我想知道是否可以在一个电话链中完成。简短回答
如果您的目标是使代码尽可能美观,我建议您使用,以及它的updateById
函数。我在长篇回答中介绍了各种可能的解决方案,但就我个人而言,我会避免直接链接,而是采取以下措施:
const lodashId = require('lodash-id');
const low = require('lowdb');
const FileSync = require('lowdb/adapters/FileSync');
const adapter = new FileSync('db.json');
const db = low(adapter);
db._.mixin(lodashId);
const posts = db.get("posts");
posts.updateById( 1, { title: 'edited title 1' }).commit();
posts.updateById( 2, { title: 'edited title 2' }).commit();
posts.updateById( 3, { title: 'edited title 3' }).commit();
posts.write();
长话短说
有几种方法可以解决这个问题,到目前为止,updateById
不是唯一的方法。事实上,也可以通过使用tap
功能在一个链中执行此操作。下面我将介绍几个备选方案
我可以在浏览器中快速尝试这些功能吗?
当然!实际上,我制作了一个代码笔,您可以直接在浏览器中尝试所有可能的方法,只需记住打开浏览器控制台即可查看测试结果!以下是链接:
如何调用这些函数?
所有这些函数都接收一个适配器作为输入。你可以这样称呼他们:
const result = testcaseXY(adapter);
1A)点击并查找+分配
什么是点击
- 这是一个实用函数
- 它不会直接更改数据库
- 但是,它会将对对象hold的引用传递给
拦截器
函数(在本例中,我使用了一个箭头函数)
- 可以更改对值的引用,从而影响数据库中的对象
- 在数据库上调用
.write()
,可以确定更改
2A)点击并查找+设置
什么是设置
- 这是一种比
.assign
略短的方法,但只使用一个属性
- 您可能希望
.set
比更快。assign
3A)点击和更新bYID
什么是updateById
- 这是一个由
- 您必须首先将其作为mixin添加到lowdb
- 为此,您首先需要使用
const lodashId=require('lodash-id')来要求它
- 然后,您需要调用
db.\u.mixin(lodashId)
李>
- 在这里,我直接调用
.mixin(lodashId)
,因为我直接在点击
函数中使用lodash,而不需要经过lowdb
1B)临时变量&查找+赋值
正如您可能看到的,在这里使用临时变量可以使我们的代码更紧凑,更易于阅读、调试和重构
2B)临时变量和查找+设置
3B)临时变量和updateById
谢谢你的阅读!
我很乐意作进一步的解释,也许你需要它。
作为奖励,我想补充的是,可以编写一些聪明的实用程序函数作为lowdb/lodash mixin,以使我们能够使用更短的语法,并且仍然能够正确地链接。不过,这可能比您要寻找的要多。这就是原因!谢谢你的时间和努力。
function testcase1A(adapter)
{
const db = low(adapter);
return db
.get("posts")
.tap( (posts) => _.chain(posts).find({ id: 1 }).assign({ title: 'edited title 1' }).commit())
.tap( (posts) => _.chain(posts).find({ id: 2 }).assign({ title: 'edited title 2' }).commit())
.tap( (posts) => _.chain(posts).find({ id: 3 }).assign({ title: 'edited title 3' }).commit())
.write();
}
function testcase2A(adapter)
{
const db = low(adapter);
_.mixin(lodashId);
return db
.get("posts")
.tap( (posts) => _.chain(posts).find({ id: 1 }).set("title", 'edited title 1').commit())
.tap( (posts) => _.chain(posts).find({ id: 2 }).set("title", 'edited title 2').commit())
.tap( (posts) => _.chain(posts).find({ id: 3 }).set("title", 'edited title 3').commit())
.write();
}
function testcase3A(adapter)
{
const db = low(adapter);
_.mixin(lodashId);
return db
.get("posts")
.tap( (posts) => _.chain(posts).updateById( 1, { title: 'edited title 1' }).commit())
.tap( (posts) => _.chain(posts).updateById( 2, { title: 'edited title 2' }).commit())
.tap( (posts) => _.chain(posts).updateById( 3, { title: 'edited title 3' }).commit())
.write();
}
function testcase1B(adapter)
{
const db = low(adapter);
const posts = db.get("posts");
posts.find({ id: 1 }).assign({ title: 'edited title 1' }).commit();
posts.find({ id: 2 }).assign({ title: 'edited title 2' }).commit();
posts.find({ id: 3 }).assign({ title: 'edited title 3' }).commit();
return posts.write();
}
function testcase2B(adapter)
{
const db = low(adapter);
const posts = db.get("posts");
posts.find({ id: 1 }).set("title", 'edited title 1').commit();
posts.find({ id: 2 }).set("title", 'edited title 2').commit();
posts.find({ id: 3 }).set("title", 'edited title 3').commit();
return posts.write();
}
function testcase3B(adapter)
{
const db = low(adapter);
db._.mixin(lodashId);
const posts = db.get("posts");
posts.updateById( 1, { title: 'edited title 1' }).commit();
posts.updateById( 2, { title: 'edited title 2' }).commit();
posts.updateById( 3, { title: 'edited title 3' }).commit();
return posts.write();
}