joist-core 2.5.0-next.21 → 2.5.0-next.23

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.
Files changed (205) hide show
  1. package/build/BaseEntity.cjs +5 -2
  2. package/build/BaseEntity.cjs.map +1 -1
  3. package/build/BaseEntity.d.cts +2 -0
  4. package/build/BaseEntity.d.cts.map +1 -1
  5. package/build/BaseEntity.d.mts +2 -0
  6. package/build/BaseEntity.d.mts.map +1 -1
  7. package/build/BaseEntity.js +5 -2
  8. package/build/BaseEntity.js.map +1 -1
  9. package/build/Entity.cjs.map +1 -1
  10. package/build/Entity.d.cts +6 -0
  11. package/build/Entity.d.cts.map +1 -1
  12. package/build/Entity.d.mts +6 -0
  13. package/build/Entity.d.mts.map +1 -1
  14. package/build/Entity.js.map +1 -1
  15. package/build/EntityManager.cjs +83 -22
  16. package/build/EntityManager.cjs.map +1 -1
  17. package/build/EntityManager.d.cts +7 -4
  18. package/build/EntityManager.d.cts.map +1 -1
  19. package/build/EntityManager.d.mts +7 -4
  20. package/build/EntityManager.d.mts.map +1 -1
  21. package/build/EntityManager.js +83 -22
  22. package/build/EntityManager.js.map +1 -1
  23. package/build/InstanceData.cjs +2 -2
  24. package/build/InstanceData.cjs.map +1 -1
  25. package/build/InstanceData.d.cts +2 -2
  26. package/build/InstanceData.d.mts +2 -2
  27. package/build/InstanceData.js +2 -2
  28. package/build/InstanceData.js.map +1 -1
  29. package/build/PluginManager.cjs +6 -0
  30. package/build/PluginManager.cjs.map +1 -1
  31. package/build/PluginManager.d.cts +10 -0
  32. package/build/PluginManager.d.cts.map +1 -1
  33. package/build/PluginManager.d.mts +10 -0
  34. package/build/PluginManager.d.mts.map +1 -1
  35. package/build/PluginManager.js +6 -0
  36. package/build/PluginManager.js.map +1 -1
  37. package/build/clientIds.cjs +47 -0
  38. package/build/clientIds.cjs.map +1 -0
  39. package/build/clientIds.d.cts +26 -0
  40. package/build/clientIds.d.cts.map +1 -0
  41. package/build/clientIds.d.mts +26 -0
  42. package/build/clientIds.d.mts.map +1 -0
  43. package/build/clientIds.js +42 -0
  44. package/build/clientIds.js.map +1 -0
  45. package/build/configure.cjs +3 -0
  46. package/build/configure.cjs.map +1 -1
  47. package/build/configure.d.cts.map +1 -1
  48. package/build/configure.d.mts.map +1 -1
  49. package/build/configure.js +3 -0
  50. package/build/configure.js.map +1 -1
  51. package/build/getProperties.cjs +4 -1
  52. package/build/getProperties.cjs.map +1 -1
  53. package/build/getProperties.js +4 -1
  54. package/build/getProperties.js.map +1 -1
  55. package/build/index.cjs +15 -4
  56. package/build/index.cjs.map +1 -1
  57. package/build/index.d.cts +9 -7
  58. package/build/index.d.cts.map +1 -1
  59. package/build/index.d.mts +9 -7
  60. package/build/index.d.mts.map +1 -1
  61. package/build/index.js +11 -5
  62. package/build/index.js.map +1 -1
  63. package/build/keys.cjs +4 -0
  64. package/build/keys.cjs.map +1 -1
  65. package/build/keys.d.cts.map +1 -1
  66. package/build/keys.d.mts.map +1 -1
  67. package/build/keys.js +4 -0
  68. package/build/keys.js.map +1 -1
  69. package/build/newTestInstance.cjs +8 -8
  70. package/build/newTestInstance.cjs.map +1 -1
  71. package/build/newTestInstance.d.cts.map +1 -1
  72. package/build/newTestInstance.d.mts.map +1 -1
  73. package/build/newTestInstance.js +8 -8
  74. package/build/newTestInstance.js.map +1 -1
  75. package/build/queries/ConditionBuilder.cjs +2 -1
  76. package/build/queries/ConditionBuilder.cjs.map +1 -1
  77. package/build/queries/ConditionBuilder.d.cts +9 -1
  78. package/build/queries/ConditionBuilder.d.cts.map +1 -1
  79. package/build/queries/ConditionBuilder.d.mts +9 -1
  80. package/build/queries/ConditionBuilder.d.mts.map +1 -1
  81. package/build/queries/ConditionBuilder.js +2 -2
  82. package/build/queries/ConditionBuilder.js.map +1 -1
  83. package/build/queries/conditions.cjs.map +1 -1
  84. package/build/queries/conditions.d.cts +4 -1
  85. package/build/queries/conditions.d.cts.map +1 -1
  86. package/build/queries/conditions.d.mts +4 -1
  87. package/build/queries/conditions.d.mts.map +1 -1
  88. package/build/queries/conditions.js.map +1 -1
  89. package/build/queries/sql/Expr.cjs +79 -134
  90. package/build/queries/sql/Expr.cjs.map +1 -1
  91. package/build/queries/sql/Expr.d.cts +26 -34
  92. package/build/queries/sql/Expr.d.cts.map +1 -1
  93. package/build/queries/sql/Expr.d.mts +26 -34
  94. package/build/queries/sql/Expr.d.mts.map +1 -1
  95. package/build/queries/sql/Expr.js +80 -133
  96. package/build/queries/sql/Expr.js.map +1 -1
  97. package/build/queries/sql/ParsedQuery.cjs +119 -0
  98. package/build/queries/sql/ParsedQuery.cjs.map +1 -0
  99. package/build/queries/sql/ParsedQuery.d.cts +105 -0
  100. package/build/queries/sql/ParsedQuery.d.cts.map +1 -0
  101. package/build/queries/sql/ParsedQuery.d.mts +105 -0
  102. package/build/queries/sql/ParsedQuery.d.mts.map +1 -0
  103. package/build/queries/sql/ParsedQuery.js +116 -0
  104. package/build/queries/sql/ParsedQuery.js.map +1 -0
  105. package/build/queries/sql/ParsedSqlExpression.cjs +74 -0
  106. package/build/queries/sql/ParsedSqlExpression.cjs.map +1 -0
  107. package/build/queries/sql/ParsedSqlExpression.d.cts +65 -0
  108. package/build/queries/sql/ParsedSqlExpression.d.cts.map +1 -0
  109. package/build/queries/sql/ParsedSqlExpression.d.mts +65 -0
  110. package/build/queries/sql/ParsedSqlExpression.d.mts.map +1 -0
  111. package/build/queries/sql/ParsedSqlExpression.js +69 -0
  112. package/build/queries/sql/ParsedSqlExpression.js.map +1 -0
  113. package/build/queries/sql/Tables.cjs +61 -46
  114. package/build/queries/sql/Tables.cjs.map +1 -1
  115. package/build/queries/sql/Tables.d.cts.map +1 -1
  116. package/build/queries/sql/Tables.d.mts.map +1 -1
  117. package/build/queries/sql/Tables.js +61 -46
  118. package/build/queries/sql/Tables.js.map +1 -1
  119. package/build/queries/sql/execute.cjs +45 -62
  120. package/build/queries/sql/execute.cjs.map +1 -1
  121. package/build/queries/sql/execute.d.cts +4 -3
  122. package/build/queries/sql/execute.d.cts.map +1 -1
  123. package/build/queries/sql/execute.d.mts +4 -3
  124. package/build/queries/sql/execute.d.mts.map +1 -1
  125. package/build/queries/sql/execute.js +47 -64
  126. package/build/queries/sql/execute.js.map +1 -1
  127. package/build/queries/sql/expressions/arrayAgg.cjs +14 -15
  128. package/build/queries/sql/expressions/arrayAgg.cjs.map +1 -1
  129. package/build/queries/sql/expressions/arrayAgg.d.cts +3 -2
  130. package/build/queries/sql/expressions/arrayAgg.d.cts.map +1 -1
  131. package/build/queries/sql/expressions/arrayAgg.d.mts +3 -2
  132. package/build/queries/sql/expressions/arrayAgg.d.mts.map +1 -1
  133. package/build/queries/sql/expressions/arrayAgg.js +15 -16
  134. package/build/queries/sql/expressions/arrayAgg.js.map +1 -1
  135. package/build/queries/sql/expressions/case.cjs +11 -16
  136. package/build/queries/sql/expressions/case.cjs.map +1 -1
  137. package/build/queries/sql/expressions/case.d.cts +3 -2
  138. package/build/queries/sql/expressions/case.d.cts.map +1 -1
  139. package/build/queries/sql/expressions/case.d.mts +3 -2
  140. package/build/queries/sql/expressions/case.d.mts.map +1 -1
  141. package/build/queries/sql/expressions/case.js +11 -16
  142. package/build/queries/sql/expressions/case.js.map +1 -1
  143. package/build/queries/sql/expressions/coalesce.cjs.map +1 -1
  144. package/build/queries/sql/expressions/coalesce.d.cts +3 -2
  145. package/build/queries/sql/expressions/coalesce.d.cts.map +1 -1
  146. package/build/queries/sql/expressions/coalesce.d.mts +3 -2
  147. package/build/queries/sql/expressions/coalesce.d.mts.map +1 -1
  148. package/build/queries/sql/expressions/coalesce.js.map +1 -1
  149. package/build/queries/sql/expressions/expression.cjs +10 -12
  150. package/build/queries/sql/expressions/expression.cjs.map +1 -1
  151. package/build/queries/sql/expressions/expression.d.cts +4 -3
  152. package/build/queries/sql/expressions/expression.d.cts.map +1 -1
  153. package/build/queries/sql/expressions/expression.d.mts +4 -3
  154. package/build/queries/sql/expressions/expression.d.mts.map +1 -1
  155. package/build/queries/sql/expressions/expression.js +11 -13
  156. package/build/queries/sql/expressions/expression.js.map +1 -1
  157. package/build/queries/sql/expressions/greatest.cjs.map +1 -1
  158. package/build/queries/sql/expressions/greatest.d.cts +3 -2
  159. package/build/queries/sql/expressions/greatest.d.cts.map +1 -1
  160. package/build/queries/sql/expressions/greatest.d.mts +3 -2
  161. package/build/queries/sql/expressions/greatest.d.mts.map +1 -1
  162. package/build/queries/sql/expressions/greatest.js.map +1 -1
  163. package/build/queries/sql/expressions/least.cjs.map +1 -1
  164. package/build/queries/sql/expressions/least.d.cts +3 -2
  165. package/build/queries/sql/expressions/least.d.cts.map +1 -1
  166. package/build/queries/sql/expressions/least.d.mts +3 -2
  167. package/build/queries/sql/expressions/least.d.mts.map +1 -1
  168. package/build/queries/sql/expressions/least.js.map +1 -1
  169. package/build/queries/sql/expressions/nullIf.cjs.map +1 -1
  170. package/build/queries/sql/expressions/nullIf.d.cts +3 -2
  171. package/build/queries/sql/expressions/nullIf.d.cts.map +1 -1
  172. package/build/queries/sql/expressions/nullIf.d.mts +3 -2
  173. package/build/queries/sql/expressions/nullIf.d.mts.map +1 -1
  174. package/build/queries/sql/expressions/nullIf.js.map +1 -1
  175. package/build/queries/sql/query.cjs +168 -381
  176. package/build/queries/sql/query.cjs.map +1 -1
  177. package/build/queries/sql/query.d.cts +44 -45
  178. package/build/queries/sql/query.d.cts.map +1 -1
  179. package/build/queries/sql/query.d.mts +44 -45
  180. package/build/queries/sql/query.d.mts.map +1 -1
  181. package/build/queries/sql/query.js +172 -383
  182. package/build/queries/sql/query.js.map +1 -1
  183. package/build/queries/sql/renderSql.cjs +321 -0
  184. package/build/queries/sql/renderSql.cjs.map +1 -0
  185. package/build/queries/sql/renderSql.d.cts +14 -0
  186. package/build/queries/sql/renderSql.d.cts.map +1 -0
  187. package/build/queries/sql/renderSql.d.mts +14 -0
  188. package/build/queries/sql/renderSql.d.mts.map +1 -0
  189. package/build/queries/sql/renderSql.js +318 -0
  190. package/build/queries/sql/renderSql.js.map +1 -0
  191. package/build/resurrection.cjs +5 -3
  192. package/build/resurrection.cjs.map +1 -1
  193. package/build/resurrection.d.cts.map +1 -1
  194. package/build/resurrection.d.mts.map +1 -1
  195. package/build/resurrection.js +5 -3
  196. package/build/resurrection.js.map +1 -1
  197. package/build/upsert.cjs +6 -3
  198. package/build/upsert.cjs.map +1 -1
  199. package/build/upsert.d.cts +6 -3
  200. package/build/upsert.d.cts.map +1 -1
  201. package/build/upsert.d.mts +6 -3
  202. package/build/upsert.d.mts.map +1 -1
  203. package/build/upsert.js +6 -3
  204. package/build/upsert.js.map +1 -1
  205. package/package.json +2 -2
