sails.js 水线保存、读取、解析列表或数组
sails.js Waterline save, read, parse lists or arrays
在 Sails Waterline 中创建、读取和解析列表的正确方法是什么?
我在 Stack Overflow 或 Waterline 上找不到任何东西 Github。
准确的说我想在一个想法模型中保存一个标签列表,然后可以通过标签进行搜索。
创意模型:
attributes: {
tags: {
type: 'string'
}
}
创建函数(标签以逗号分隔的字符串形式传递)
create: function (req, res, next) {
tags: req.param('tags').split(',');
Idea.create(tags, function ideaCreate(err,idea) {
//do stuff after create
});
},
这成功存储了标签:即`tags = ['tag1', 'tag2', ..., 'tagN']
我试过 .find
.where
的时髦组合,但无济于事。
简单版:我如何return一些用户请求的想法tagX
更难的版本:我怎样才能 return 有至少一个标签列表中的任何想法?
选项一
我建议你为你的标签
创建一个模型
attributes: {
ideas: {
collection: 'idea'
},
name: {
type: 'string'
}
}
接下来,编辑您的 Idea 模型以引用您的 Tag 模型
attributes: {
tags: {
collection: 'tag'
},
name: {
type: 'string'
}
}
然后要获得与 "tagX" 相关的所有想法,您需要执行以下操作:
var tag = "tagX";
// This same code should also work with an array,
// but then you will have to use find instead of findOne
// var tag = ["tagX", "tagY", "tagZ"]
Tag.findOne({name: tag}).populate('ideas').then(function (tag) {
// Do anything you want with the Ideas.
tag.ideas.forEach(function(idea) {
console.log(idea);
});
}).catch(console.err);
使用标签 'tagX' 和 'tagY' 创建创意 'Some Grand Idea'
添加和删除标签 to/from 一个集合非常容易。
Promise.all([Idea.create({ name: 'Some Grand Idea'}),
Tag.create({ name: 'TagX'}),
Tag.create({ name: 'TagY'})]).
spread(function (idea, tagX, tagY) {
// Add tagX
idea.tags.add(tagX.id);
// Add tagY
idea.tags.add(tagY.id);
// To remove a tag, simply call remove
// idea.tags.remove(1)
idea.save(console.log);
}).catch(console.log);
所以总而言之,得到一个 Idea 模型。 add/remove 标记模型 to/from Idea.tags 集合。
这两种方式都有效,即你可以获得一个标签模型并将一个想法添加到 Tag.ideas 集合 tag.ideas.add(someIdea.id)
并且它的工作方式相同。
选项二
或者,按照您设置的方式使用 Idea 模型。
通过一些标签获得灵感:
Idea.find({ tags: { 'like': '%tagX%' }})
通过标签列表获得灵感:
Idea.find({
or : [
{ tags: { 'like': '%\'tagX\'%' } },
{ tags: { 'like': '%\'tagY\'%' } },
{ tags: { 'like': '%\'tagZ\'%' } }
]
})
在 Sails Waterline 中创建、读取和解析列表的正确方法是什么?
我在 Stack Overflow 或 Waterline 上找不到任何东西 Github。
准确的说我想在一个想法模型中保存一个标签列表,然后可以通过标签进行搜索。
创意模型:
attributes: {
tags: {
type: 'string'
}
}
创建函数(标签以逗号分隔的字符串形式传递)
create: function (req, res, next) {
tags: req.param('tags').split(',');
Idea.create(tags, function ideaCreate(err,idea) {
//do stuff after create
});
},
这成功存储了标签:即`tags = ['tag1', 'tag2', ..., 'tagN']
我试过 .find
.where
的时髦组合,但无济于事。
简单版:我如何return一些用户请求的想法tagX
更难的版本:我怎样才能 return 有至少一个标签列表中的任何想法?
选项一
我建议你为你的标签
创建一个模型attributes: {
ideas: {
collection: 'idea'
},
name: {
type: 'string'
}
}
接下来,编辑您的 Idea 模型以引用您的 Tag 模型
attributes: {
tags: {
collection: 'tag'
},
name: {
type: 'string'
}
}
然后要获得与 "tagX" 相关的所有想法,您需要执行以下操作:
var tag = "tagX";
// This same code should also work with an array,
// but then you will have to use find instead of findOne
// var tag = ["tagX", "tagY", "tagZ"]
Tag.findOne({name: tag}).populate('ideas').then(function (tag) {
// Do anything you want with the Ideas.
tag.ideas.forEach(function(idea) {
console.log(idea);
});
}).catch(console.err);
使用标签 'tagX' 和 'tagY' 创建创意 'Some Grand Idea' 添加和删除标签 to/from 一个集合非常容易。
Promise.all([Idea.create({ name: 'Some Grand Idea'}),
Tag.create({ name: 'TagX'}),
Tag.create({ name: 'TagY'})]).
spread(function (idea, tagX, tagY) {
// Add tagX
idea.tags.add(tagX.id);
// Add tagY
idea.tags.add(tagY.id);
// To remove a tag, simply call remove
// idea.tags.remove(1)
idea.save(console.log);
}).catch(console.log);
所以总而言之,得到一个 Idea 模型。 add/remove 标记模型 to/from Idea.tags 集合。
这两种方式都有效,即你可以获得一个标签模型并将一个想法添加到 Tag.ideas 集合 tag.ideas.add(someIdea.id)
并且它的工作方式相同。
选项二
或者,按照您设置的方式使用 Idea 模型。
通过一些标签获得灵感:
Idea.find({ tags: { 'like': '%tagX%' }})
通过标签列表获得灵感:
Idea.find({
or : [
{ tags: { 'like': '%\'tagX\'%' } },
{ tags: { 'like': '%\'tagY\'%' } },
{ tags: { 'like': '%\'tagZ\'%' } }
]
})