@farm.js/sanity 0.1.0-beta.107 → 0.1.0-beta.108

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"content.d.ts","sourceRoot":"","sources":["../src/content.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAyB,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AACnF,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAInD,MAAM,WAAW,0BAA0B;IACzC,qDAAqD;IACrD,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACjC,8EAA8E;IAC9E,MAAM,CAAC,EAAE,YAAY,CAAC;IACtB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf;;;;OAIG;IACH,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,gEAAgE;IAChE,IAAI,CAAC,EAAE,MAAM,CAAC;IACd;;;OAGG;IACH,EAAE,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,MAAM,CAAC;IACnD,6EAA6E;IAC7E,IAAI,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,MAAM,GAAG,SAAS,CAAC;IACjE,oDAAoD;IACpD,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB;;;;OAIG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,8EAA8E;IAC9E,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,0BAA0B,GAAG,mBAAmB,CA8IrF"}
1
+ {"version":3,"file":"content.d.ts","sourceRoot":"","sources":["../src/content.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAyB,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AACnF,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAInD,MAAM,WAAW,0BAA0B;IACzC,qDAAqD;IACrD,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACjC,8EAA8E;IAC9E,MAAM,CAAC,EAAE,YAAY,CAAC;IACtB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf;;;;OAIG;IACH,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,gEAAgE;IAChE,IAAI,CAAC,EAAE,MAAM,CAAC;IACd;;;OAGG;IACH,EAAE,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,MAAM,CAAC;IACnD,6EAA6E;IAC7E,IAAI,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,MAAM,GAAG,SAAS,CAAC;IACjE,oDAAoD;IACpD,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB;;;;OAIG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,8EAA8E;IAC9E,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,0BAA0B,GAAG,mBAAmB,CA0KrF"}
package/dist/content.js CHANGED
@@ -44,29 +44,44 @@ export function sanitySource(options) {
44
44
  return writeClient;
45
45
  };
46
46
  // Entry ids are slugs by default, but Sanity mutations address `_id`.
47
- // Resolve live on every write: a cached mapping goes stale the moment an
48
- // editor reassigns a slug, and writes are rare enough that one lookup is
49
- // the right price for addressing the intended document. When `createType`
50
- // is configured the lookup is constrained to it, so a slug shared with an
51
- // unrelated document type cannot redirect the mutation.
47
+ //
48
+ // Resolve against the collection's own read query, so a write can only ever
49
+ // address a document this collection actually reads. Two earlier shapes were
50
+ // both wrong: an unconstrained `slug.current == $id` lookup could resolve a
51
+ // document of an unrelated type that happened to share the slug, and
52
+ // deleting it destroyed data the collection does not own; constraining to
53
+ // `createType` instead left every entry of any other type in a multi-type
54
+ // collection unresolvable, so ordinary updates started throwing.
55
+ //
56
+ // Resolved live on every write, because a cached mapping goes stale the
57
+ // moment an editor reassigns a slug. That costs one query per write, which
58
+ // is the same query the read path already runs, and writes are rare next to
59
+ // reads.
52
60
  const resolveDocumentId = async (id) => {
53
- const query = options.createType
54
- ? "*[slug.current == $id && _type == $type][0]._id"
55
- : "*[slug.current == $id][0]._id";
56
- const bySlug = await resolveWriteClient().fetch(query, {
57
- id,
58
- ...(options.createType ? { type: options.createType } : {}),
59
- });
60
- if (bySlug)
61
- return bySlug;
62
- // An id that is itself a document _id (custom `id` option) still works.
63
- const direct = await resolveWriteClient().fetch("*[_id == $id][0]._id", {
64
- id,
65
- });
66
- if (direct)
67
- return direct;
68
- throw new Error(`sanitySource(${JSON.stringify(name)}) could not resolve ${JSON.stringify(id)} to a ` +
69
- "document; the write was not sent. The entry may have been deleted or its slug changed.");
61
+ const documents = await resolveWriteClient().fetch(options.query, options.params ?? {});
62
+ if (!Array.isArray(documents)) {
63
+ throw new Error(`sanitySource(${JSON.stringify(name)}) query must resolve to an array of documents; ` +
64
+ "the write was not sent.");
65
+ }
66
+ const records = documents;
67
+ const match = records.find((record) => resolveId(record) === id) ??
68
+ // A custom `id` option can address documents by their own `_id`.
69
+ records.find((record) => record._id === id);
70
+ if (!match) {
71
+ throw new Error(`sanitySource(${JSON.stringify(name)}) could not resolve ${JSON.stringify(id)} to a ` +
72
+ "document this collection reads; the write was not sent. The entry may have been " +
73
+ "deleted, its slug may have changed, or it may belong to another collection.");
74
+ }
75
+ const documentId = match._id;
76
+ if (typeof documentId !== "string" || !documentId) {
77
+ // Reads only need an id the projection can derive, so a query that omits
78
+ // `_id` loads fine and then cannot be written back. Say which projection
79
+ // is short rather than claiming the entry does not exist.
80
+ throw new Error(`sanitySource(${JSON.stringify(name)}) resolved ${JSON.stringify(id)} to a document ` +
81
+ "without an `_id`; the write was not sent. Add `_id` to the collection query's " +
82
+ "projection so writes can address the document.");
83
+ }
84
+ return documentId;
70
85
  };
71
86
  const toDocument = (record) => {
72
87
  const id = resolveId(record) || record._id;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@farm.js/sanity",
3
- "version": "0.1.0-beta.107",
3
+ "version": "0.1.0-beta.108",
4
4
  "description": "Sanity CMS integration for Farm.js",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -27,8 +27,8 @@
27
27
  "dependencies": {
28
28
  "@sanity/image-url": "^2.1.1",
29
29
  "@sanity/webhook": "^4.0.4",
30
- "@farm.js/core": "0.1.0-beta.107",
31
- "@farm.js/integration-utils": "0.1.0-beta.107"
30
+ "@farm.js/core": "0.1.0-beta.108",
31
+ "@farm.js/integration-utils": "0.1.0-beta.108"
32
32
  },
33
33
  "devDependencies": {
34
34
  "@sanity/client": "^8.4.0",