@@ -1 +1 @@
1
- {"version":3,"file":"upsert.cjs","names":["getMetadata","getProperties","isEntity","isKey","getConstructorFromTaggedId","isPlainObject","toArray","findExistingIfUniqueBy","asConcreteCstr"],"sources":["../src/upsert.ts"],"sourcesContent":["import { isPlainObject } from \"joist-utils\";\nimport { setSyncDefaults } from \"src/defaults.ts\";\nimport { type Entity, isEntity } from \"src/Entity.ts\";\nimport { type EntityManager, type IdOf, type MaybeAbstractEntityConstructor, isKey } from \"src/EntityManager.ts\";\nimport {\n type ManyToManyField,\n type ManyToOneField,\n type OneToManyField,\n type OneToOneField,\n getMetadata,\n} from \"src/EntityMetadata.ts\";\nimport {\n type PartialOrNull,\n type TimestampSerde,\n asConcreteCstr,\n getConstructorFromTaggedId,\n getProperties,\n setOpt,\n} from \"src/index.ts\";\nimport { findExistingIfUniqueBy } from \"src/resurrection.ts\";\nimport { type OptIdsOf, type OptsOf } from \"src/typeMap.ts\";\nimport { type NullOrDefinedOr, toArray } from \"src/utils.ts\";\n\n/**\n * The type for `EntityManager.createOrUpdateUnsafe` that allows \"upsert\"-ish behavior.\n *\n * I.e. `T` is an entity with an optional id (create if unset, update if set), and we recurse\n * into any relations (references or collections) to allow those relations themselves to be\n * any combination of 1) ids to existing entities, 2) entities directly, 3) null/undefined\n * with the appropriate partial-update behavior, or 4) partials.\n */\nexport type DeepPartialOrNull<T extends Entity> = { id?: IdOf<T> | null } & AllowRelationsToBeIdsOrEntitiesOrPartials<\n PartialOrNull<OptsOf<T> & OptIdsOf<T>>\n> &\n OptIdsOf<T>;\n\n/** Flags that allow `upsert` to delete or remove entities. */\ntype CollectionFlags = {\n delete?: boolean | null;\n remove?: boolean | null;\n op?: \"remove\" | \"delete\" | \"include\" | \"incremental\";\n};\n\n/** Flag that allows `upsert` to delete. */\ntype DeleteFlag = {\n delete?: boolean | null;\n};\n\ntype AllowRelationsToBeIdsOrEntitiesOrPartials<T> = {\n [P in keyof T]: T[P] extends NullOrDefinedOr<infer U>\n ? U extends Array<infer V>\n ? V extends Entity\n ? Array<V | (DeepPartialOrNull<V> & CollectionFlags) | IdOf<V>> | null\n : T[P]\n : U extends Entity\n ? U | (DeepPartialOrNull<U> & DeleteFlag) | IdOf<U> | null\n : T[P]\n : T[P];\n};\n\n/**\n * A utility function to do partial-update style updates.\n *\n * The difference between this and `Entity.setPartial` is that we accept `DeepPartialOrNull`\n * updates, similar to `createOrUpdateUnsafe`, instead of just a flat/shallow list of opts.\n */\nexport async function updatePartial<T extends Entity>(entity: T, input: DeepPartialOrNull<T>): Promise<void> {\n if (Object.keys(input).length === 0) return;\n const meta = getMetadata(entity);\n const { em } = entity;\n // Do a Promise.all so we .load() relations in parallel\n await Promise.all(\n Object.entries(input).map(async ([key, value]) => {\n // setPartial ==> `undefined` is a noop\n if (value === undefined) return;\n\n // Watch for the `bookId` / `bookIds` aliases\n const field = meta.allFields[key] || Object.values(meta.allFields).find((f) => f.fieldIdName === key);\n\n if (!field) {\n // Allow (ignore here) delete/remove flags that we assume the API layer (i.e. GraphQL) will have specifically\n // allowed, i.e. this isn't the Rails form bug where users can POST in any random field they want.\n const flagField = key === \"delete\" || key === \"remove\" || key === \"op\";\n if (flagField) return;\n // Look for non-field properties like a fullName setter\n const prop = getProperties(meta)[key];\n if (prop) {\n setOpt(meta, entity, key, value);\n return;\n } else {\n throw new Error(`Unknown field ${key}`);\n }\n }\n\n // Don't use key b/c it might be the bookId alias\n const name = field.fieldName;\n\n if (field.kind === \"poly\" && value && !isEntity(value)) {\n // Resolve non-entity poly values\n if (isKey(value)) {\n setOpt(meta, entity, name, await em.load(getConstructorFromTaggedId(value), value));\n } else {\n throw new Error(`Cannot use partial value for polymorphic field `);\n }\n } else if (field.kind === \"m2o\" && value && !isEntity(value)) {\n // This is a many-to-one reference\n if (isKey(value)) {\n setOpt(meta, entity, name, await em.load(field.otherMetadata().cstr, value));\n } else if (typeof value === \"object\" && value) {\n let other: any;\n if (\"id\" in value) {\n // This is a many-to-one partial update to an existing entity (they passed an id)\n other = await upsert(em, field.otherMetadata().cstr, value as any);\n } else {\n // This is a many-to-one partial w/o passing an id, i.e. `upsert(Author, { id: \"a:1\", publisher: { name: \"p1\" } })`\n if (entity.isNewEntity) {\n // If we're brand new, our child/other has to be brand new as well\n other = await upsert(em, field.otherMetadata().cstr, value);\n } else {\n // the o2o upsert hash didn't have an id, so find-or-create it\n other = await (entity as any)[name].load();\n if (other) {\n await updatePartial(other, value);\n } else {\n other = await upsert(em, field.otherMetadata().cstr, value);\n }\n }\n }\n const deleteMarker = getDeleteMarker(field, value);\n if (deleteMarker) {\n em.delete(other);\n other = undefined;\n }\n setOpt(meta, entity, name, other);\n }\n } else if (field.kind === \"o2o\") {\n // ^-- o2o always need loaded to be set, so don't fall through\n if (value === null || isEntity(value)) {\n await (entity as any)[name].load();\n setOpt(meta, entity, name, value);\n } else if (isKey(value)) {\n const [, other] = await Promise.all([\n (entity as any)[name].load(),\n em.load(field.otherMetadata().cstr, value),\n ]);\n setOpt(meta, entity, name, other);\n } else if (typeof value === \"object\" && isPlainObject(value)) {\n const deleteMarker = getDeleteMarker(field, value);\n // Always load the o2o so we can set it\n const current = await (entity as any)[name].load();\n let other: any;\n if (\"id\" in value) {\n // The o2o upsert hash was passed an id, so load it\n other = await upsert(em, field.otherMetadata().cstr, value as any);\n } else {\n // the o2o upsert hash didn't have an id, so find-or-create it\n other = current;\n if (other) {\n await updatePartial(other, value);\n } else {\n other = await upsert(em, field.otherMetadata().cstr, value);\n }\n }\n if (deleteMarker) {\n em.delete(other);\n } else {\n setOpt(meta, entity, name, other);\n }\n } else {\n throw new Error(`Invalid value ${value}`);\n }\n } else if (field.kind === \"o2m\" || field.kind === \"m2m\") {\n // Look for one-to-many/many-to-many partials\n // `null` is handled later, and treated as `[]`, which needs the collection loaded\n\n const values = toArray(value);\n const otherMeta = field.otherMetadata();\n const maybeSoftDelete = otherMeta.timestampFields?.deletedAt;\n\n // Incremental handling. Detect incremental mode by a meaningful `op` value--`op: undefined`/`op: null`\n // is the natural shape of an optional field and must behave like \"no op\", consistent with how\n // updatePartial treats `value === undefined` and how delete/remove are read by truthiness.\n const anyValueHasOp = values.some((v: any) => v && typeof v === \"object\" && !isEntity(v) && v.op != null);\n if (anyValueHasOp) {\n let anyValueMissingOp = false;\n const current = (entity as any)[name];\n // The `op` behavior means incremental and wants to do this:\n await Promise.all(\n values.map(async (value: any) => {\n const [, , op] = getCollectionMarkers(field, value);\n if (op === \"delete\") {\n const other = await upsert(em, otherMeta.cstr, withOwnerField(field, value, entity));\n // We need to check if this is a soft-deletable entity, and if so, we will soft-delete it.\n if (maybeSoftDelete) {\n const serde = otherMeta.fields[maybeSoftDelete].serde as TimestampSerde<unknown>;\n const now = new Date();\n other.set({ [maybeSoftDelete]: serde.mapFromNow(now) });\n } else {\n entity.em.delete(other);\n }\n } else if (op === \"remove\") {\n const other = await upsert(em, otherMeta.cstr, withOwnerField(field, value, entity));\n current.remove(other);\n } else if (op === \"include\") {\n const other = await upsert(em, otherMeta.cstr, withOwnerField(field, value, entity));\n current.add(other);\n } else if (op === \"incremental\") {\n // This is a marker entry, just ignore it\n } else {\n // A child entered incremental mode (a sibling had a real op) but has no op of its own;\n // never silently skip it--flag it so the guard below throws instead of dropping the row.\n anyValueMissingOp = true;\n }\n }),\n );\n if (anyValueMissingOp) {\n throw new Error(\"If any child sets the `op` key, then all children must have the `op` key.\");\n }\n return; // return from the op-based incremental behavior\n }\n\n // Otherwise we do exhaustive sets, will still looking for `delete`/`remove` markers\n const others = await Promise.all(\n values.map(async (value: any) => {\n if (isEntity(value)) {\n return value;\n } else if (isKey(value)) {\n return em.load(field.otherMetadata().cstr, value);\n } else if (value === null || value === undefined) {\n return undefined;\n } else if (!isPlainObject(value)) {\n throw new Error(`Invalid value ${value}`);\n } else {\n // Look for `delete: true/false` and `remove: true/false` markers\n const [deleteMarker, removeMarker] = getCollectionMarkers(field, value);\n const other = await upsert(em, field.otherMetadata().cstr, withOwnerField(field, value, entity));\n if (deleteMarker) {\n // if (maybeSoftDelete) {\n // const serde = meta.fields[maybeSoftDelete].serde as TimestampSerde<unknown>;\n // const now = new Date();\n // other.set({ [maybeSoftDelete]: serde.mapFromNow(now) });\n // } else {\n // }\n em.delete(other);\n return other;\n } else if (removeMarker) {\n return undefined;\n } else {\n return other;\n }\n }\n }),\n );\n\n await (entity as any)[name].load();\n setOpt(\n meta,\n entity,\n name,\n others.filter((v) => v !== undefined),\n );\n } else {\n // If we get here, value should be a vanilla value that `setOpt` can handle/pass through as-is\n setOpt(meta, entity, name, value);\n }\n }),\n );\n}\n\n/**\n * A utility function to create-or-update/upsert entities coming from a partial-update style API.\n */\nexport async function upsert<T extends Entity>(\n em: EntityManager<any, any, any>,\n constructor: MaybeAbstractEntityConstructor<T>,\n input: DeepPartialOrNull<T>,\n): Promise<T> {\n const { id, ...rest } = input;\n const entity =\n id !== null && id !== undefined\n ? await em.load(constructor, id)\n : // Look for an existing entity we could resurrect\n ((id === undefined ? await findExistingIfUniqueBy(em, constructor, rest) : undefined) ??\n em.createPartial(asConcreteCstr(constructor), {}));\n await updatePartial(entity, rest as any);\n // Do this manually since we're not going through setOpts anymore\n if (entity.isNewEntity) {\n setSyncDefaults(entity);\n }\n return entity;\n}\n\n/**\n * Adds the o2m owner field so child resurrection can find rows whose `uniqueBy` includes their parent.\n * I.e. upserting `Author.books: [{ title: \"b1\" }]` should lookup `Book.uniqueBy = [[\"author\", \"title\"]]`.\n */\nfunction withOwnerField(field: OneToManyField | ManyToManyField, value: any, owner: Entity): any {\n if (field.kind !== \"o2m\" || !isPlainObject(value)) return value;\n const ownerField = field.otherFieldName;\n const ownerFieldId = field.otherMetadata().allFields[ownerField]?.fieldIdName;\n if (Object.hasOwn(value, ownerField) || (ownerFieldId && Object.hasOwn(value, ownerFieldId))) return value;\n if (ownerFieldId && !owner.isNewEntity) return { ...value, [ownerFieldId]: owner.id };\n return { ...value, [ownerField]: owner };\n}\n\nfunction getCollectionMarkers(field: OneToManyField | ManyToManyField, value: any): [any, any, any] {\n const allowDelete = !field.otherMetadata().fields[\"delete\"];\n const allowRemove = !field.otherMetadata().fields[\"remove\"];\n const allowOp = !field.otherMetadata().fields[\"op\"];\n\n // Look for `delete: true/false` and `remove: true/false` markers\n const deleteMarker: any = allowDelete && value[\"delete\"];\n const removeMarker: any = allowRemove && value[\"remove\"];\n const opMarker: any = allowOp && value[\"op\"];\n\n // Remove the markers, regardless of true/false, before recursing into upsert to avoid unknown fields\n if (deleteMarker !== undefined) delete value.delete;\n if (removeMarker !== undefined) delete value.remove;\n if (opMarker !== undefined) delete value.op;\n\n return [deleteMarker, removeMarker, opMarker];\n}\nfunction getDeleteMarker(field: OneToOneField | ManyToOneField, value: any): boolean {\n const allowDelete = !field.otherMetadata().fields[\"delete\"];\n const deleteMarker: any = allowDelete && value[\"delete\"];\n if (deleteMarker !== undefined) delete value.delete;\n return deleteMarker;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAkEA,eAAsB,cAAgC,QAAW,OAA4C;CAC3G,IAAI,OAAO,KAAK,KAAK,CAAC,CAAC,WAAW,GAAG;CACrC,MAAM,OAAOA,uBAAAA,YAAY,MAAM;CAC/B,MAAM,EAAE,OAAO;CAEf,MAAM,QAAQ,IACZ,OAAO,QAAQ,KAAK,CAAC,CAAC,IAAI,OAAO,CAAC,KAAK,WAAW;EAEhD,IAAI,UAAU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AAQ,aAAa;CAAM;AACzC;AAEA,SAAS,qBAAqB,OAAyC,OAA6B;CAClG,MAAM,cAAc,CAAC,MAAM,cAAc,CAAC,CAAC,OAAO;CAClD,MAAM,cAAc,CAAC,MAAM,cAAc,CAAC,CAAC,OAAO;CAClD,MAAM,UAAU,CAAC,MAAM,cAAc,CAAC,CAAC,OAAO;CAG9C,MAAM,eAAoB,eAAe,MAAM;CAC/C,MAAM,eAAoB,eAAe,MAAM;CAC/C,MAAM,WAAgB,WAAW,MAAM;CAGvC,IAAI,iBAAiB,KAAA,GAAW,OAAO,MAAM;CAC7C,IAAI,iBAAiB,KAAA,GAAW,OAAO,MAAM;CAC7C,IAAI,aAAa,KAAA,GAAW,OAAO,MAAM;CAEzC,OAAO;EAAC;EAAc;EAAc;CAAQ;AAC9C;AACA,SAAS,gBAAgB,OAAuC,OAAqB;CAEnF,MAAM,eAAoB,CADL,MAAM,cAAc,CAAC,CAAC,OAAO,aACT,MAAM;CAC/C,IAAI,iBAAiB,KAAA,GAAW,OAAO,MAAM;CAC7C,OAAO;AACT"}
1
+ {"version":3,"file":"upsert.cjs","names":["getMetadata","getProperties","isEntity","isKey","getConstructorFromTaggedId","isPlainObject","toArray","findExistingIfUniqueBy","asConcreteCstr"],"sources":["../src/upsert.ts"],"sourcesContent":["import { isPlainObject } from \"joist-utils\";\nimport { setSyncDefaults } from \"src/defaults.ts\";\nimport { type Entity, isEntity } from \"src/Entity.ts\";\nimport { type EntityManager, type IdOf, type MaybeAbstractEntityConstructor, isKey } from \"src/EntityManager.ts\";\nimport {\n type ManyToManyField,\n type ManyToOneField,\n type OneToManyField,\n type OneToOneField,\n getMetadata,\n} from \"src/EntityMetadata.ts\";\nimport {\n type PartialOrNull,\n type TimestampSerde,\n asConcreteCstr,\n getConstructorFromTaggedId,\n getProperties,\n setOpt,\n} from \"src/index.ts\";\nimport { findExistingIfUniqueBy } from \"src/resurrection.ts\";\nimport { type OptIdsOf, type OptsOf } from \"src/typeMap.ts\";\nimport { type NullOrDefinedOr, toArray } from \"src/utils.ts\";\n\n/**\n * Deep partial input for `EntityManager.upsert` and `Entity.setDeepPartial`.\n *\n * I.e. `T` is an entity with an optional id (create if unset, update if set), and we recurse\n * into any relations (references or collections) to allow those relations themselves to be\n * any combination of 1) ids to existing entities, 2) entities directly, 3) null/undefined\n * with the appropriate partial-update behavior, or 4) partials.\n */\nexport type DeepPartialOrNull<T extends Entity> = { id?: IdOf<T> | null } & AllowRelationsToBeIdsOrEntitiesOrPartials<\n PartialOrNull<OptsOf<T> & OptIdsOf<T>>\n> &\n OptIdsOf<T>;\n\n/** Flags that allow `upsert` to delete or remove entities. */\ntype CollectionFlags = {\n delete?: boolean | null;\n remove?: boolean | null;\n op?: \"remove\" | \"delete\" | \"include\" | \"incremental\";\n};\n\n/** Flag that allows `upsert` to delete. */\ntype DeleteFlag = {\n delete?: boolean | null;\n};\n\ntype AllowRelationsToBeIdsOrEntitiesOrPartials<T> = {\n [P in keyof T]: T[P] extends NullOrDefinedOr<infer U>\n ? U extends Array<infer V>\n ? V extends Entity\n ? Array<V | (DeepPartialOrNull<V> & CollectionFlags) | IdOf<V>> | null\n : T[P]\n : U extends Entity\n ? U | (DeepPartialOrNull<U> & DeleteFlag) | IdOf<U> | null\n : T[P]\n : T[P];\n};\n\n/**\n * A utility function to do partial-update style updates.\n *\n * The difference between this and `Entity.setPartial` is that we accept `DeepPartialOrNull`\n * updates, similar to `upsert`, instead of just a flat/shallow list of opts.\n */\nexport async function updatePartial<T extends Entity>(entity: T, input: DeepPartialOrNull<T>): Promise<void> {\n if (Object.keys(input).length === 0) return;\n const meta = getMetadata(entity);\n const { em } = entity;\n // Do a Promise.all so we .load() relations in parallel\n await Promise.all(\n Object.entries(input).map(async ([key, value]) => {\n // setPartial ==> `undefined` is a noop\n if (value === undefined) return;\n\n // Watch for the `bookId` / `bookIds` aliases\n const field = meta.allFields[key] || Object.values(meta.allFields).find((f) => f.fieldIdName === key);\n\n if (!field) {\n // Allow (ignore here) delete/remove flags that we assume the API layer (i.e. GraphQL) will have specifically\n // allowed, i.e. this isn't the Rails form bug where users can POST in any random field they want.\n const flagField = key === \"delete\" || key === \"remove\" || key === \"op\";\n if (flagField) return;\n // Look for non-field properties like a fullName setter\n const prop = getProperties(meta)[key];\n // Let setOpt explain why the creation-only $clientId option cannot be assigned here.\n if (prop || key === \"$clientId\") {\n setOpt(meta, entity, key, value);\n return;\n } else {\n throw new Error(`Unknown field ${key}`);\n }\n }\n\n // Don't use key b/c it might be the bookId alias\n const name = field.fieldName;\n\n if (field.kind === \"poly\" && value && !isEntity(value)) {\n // Resolve non-entity poly values\n if (isKey(value)) {\n setOpt(meta, entity, name, await em.load(getConstructorFromTaggedId(value), value));\n } else {\n throw new Error(`Cannot use partial value for polymorphic field `);\n }\n } else if (field.kind === \"m2o\" && value && !isEntity(value)) {\n // This is a many-to-one reference\n if (isKey(value)) {\n setOpt(meta, entity, name, await em.load(field.otherMetadata().cstr, value));\n } else if (typeof value === \"object\" && value) {\n let other: any;\n if (\"id\" in value) {\n // This is a many-to-one partial update to an existing entity (they passed an id)\n other = await upsert(em, field.otherMetadata().cstr, value as any);\n } else {\n // This is a many-to-one partial w/o passing an id, i.e. `upsert(Author, { id: \"a:1\", publisher: { name: \"p1\" } })`\n if (entity.isNewEntity) {\n // If we're brand new, our child/other has to be brand new as well\n other = await upsert(em, field.otherMetadata().cstr, value);\n } else {\n // the o2o upsert hash didn't have an id, so find-or-create it\n other = await (entity as any)[name].load();\n if (other) {\n await updatePartial(other, value);\n } else {\n other = await upsert(em, field.otherMetadata().cstr, value);\n }\n }\n }\n const deleteMarker = getDeleteMarker(field, value);\n if (deleteMarker) {\n em.delete(other);\n other = undefined;\n }\n setOpt(meta, entity, name, other);\n }\n } else if (field.kind === \"o2o\") {\n // ^-- o2o always need loaded to be set, so don't fall through\n if (value === null || isEntity(value)) {\n await (entity as any)[name].load();\n setOpt(meta, entity, name, value);\n } else if (isKey(value)) {\n const [, other] = await Promise.all([\n (entity as any)[name].load(),\n em.load(field.otherMetadata().cstr, value),\n ]);\n setOpt(meta, entity, name, other);\n } else if (typeof value === \"object\" && isPlainObject(value)) {\n const deleteMarker = getDeleteMarker(field, value);\n // Always load the o2o so we can set it\n const current = await (entity as any)[name].load();\n let other: any;\n if (\"id\" in value) {\n // The o2o upsert hash was passed an id, so load it\n other = await upsert(em, field.otherMetadata().cstr, value as any);\n } else {\n // the o2o upsert hash didn't have an id, so find-or-create it\n other = current;\n if (other) {\n await updatePartial(other, value);\n } else {\n other = await upsert(em, field.otherMetadata().cstr, value);\n }\n }\n if (deleteMarker) {\n em.delete(other);\n } else {\n setOpt(meta, entity, name, other);\n }\n } else {\n throw new Error(`Invalid value ${value}`);\n }\n } else if (field.kind === \"o2m\" || field.kind === \"m2m\") {\n // Look for one-to-many/many-to-many partials\n // `null` is handled later, and treated as `[]`, which needs the collection loaded\n\n const values = toArray(value);\n const otherMeta = field.otherMetadata();\n const maybeSoftDelete = otherMeta.timestampFields?.deletedAt;\n\n // Incremental handling. Detect incremental mode by a meaningful `op` value--`op: undefined`/`op: null`\n // is the natural shape of an optional field and must behave like \"no op\", consistent with how\n // updatePartial treats `value === undefined` and how delete/remove are read by truthiness.\n const anyValueHasOp = values.some((v: any) => v && typeof v === \"object\" && !isEntity(v) && v.op != null);\n if (anyValueHasOp) {\n let anyValueMissingOp = false;\n const current = (entity as any)[name];\n // The `op` behavior means incremental and wants to do this:\n await Promise.all(\n values.map(async (value: any) => {\n const [, , op] = getCollectionMarkers(field, value);\n if (op === \"delete\") {\n const other = await upsert(em, otherMeta.cstr, withOwnerField(field, value, entity));\n // We need to check if this is a soft-deletable entity, and if so, we will soft-delete it.\n if (maybeSoftDelete) {\n const serde = otherMeta.fields[maybeSoftDelete].serde as TimestampSerde<unknown>;\n const now = new Date();\n other.set({ [maybeSoftDelete]: serde.mapFromNow(now) });\n } else {\n entity.em.delete(other);\n }\n } else if (op === \"remove\") {\n const other = await upsert(em, otherMeta.cstr, withOwnerField(field, value, entity));\n current.remove(other);\n } else if (op === \"include\") {\n const other = await upsert(em, otherMeta.cstr, withOwnerField(field, value, entity));\n current.add(other);\n } else if (op === \"incremental\") {\n // This is a marker entry, just ignore it\n } else {\n // A child entered incremental mode (a sibling had a real op) but has no op of its own;\n // never silently skip it--flag it so the guard below throws instead of dropping the row.\n anyValueMissingOp = true;\n }\n }),\n );\n if (anyValueMissingOp) {\n throw new Error(\"If any child sets the `op` key, then all children must have the `op` key.\");\n }\n return; // return from the op-based incremental behavior\n }\n\n // Otherwise we do exhaustive sets, will still looking for `delete`/`remove` markers\n const others = await Promise.all(\n values.map(async (value: any) => {\n if (isEntity(value)) {\n return value;\n } else if (isKey(value)) {\n return em.load(field.otherMetadata().cstr, value);\n } else if (value === null || value === undefined) {\n return undefined;\n } else if (!isPlainObject(value)) {\n throw new Error(`Invalid value ${value}`);\n } else {\n // Look for `delete: true/false` and `remove: true/false` markers\n const [deleteMarker, removeMarker] = getCollectionMarkers(field, value);\n const other = await upsert(em, field.otherMetadata().cstr, withOwnerField(field, value, entity));\n if (deleteMarker) {\n // if (maybeSoftDelete) {\n // const serde = meta.fields[maybeSoftDelete].serde as TimestampSerde<unknown>;\n // const now = new Date();\n // other.set({ [maybeSoftDelete]: serde.mapFromNow(now) });\n // } else {\n // }\n em.delete(other);\n return other;\n } else if (removeMarker) {\n return undefined;\n } else {\n return other;\n }\n }\n }),\n );\n\n await (entity as any)[name].load();\n setOpt(\n meta,\n entity,\n name,\n others.filter((v) => v !== undefined),\n );\n } else {\n // If we get here, value should be a vanilla value that `setOpt` can handle/pass through as-is\n setOpt(meta, entity, name, value);\n }\n }),\n );\n}\n\n/**\n * Creates or updates an entity from partial input.\n *\n * An id selects an existing entity by database ID or registered client ID. Omitting id keeps the\n * existing creation and soft-delete resurrection behavior.\n */\nexport async function upsert<T extends Entity>(\n em: EntityManager<any, any, any>,\n constructor: MaybeAbstractEntityConstructor<T>,\n input: DeepPartialOrNull<T>,\n): Promise<T> {\n const { id, ...rest } = input;\n const entity =\n id !== null && id !== undefined\n ? await em.load(constructor, id)\n : // Look for an existing entity we could resurrect.\n ((id === undefined ? await findExistingIfUniqueBy(em, constructor, rest) : undefined) ??\n em.createPartial(asConcreteCstr(constructor), {}));\n await updatePartial(entity, rest as any);\n // Do this manually since we're not going through setOpts anymore\n if (entity.isNewEntity) {\n setSyncDefaults(entity);\n }\n return entity;\n}\n\n/**\n * Adds the o2m owner field so child resurrection can find rows whose `uniqueBy` includes their parent.\n * I.e. upserting `Author.books: [{ title: \"b1\" }]` should lookup `Book.uniqueBy = [[\"author\", \"title\"]]`.\n */\nfunction withOwnerField(field: OneToManyField | ManyToManyField, value: any, owner: Entity): any {\n if (field.kind !== \"o2m\" || !isPlainObject(value)) return value;\n const ownerField = field.otherFieldName;\n const ownerFieldId = field.otherMetadata().allFields[ownerField]?.fieldIdName;\n if (Object.hasOwn(value, ownerField) || (ownerFieldId && Object.hasOwn(value, ownerFieldId))) return value;\n if (ownerFieldId && !owner.isNewEntity) return { ...value, [ownerFieldId]: owner.id };\n return { ...value, [ownerField]: owner };\n}\n\nfunction getCollectionMarkers(field: OneToManyField | ManyToManyField, value: any): [any, any, any] {\n const allowDelete = !field.otherMetadata().fields[\"delete\"];\n const allowRemove = !field.otherMetadata().fields[\"remove\"];\n const allowOp = !field.otherMetadata().fields[\"op\"];\n\n // Look for `delete: true/false` and `remove: true/false` markers\n const deleteMarker: any = allowDelete && value[\"delete\"];\n const removeMarker: any = allowRemove && value[\"remove\"];\n const opMarker: any = allowOp && value[\"op\"];\n\n // Remove the markers, regardless of true/false, before recursing into upsert to avoid unknown fields\n if (deleteMarker !== undefined) delete value.delete;\n if (removeMarker !== undefined) delete value.remove;\n if (opMarker !== undefined) delete value.op;\n\n return [deleteMarker, removeMarker, opMarker];\n}\nfunction getDeleteMarker(field: OneToOneField | ManyToOneField, value: any): boolean {\n const allowDelete = !field.otherMetadata().fields[\"delete\"];\n const deleteMarker: any = allowDelete && value[\"delete\"];\n if (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AAI,MAAM,SAAS,SAAS,EAAA,GAACH,YAAAA,cAAAA,CAAc,KAAK,GAAG,OAAO;CAC1D,MAAM,aAAa,MAAM;CACzB,MAAM,eAAe,MAAM,cAAc,CAAC,CAAC,UAAU,WAAW,EAAE;CAClE,IAAI,OAAO,OAAO,OAAO,UAAU,KAAM,gBAAgB,OAAO,OAAO,OAAO,YAAY,GAAI,OAAO;CACrG,IAAI,gBAAgB,CAAC,MAAM,aAAa,OAAO;EAAE,GAAG;GAAQ,eAAe,MAAM;CAAG;CACpF,OAAO;EAAE,GAAG;GAAQ,aAAa;CAAM;AACzC;AAEA,SAAS,qBAAqB,OAAyC,OAA6B;CAClG,MAAM,cAAc,CAAC,MAAM,cAAc,CAAC,CAAC,OAAO;CAClD,MAAM,cAAc,CAAC,MAAM,cAAc,CAAC,CAAC,OAAO;CAClD,MAAM,UAAU,CAAC,MAAM,cAAc,CAAC,CAAC,OAAO;CAG9C,MAAM,eAAoB,eAAe,MAAM;CAC/C,MAAM,eAAoB,eAAe,MAAM;CAC/C,MAAM,WAAgB,WAAW,MAAM;CAGvC,IAAI,iBAAiB,KAAA,GAAW,OAAO,MAAM;CAC7C,IAAI,iBAAiB,KAAA,GAAW,OAAO,MAAM;CAC7C,IAAI,aAAa,KAAA,GAAW,OAAO,MAAM;CAEzC,OAAO;EAAC;EAAc;EAAc;CAAQ;AAC9C;AACA,SAAS,gBAAgB,OAAuC,OAAqB;CAEnF,MAAM,eAAoB,CADL,MAAM,cAAc,CAAC,CAAC,OAAO,aACT,MAAM;CAC/C,IAAI,iBAAiB,KAAA,GAAW,OAAO,MAAM;CAC7C,OAAO;AACT"}
@@ -4,7 +4,7 @@ import { PartialOrNull } from "./index.cjs";
4
4
  import { OptIdsOf, OptsOf } from "./typeMap.cjs";
5
5
  import { NullOrDefinedOr } from "./utils.cjs";
6
6
  /**
7
- * The type for `EntityManager.createOrUpdateUnsafe` that allows "upsert"-ish behavior.
7
+ * Deep partial input for `EntityManager.upsert` and `Entity.setDeepPartial`.
8
8
  *
9
9
  * I.e. `T` is an entity with an optional id (create if unset, update if set), and we recurse
10
10
  * into any relations (references or collections) to allow those relations themselves to be
@@ -29,11 +29,14 @@ type AllowRelationsToBeIdsOrEntitiesOrPartials<T> = { [P in keyof T]: T[P] exten
29
29
  * A utility function to do partial-update style updates.
30
30
  *
31
31
  * The difference between this and `Entity.setPartial` is that we accept `DeepPartialOrNull`
32
- * updates, similar to `createOrUpdateUnsafe`, instead of just a flat/shallow list of opts.
32
+ * updates, similar to `upsert`, instead of just a flat/shallow list of opts.
33
33
  */
34
34
  export declare function updatePartial<T extends Entity>(entity: T, input: DeepPartialOrNull<T>): Promise<void>;
35
35
  /**
36
- * A utility function to create-or-update/upsert entities coming from a partial-update style API.
36
+ * Creates or updates an entity from partial input.
37
+ *
38
+ * An id selects an existing entity by database ID or registered client ID. Omitting id keeps the
39
+ * existing creation and soft-delete resurrection behavior.
37
40
  */
38
41
  export declare function upsert<T extends Entity>(em: EntityManager<any, any, any>, constructor: MaybeAbstractEntityConstructor<T>, input: DeepPartialOrNull<T>): Promise<T>;
39
42
  export {};
@@ -1 +1 @@
1
- {"version":3,"file":"upsert.d.cts","names":[],"sources":["../src/upsert.ts"],"mappings":";;;;;;;;;;;;;YA+BY,kBAAkB,UAAU;EAAY,KAAK,KAAK;IAAc,0CAC1E,cAAc,OAAO,KAAK,SAAS,OAEnC,SAAS;;KAGN;EACH;EACA;EACA;;;KAIG;EACH;;KAGG,0CAA0C,QAC5C,WAAW,IAAI,EAAE,WAAW,sBAAsB,KAC/C,UAAU,YAAY,KACpB,UAAU,SACR,MAAM,KAAK,kBAAkB,KAAK,mBAAmB,KAAK,aAC1D,EAAE,KACJ,UAAU,SACR,KAAK,kBAAkB,KAAK,cAAc,KAAK,YAC/C,EAAE,KACN,EAAE;;;;;;;wBASc,cAAc,UAAU,QAAQ,QAAQ,GAAG,OAAO,kBAAkB,KAAK;;;;wBA8MzE,OAAO,UAAU,QACrC,IAAI,8BACJ,aAAa,+BAA+B,IAC5C,OAAO,kBAAkB,KACxB,QAAQ"}
1
+ {"version":3,"file":"upsert.d.cts","names":[],"sources":["../src/upsert.ts"],"mappings":";;;;;;;;;;;;;YA+BY,kBAAkB,UAAU;EAAY,KAAK,KAAK;IAAc,0CAC1E,cAAc,OAAO,KAAK,SAAS,OAEnC,SAAS;;KAGN;EACH;EACA;EACA;;;KAIG;EACH;;KAGG,0CAA0C,QAC5C,WAAW,IAAI,EAAE,WAAW,sBAAsB,KAC/C,UAAU,YAAY,KACpB,UAAU,SACR,MAAM,KAAK,kBAAkB,KAAK,mBAAmB,KAAK,aAC1D,EAAE,KACJ,UAAU,SACR,KAAK,kBAAkB,KAAK,cAAc,KAAK,YAC/C,EAAE,KACN,EAAE;;;;;;;wBASc,cAAc,UAAU,QAAQ,QAAQ,GAAG,OAAO,kBAAkB,KAAK;;;;;;;wBAkNzE,OAAO,UAAU,QACrC,IAAI,8BACJ,aAAa,+BAA+B,IAC5C,OAAO,kBAAkB,KACxB,QAAQ"}
@@ -4,7 +4,7 @@ import { PartialOrNull } from "./index.mjs";
4
4
  import { OptIdsOf, OptsOf } from "./typeMap.mjs";
5
5
  import { NullOrDefinedOr } from "./utils.mjs";
6
6
  /**
7
- * The type for `EntityManager.createOrUpdateUnsafe` that allows "upsert"-ish behavior.
7
+ * Deep partial input for `EntityManager.upsert` and `Entity.setDeepPartial`.
8
8
  *
9
9
  * I.e. `T` is an entity with an optional id (create if unset, update if set), and we recurse
10
10
  * into any relations (references or collections) to allow those relations themselves to be
@@ -29,11 +29,14 @@ type AllowRelationsToBeIdsOrEntitiesOrPartials<T> = { [P in keyof T]: T[P] exten
29
29
  * A utility function to do partial-update style updates.
30
30
  *
31
31
  * The difference between this and `Entity.setPartial` is that we accept `DeepPartialOrNull`
32
- * updates, similar to `createOrUpdateUnsafe`, instead of just a flat/shallow list of opts.
32
+ * updates, similar to `upsert`, instead of just a flat/shallow list of opts.
33
33
  */
34
34
  export declare function updatePartial<T extends Entity>(entity: T, input: DeepPartialOrNull<T>): Promise<void>;
35
35
  /**
36
- * A utility function to create-or-update/upsert entities coming from a partial-update style API.
36
+ * Creates or updates an entity from partial input.
37
+ *
38
+ * An id selects an existing entity by database ID or registered client ID. Omitting id keeps the
39
+ * existing creation and soft-delete resurrection behavior.
37
40
  */
38
41
  export declare function upsert<T extends Entity>(em: EntityManager<any, any, any>, constructor: MaybeAbstractEntityConstructor<T>, input: DeepPartialOrNull<T>): Promise<T>;
39
42
  export {};
@@ -1 +1 @@
1
- {"version":3,"file":"upsert.d.mts","names":[],"sources":["../src/upsert.ts"],"mappings":";;;;;;;;;;;;;YA+BY,kBAAkB,UAAU;EAAY,KAAK,KAAK;IAAc,0CAC1E,cAAc,OAAO,KAAK,SAAS,OAEnC,SAAS;;KAGN;EACH;EACA;EACA;;;KAIG;EACH;;KAGG,0CAA0C,QAC5C,WAAW,IAAI,EAAE,WAAW,sBAAsB,KAC/C,UAAU,YAAY,KACpB,UAAU,SACR,MAAM,KAAK,kBAAkB,KAAK,mBAAmB,KAAK,aAC1D,EAAE,KACJ,UAAU,SACR,KAAK,kBAAkB,KAAK,cAAc,KAAK,YAC/C,EAAE,KACN,EAAE;;;;;;;wBASc,cAAc,UAAU,QAAQ,QAAQ,GAAG,OAAO,kBAAkB,KAAK;;;;wBA8MzE,OAAO,UAAU,QACrC,IAAI,8BACJ,aAAa,+BAA+B,IAC5C,OAAO,kBAAkB,KACxB,QAAQ"}
1
+ {"version":3,"file":"upsert.d.mts","names":[],"sources":["../src/upsert.ts"],"mappings":";;;;;;;;;;;;;YA+BY,kBAAkB,UAAU;EAAY,KAAK,KAAK;IAAc,0CAC1E,cAAc,OAAO,KAAK,SAAS,OAEnC,SAAS;;KAGN;EACH;EACA;EACA;;;KAIG;EACH;;KAGG,0CAA0C,QAC5C,WAAW,IAAI,EAAE,WAAW,sBAAsB,KAC/C,UAAU,YAAY,KACpB,UAAU,SACR,MAAM,KAAK,kBAAkB,KAAK,mBAAmB,KAAK,aAC1D,EAAE,KACJ,UAAU,SACR,KAAK,kBAAkB,KAAK,cAAc,KAAK,YAC/C,EAAE,KACN,EAAE;;;;;;;wBASc,cAAc,UAAU,QAAQ,QAAQ,GAAG,OAAO,kBAAkB,KAAK;;;;;;;wBAkNzE,OAAO,UAAU,QACrC,IAAI,8BACJ,aAAa,+BAA+B,IAC5C,OAAO,kBAAkB,KACxB,QAAQ"}
package/build/upsert.js CHANGED
@@ -14,7 +14,7 @@ import { isPlainObject } from "joist-utils";
14
14
  * A utility function to do partial-update style updates.
15
15
  *
16
16
  * The difference between this and `Entity.setPartial` is that we accept `DeepPartialOrNull`
17
- * updates, similar to `createOrUpdateUnsafe`, instead of just a flat/shallow list of opts.
17
+ * updates, similar to `upsert`, instead of just a flat/shallow list of opts.
18
18
  */
19
19
  async function updatePartial(entity, input) {
20
20
  if (Object.keys(input).length === 0) return;
@@ -25,7 +25,7 @@ async function updatePartial(entity, input) {
25
25
  const field = meta.allFields[key] || Object.values(meta.allFields).find((f) => f.fieldIdName === key);
26
26
  if (!field) {
27
27
  if (key === "delete" || key === "remove" || key === "op") return;
28
- if (getProperties(meta)[key]) {
28
+ if (getProperties(meta)[key] || key === "$clientId") {
29
29
  setOpt(meta, entity, key, value);
30
30
  return;
31
31
  } else throw new Error(`Unknown field ${key}`);
@@ -119,7 +119,10 @@ async function updatePartial(entity, input) {
119
119
  }));
120
120
  }
121
121
  /**
122
- * A utility function to create-or-update/upsert entities coming from a partial-update style API.
122
+ * Creates or updates an entity from partial input.
123
+ *
124
+ * An id selects an existing entity by database ID or registered client ID. Omitting id keeps the
125
+ * existing creation and soft-delete resurrection behavior.
123
126
  */
124
127
  async function upsert(em, constructor, input) {
125
128
  const { id, ...rest } = input;
@@ -1 +1 @@
1
- {"version":3,"file":"upsert.js","names":[],"sources":["../src/upsert.ts"],"sourcesContent":["import { isPlainObject } from \"joist-utils\";\nimport { setSyncDefaults } from \"src/defaults.ts\";\nimport { type Entity, isEntity } from \"src/Entity.ts\";\nimport { type EntityManager, type IdOf, type MaybeAbstractEntityConstructor, isKey } from \"src/EntityManager.ts\";\nimport {\n type ManyToManyField,\n type ManyToOneField,\n type OneToManyField,\n type OneToOneField,\n getMetadata,\n} from \"src/EntityMetadata.ts\";\nimport {\n type PartialOrNull,\n type TimestampSerde,\n asConcreteCstr,\n getConstructorFromTaggedId,\n getProperties,\n setOpt,\n} from \"src/index.ts\";\nimport { findExistingIfUniqueBy } from \"src/resurrection.ts\";\nimport { type OptIdsOf, type OptsOf } from \"src/typeMap.ts\";\nimport { type NullOrDefinedOr, toArray } from \"src/utils.ts\";\n\n/**\n * The type for `EntityManager.createOrUpdateUnsafe` that allows \"upsert\"-ish behavior.\n *\n * I.e. `T` is an entity with an optional id (create if unset, update if set), and we recurse\n * into any relations (references or collections) to allow those relations themselves to be\n * any combination of 1) ids to existing entities, 2) entities directly, 3) null/undefined\n * with the appropriate partial-update behavior, or 4) partials.\n */\nexport type DeepPartialOrNull<T extends Entity> = { id?: IdOf<T> | null } & AllowRelationsToBeIdsOrEntitiesOrPartials<\n PartialOrNull<OptsOf<T> & OptIdsOf<T>>\n> &\n OptIdsOf<T>;\n\n/** Flags that allow `upsert` to delete or remove entities. */\ntype CollectionFlags = {\n delete?: boolean | null;\n remove?: boolean | null;\n op?: \"remove\" | \"delete\" | \"include\" | \"incremental\";\n};\n\n/** Flag that allows `upsert` to delete. */\ntype DeleteFlag = {\n delete?: boolean | null;\n};\n\ntype AllowRelationsToBeIdsOrEntitiesOrPartials<T> = {\n [P in keyof T]: T[P] extends NullOrDefinedOr<infer U>\n ? U extends Array<infer V>\n ? V extends Entity\n ? Array<V | (DeepPartialOrNull<V> & CollectionFlags) | IdOf<V>> | null\n : T[P]\n : U extends Entity\n ? U | (DeepPartialOrNull<U> & DeleteFlag) | IdOf<U> | null\n : T[P]\n : T[P];\n};\n\n/**\n * A utility function to do partial-update style updates.\n *\n * The difference between this and `Entity.setPartial` is that we accept `DeepPartialOrNull`\n * updates, similar to `createOrUpdateUnsafe`, instead of just a flat/shallow list of opts.\n */\nexport async function updatePartial<T extends Entity>(entity: T, input: DeepPartialOrNull<T>): Promise<void> {\n if (Object.keys(input).length === 0) return;\n const meta = getMetadata(entity);\n const { em } = entity;\n // Do a Promise.all so we .load() relations in parallel\n await Promise.all(\n Object.entries(input).map(async ([key, value]) => {\n // setPartial ==> `undefined` is a noop\n if (value === undefined) return;\n\n // Watch for the `bookId` / `bookIds` aliases\n const field = meta.allFields[key] || Object.values(meta.allFields).find((f) => f.fieldIdName === key);\n\n if (!field) {\n // Allow (ignore here) delete/remove flags that we assume the API layer (i.e. GraphQL) will have specifically\n // allowed, i.e. this isn't the Rails form bug where users can POST in any random field they want.\n const flagField = key === \"delete\" || key === \"remove\" || key === \"op\";\n if (flagField) return;\n // Look for non-field properties like a fullName setter\n const prop = getProperties(meta)[key];\n if (prop) {\n setOpt(meta, entity, key, value);\n return;\n } else {\n throw new Error(`Unknown field ${key}`);\n }\n }\n\n // Don't use key b/c it might be the bookId alias\n const name = field.fieldName;\n\n if (field.kind === \"poly\" && value && !isEntity(value)) {\n // Resolve non-entity poly values\n if (isKey(value)) {\n setOpt(meta, entity, name, await em.load(getConstructorFromTaggedId(value), value));\n } else {\n throw new Error(`Cannot use partial value for polymorphic field `);\n }\n } else if (field.kind === \"m2o\" && value && !isEntity(value)) {\n // This is a many-to-one reference\n if (isKey(value)) {\n setOpt(meta, entity, name, await em.load(field.otherMetadata().cstr, value));\n } else if (typeof value === \"object\" && value) {\n let other: any;\n if (\"id\" in value) {\n // This is a many-to-one partial update to an existing entity (they passed an id)\n other = await upsert(em, field.otherMetadata().cstr, value as any);\n } else {\n // This is a many-to-one partial w/o passing an id, i.e. `upsert(Author, { id: \"a:1\", publisher: { name: \"p1\" } })`\n if (entity.isNewEntity) {\n // If we're brand new, our child/other has to be brand new as well\n other = await upsert(em, field.otherMetadata().cstr, value);\n } else {\n // the o2o upsert hash didn't have an id, so find-or-create it\n other = await (entity as any)[name].load();\n if (other) {\n await updatePartial(other, value);\n } else {\n other = await upsert(em, field.otherMetadata().cstr, value);\n }\n }\n }\n const deleteMarker = getDeleteMarker(field, value);\n if (deleteMarker) {\n em.delete(other);\n other = undefined;\n }\n setOpt(meta, entity, name, other);\n }\n } else if (field.kind === \"o2o\") {\n // ^-- o2o always need loaded to be set, so don't fall through\n if (value === null || isEntity(value)) {\n await (entity as any)[name].load();\n setOpt(meta, entity, name, value);\n } else if (isKey(value)) {\n const [, other] = await Promise.all([\n (entity as any)[name].load(),\n em.load(field.otherMetadata().cstr, value),\n ]);\n setOpt(meta, entity, name, other);\n } else if (typeof value === \"object\" && isPlainObject(value)) {\n const deleteMarker = getDeleteMarker(field, value);\n // Always load the o2o so we can set it\n const current = await (entity as any)[name].load();\n let other: any;\n if (\"id\" in value) {\n // The o2o upsert hash was passed an id, so load it\n other = await upsert(em, field.otherMetadata().cstr, value as any);\n } else {\n // the o2o upsert hash didn't have an id, so find-or-create it\n other = current;\n if (other) {\n await updatePartial(other, value);\n } else {\n other = await upsert(em, field.otherMetadata().cstr, value);\n }\n }\n if (deleteMarker) {\n em.delete(other);\n } else {\n setOpt(meta, entity, name, other);\n }\n } else {\n throw new Error(`Invalid value ${value}`);\n }\n } else if (field.kind === \"o2m\" || field.kind === \"m2m\") {\n // Look for one-to-many/many-to-many partials\n // `null` is handled later, and treated as `[]`, which needs the collection loaded\n\n const values = toArray(value);\n const otherMeta = field.otherMetadata();\n const maybeSoftDelete = otherMeta.timestampFields?.deletedAt;\n\n // Incremental handling. Detect incremental mode by a meaningful `op` value--`op: undefined`/`op: null`\n // is the natural shape of an optional field and must behave like \"no op\", consistent with how\n // updatePartial treats `value === undefined` and how delete/remove are read by truthiness.\n const anyValueHasOp = values.some((v: any) => v && typeof v === \"object\" && !isEntity(v) && v.op != null);\n if (anyValueHasOp) {\n let anyValueMissingOp = false;\n const current = (entity as any)[name];\n // The `op` behavior means incremental and wants to do this:\n await Promise.all(\n values.map(async (value: any) => {\n const [, , op] = getCollectionMarkers(field, value);\n if (op === \"delete\") {\n const other = await upsert(em, otherMeta.cstr, withOwnerField(field, value, entity));\n // We need to check if this is a soft-deletable entity, and if so, we will soft-delete it.\n if (maybeSoftDelete) {\n const serde = otherMeta.fields[maybeSoftDelete].serde as TimestampSerde<unknown>;\n const now = new Date();\n other.set({ [maybeSoftDelete]: serde.mapFromNow(now) });\n } else {\n entity.em.delete(other);\n }\n } else if (op === \"remove\") {\n const other = await upsert(em, otherMeta.cstr, withOwnerField(field, value, entity));\n current.remove(other);\n } else if (op === \"include\") {\n const other = await upsert(em, otherMeta.cstr, withOwnerField(field, value, entity));\n current.add(other);\n } else if (op === \"incremental\") {\n // This is a marker entry, just ignore it\n } else {\n // A child entered incremental mode (a sibling had a real op) but has no op of its own;\n // never silently skip it--flag it so the guard below throws instead of dropping the row.\n anyValueMissingOp = true;\n }\n }),\n );\n if (anyValueMissingOp) {\n throw new Error(\"If any child sets the `op` key, then all children must have the `op` key.\");\n }\n return; // return from the op-based incremental behavior\n }\n\n // Otherwise we do exhaustive sets, will still looking for `delete`/`remove` markers\n const others = await Promise.all(\n values.map(async (value: any) => {\n if (isEntity(value)) {\n return value;\n } else if (isKey(value)) {\n return em.load(field.otherMetadata().cstr, value);\n } else if (value === null || value === undefined) {\n return undefined;\n } else if (!isPlainObject(value)) {\n throw new Error(`Invalid value ${value}`);\n } else {\n // Look for `delete: true/false` and `remove: true/false` markers\n const [deleteMarker, removeMarker] = getCollectionMarkers(field, value);\n const other = await upsert(em, field.otherMetadata().cstr, withOwnerField(field, value, entity));\n if (deleteMarker) {\n // if (maybeSoftDelete) {\n // const serde = meta.fields[maybeSoftDelete].serde as TimestampSerde<unknown>;\n // const now = new Date();\n // other.set({ [maybeSoftDelete]: serde.mapFromNow(now) });\n // } else {\n // }\n em.delete(other);\n return other;\n } else if (removeMarker) {\n return undefined;\n } else {\n return other;\n }\n }\n }),\n );\n\n await (entity as any)[name].load();\n setOpt(\n meta,\n entity,\n name,\n others.filter((v) => v !== undefined),\n );\n } else {\n // If we get here, value should be a vanilla value that `setOpt` can handle/pass through as-is\n setOpt(meta, entity, name, value);\n }\n }),\n );\n}\n\n/**\n * A utility function to create-or-update/upsert entities coming from a partial-update style API.\n */\nexport async function upsert<T extends Entity>(\n em: EntityManager<any, any, any>,\n constructor: MaybeAbstractEntityConstructor<T>,\n input: DeepPartialOrNull<T>,\n): Promise<T> {\n const { id, ...rest } = input;\n const entity =\n id !== null && id !== undefined\n ? await em.load(constructor, id)\n : // Look for an existing entity we could resurrect\n ((id === undefined ? await findExistingIfUniqueBy(em, constructor, rest) : undefined) ??\n em.createPartial(asConcreteCstr(constructor), {}));\n await updatePartial(entity, rest as any);\n // Do this manually since we're not going through setOpts anymore\n if (entity.isNewEntity) {\n setSyncDefaults(entity);\n }\n return entity;\n}\n\n/**\n * Adds the o2m owner field so child resurrection can find rows whose `uniqueBy` includes their parent.\n * I.e. upserting `Author.books: [{ title: \"b1\" }]` should lookup `Book.uniqueBy = [[\"author\", \"title\"]]`.\n */\nfunction withOwnerField(field: OneToManyField | ManyToManyField, value: any, owner: Entity): any {\n if (field.kind !== \"o2m\" || !isPlainObject(value)) return value;\n const ownerField = field.otherFieldName;\n const ownerFieldId = field.otherMetadata().allFields[ownerField]?.fieldIdName;\n if (Object.hasOwn(value, ownerField) || (ownerFieldId && Object.hasOwn(value, ownerFieldId))) return value;\n if (ownerFieldId && !owner.isNewEntity) return { ...value, [ownerFieldId]: owner.id };\n return { ...value, [ownerField]: owner };\n}\n\nfunction getCollectionMarkers(field: OneToManyField | ManyToManyField, value: any): [any, any, any] {\n const allowDelete = !field.otherMetadata().fields[\"delete\"];\n const allowRemove = !field.otherMetadata().fields[\"remove\"];\n const allowOp = !field.otherMetadata().fields[\"op\"];\n\n // Look for `delete: true/false` and `remove: true/false` markers\n const deleteMarker: any = allowDelete && value[\"delete\"];\n const removeMarker: any = allowRemove && value[\"remove\"];\n const opMarker: any = allowOp && value[\"op\"];\n\n // Remove the markers, regardless of true/false, before recursing into upsert to avoid unknown fields\n if (deleteMarker !== undefined) delete value.delete;\n if (removeMarker !== undefined) delete value.remove;\n if (opMarker !== undefined) delete value.op;\n\n return [deleteMarker, removeMarker, opMarker];\n}\nfunction getDeleteMarker(field: OneToOneField | ManyToOneField, value: any): boolean {\n const allowDelete = !field.otherMetadata().fields[\"delete\"];\n const deleteMarker: any = allowDelete && value[\"delete\"];\n if (deleteMarker !== undefined) delete value.delete;\n return deleteMarker;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAkEA,eAAsB,cAAgC,QAAW,OAA4C;CAC3G,IAAI,OAAO,KAAK,KAAK,CAAC,CAAC,WAAW,GAAG;CACrC,MAAM,OAAO,YAAY,MAAM;CAC/B,MAAM,EAAE,OAAO;CAEf,MAAM,QAAQ,IACZ,OAAO,QAAQ,KAAK,CAAC,CAAC,IAAI,OAAO,CAAC,KAAK,WAAW;EAEhD,IAAI,UAAU,KAAA,GAAW;EAGzB,MAAM,QAAQ,KAAK,UAAU,QAAQ,OAAO,OAAO,KAAK,SAAS,CAAC,CAAC,MAAM,MAAM,EAAE,gBAAgB,GAAG;EAEpG,IAAI,CAAC,OAAO;GAIV,IADkB,QAAQ,YAAY,QAAQ,YAAY,QAAQ,MACnD;GAGf,IADa,cAAc,IAAI,CAAC,CAAC,MACvB;IACR,OAAO,MAAM,QAAQ,KAAK,KAAK;IAC/B;GACF,OACE,MAAM,IAAI,MAAM,iBAAiB,KAAK;EAE1C;EAGA,MAAM,OAAO,MAAM;EAEnB,IAAI,MAAM,SAAS,UAAU,SAAS,CAAC,SAAS,KAAK,GAAG;GAEtD,IAAI,MAAM,KAAK,GACb,OAAO,MAAM,QAAQ,MAAM,MAAM,GAAG,KAAK,2BAA2B,KAAK,GAAG,KAAK,CAAC;QAElF,MAAM,IAAI,MAAM,iDAAiD;EAErE,OAAO,IAAI,MAAM,SAAS,SAAS,SAAS,CAAC,SAAS,KAAK,GAAG;GAE5D,IAAI,MAAM,KAAK,GACb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/F,IAAI,MAAM,SAAS,SAAS,CAAC,cAAc,KAAK,GAAG,OAAO;CAC1D,MAAM,aAAa,MAAM;CACzB,MAAM,eAAe,MAAM,cAAc,CAAC,CAAC,UAAU,WAAW,EAAE;CAClE,IAAI,OAAO,OAAO,OAAO,UAAU,KAAM,gBAAgB,OAAO,OAAO,OAAO,YAAY,GAAI,OAAO;CACrG,IAAI,gBAAgB,CAAC,MAAM,aAAa,OAAO;EAAE,GAAG;GAAQ,eAAe,MAAM;CAAG;CACpF,OAAO;EAAE,GAAG;GAAQ,aAAa;CAAM;AACzC;AAEA,SAAS,qBAAqB,OAAyC,OAA6B;CAClG,MAAM,cAAc,CAAC,MAAM,cAAc,CAAC,CAAC,OAAO;CAClD,MAAM,cAAc,CAAC,MAAM,cAAc,CAAC,CAAC,OAAO;CAClD,MAAM,UAAU,CAAC,MAAM,cAAc,CAAC,CAAC,OAAO;CAG9C,MAAM,eAAoB,eAAe,MAAM;CAC/C,MAAM,eAAoB,eAAe,MAAM;CAC/C,MAAM,WAAgB,WAAW,MAAM;CAGvC,IAAI,iBAAiB,KAAA,GAAW,OAAO,MAAM;CAC7C,IAAI,iBAAiB,KAAA,GAAW,OAAO,MAAM;CAC7C,IAAI,aAAa,KAAA,GAAW,OAAO,MAAM;CAEzC,OAAO;EAAC;EAAc;EAAc;CAAQ;AAC9C;AACA,SAAS,gBAAgB,OAAuC,OAAqB;CAEnF,MAAM,eAAoB,CADL,MAAM,cAAc,CAAC,CAAC,OAAO,aACT,MAAM;CAC/C,IAAI,iBAAiB,KAAA,GAAW,OAAO,MAAM;CAC7C,OAAO;AACT"}
1
+ {"version":3,"file":"upsert.js","names":[],"sources":["../src/upsert.ts"],"sourcesContent":["import { isPlainObject } from \"joist-utils\";\nimport { setSyncDefaults } from \"src/defaults.ts\";\nimport { type Entity, isEntity } from \"src/Entity.ts\";\nimport { type EntityManager, type IdOf, type MaybeAbstractEntityConstructor, isKey } from \"src/EntityManager.ts\";\nimport {\n type ManyToManyField,\n type ManyToOneField,\n type OneToManyField,\n type OneToOneField,\n getMetadata,\n} from \"src/EntityMetadata.ts\";\nimport {\n type PartialOrNull,\n type TimestampSerde,\n asConcreteCstr,\n getConstructorFromTaggedId,\n getProperties,\n setOpt,\n} from \"src/index.ts\";\nimport { findExistingIfUniqueBy } from \"src/resurrection.ts\";\nimport { type OptIdsOf, type OptsOf } from \"src/typeMap.ts\";\nimport { type NullOrDefinedOr, toArray } from \"src/utils.ts\";\n\n/**\n * Deep partial input for `EntityManager.upsert` and `Entity.setDeepPartial`.\n *\n * I.e. `T` is an entity with an optional id (create if unset, update if set), and we recurse\n * into any relations (references or collections) to allow those relations themselves to be\n * any combination of 1) ids to existing entities, 2) entities directly, 3) null/undefined\n * with the appropriate partial-update behavior, or 4) partials.\n */\nexport type DeepPartialOrNull<T extends Entity> = { id?: IdOf<T> | null } & AllowRelationsToBeIdsOrEntitiesOrPartials<\n PartialOrNull<OptsOf<T> & OptIdsOf<T>>\n> &\n OptIdsOf<T>;\n\n/** Flags that allow `upsert` to delete or remove entities. */\ntype CollectionFlags = {\n delete?: boolean | null;\n remove?: boolean | null;\n op?: \"remove\" | \"delete\" | \"include\" | \"incremental\";\n};\n\n/** Flag that allows `upsert` to delete. */\ntype DeleteFlag = {\n delete?: boolean | null;\n};\n\ntype AllowRelationsToBeIdsOrEntitiesOrPartials<T> = {\n [P in keyof T]: T[P] extends NullOrDefinedOr<infer U>\n ? U extends Array<infer V>\n ? V extends Entity\n ? Array<V | (DeepPartialOrNull<V> & CollectionFlags) | IdOf<V>> | null\n : T[P]\n : U extends Entity\n ? U | (DeepPartialOrNull<U> & DeleteFlag) | IdOf<U> | null\n : T[P]\n : T[P];\n};\n\n/**\n * A utility function to do partial-update style updates.\n *\n * The difference between this and `Entity.setPartial` is that we accept `DeepPartialOrNull`\n * updates, similar to `upsert`, instead of just a flat/shallow list of opts.\n */\nexport async function updatePartial<T extends Entity>(entity: T, input: DeepPartialOrNull<T>): Promise<void> {\n if (Object.keys(input).length === 0) return;\n const meta = getMetadata(entity);\n const { em } = entity;\n // Do a Promise.all so we .load() relations in parallel\n await Promise.all(\n Object.entries(input).map(async ([key, value]) => {\n // setPartial ==> `undefined` is a noop\n if (value === undefined) return;\n\n // Watch for the `bookId` / `bookIds` aliases\n const field = meta.allFields[key] || Object.values(meta.allFields).find((f) => f.fieldIdName === key);\n\n if (!field) {\n // Allow (ignore here) delete/remove flags that we assume the API layer (i.e. GraphQL) will have specifically\n // allowed, i.e. this isn't the Rails form bug where users can POST in any random field they want.\n const flagField = key === \"delete\" || key === \"remove\" || key === \"op\";\n if (flagField) return;\n // Look for non-field properties like a fullName setter\n const prop = getProperties(meta)[key];\n // Let setOpt explain why the creation-only $clientId option cannot be assigned here.\n if (prop || key === \"$clientId\") {\n setOpt(meta, entity, key, value);\n return;\n } else {\n throw new Error(`Unknown field ${key}`);\n }\n }\n\n // Don't use key b/c it might be the bookId alias\n const name = field.fieldName;\n\n if (field.kind === \"poly\" && value && !isEntity(value)) {\n // Resolve non-entity poly values\n if (isKey(value)) {\n setOpt(meta, entity, name, await em.load(getConstructorFromTaggedId(value), value));\n } else {\n throw new Error(`Cannot use partial value for polymorphic field `);\n }\n } else if (field.kind === \"m2o\" && value && !isEntity(value)) {\n // This is a many-to-one reference\n if (isKey(value)) {\n setOpt(meta, entity, name, await em.load(field.otherMetadata().cstr, value));\n } else if (typeof value === \"object\" && value) {\n let other: any;\n if (\"id\" in value) {\n // This is a many-to-one partial update to an existing entity (they passed an id)\n other = await upsert(em, field.otherMetadata().cstr, value as any);\n } else {\n // This is a many-to-one partial w/o passing an id, i.e. `upsert(Author, { id: \"a:1\", publisher: { name: \"p1\" } })`\n if (entity.isNewEntity) {\n // If we're brand new, our child/other has to be brand new as well\n other = await upsert(em, field.otherMetadata().cstr, value);\n } else {\n // the o2o upsert hash didn't have an id, so find-or-create it\n other = await (entity as any)[name].load();\n if (other) {\n await updatePartial(other, value);\n } else {\n other = await upsert(em, field.otherMetadata().cstr, value);\n }\n }\n }\n const deleteMarker = getDeleteMarker(field, value);\n if (deleteMarker) {\n em.delete(other);\n other = undefined;\n }\n setOpt(meta, entity, name, other);\n }\n } else if (field.kind === \"o2o\") {\n // ^-- o2o always need loaded to be set, so don't fall through\n if (value === null || isEntity(value)) {\n await (entity as any)[name].load();\n setOpt(meta, entity, name, value);\n } else if (isKey(value)) {\n const [, other] = await Promise.all([\n (entity as any)[name].load(),\n em.load(field.otherMetadata().cstr, value),\n ]);\n setOpt(meta, entity, name, other);\n } else if (typeof value === \"object\" && isPlainObject(value)) {\n const deleteMarker = getDeleteMarker(field, value);\n // Always load the o2o so we can set it\n const current = await (entity as any)[name].load();\n let other: any;\n if (\"id\" in value) {\n // The o2o upsert hash was passed an id, so load it\n other = await upsert(em, field.otherMetadata().cstr, value as any);\n } else {\n // the o2o upsert hash didn't have an id, so find-or-create it\n other = current;\n if (other) {\n await updatePartial(other, value);\n } else {\n other = await upsert(em, field.otherMetadata().cstr, value);\n }\n }\n if (deleteMarker) {\n em.delete(other);\n } else {\n setOpt(meta, entity, name, other);\n }\n } else {\n throw new Error(`Invalid value ${value}`);\n }\n } else if (field.kind === \"o2m\" || field.kind === \"m2m\") {\n // Look for one-to-many/many-to-many partials\n // `null` is handled later, and treated as `[]`, which needs the collection loaded\n\n const values = toArray(value);\n const otherMeta = field.otherMetadata();\n const maybeSoftDelete = otherMeta.timestampFields?.deletedAt;\n\n // Incremental handling. Detect incremental mode by a meaningful `op` value--`op: undefined`/`op: null`\n // is the natural shape of an optional field and must behave like \"no op\", consistent with how\n // updatePartial treats `value === undefined` and how delete/remove are read by truthiness.\n const anyValueHasOp = values.some((v: any) => v && typeof v === \"object\" && !isEntity(v) && v.op != null);\n if (anyValueHasOp) {\n let anyValueMissingOp = false;\n const current = (entity as any)[name];\n // The `op` behavior means incremental and wants to do this:\n await Promise.all(\n values.map(async (value: any) => {\n const [, , op] = getCollectionMarkers(field, value);\n if (op === \"delete\") {\n const other = await upsert(em, otherMeta.cstr, withOwnerField(field, value, entity));\n // We need to check if this is a soft-deletable entity, and if so, we will soft-delete it.\n if (maybeSoftDelete) {\n const serde = otherMeta.fields[maybeSoftDelete].serde as TimestampSerde<unknown>;\n const now = new Date();\n other.set({ [maybeSoftDelete]: serde.mapFromNow(now) });\n } else {\n entity.em.delete(other);\n }\n } else if (op === \"remove\") {\n const other = await upsert(em, otherMeta.cstr, withOwnerField(field, value, entity));\n current.remove(other);\n } else if (op === \"include\") {\n const other = await upsert(em, otherMeta.cstr, withOwnerField(field, value, entity));\n current.add(other);\n } else if (op === \"incremental\") {\n // This is a marker entry, just ignore it\n } else {\n // A child entered incremental mode (a sibling had a real op) but has no op of its own;\n // never silently skip it--flag it so the guard below throws instead of dropping the row.\n anyValueMissingOp = true;\n }\n }),\n );\n if (anyValueMissingOp) {\n throw new Error(\"If any child sets the `op` key, then all children must have the `op` key.\");\n }\n return; // return from the op-based incremental behavior\n }\n\n // Otherwise we do exhaustive sets, will still looking for `delete`/`remove` markers\n const others = await Promise.all(\n values.map(async (value: any) => {\n if (isEntity(value)) {\n return value;\n } else if (isKey(value)) {\n return em.load(field.otherMetadata().cstr, value);\n } else if (value === null || value === undefined) {\n return undefined;\n } else if (!isPlainObject(value)) {\n throw new Error(`Invalid value ${value}`);\n } else {\n // Look for `delete: true/false` and `remove: true/false` markers\n const [deleteMarker, removeMarker] = getCollectionMarkers(field, value);\n const other = await upsert(em, field.otherMetadata().cstr, withOwnerField(field, value, entity));\n if (deleteMarker) {\n // if (maybeSoftDelete) {\n // const serde = meta.fields[maybeSoftDelete].serde as TimestampSerde<unknown>;\n // const now = new Date();\n // other.set({ [maybeSoftDelete]: serde.mapFromNow(now) });\n // } else {\n // }\n em.delete(other);\n return other;\n } else if (removeMarker) {\n return undefined;\n } else {\n return other;\n }\n }\n }),\n );\n\n await (entity as any)[name].load();\n setOpt(\n meta,\n entity,\n name,\n others.filter((v) => v !== undefined),\n );\n } else {\n // If we get here, value should be a vanilla value that `setOpt` can handle/pass through as-is\n setOpt(meta, entity, name, value);\n }\n }),\n );\n}\n\n/**\n * Creates or updates an entity from partial input.\n *\n * An id selects an existing entity by database ID or registered client ID. Omitting id keeps the\n * existing creation and soft-delete resurrection behavior.\n */\nexport async function upsert<T extends Entity>(\n em: EntityManager<any, any, any>,\n constructor: MaybeAbstractEntityConstructor<T>,\n input: DeepPartialOrNull<T>,\n): Promise<T> {\n const { id, ...rest } = input;\n const entity =\n id !== null && id !== undefined\n ? await em.load(constructor, id)\n : // Look for an existing entity we could resurrect.\n ((id === undefined ? await findExistingIfUniqueBy(em, constructor, rest) : undefined) ??\n em.createPartial(asConcreteCstr(constructor), {}));\n await updatePartial(entity, rest as any);\n // Do this manually since we're not going through setOpts anymore\n if (entity.isNewEntity) {\n setSyncDefaults(entity);\n }\n return entity;\n}\n\n/**\n * Adds the o2m owner field so child resurrection can find rows whose `uniqueBy` includes their parent.\n * I.e. upserting `Author.books: [{ title: \"b1\" }]` should lookup `Book.uniqueBy = [[\"author\", \"title\"]]`.\n */\nfunction withOwnerField(field: OneToManyField | ManyToManyField, value: any, owner: Entity): any {\n if (field.kind !== \"o2m\" || !isPlainObject(value)) return value;\n const ownerField = field.otherFieldName;\n const ownerFieldId = field.otherMetadata().allFields[ownerField]?.fieldIdName;\n if (Object.hasOwn(value, ownerField) || (ownerFieldId && Object.hasOwn(value, ownerFieldId))) return value;\n if (ownerFieldId && !owner.isNewEntity) return { ...value, [ownerFieldId]: owner.id };\n return { ...value, [ownerField]: owner };\n}\n\nfunction getCollectionMarkers(field: OneToManyField | ManyToManyField, value: any): [any, any, any] {\n const allowDelete = !field.otherMetadata().fields[\"delete\"];\n const allowRemove = !field.otherMetadata().fields[\"remove\"];\n const allowOp = !field.otherMetadata().fields[\"op\"];\n\n // Look for `delete: true/false` and `remove: true/false` markers\n const deleteMarker: any = allowDelete && value[\"delete\"];\n const removeMarker: any = allowRemove && value[\"remove\"];\n const opMarker: any = allowOp && value[\"op\"];\n\n // Remove the markers, regardless of true/false, before recursing into upsert to avoid unknown fields\n if (deleteMarker !== undefined) delete value.delete;\n if (removeMarker !== undefined) delete value.remove;\n if (opMarker !== undefined) delete value.op;\n\n return [deleteMarker, removeMarker, opMarker];\n}\nfunction getDeleteMarker(field: OneToOneField | ManyToOneField, value: any): boolean {\n const allowDelete = !field.otherMetadata().fields[\"delete\"];\n const deleteMarker: any = allowDelete && value[\"delete\"];\n if (deleteMarker !== undefined) delete value.delete;\n return deleteMarker;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAkEA,eAAsB,cAAgC,QAAW,OAA4C;CAC3G,IAAI,OAAO,KAAK,KAAK,CAAC,CAAC,WAAW,GAAG;CACrC,MAAM,OAAO,YAAY,MAAM;CAC/B,MAAM,EAAE,OAAO;CAEf,MAAM,QAAQ,IACZ,OAAO,QAAQ,KAAK,CAAC,CAAC,IAAI,OAAO,CAAC,KAAK,WAAW;EAEhD,IAAI,UAAU,KAAA,GAAW;EAGzB,MAAM,QAAQ,KAAK,UAAU,QAAQ,OAAO,OAAO,KAAK,SAAS,CAAC,CAAC,MAAM,MAAM,EAAE,gBAAgB,GAAG;EAEpG,IAAI,CAAC,OAAO;GAIV,IADkB,QAAQ,YAAY,QAAQ,YAAY,QAAQ,MACnD;GAIf,IAFa,cAAc,IAAI,CAAC,CAAC,QAErB,QAAQ,aAAa;IAC/B,OAAO,MAAM,QAAQ,KAAK,KAAK;IAC/B;GACF,OACE,MAAM,IAAI,MAAM,iBAAiB,KAAK;EAE1C;EAGA,MAAM,OAAO,MAAM;EAEnB,IAAI,MAAM,SAAS,UAAU,SAAS,CAAC,SAAS,KAAK,GAAG;GAEtD,IAAI,MAAM,KAAK,GACb,OAAO,MAAM,QAAQ,MAAM,MAAM,GAAG,KAAK,2BAA2B,KAAK,GAAG,KAAK,CAAC;QAElF,MAAM,IAAI,MAAM,iDAAiD;EAErE,OAAO,IAAI,MAAM,SAAS,SAAS,SAAS,CAAC,SAAS,KAAK,GAAG;GAE5D,IAAI,MAA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AAY,OAAoB;CAC/F,IAAI,MAAM,SAAS,SAAS,CAAC,cAAc,KAAK,GAAG,OAAO;CAC1D,MAAM,aAAa,MAAM;CACzB,MAAM,eAAe,MAAM,cAAc,CAAC,CAAC,UAAU,WAAW,EAAE;CAClE,IAAI,OAAO,OAAO,OAAO,UAAU,KAAM,gBAAgB,OAAO,OAAO,OAAO,YAAY,GAAI,OAAO;CACrG,IAAI,gBAAgB,CAAC,MAAM,aAAa,OAAO;EAAE,GAAG;GAAQ,eAAe,MAAM;CAAG;CACpF,OAAO;EAAE,GAAG;GAAQ,aAAa;CAAM;AACzC;AAEA,SAAS,qBAAqB,OAAyC,OAA6B;CAClG,MAAM,cAAc,CAAC,MAAM,cAAc,CAAC,CAAC,OAAO;CAClD,MAAM,cAAc,CAAC,MAAM,cAAc,CAAC,CAAC,OAAO;CAClD,MAAM,UAAU,CAAC,MAAM,cAAc,CAAC,CAAC,OAAO;CAG9C,MAAM,eAAoB,eAAe,MAAM;CAC/C,MAAM,eAAoB,eAAe,MAAM;CAC/C,MAAM,WAAgB,WAAW,MAAM;CAGvC,IAAI,iBAAiB,KAAA,GAAW,OAAO,MAAM;CAC7C,IAAI,iBAAiB,KAAA,GAAW,OAAO,MAAM;CAC7C,IAAI,aAAa,KAAA,GAAW,OAAO,MAAM;CAEzC,OAAO;EAAC;EAAc;EAAc;CAAQ;AAC9C;AACA,SAAS,gBAAgB,OAAuC,OAAqB;CAEnF,MAAM,eAAoB,CADL,MAAM,cAAc,CAAC,CAAC,OAAO,aACT,MAAM;CAC/C,IAAI,iBAAiB,KAAA,GAAW,OAAO,MAAM;CAC7C,OAAO;AACT"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "joist-core",
3
- "version": "2.5.0-next.21",
3
+ "version": "2.5.0-next.23",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "repository": {
@@ -42,7 +42,7 @@
42
42
  "build"
43
43
  ],
44
44
  "peerDependencies": {
45
- "joist-utils": "2.5.0-next.21"
45
+ "joist-utils": "2.5.0-next.23"
46
46
  },
47
47
  "dependencies": {
48
48
  "ansis": "^4.5.0",