Primary and Embedded Updates

TIL how to update embedded documents at the same time as parent documents.
game.actors.getName("Foo").update({
'data.traits.size': 'sm',
items: [ {_id: "DHXcpH7kWIWXCaSB", 'data.quantity': 80} ]
});
game.actors.getName("Foo").update({
'data.traits.size': 'sm',
items: [ {_id: "DHXcpH7kWIWXCaSB", 'data.quantity': 80} ]
});
This makes sense when you look at the data._source of the Actor and see how the items are stored there.
C
Calego927d ago
Data structure for example (5e actors)
No description
C
Calego927d ago
Since items is expected to be an embedded collection, the .update method does the dirty work of exploding out the array as individual updates automatically. Setting an array in the update method for data that isn't expected to be an embedded collection would simply override the array entirely. Interestingly, this works just fine if you have multiple objects in the updateData items array with the same _id. Presumably each one is operated on sequentially.
game.actors.getName("Blank").update({
'data.traits.size': 'sm',
items: [
{_id: "DHXcpH7kWIWXCaSB", 'name': 'Arrow +2'},
{_id: "DHXcpH7kWIWXCaSB", 'name': 'Arrow +2.1'},
{_id: "DHXcpH7kWIWXCaSB", 'name': 'Arrow +2.2'},
]
});
game.actors.getName("Blank").update({
'data.traits.size': 'sm',
items: [
{_id: "DHXcpH7kWIWXCaSB", 'name': 'Arrow +2'},
{_id: "DHXcpH7kWIWXCaSB", 'name': 'Arrow +2.1'},
{_id: "DHXcpH7kWIWXCaSB", 'name': 'Arrow +2.2'},
]
});
Resulted in an item named Arrow +2.2
E
Ethck927d ago
Would it be possible to update a parent but include a new child item as well? I'm trying to limit the number of round trips to the server so if I can condense it that'd be chefkiss
C
Calego927d ago
Pretty sure definitely yes yeah! So
E
Ethck927d ago
So (based off of memory) if I call an update with data that does NOT contain an ID it usually fails. How do you get around that?
C
Calego927d ago
if the _id is there, it's an update operation. but if not it's a create
E
Ethck927d ago
ooh Sweet!
C
Calego927d ago
So you've gotta have all the right fields present for items, that's name and type
E
Ethck927d ago
That's what I was hoping.
C
Calego927d ago
No description
E
Ethck927d ago
Yeah, I just remember running into issues with update(s) and not having an _id lol.
C
Calego927d ago
No description
E
Ethck927d ago
So perfect! Thanks @calego
LTL
Leo The League Lion927d ago
@ethck gave vote LeaguePoints™ to @calego (#1 • 1258)
C
Calego927d ago
NICE documents rock
E
Ethck927d ago
definitely helps in situations like this.
C
Calego927d ago
Ohhh ok, interesting, found this snippet:
const item = new CONFIG.Item.documentClass({name: 'Foo', type: 'feat'});
const items = this.items.map(i => i.toObject());
items.push(item.toObject());
const item = new CONFIG.Item.documentClass({name: 'Foo', type: 'feat'});
const items = this.items.map(i => i.toObject());
items.push(item.toObject());
so you could construct the ephemeral Item beforehand to more easily interact with its data, then toObject it and push that into the update data
E
Ethck927d ago
What's the difference between doing CONFIG.Item.documentClass and just Item? Aren't they the same (usually)?
C
Calego927d ago
CONFIG.Item.documentClass would get you the system's item class, e.g. Item5e
E
Ethck927d ago
Ah, and I assume that's probably the more preferred route, good to know!
C
Calego927d ago
its more bulletproof yeah, in case a system does something particular about its created items, this would run the right constructor and suchforth and so on, instead of only running the Core ones.
E
Ethck927d ago
Makes sense.
M
Mana926d ago
BTW, this combined update is dangerous with unlinked tokens.
E
Ethck926d ago
How so?
M
Mana926d ago
Updating single small piece of an item with it causes it to replace the entire item list with what's in the update. So you end up destroying all items and their data that you didn't include. The joys of unlinked tokens, you know.
Want results from more Discord servers?
Add your server
More Posts
compendium errors upgrading to v9hmm im not entirely sure how this will work though.. because I did just a quick smell test to my modBlind Chat MessagesAnyone have good examples of creating a chat message that should only be visible to a connected GM, compendium shennaneginsTIL about `CompendiumContent.getDocuments` taking nedb queries. ``` game.packs.get('dnd5e.heroes').gwith just a parent class and the class name, is there a way to instantiate a new class?JamesB and @veterini have a question for y'all in #coc7-dev https://discord.com/channels/7323252527More Hooks 5ehttps://github.com/ElfFriend-DnD/foundryvtt-more-hooks-5e I'm going to try to use this library to hacreateEmbeddedDocuments issueI'm going to throw my code in a thread so I don't clutter this channel up with a wall of textBase Item@otigon it occurs to me that the new `baseItem` property on weapons and such is probably of interestCompendium loadinghmm ghost, I added some console.time tags here and there to measure the difference between loading oSkill and Ability bonusesA massive enhancement of Skills and Ability Checks/Saves allows them to be individually affected by 150 Sheet Changes@sdenec @lordzeel Sheet Changes required: - New Cog-menu for Ability Scores and Skills to support neDocumentData shennaneginsTIL: It is possible to `update` an Actor or Item's `data.data` with arbitrary information that isn'tflag shenanneginsTIL you can set the `flags` key on a document to whatever you want to (e.g. a string). This is a surraaeAfter some discussion yesterday in #active-effects , I decided to try to create a system-agnostic modevMode json changed warningI checked the module repo and I honestly can't figure out where I would plug in my code. I'd be guesS3 File Picker SettingsCan someone with an S3 configuration give me a test of the FilePicker settings api and tell me if a item preCreate@sol.folango @mrprimate (pinging you two in particular because you do import stuff involving existinItem Macro Compendium WorkflowOkay, here's a long one that's a bit of a doozy. I'm looking at setting up some sort of tooling/workItem Specific Crit DetailsOh that critical hit thing is gonna hit MRE too isn't it... hrm...1.5.x 72%@dnd5e No action required (but suggested 🙂 ) The 1.5.0 milestone is ~72% complete. It has a due daV9 Tabs IssueIf you never figured this out, here's why this happened: A small change in `Tabs` during v9 causes s