Building content
A Record is a content asset: one of the content model's types, with its field values. A script builds one to create content, or reads one from what a Content Fetch returned to edit it, and hands the result to a Content Edit Save as described under Handing content to a Save. Every call on this page is listed with its signature under Record in the reference.
Creating an asset
Name the type, then set its fields:
1const { Record } = dari;23const article = new Record("com.example.Article");4article.put("headline", "Storm closes harbor");5article.put("readCount", 0);
put returns the record, so it chains:
1const article = new Record("com.example.Article")2.put("headline", "Storm closes harbor")3.put("featured", true);
putAll sets several at once:
1article.putAll({2headline: "Storm closes harbor",3subheadline: "Ferries cancelled through Friday",4featured: true5});
A new record takes a fresh id when it is built. To build one under an id you already hold, pass it as the second argument, new Record("com.example.Article", id). dari.uuid() mints an id of the same kind for anything else that needs one.
Naming a type
The full internal name always works. When the last part of it is unique across the content model, that part alone works too. Without a connection, a short name is tried against this workspace's own types first:
1new Record("Article"); // same type, when only one is called Article2new Record("com.example.Article"); // always
Matching is case-insensitive, and a full name is tried first, so a type genuinely called Article is never shadowed by a shorthand match on something else. A nested type keeps its outer class: Content$ObjectModification, not ObjectModification.
If more than one type shares the ending, the step fails and names the candidates, and you qualify:
1"Renderer" matches more than one content type (com.psddev.cms.db.Renderer,2com.psddev.cms.render.Renderer). Use the full internal name.
Whether a shorthand is unambiguous is a fact about the content model, not about your script. A new type that shares the ending can make a working script start failing. It fails loudly and the fix is to qualify the name, but a script you want immune to that should use full names.
For a connected CMS, the full internal name is the Java class name, the same name a developer on your team would use. For a type defined on the Content Types page, it is on the type's Developer tab, and its last part is the type's name in Pascal case: PressRelease for a type named Press Release. Name a type the content model does not have and the script stops there:
1No content type named "com.example.Nope".
To look at a type rather than build one, dari.type("com.example.Article") gives its id, its full name, its fields by internal name, and the modification classes that apply to it; see TypeInfo.
Setting fields
Field names are checked against the type, and a name that does not exist lists the ones that do:
1com.example.Article has no field named "headlien". Available: headline, subheadline, body, …
Values are converted to whatever the field holds, so ordinary JavaScript values work:
1article.putAll({2headline: "Storm closes harbor", // text3readCount: 42, // number4featured: true, // true/false5status: "PUBLISHED", // a value from a fixed list6tagNames: ["storm", "harbor"], // a list or set7labels: { en: "Storm" } // a map8});
A field with a fixed set of values checks what you give it:
1Field "status" only accepts DRAFT, PUBLISHED, but got "ARCHIVED".
What each kind of field accepts, and the classes for the values JavaScript has no type for, is under Values. A field that is computed by the type rather than stored can't be set, and the script says so.
Other useful calls on a record:
| Call | What it does |
|---|---|
article.get("headline") | Reads a field |
article.remove("headline") | Removes the field |
article.put("headline", null) | Clears the value, keeping the field |
article.has("headline") | Whether the field is set |
article.keys() | Every field name that is set |
article.list("tags") | The list at a field, creating an empty one if it is not set yet |
article.entries() | Every field name and value that is set, in pairs |
article.id | The asset's id |
article.descendants() | Every record inside this one, including itself |
article.toRef() | A pointer to this asset, for setting on a reference field elsewhere |
article.toState() / article.toStateJson(indent) | Everything the asset holds, as data or as JSON text |
article.putRaw(name, value) | Writes a value under a stored name with no checking: the escape hatch for something the checks refuse |
Embedded objects and references
Brightspot stores an asset's related content in one of two ways, and which one a field uses is part of your content model rather than something you choose in the script.
An embedded object lives inside its parent and has no life of its own. Build one with Record and set it:
1const { Record } = dari;23const article = new Record("com.example.Article");45article.put("caption", new Record("com.example.Caption").put("text", "The harbor at dawn"));67article.put("gallery", [8new Record("com.example.Caption").put("text", "One"),9new Record("com.example.Caption").put("text", "Two")10]);
A reference points at an asset stored separately, which has to already exist. Point at one with RecordRef and its id, or with the id alone:
1const { RecordRef } = dari;23article.put("author", new RecordRef(input.authorId));
Get it the wrong way round and the script tells you which one the field wants:
1Field "author" holds a reference to separately stored content, so it needs a RecordRef.2A record built in this script has nothing to point at yet — save it first, then reference it by id.
That message names the one real constraint here: a script cannot create an asset and point another field at it in the same step, because the asset does not exist until a Save step runs. Create it in one step, save it, then reference it by id in the next. For the same reason, one record can't be set on two fields; toRef() points at it from the second place once it has been saved.
Editing an existing asset
Read an asset with a Content Fetch step, then load its values into the script:
1const { Record } = dari;23const article = Record.fromState(payload.values);45article.get("headline"); // "Storm closes harbor"6article.get("gallery").map(s => s.get("text")); // ["One", "Two"]
To edit a revision rather than live content, set the Fetch's Revision, and bind the Save that writes the result to the same Revision, so the changes land in that revision and live is untouched.
Embedded objects come back as records you can change in place, references as RecordRefs, and lists as ordinary arrays:
1article.put("headline", "Storm closes harbor early");23article.get("gallery")[0].put("text", "One, revised");45article.list("gallery").push(new Record("com.example.Caption").put("text", "Three"));
What to hand the Save once the edits are made, and how to skip the Save when nothing changed, is under Differences.
Fields that come from elsewhere
Some fields on your content are not declared by the content type itself. Brightspot adds publishing dates, import provenance, SEO settings and much else through modifications, separate classes that attach their fields to a type. Those fields are stored under prefixed names you would otherwise have to know.
as addresses them by the class and the plain field name instead:
1article.as("brightspot.importapi.ImportObjectModification")2.put("externalId", input.wireId)3.put("sourceUrl", input.wireUrl);45article.as("com.psddev.cms.db.Content$ObjectModification")6.put("publishDate", new Date());
You name the class and the field as they appear in the code; the prefix is worked out for you. A class that does not apply to your type lists the ones that do, and a field the class does not have lists its fields.
as supports the same calls as a record, get, put, putAll, remove and has, all reading and writing the same asset. internalName("publishDate") gives back the prefixed name the field is actually stored under, for the rare case where you need to write it yourself with putRaw. See ModificationView.
Related
- Values—what each kind of field accepts.
- Differences—turning an edited or new record into what a Save takes.
RecordandRecordRefin the reference.