joist-core 2.5.0-next.22 → 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.
- package/build/BaseEntity.cjs +5 -2
- package/build/BaseEntity.cjs.map +1 -1
- package/build/BaseEntity.d.cts +2 -0
- package/build/BaseEntity.d.cts.map +1 -1
- package/build/BaseEntity.d.mts +2 -0
- package/build/BaseEntity.d.mts.map +1 -1
- package/build/BaseEntity.js +5 -2
- package/build/BaseEntity.js.map +1 -1
- package/build/Entity.cjs.map +1 -1
- package/build/Entity.d.cts +6 -0
- package/build/Entity.d.cts.map +1 -1
- package/build/Entity.d.mts +6 -0
- package/build/Entity.d.mts.map +1 -1
- package/build/Entity.js.map +1 -1
- package/build/EntityManager.cjs +68 -19
- package/build/EntityManager.cjs.map +1 -1
- package/build/EntityManager.d.cts +4 -1
- package/build/EntityManager.d.cts.map +1 -1
- package/build/EntityManager.d.mts +4 -1
- package/build/EntityManager.d.mts.map +1 -1
- package/build/EntityManager.js +68 -19
- package/build/EntityManager.js.map +1 -1
- package/build/InstanceData.cjs +2 -2
- package/build/InstanceData.cjs.map +1 -1
- package/build/InstanceData.d.cts +2 -2
- package/build/InstanceData.d.mts +2 -2
- package/build/InstanceData.js +2 -2
- package/build/InstanceData.js.map +1 -1
- package/build/clientIds.cjs +47 -0
- package/build/clientIds.cjs.map +1 -0
- package/build/clientIds.d.cts +26 -0
- package/build/clientIds.d.cts.map +1 -0
- package/build/clientIds.d.mts +26 -0
- package/build/clientIds.d.mts.map +1 -0
- package/build/clientIds.js +42 -0
- package/build/clientIds.js.map +1 -0
- package/build/configure.cjs +3 -0
- package/build/configure.cjs.map +1 -1
- package/build/configure.d.cts.map +1 -1
- package/build/configure.d.mts.map +1 -1
- package/build/configure.js +3 -0
- package/build/configure.js.map +1 -1
- package/build/getProperties.cjs +4 -1
- package/build/getProperties.cjs.map +1 -1
- package/build/getProperties.js +4 -1
- package/build/getProperties.js.map +1 -1
- package/build/index.cjs +5 -1
- package/build/index.cjs.map +1 -1
- package/build/index.d.cts.map +1 -1
- package/build/index.d.mts.map +1 -1
- package/build/index.js +5 -1
- package/build/index.js.map +1 -1
- package/build/keys.cjs +4 -0
- package/build/keys.cjs.map +1 -1
- package/build/keys.d.cts.map +1 -1
- package/build/keys.d.mts.map +1 -1
- package/build/keys.js +4 -0
- package/build/keys.js.map +1 -1
- package/build/newTestInstance.cjs +8 -8
- package/build/newTestInstance.cjs.map +1 -1
- package/build/newTestInstance.d.cts.map +1 -1
- package/build/newTestInstance.d.mts.map +1 -1
- package/build/newTestInstance.js +8 -8
- package/build/newTestInstance.js.map +1 -1
- package/build/resurrection.cjs +5 -3
- package/build/resurrection.cjs.map +1 -1
- package/build/resurrection.d.cts.map +1 -1
- package/build/resurrection.d.mts.map +1 -1
- package/build/resurrection.js +5 -3
- package/build/resurrection.js.map +1 -1
- package/build/upsert.cjs +6 -3
- package/build/upsert.cjs.map +1 -1
- package/build/upsert.d.cts +6 -3
- package/build/upsert.d.cts.map +1 -1
- package/build/upsert.d.mts +6 -3
- package/build/upsert.d.mts.map +1 -1
- package/build/upsert.js +6 -3
- package/build/upsert.js.map +1 -1
- package/package.json +2 -2
package/build/BaseEntity.cjs
CHANGED
|
@@ -39,6 +39,10 @@ var BaseEntity = class {
|
|
|
39
39
|
get fullNonReactiveAccess() {
|
|
40
40
|
return this;
|
|
41
41
|
}
|
|
42
|
+
/** The stable EM-local alias, independent of database identity and debug formatting. */
|
|
43
|
+
get $clientId() {
|
|
44
|
+
return this.__data.clientId;
|
|
45
|
+
}
|
|
42
46
|
/** @returns the entity's id, always untagged, or a runtime error if it's unassigned. */
|
|
43
47
|
get idUntagged() {
|
|
44
48
|
return require_keys.deTagId(require_EntityMetadata.getMetadata(this), this.id);
|
|
@@ -94,8 +98,7 @@ var BaseEntity = class {
|
|
|
94
98
|
function toStringWithPrefix(meta, entity, prefix) {
|
|
95
99
|
let result = prefix;
|
|
96
100
|
const data = getInstanceData(entity);
|
|
97
|
-
|
|
98
|
-
if (createId) result += `#${createId}`;
|
|
101
|
+
if (data.isOrWasNew && data.clientId) result += data.clientId.slice(meta.tagName.length);
|
|
99
102
|
const dbId = entity.idTaggedMaybe;
|
|
100
103
|
if (data.pendingOperation !== "insert" && dbId) result += `:${require_keys.keyToNumber(meta, dbId)}`;
|
|
101
104
|
return result;
|
package/build/BaseEntity.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BaseEntity.cjs","names":["deTagId","getMetadata","keyToNumber","InstanceData","hasVisibilityHook","isInTrustedContext"],"sources":["../src/BaseEntity.ts"],"sourcesContent":["import { type IdType } from \"src/Entity.ts\";\nimport { hasVisibilityHook } from \"src/EntityManager.ts\";\nimport {\n type Entity,\n type EntityManager,\n type EntityMetadata,\n InstanceData,\n type TaggedId,\n deTagId,\n getMetadata,\n keyToNumber,\n} from \"src/index.ts\";\nimport { isInTrustedContext } from \"src/trusted.ts\";\n\n// getProperties deletes its throwaway entity's own fields so callback access to `this` hits a guard proxy.\n// That includes these non-enumerable words, so they must be configurable (deletable).\nconst readWordDescriptor: PropertyDescriptor = { value: 0, enumerable: false, writable: true, configurable: true };\nconst trustedReadWordDescriptors: PropertyDescriptor[] = [];\n\n/**\n * Returns the internal `__data` tracking field for `entity`.\n *\n * This should be treated as an internal API and may change without notice.\n */\nexport function getInstanceData(entity: Entity): InstanceData {\n return BaseEntity.getInstanceData(entity);\n}\n\n/**\n * The base class for all entities.\n *\n * Currently, this just adds the `.load(lensFn)` method for declarative reference traversal.\n */\nexport abstract class BaseEntity<EM extends EntityManager, I extends IdType = IdType> implements Entity {\n public static getInstanceData(entity: Entity): InstanceData {\n return (entity as BaseEntity<any>).__data;\n }\n\n // We use a protected field so that subclass getters can easily access `this.__data.relations`.\n protected readonly __data!: InstanceData;\n /** First-read completion bits; extra words are declared by each generated root type as needed. */\n declare protected __readFields: number;\n\n /** The single-arg constructor is our psuedo-public API that can only be called by the EntityManager. */\n constructor(em: EntityManager);\n constructor(em: EntityManager, isNew?: boolean) {\n if (isNew === undefined) {\n throw new Error(\"Entities must be constructed by calling em.create or em.load\");\n }\n // This code path won't be reused for real entities, as they go through the `newEntity`\n // construction process that lazifies the relations.\n baseEntityCstr(em, this, isNew);\n }\n\n // This gives rules a way to access the fully typed object instead of their Reacted view.\n // And we make it public so that a function that takes Reacted<...> can accept a Loaded<...>\n // that sufficiently overlaps.\n get fullNonReactiveAccess(): this {\n return this;\n }\n\n /** @returns the entity's id, tagged/untagged based on your config, or a runtime error if it's new/unassigned. */\n abstract id: I;\n\n /** @returns the entity's, tagged/untagged based on your config, or undefined if it's new/unassigned. */\n abstract get idMaybe(): I | undefined;\n\n /** @returns the entity's id, always tagged, or a runtime error if it's new/unassigned. */\n abstract idTagged: TaggedId;\n\n /** @returns the entity's id, always tagged, or undefined if it's new/unassigned. */\n abstract get idTaggedMaybe(): TaggedId | undefined;\n\n /** @returns the entity's id, always untagged, or a runtime error if it's unassigned. */\n get idUntagged(): string {\n return deTagId(getMetadata(this), this.id);\n }\n\n abstract set(opts: unknown): void;\n\n /**\n * Similar to `set` but applies \"Partial API\" semantics, i.e. `null` means unset and `undefined` means don't change.\n */\n abstract setPartial(opts: unknown): void;\n\n /**\n * Similar to `set` but applies \"Partial API\" semantics, i.e. `null` means unset and `undefined` means don't change.\n */\n abstract setDeepPartial(opts: unknown): Promise<void>;\n\n /**\n * Returns whether the entity is new.\n *\n * This is not just `this.id === undefined`, because just assigning an id doesn't mean the entity\n * is no longer new; this only flips to `false` after the `flush` transaction has been committed.\n */\n get isNewEntity(): boolean {\n return this.__data.isNewEntity;\n }\n\n get isDeletedEntity(): boolean {\n return this.__data.isDeletedEntity;\n }\n\n get isDirtyEntity(): boolean {\n return this.__data.isDirtyEntity;\n }\n\n toString(): string {\n const meta = getMetadata(this);\n return toStringWithPrefix(meta, this, meta.type);\n }\n\n toTaggedString(): string {\n const meta = getMetadata(this);\n return toStringWithPrefix(meta, this, meta.tagName);\n }\n\n public get em(): EM {\n return this.__data.em as EM;\n }\n\n /**\n * A very simple toJSON that just returns the id.\n *\n * This is for security reasons, to avoid accidentally logging sensitive data; you\n * can specify more fields, and potentially nested return data, by using passing\n * a {@link JsonHint} to `toJSON`.\n */\n public toJSON(): object {\n return { id: this.idMaybe || null };\n }\n\n [Symbol.toStringTag](): string {\n return this.toString();\n }\n\n /**\n * Hooks into node's `console.log` to avoid sprawling output of our relations/internal state.\n *\n * See https://nodejs.org/api/util.html#custom-inspection-functions-on-objects.\n */\n [Symbol.for(\"nodejs.util.inspect.custom\")](): string {\n return this.toString();\n }\n\n /** A bag of non-persisted state, typically for flags that control business logic behavior. */\n declare readonly transientFields: Record<string, unknown>;\n}\n\nfunction toStringWithPrefix(meta: EntityMetadata, entity: Entity, prefix: string): string {\n // Start with `Author` or `a`, depending on the caller\n let result = prefix;\n // Add `#1` if this is an em.create-d entity\n const data = getInstanceData(entity);\n const createId = data.createId;\n if (createId) {\n result += `#${createId}`;\n }\n // Add the `:1` but only after our flush is complete, otherwise the id is\n // internally assigned but not really flushed/committed to the db yet\n const dbId = entity.idTaggedMaybe;\n if (data.pendingOperation !== \"insert\" && dbId) {\n // Strip the tag because we add back the entity prefix\n result += `:${keyToNumber(meta, dbId)}`;\n }\n return result;\n}\n\n/**\n * The constructor logic for `BaseEntity` but exposed so that `newEntity` can use it too\n * on its `Object.create`-d instances.\n */\nexport function baseEntityCstr(em: EntityManager, entity: BaseEntity<any, any>, isNew: boolean): void {\n const data = new InstanceData(em, entity, (entity.constructor as any).metadata, isNew);\n // This makes it non-enumerable to avoid Jest/recursive things tripping over it\n Object.defineProperty(entity, \"__data\", { value: data, enumerable: false, writable: false, configurable: false });\n // Constructors are bypassed for real entities, so initialize every needed word here,\n // in the same order for the entire inheritance family. Reusing the descriptor avoids per-entity descriptor objects.\n for (let word = 0; word < data.metadata.fieldReadWordCount; word++) {\n Object.defineProperty(entity, word === 0 ? \"__readFields\" : `__readFields${word}`, readWordDescriptor);\n }\n if (hasVisibilityHook(entity)) useTrustedReadCache(entity);\n}\n\n/** Replaces the __readFields fields with getters/setters that always return 0 for untrusted reads, so they are always access checked. */\nexport function useTrustedReadCache(entity: Entity): void {\n const data = getInstanceData(entity);\n if (data.trustedReadFields) return;\n data.trustedReadFields = [];\n for (let word = 0; word < data.metadata.fieldReadWordCount; word++) {\n const descriptor = (trustedReadWordDescriptors[word] ??= {\n configurable: true,\n enumerable: false,\n get: function (this: Entity): number {\n return isInTrustedContext() ? (getInstanceData(this).trustedReadFields![word] ?? 0) : 0;\n },\n set: function (this: Entity, value: number): void {\n if (isInTrustedContext()) getInstanceData(this).trustedReadFields![word] = value;\n },\n });\n Object.defineProperty(entity, word === 0 ? \"__readFields\" : `__readFields${word}`, descriptor);\n }\n}\n"],"mappings":";;;;;;;;;AAgBA,MAAM,qBAAyC;CAAE,OAAO;CAAG,YAAY;CAAO,UAAU;CAAM,cAAc;AAAK;AACjH,MAAM,6BAAmD,CAAC;;;;;;AAO1D,SAAgB,gBAAgB,QAA8B;CAC5D,OAAO,WAAW,gBAAgB,MAAM;AAC1C;;;;;;AAOA,IAAsB,aAAtB,MAAwG;CACtG,OAAc,gBAAgB,QAA8B;EAC1D,OAAQ,OAA2B;CACrC;CAGA;CAMA,YAAY,IAAmB,OAAiB;EAC9C,IAAI,UAAU,KAAA,GACZ,MAAM,IAAI,MAAM,8DAA8D;EAIhF,eAAe,IAAI,MAAM,KAAK;CAChC;CAKA,IAAI,wBAA8B;EAChC,OAAO;CACT;;CAeA,IAAI,aAAqB;EACvB,OAAOA,aAAAA,QAAQC,uBAAAA,YAAY,IAAI,GAAG,KAAK,EAAE;CAC3C;;;;;;;CAoBA,IAAI,cAAuB;EACzB,OAAO,KAAK,OAAO;CACrB;CAEA,IAAI,kBAA2B;EAC7B,OAAO,KAAK,OAAO;CACrB;CAEA,IAAI,gBAAyB;EAC3B,OAAO,KAAK,OAAO;CACrB;CAEA,WAAmB;EACjB,MAAM,OAAOA,uB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AAI,OAAO,GAAG,OAAO,KAAK,SAAS,oBAAoB,QAAQ;EAClE,MAAM,aAAc,2BAA2B,UAAU;GACvD,cAAc;GACd,YAAY;GACZ,KAAK,WAAgC;IACnC,OAAOC,gBAAAA,mBAAmB,IAAK,gBAAgB,IAAI,CAAC,CAAC,kBAAmB,SAAS,IAAK;GACxF;GACA,KAAK,SAAwB,OAAqB;IAChD,IAAIA,gBAAAA,mBAAmB,GAAG,gBAAgB,IAAI,CAAC,CAAC,kBAAmB,QAAQ;GAC7E;EACF;EACA,OAAO,eAAe,QAAQ,SAAS,IAAI,iBAAiB,eAAe,QAAQ,UAAU;CAC/F;AACF"}
|
|
1
|
+
{"version":3,"file":"BaseEntity.cjs","names":["deTagId","getMetadata","keyToNumber","InstanceData","hasVisibilityHook","isInTrustedContext"],"sources":["../src/BaseEntity.ts"],"sourcesContent":["import { type IdType } from \"src/Entity.ts\";\nimport { hasVisibilityHook } from \"src/EntityManager.ts\";\nimport {\n type Entity,\n type EntityManager,\n type EntityMetadata,\n InstanceData,\n type TaggedId,\n deTagId,\n getMetadata,\n keyToNumber,\n} from \"src/index.ts\";\nimport { isInTrustedContext } from \"src/trusted.ts\";\n\n// getProperties deletes its throwaway entity's own fields so callback access to `this` hits a guard proxy.\n// That includes these non-enumerable words, so they must be configurable (deletable).\nconst readWordDescriptor: PropertyDescriptor = { value: 0, enumerable: false, writable: true, configurable: true };\nconst trustedReadWordDescriptors: PropertyDescriptor[] = [];\n\n/**\n * Returns the internal `__data` tracking field for `entity`.\n *\n * This should be treated as an internal API and may change without notice.\n */\nexport function getInstanceData(entity: Entity): InstanceData {\n return BaseEntity.getInstanceData(entity);\n}\n\n/**\n * The base class for all entities.\n *\n * Currently, this just adds the `.load(lensFn)` method for declarative reference traversal.\n */\nexport abstract class BaseEntity<EM extends EntityManager, I extends IdType = IdType> implements Entity {\n public static getInstanceData(entity: Entity): InstanceData {\n return (entity as BaseEntity<any>).__data;\n }\n\n // We use a protected field so that subclass getters can easily access `this.__data.relations`.\n protected readonly __data!: InstanceData;\n /** First-read completion bits; extra words are declared by each generated root type as needed. */\n declare protected __readFields: number;\n\n /** The single-arg constructor is our psuedo-public API that can only be called by the EntityManager. */\n constructor(em: EntityManager);\n constructor(em: EntityManager, isNew?: boolean) {\n if (isNew === undefined) {\n throw new Error(\"Entities must be constructed by calling em.create or em.load\");\n }\n // This code path won't be reused for real entities, as they go through the `newEntity`\n // construction process that lazifies the relations.\n baseEntityCstr(em, this, isNew);\n }\n\n // This gives rules a way to access the fully typed object instead of their Reacted view.\n // And we make it public so that a function that takes Reacted<...> can accept a Loaded<...>\n // that sufficiently overlaps.\n get fullNonReactiveAccess(): this {\n return this;\n }\n\n /** @returns the entity's id, tagged/untagged based on your config, or a runtime error if it's new/unassigned. */\n abstract id: I;\n\n /** @returns the entity's, tagged/untagged based on your config, or undefined if it's new/unassigned. */\n abstract get idMaybe(): I | undefined;\n\n /** @returns the entity's id, always tagged, or a runtime error if it's new/unassigned. */\n abstract idTagged: TaggedId;\n\n /** @returns the entity's id, always tagged, or undefined if it's new/unassigned. */\n abstract get idTaggedMaybe(): TaggedId | undefined;\n\n /** The stable EM-local alias, independent of database identity and debug formatting. */\n get $clientId(): TaggedId | undefined {\n return this.__data.clientId;\n }\n\n /** @returns the entity's id, always untagged, or a runtime error if it's unassigned. */\n get idUntagged(): string {\n return deTagId(getMetadata(this), this.id);\n }\n\n abstract set(opts: unknown): void;\n\n /**\n * Similar to `set` but applies \"Partial API\" semantics, i.e. `null` means unset and `undefined` means don't change.\n */\n abstract setPartial(opts: unknown): void;\n\n /**\n * Similar to `set` but applies \"Partial API\" semantics, i.e. `null` means unset and `undefined` means don't change.\n */\n abstract setDeepPartial(opts: unknown): Promise<void>;\n\n /**\n * Returns whether the entity is new.\n *\n * This is not just `this.id === undefined`, because just assigning an id doesn't mean the entity\n * is no longer new; this only flips to `false` after the `flush` transaction has been committed.\n */\n get isNewEntity(): boolean {\n return this.__data.isNewEntity;\n }\n\n get isDeletedEntity(): boolean {\n return this.__data.isDeletedEntity;\n }\n\n get isDirtyEntity(): boolean {\n return this.__data.isDirtyEntity;\n }\n\n toString(): string {\n const meta = getMetadata(this);\n return toStringWithPrefix(meta, this, meta.type);\n }\n\n toTaggedString(): string {\n const meta = getMetadata(this);\n return toStringWithPrefix(meta, this, meta.tagName);\n }\n\n public get em(): EM {\n return this.__data.em as EM;\n }\n\n /**\n * A very simple toJSON that just returns the id.\n *\n * This is for security reasons, to avoid accidentally logging sensitive data; you\n * can specify more fields, and potentially nested return data, by using passing\n * a {@link JsonHint} to `toJSON`.\n */\n public toJSON(): object {\n return { id: this.idMaybe || null };\n }\n\n [Symbol.toStringTag](): string {\n return this.toString();\n }\n\n /**\n * Hooks into node's `console.log` to avoid sprawling output of our relations/internal state.\n *\n * See https://nodejs.org/api/util.html#custom-inspection-functions-on-objects.\n */\n [Symbol.for(\"nodejs.util.inspect.custom\")](): string {\n return this.toString();\n }\n\n /** A bag of non-persisted state, typically for flags that control business logic behavior. */\n declare readonly transientFields: Record<string, unknown>;\n}\n\nfunction toStringWithPrefix(meta: EntityMetadata, entity: Entity, prefix: string): string {\n // Start with `Author` or `a`, depending on the caller\n let result = prefix;\n // Add `#1` if this is an em.create-d entity\n const data = getInstanceData(entity);\n if (data.isOrWasNew && data.clientId) {\n result += data.clientId.slice(meta.tagName.length);\n }\n // Add the `:1` but only after our flush is complete, otherwise the id is\n // internally assigned but not really flushed/committed to the db yet\n const dbId = entity.idTaggedMaybe;\n if (data.pendingOperation !== \"insert\" && dbId) {\n // Strip the tag because we add back the entity prefix\n result += `:${keyToNumber(meta, dbId)}`;\n }\n return result;\n}\n\n/**\n * The constructor logic for `BaseEntity` but exposed so that `newEntity` can use it too\n * on its `Object.create`-d instances.\n */\nexport function baseEntityCstr(em: EntityManager, entity: BaseEntity<any, any>, isNew: boolean): void {\n const data = new InstanceData(em, entity, (entity.constructor as any).metadata, isNew);\n // This makes it non-enumerable to avoid Jest/recursive things tripping over it\n Object.defineProperty(entity, \"__data\", { value: data, enumerable: false, writable: false, configurable: false });\n // Constructors are bypassed for real entities, so initialize every needed word here,\n // in the same order for the entire inheritance family. Reusing the descriptor avoids per-entity descriptor objects.\n for (let word = 0; word < data.metadata.fieldReadWordCount; word++) {\n Object.defineProperty(entity, word === 0 ? \"__readFields\" : `__readFields${word}`, readWordDescriptor);\n }\n if (hasVisibilityHook(entity)) useTrustedReadCache(entity);\n}\n\n/** Replaces the __readFields fields with getters/setters that always return 0 for untrusted reads, so they are always access checked. */\nexport function useTrustedReadCache(entity: Entity): void {\n const data = getInstanceData(entity);\n if (data.trustedReadFields) return;\n data.trustedReadFields = [];\n for (let word = 0; word < data.metadata.fieldReadWordCount; word++) {\n const descriptor = (trustedReadWordDescriptors[word] ??= {\n configurable: true,\n enumerable: false,\n get: function (this: Entity): number {\n return isInTrustedContext() ? (getInstanceData(this).trustedReadFields![word] ?? 0) : 0;\n },\n set: function (this: Entity, value: number): void {\n if (isInTrustedContext()) getInstanceData(this).trustedReadFields![word] = value;\n },\n });\n Object.defineProperty(entity, word === 0 ? \"__readFields\" : `__readFields${word}`, descriptor);\n }\n}\n"],"mappings":";;;;;;;;;AAgBA,MAAM,qBAAyC;CAAE,OAAO;CAAG,YAAY;CAAO,UAAU;CAAM,cAAc;AAAK;AACjH,MAAM,6BAAmD,CAAC;;;;;;AAO1D,SAAgB,gBAAgB,QAA8B;CAC5D,OAAO,WAAW,gBAAgB,MAAM;AAC1C;;;;;;AAOA,IAAsB,aAAtB,MAAwG;CACtG,OAAc,gBAAgB,QAA8B;EAC1D,OAAQ,OAA2B;CACrC;CAGA;CAMA,YAAY,IAAmB,OAAiB;EAC9C,IAAI,UAAU,KAAA,GACZ,MAAM,IAAI,MAAM,8DAA8D;EAIhF,eAAe,IAAI,MAAM,KAAK;CAChC;CAKA,IAAI,wBAA8B;EAChC,OAAO;CACT;;CAeA,IAAI,YAAkC;EACpC,OAAO,KAAK,OAAO;CACrB;;CAGA,IAAI,aAAqB;E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AG,OAAO,KAAK,SAAS,oBAAoB,QAC1D,OAAO,eAAe,QAAQ,SAAS,IAAI,iBAAiB,eAAe,QAAQ,kBAAkB;CAEvG,IAAIC,sBAAAA,kBAAkB,MAAM,GAAG,oBAAoB,MAAM;AAC3D;;AAGA,SAAgB,oBAAoB,QAAsB;CACxD,MAAM,OAAO,gBAAgB,MAAM;CACnC,IAAI,KAAK,mBAAmB;CAC5B,KAAK,oBAAoB,CAAC;CAC1B,KAAK,IAAI,OAAO,GAAG,OAAO,KAAK,SAAS,oBAAoB,QAAQ;EAClE,MAAM,aAAc,2BAA2B,UAAU;GACvD,cAAc;GACd,YAAY;GACZ,KAAK,WAAgC;IACnC,OAAOC,gBAAAA,mBAAmB,IAAK,gBAAgB,IAAI,CAAC,CAAC,kBAAmB,SAAS,IAAK;GACxF;GACA,KAAK,SAAwB,OAAqB;IAChD,IAAIA,gBAAAA,mBAAmB,GAAG,gBAAgB,IAAI,CAAC,CAAC,kBAAmB,QAAQ;GAC7E;EACF;EACA,OAAO,eAAe,QAAQ,SAAS,IAAI,iBAAiB,eAAe,QAAQ,UAAU;CAC/F;AACF"}
|
package/build/BaseEntity.d.cts
CHANGED
|
@@ -29,6 +29,8 @@ export declare abstract class BaseEntity<EM extends EntityManager, I extends IdT
|
|
|
29
29
|
abstract idTagged: TaggedId;
|
|
30
30
|
/** @returns the entity's id, always tagged, or undefined if it's new/unassigned. */
|
|
31
31
|
abstract get idTaggedMaybe(): TaggedId | undefined;
|
|
32
|
+
/** The stable EM-local alias, independent of database identity and debug formatting. */
|
|
33
|
+
get $clientId(): TaggedId | undefined;
|
|
32
34
|
/** @returns the entity's id, always untagged, or a runtime error if it's unassigned. */
|
|
33
35
|
get idUntagged(): string;
|
|
34
36
|
abstract set(opts: unknown): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BaseEntity.d.cts","names":[],"sources":["../src/BaseEntity.ts"],"mappings":";;;;;;;;;wBAwBgB,gBAAgB,QAAQ,SAAS;;;;;;8BAS3B,WAAW,WAAW,eAAe,UAAU,SAAS,mBAAmB;SACjF,gBAAgB,QAAQ,SAAS;qBAK5B,QAAS;;YAEV;;EAGlB,YAAY,IAAI;MAaZ;;WAKK,IAAI;;eAGA,WAAW;;WAGf,UAAU;;eAGN,iBAAiB;;MAG1B;WAIK,IAAI;;;;WAKJ,WAAW;;;;WAKX,eAAe,gBAAgB;;;;;;;MAQpC;MAIA;MAIA;EAIJ;EAKA;MAKW,MAAM;;;;;;;;EAWV;GAIN,OAAO;;WAcS,iBAAiB;;;;;;
|
|
1
|
+
{"version":3,"file":"BaseEntity.d.cts","names":[],"sources":["../src/BaseEntity.ts"],"mappings":";;;;;;;;;wBAwBgB,gBAAgB,QAAQ,SAAS;;;;;;8BAS3B,WAAW,WAAW,eAAe,UAAU,SAAS,mBAAmB;SACjF,gBAAgB,QAAQ,SAAS;qBAK5B,QAAS;;YAEV;;EAGlB,YAAY,IAAI;MAaZ;;WAKK,IAAI;;eAGA,WAAW;;WAGf,UAAU;;eAGN,iBAAiB;;MAG1B,aAAa;;MAKb;WAIK,IAAI;;;;WAKJ,WAAW;;;;WAKX,eAAe,gBAAgB;;;;;;;MAQpC;MAIA;MAIA;EAIJ;EAKA;MAKW,MAAM;;;;;;;;EAWV;GAIN,OAAO;;WAcS,iBAAiB;;;;;;wBAyBpB,eAAe,IAAI,eAAe,QAAQ,sBAAsB;;wBAahE,oBAAoB,QAAQ"}
|
package/build/BaseEntity.d.mts
CHANGED
|
@@ -29,6 +29,8 @@ export declare abstract class BaseEntity<EM extends EntityManager, I extends IdT
|
|
|
29
29
|
abstract idTagged: TaggedId;
|
|
30
30
|
/** @returns the entity's id, always tagged, or undefined if it's new/unassigned. */
|
|
31
31
|
abstract get idTaggedMaybe(): TaggedId | undefined;
|
|
32
|
+
/** The stable EM-local alias, independent of database identity and debug formatting. */
|
|
33
|
+
get $clientId(): TaggedId | undefined;
|
|
32
34
|
/** @returns the entity's id, always untagged, or a runtime error if it's unassigned. */
|
|
33
35
|
get idUntagged(): string;
|
|
34
36
|
abstract set(opts: unknown): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BaseEntity.d.mts","names":[],"sources":["../src/BaseEntity.ts"],"mappings":";;;;;;;;;wBAwBgB,gBAAgB,QAAQ,SAAS;;;;;;8BAS3B,WAAW,WAAW,eAAe,UAAU,SAAS,mBAAmB;SACjF,gBAAgB,QAAQ,SAAS;qBAK5B,QAAS;;YAEV;;EAGlB,YAAY,IAAI;MAaZ;;WAKK,IAAI;;eAGA,WAAW;;WAGf,UAAU;;eAGN,iBAAiB;;MAG1B;WAIK,IAAI;;;;WAKJ,WAAW;;;;WAKX,eAAe,gBAAgB;;;;;;;MAQpC;MAIA;MAIA;EAIJ;EAKA;MAKW,MAAM;;;;;;;;EAWV;GAIN,OAAO;;WAcS,iBAAiB;;;;;;
|
|
1
|
+
{"version":3,"file":"BaseEntity.d.mts","names":[],"sources":["../src/BaseEntity.ts"],"mappings":";;;;;;;;;wBAwBgB,gBAAgB,QAAQ,SAAS;;;;;;8BAS3B,WAAW,WAAW,eAAe,UAAU,SAAS,mBAAmB;SACjF,gBAAgB,QAAQ,SAAS;qBAK5B,QAAS;;YAEV;;EAGlB,YAAY,IAAI;MAaZ;;WAKK,IAAI;;eAGA,WAAW;;WAGf,UAAU;;eAGN,iBAAiB;;MAG1B,aAAa;;MAKb;WAIK,IAAI;;;;WAKJ,WAAW;;;;WAKX,eAAe,gBAAgB;;;;;;;MAQpC;MAIA;MAIA;EAIJ;EAKA;MAKW,MAAM;;;;;;;;EAWV;GAIN,OAAO;;WAcS,iBAAiB;;;;;;wBAyBpB,eAAe,IAAI,eAAe,QAAQ,sBAAsB;;wBAahE,oBAAoB,QAAQ"}
|
package/build/BaseEntity.js
CHANGED
|
@@ -38,6 +38,10 @@ var BaseEntity = class {
|
|
|
38
38
|
get fullNonReactiveAccess() {
|
|
39
39
|
return this;
|
|
40
40
|
}
|
|
41
|
+
/** The stable EM-local alias, independent of database identity and debug formatting. */
|
|
42
|
+
get $clientId() {
|
|
43
|
+
return this.__data.clientId;
|
|
44
|
+
}
|
|
41
45
|
/** @returns the entity's id, always untagged, or a runtime error if it's unassigned. */
|
|
42
46
|
get idUntagged() {
|
|
43
47
|
return deTagId(getMetadata(this), this.id);
|
|
@@ -93,8 +97,7 @@ var BaseEntity = class {
|
|
|
93
97
|
function toStringWithPrefix(meta, entity, prefix) {
|
|
94
98
|
let result = prefix;
|
|
95
99
|
const data = getInstanceData(entity);
|
|
96
|
-
|
|
97
|
-
if (createId) result += `#${createId}`;
|
|
100
|
+
if (data.isOrWasNew && data.clientId) result += data.clientId.slice(meta.tagName.length);
|
|
98
101
|
const dbId = entity.idTaggedMaybe;
|
|
99
102
|
if (data.pendingOperation !== "insert" && dbId) result += `:${keyToNumber(meta, dbId)}`;
|
|
100
103
|
return result;
|
package/build/BaseEntity.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BaseEntity.js","names":[],"sources":["../src/BaseEntity.ts"],"sourcesContent":["import { type IdType } from \"src/Entity.ts\";\nimport { hasVisibilityHook } from \"src/EntityManager.ts\";\nimport {\n type Entity,\n type EntityManager,\n type EntityMetadata,\n InstanceData,\n type TaggedId,\n deTagId,\n getMetadata,\n keyToNumber,\n} from \"src/index.ts\";\nimport { isInTrustedContext } from \"src/trusted.ts\";\n\n// getProperties deletes its throwaway entity's own fields so callback access to `this` hits a guard proxy.\n// That includes these non-enumerable words, so they must be configurable (deletable).\nconst readWordDescriptor: PropertyDescriptor = { value: 0, enumerable: false, writable: true, configurable: true };\nconst trustedReadWordDescriptors: PropertyDescriptor[] = [];\n\n/**\n * Returns the internal `__data` tracking field for `entity`.\n *\n * This should be treated as an internal API and may change without notice.\n */\nexport function getInstanceData(entity: Entity): InstanceData {\n return BaseEntity.getInstanceData(entity);\n}\n\n/**\n * The base class for all entities.\n *\n * Currently, this just adds the `.load(lensFn)` method for declarative reference traversal.\n */\nexport abstract class BaseEntity<EM extends EntityManager, I extends IdType = IdType> implements Entity {\n public static getInstanceData(entity: Entity): InstanceData {\n return (entity as BaseEntity<any>).__data;\n }\n\n // We use a protected field so that subclass getters can easily access `this.__data.relations`.\n protected readonly __data!: InstanceData;\n /** First-read completion bits; extra words are declared by each generated root type as needed. */\n declare protected __readFields: number;\n\n /** The single-arg constructor is our psuedo-public API that can only be called by the EntityManager. */\n constructor(em: EntityManager);\n constructor(em: EntityManager, isNew?: boolean) {\n if (isNew === undefined) {\n throw new Error(\"Entities must be constructed by calling em.create or em.load\");\n }\n // This code path won't be reused for real entities, as they go through the `newEntity`\n // construction process that lazifies the relations.\n baseEntityCstr(em, this, isNew);\n }\n\n // This gives rules a way to access the fully typed object instead of their Reacted view.\n // And we make it public so that a function that takes Reacted<...> can accept a Loaded<...>\n // that sufficiently overlaps.\n get fullNonReactiveAccess(): this {\n return this;\n }\n\n /** @returns the entity's id, tagged/untagged based on your config, or a runtime error if it's new/unassigned. */\n abstract id: I;\n\n /** @returns the entity's, tagged/untagged based on your config, or undefined if it's new/unassigned. */\n abstract get idMaybe(): I | undefined;\n\n /** @returns the entity's id, always tagged, or a runtime error if it's new/unassigned. */\n abstract idTagged: TaggedId;\n\n /** @returns the entity's id, always tagged, or undefined if it's new/unassigned. */\n abstract get idTaggedMaybe(): TaggedId | undefined;\n\n /** @returns the entity's id, always untagged, or a runtime error if it's unassigned. */\n get idUntagged(): string {\n return deTagId(getMetadata(this), this.id);\n }\n\n abstract set(opts: unknown): void;\n\n /**\n * Similar to `set` but applies \"Partial API\" semantics, i.e. `null` means unset and `undefined` means don't change.\n */\n abstract setPartial(opts: unknown): void;\n\n /**\n * Similar to `set` but applies \"Partial API\" semantics, i.e. `null` means unset and `undefined` means don't change.\n */\n abstract setDeepPartial(opts: unknown): Promise<void>;\n\n /**\n * Returns whether the entity is new.\n *\n * This is not just `this.id === undefined`, because just assigning an id doesn't mean the entity\n * is no longer new; this only flips to `false` after the `flush` transaction has been committed.\n */\n get isNewEntity(): boolean {\n return this.__data.isNewEntity;\n }\n\n get isDeletedEntity(): boolean {\n return this.__data.isDeletedEntity;\n }\n\n get isDirtyEntity(): boolean {\n return this.__data.isDirtyEntity;\n }\n\n toString(): string {\n const meta = getMetadata(this);\n return toStringWithPrefix(meta, this, meta.type);\n }\n\n toTaggedString(): string {\n const meta = getMetadata(this);\n return toStringWithPrefix(meta, this, meta.tagName);\n }\n\n public get em(): EM {\n return this.__data.em as EM;\n }\n\n /**\n * A very simple toJSON that just returns the id.\n *\n * This is for security reasons, to avoid accidentally logging sensitive data; you\n * can specify more fields, and potentially nested return data, by using passing\n * a {@link JsonHint} to `toJSON`.\n */\n public toJSON(): object {\n return { id: this.idMaybe || null };\n }\n\n [Symbol.toStringTag](): string {\n return this.toString();\n }\n\n /**\n * Hooks into node's `console.log` to avoid sprawling output of our relations/internal state.\n *\n * See https://nodejs.org/api/util.html#custom-inspection-functions-on-objects.\n */\n [Symbol.for(\"nodejs.util.inspect.custom\")](): string {\n return this.toString();\n }\n\n /** A bag of non-persisted state, typically for flags that control business logic behavior. */\n declare readonly transientFields: Record<string, unknown>;\n}\n\nfunction toStringWithPrefix(meta: EntityMetadata, entity: Entity, prefix: string): string {\n // Start with `Author` or `a`, depending on the caller\n let result = prefix;\n // Add `#1` if this is an em.create-d entity\n const data = getInstanceData(entity);\n const createId = data.createId;\n if (createId) {\n result += `#${createId}`;\n }\n // Add the `:1` but only after our flush is complete, otherwise the id is\n // internally assigned but not really flushed/committed to the db yet\n const dbId = entity.idTaggedMaybe;\n if (data.pendingOperation !== \"insert\" && dbId) {\n // Strip the tag because we add back the entity prefix\n result += `:${keyToNumber(meta, dbId)}`;\n }\n return result;\n}\n\n/**\n * The constructor logic for `BaseEntity` but exposed so that `newEntity` can use it too\n * on its `Object.create`-d instances.\n */\nexport function baseEntityCstr(em: EntityManager, entity: BaseEntity<any, any>, isNew: boolean): void {\n const data = new InstanceData(em, entity, (entity.constructor as any).metadata, isNew);\n // This makes it non-enumerable to avoid Jest/recursive things tripping over it\n Object.defineProperty(entity, \"__data\", { value: data, enumerable: false, writable: false, configurable: false });\n // Constructors are bypassed for real entities, so initialize every needed word here,\n // in the same order for the entire inheritance family. Reusing the descriptor avoids per-entity descriptor objects.\n for (let word = 0; word < data.metadata.fieldReadWordCount; word++) {\n Object.defineProperty(entity, word === 0 ? \"__readFields\" : `__readFields${word}`, readWordDescriptor);\n }\n if (hasVisibilityHook(entity)) useTrustedReadCache(entity);\n}\n\n/** Replaces the __readFields fields with getters/setters that always return 0 for untrusted reads, so they are always access checked. */\nexport function useTrustedReadCache(entity: Entity): void {\n const data = getInstanceData(entity);\n if (data.trustedReadFields) return;\n data.trustedReadFields = [];\n for (let word = 0; word < data.metadata.fieldReadWordCount; word++) {\n const descriptor = (trustedReadWordDescriptors[word] ??= {\n configurable: true,\n enumerable: false,\n get: function (this: Entity): number {\n return isInTrustedContext() ? (getInstanceData(this).trustedReadFields![word] ?? 0) : 0;\n },\n set: function (this: Entity, value: number): void {\n if (isInTrustedContext()) getInstanceData(this).trustedReadFields![word] = value;\n },\n });\n Object.defineProperty(entity, word === 0 ? \"__readFields\" : `__readFields${word}`, descriptor);\n }\n}\n"],"mappings":";;;;;;;;AAgBA,MAAM,qBAAyC;CAAE,OAAO;CAAG,YAAY;CAAO,UAAU;CAAM,cAAc;AAAK;AACjH,MAAM,6BAAmD,CAAC;;;;;;AAO1D,SAAgB,gBAAgB,QAA8B;CAC5D,OAAO,WAAW,gBAAgB,MAAM;AAC1C;;;;;;AAOA,IAAsB,aAAtB,MAAwG;CACtG,OAAc,gBAAgB,QAA8B;EAC1D,OAAQ,OAA2B;CACrC;CAGA;CAMA,YAAY,IAAmB,OAAiB;EAC9C,IAAI,UAAU,KAAA,GACZ,MAAM,IAAI,MAAM,8DAA8D;EAIhF,eAAe,IAAI,MAAM,KAAK;CAChC;CAKA,IAAI,wBAA8B;EAChC,OAAO;CACT;;CAeA,IAAI,aAAqB;EACvB,OAAO,QA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AkB;CAEvG,IAAI,kBAAkB,MAAM,GAAG,oBAAoB,MAAM;AAC3D;;AAGA,SAAgB,oBAAoB,QAAsB;CACxD,MAAM,OAAO,gBAAgB,MAAM;CACnC,IAAI,KAAK,mBAAmB;CAC5B,KAAK,oBAAoB,CAAC;CAC1B,KAAK,IAAI,OAAO,GAAG,OAAO,KAAK,SAAS,oBAAoB,QAAQ;EAClE,MAAM,aAAc,2BAA2B,UAAU;GACvD,cAAc;GACd,YAAY;GACZ,KAAK,WAAgC;IACnC,OAAO,mBAAmB,IAAK,gBAAgB,IAAI,CAAC,CAAC,kBAAmB,SAAS,IAAK;GACxF;GACA,KAAK,SAAwB,OAAqB;IAChD,IAAI,mBAAmB,GAAG,gBAAgB,IAAI,CAAC,CAAC,kBAAmB,QAAQ;GAC7E;EACF;EACA,OAAO,eAAe,QAAQ,SAAS,IAAI,iBAAiB,eAAe,QAAQ,UAAU;CAC/F;AACF"}
|
|
1
|
+
{"version":3,"file":"BaseEntity.js","names":[],"sources":["../src/BaseEntity.ts"],"sourcesContent":["import { type IdType } from \"src/Entity.ts\";\nimport { hasVisibilityHook } from \"src/EntityManager.ts\";\nimport {\n type Entity,\n type EntityManager,\n type EntityMetadata,\n InstanceData,\n type TaggedId,\n deTagId,\n getMetadata,\n keyToNumber,\n} from \"src/index.ts\";\nimport { isInTrustedContext } from \"src/trusted.ts\";\n\n// getProperties deletes its throwaway entity's own fields so callback access to `this` hits a guard proxy.\n// That includes these non-enumerable words, so they must be configurable (deletable).\nconst readWordDescriptor: PropertyDescriptor = { value: 0, enumerable: false, writable: true, configurable: true };\nconst trustedReadWordDescriptors: PropertyDescriptor[] = [];\n\n/**\n * Returns the internal `__data` tracking field for `entity`.\n *\n * This should be treated as an internal API and may change without notice.\n */\nexport function getInstanceData(entity: Entity): InstanceData {\n return BaseEntity.getInstanceData(entity);\n}\n\n/**\n * The base class for all entities.\n *\n * Currently, this just adds the `.load(lensFn)` method for declarative reference traversal.\n */\nexport abstract class BaseEntity<EM extends EntityManager, I extends IdType = IdType> implements Entity {\n public static getInstanceData(entity: Entity): InstanceData {\n return (entity as BaseEntity<any>).__data;\n }\n\n // We use a protected field so that subclass getters can easily access `this.__data.relations`.\n protected readonly __data!: InstanceData;\n /** First-read completion bits; extra words are declared by each generated root type as needed. */\n declare protected __readFields: number;\n\n /** The single-arg constructor is our psuedo-public API that can only be called by the EntityManager. */\n constructor(em: EntityManager);\n constructor(em: EntityManager, isNew?: boolean) {\n if (isNew === undefined) {\n throw new Error(\"Entities must be constructed by calling em.create or em.load\");\n }\n // This code path won't be reused for real entities, as they go through the `newEntity`\n // construction process that lazifies the relations.\n baseEntityCstr(em, this, isNew);\n }\n\n // This gives rules a way to access the fully typed object instead of their Reacted view.\n // And we make it public so that a function that takes Reacted<...> can accept a Loaded<...>\n // that sufficiently overlaps.\n get fullNonReactiveAccess(): this {\n return this;\n }\n\n /** @returns the entity's id, tagged/untagged based on your config, or a runtime error if it's new/unassigned. */\n abstract id: I;\n\n /** @returns the entity's, tagged/untagged based on your config, or undefined if it's new/unassigned. */\n abstract get idMaybe(): I | undefined;\n\n /** @returns the entity's id, always tagged, or a runtime error if it's new/unassigned. */\n abstract idTagged: TaggedId;\n\n /** @returns the entity's id, always tagged, or undefined if it's new/unassigned. */\n abstract get idTaggedMaybe(): TaggedId | undefined;\n\n /** The stable EM-local alias, independent of database identity and debug formatting. */\n get $clientId(): TaggedId | undefined {\n return this.__data.clientId;\n }\n\n /** @returns the entity's id, always untagged, or a runtime error if it's unassigned. */\n get idUntagged(): string {\n return deTagId(getMetadata(this), this.id);\n }\n\n abstract set(opts: unknown): void;\n\n /**\n * Similar to `set` but applies \"Partial API\" semantics, i.e. `null` means unset and `undefined` means don't change.\n */\n abstract setPartial(opts: unknown): void;\n\n /**\n * Similar to `set` but applies \"Partial API\" semantics, i.e. `null` means unset and `undefined` means don't change.\n */\n abstract setDeepPartial(opts: unknown): Promise<void>;\n\n /**\n * Returns whether the entity is new.\n *\n * This is not just `this.id === undefined`, because just assigning an id doesn't mean the entity\n * is no longer new; this only flips to `false` after the `flush` transaction has been committed.\n */\n get isNewEntity(): boolean {\n return this.__data.isNewEntity;\n }\n\n get isDeletedEntity(): boolean {\n return this.__data.isDeletedEntity;\n }\n\n get isDirtyEntity(): boolean {\n return this.__data.isDirtyEntity;\n }\n\n toString(): string {\n const meta = getMetadata(this);\n return toStringWithPrefix(meta, this, meta.type);\n }\n\n toTaggedString(): string {\n const meta = getMetadata(this);\n return toStringWithPrefix(meta, this, meta.tagName);\n }\n\n public get em(): EM {\n return this.__data.em as EM;\n }\n\n /**\n * A very simple toJSON that just returns the id.\n *\n * This is for security reasons, to avoid accidentally logging sensitive data; you\n * can specify more fields, and potentially nested return data, by using passing\n * a {@link JsonHint} to `toJSON`.\n */\n public toJSON(): object {\n return { id: this.idMaybe || null };\n }\n\n [Symbol.toStringTag](): string {\n return this.toString();\n }\n\n /**\n * Hooks into node's `console.log` to avoid sprawling output of our relations/internal state.\n *\n * See https://nodejs.org/api/util.html#custom-inspection-functions-on-objects.\n */\n [Symbol.for(\"nodejs.util.inspect.custom\")](): string {\n return this.toString();\n }\n\n /** A bag of non-persisted state, typically for flags that control business logic behavior. */\n declare readonly transientFields: Record<string, unknown>;\n}\n\nfunction toStringWithPrefix(meta: EntityMetadata, entity: Entity, prefix: string): string {\n // Start with `Author` or `a`, depending on the caller\n let result = prefix;\n // Add `#1` if this is an em.create-d entity\n const data = getInstanceData(entity);\n if (data.isOrWasNew && data.clientId) {\n result += data.clientId.slice(meta.tagName.length);\n }\n // Add the `:1` but only after our flush is complete, otherwise the id is\n // internally assigned but not really flushed/committed to the db yet\n const dbId = entity.idTaggedMaybe;\n if (data.pendingOperation !== \"insert\" && dbId) {\n // Strip the tag because we add back the entity prefix\n result += `:${keyToNumber(meta, dbId)}`;\n }\n return result;\n}\n\n/**\n * The constructor logic for `BaseEntity` but exposed so that `newEntity` can use it too\n * on its `Object.create`-d instances.\n */\nexport function baseEntityCstr(em: EntityManager, entity: BaseEntity<any, any>, isNew: boolean): void {\n const data = new InstanceData(em, entity, (entity.constructor as any).metadata, isNew);\n // This makes it non-enumerable to avoid Jest/recursive things tripping over it\n Object.defineProperty(entity, \"__data\", { value: data, enumerable: false, writable: false, configurable: false });\n // Constructors are bypassed for real entities, so initialize every needed word here,\n // in the same order for the entire inheritance family. Reusing the descriptor avoids per-entity descriptor objects.\n for (let word = 0; word < data.metadata.fieldReadWordCount; word++) {\n Object.defineProperty(entity, word === 0 ? \"__readFields\" : `__readFields${word}`, readWordDescriptor);\n }\n if (hasVisibilityHook(entity)) useTrustedReadCache(entity);\n}\n\n/** Replaces the __readFields fields with getters/setters that always return 0 for untrusted reads, so they are always access checked. */\nexport function useTrustedReadCache(entity: Entity): void {\n const data = getInstanceData(entity);\n if (data.trustedReadFields) return;\n data.trustedReadFields = [];\n for (let word = 0; word < data.metadata.fieldReadWordCount; word++) {\n const descriptor = (trustedReadWordDescriptors[word] ??= {\n configurable: true,\n enumerable: false,\n get: function (this: Entity): number {\n return isInTrustedContext() ? (getInstanceData(this).trustedReadFields![word] ?? 0) : 0;\n },\n set: function (this: Entity, value: number): void {\n if (isInTrustedContext()) getInstanceData(this).trustedReadFields![word] = value;\n },\n });\n Object.defineProperty(entity, word === 0 ? \"__readFields\" : `__readFields${word}`, descriptor);\n }\n}\n"],"mappings":";;;;;;;;AAgBA,MAAM,qBAAyC;CAAE,OAAO;CAAG,YAAY;CAAO,UAAU;CAAM,cAAc;AAAK;AACjH,MAAM,6BAAmD,CAAC;;;;;;AAO1D,SAAgB,gBAAgB,QAA8B;CAC5D,OAAO,WAAW,gBAAgB,MAAM;AAC1C;;;;;;AAOA,IAAsB,aAAtB,MAAwG;CACtG,OAAc,gBAAgB,QAA8B;EAC1D,OAAQ,OAA2B;CACrC;CAGA;CAMA,YAAY,IAAmB,OAAiB;EAC9C,IAAI,UAAU,KAAA,GACZ,MAAM,IAAI,MAAM,8DAA8D;EAIhF,eAAe,IAAI,MAAM,KAAK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AAO,UAAU;EAAO,cAAc;CAAM,CAAC;CAGhH,KAAK,IAAI,OAAO,GAAG,OAAO,KAAK,SAAS,oBAAoB,QAC1D,OAAO,eAAe,QAAQ,SAAS,IAAI,iBAAiB,eAAe,QAAQ,kBAAkB;CAEvG,IAAI,kBAAkB,MAAM,GAAG,oBAAoB,MAAM;AAC3D;;AAGA,SAAgB,oBAAoB,QAAsB;CACxD,MAAM,OAAO,gBAAgB,MAAM;CACnC,IAAI,KAAK,mBAAmB;CAC5B,KAAK,oBAAoB,CAAC;CAC1B,KAAK,IAAI,OAAO,GAAG,OAAO,KAAK,SAAS,oBAAoB,QAAQ;EAClE,MAAM,aAAc,2BAA2B,UAAU;GACvD,cAAc;GACd,YAAY;GACZ,KAAK,WAAgC;IACnC,OAAO,mBAAmB,IAAK,gBAAgB,IAAI,CAAC,CAAC,kBAAmB,SAAS,IAAK;GACxF;GACA,KAAK,SAAwB,OAAqB;IAChD,IAAI,mBAAmB,GAAG,gBAAgB,IAAI,CAAC,CAAC,kBAAmB,QAAQ;GAC7E;EACF;EACA,OAAO,eAAe,QAAQ,SAAS,IAAI,iBAAiB,eAAe,QAAQ,UAAU;CAC/F;AACF"}
|
package/build/Entity.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Entity.cjs","names":["BaseEntity"],"sources":["../src/Entity.ts"],"sourcesContent":["import { type EntityManager, type TaggedId } from \"src/EntityManager.ts\";\nimport { BaseEntity } from \"src/index.ts\";\n\nexport function isEntity(maybeEntity: unknown): maybeEntity is Entity {\n return maybeEntity instanceof BaseEntity;\n}\n\n/** All the types we support for entity `id` fields. */\nexport type IdType = number | string;\n\n/** A marker/base interface for all of our entity types. */\nexport interface Entity {\n /** The entity id, either tagged or untagged; the per-application `Entity` will specialize appropriately. */\n id: IdType;\n idMaybe: IdType | undefined;\n /** The entity id that is always tagged, regardless of the idType config. */\n idTagged: TaggedId;\n idTaggedMaybe: TaggedId | undefined;\n readonly em: EntityManager;\n readonly isNewEntity: boolean;\n readonly isDeletedEntity: boolean;\n readonly isDirtyEntity: boolean;\n readonly transientFields: Record<string, unknown>;\n set(opts: unknown): void;\n setPartial(opts: unknown): void;\n setDeepPartial(opts: unknown): Promise<void>;\n /**\n * Returns `type:id`, i.e. `Author:1` for persisted entities and `Author#1` for new entities.\n *\n * Once an entity is persisted, we'll return `Author#1:1` to denote \"the previously-new author #1\n * is now authors row id=1 in the database\".\n *\n * This is meant to be used for developer-facing logging and debugging, and not a user-facing\n * name / display name.\n */\n toString(): string;\n /**\n * Returns `tag:id`, i.e. `a:1` for persisted entities and `a#1` for new entities.\n *\n * This is meant to be used for developer-facing logging and debugging, and not a user-facing\n * name / display name.\n */\n toTaggedString(): string;\n}\n"],"mappings":";;;;;AAGA,SAAgB,SAAS,aAA6C;CACpE,OAAO,uBAAuBA,mBAAAA;AAChC"}
|
|
1
|
+
{"version":3,"file":"Entity.cjs","names":["BaseEntity"],"sources":["../src/Entity.ts"],"sourcesContent":["import { type EntityManager, type TaggedId } from \"src/EntityManager.ts\";\nimport { BaseEntity } from \"src/index.ts\";\n\nexport function isEntity(maybeEntity: unknown): maybeEntity is Entity {\n return maybeEntity instanceof BaseEntity;\n}\n\n/** All the types we support for entity `id` fields. */\nexport type IdType = number | string;\n\n/** A marker/base interface for all of our entity types. */\nexport interface Entity {\n /** The entity id, either tagged or untagged; the per-application `Entity` will specialize appropriately. */\n id: IdType;\n idMaybe: IdType | undefined;\n /** The entity id that is always tagged, regardless of the idType config. */\n idTagged: TaggedId;\n idTaggedMaybe: TaggedId | undefined;\n /**\n * An immutable EM-local alias, retained after database ID assignment and flush.\n *\n * Created entities always have one. This is undefined for entities loaded from the database.\n */\n readonly $clientId: TaggedId | undefined;\n readonly em: EntityManager;\n readonly isNewEntity: boolean;\n readonly isDeletedEntity: boolean;\n readonly isDirtyEntity: boolean;\n readonly transientFields: Record<string, unknown>;\n set(opts: unknown): void;\n setPartial(opts: unknown): void;\n setDeepPartial(opts: unknown): Promise<void>;\n /**\n * Returns `type:id`, i.e. `Author:1` for persisted entities and `Author#1` for new entities.\n *\n * Once an entity is persisted, we'll return `Author#1:1` to denote \"the previously-new author #1\n * is now authors row id=1 in the database\".\n *\n * This is meant to be used for developer-facing logging and debugging, and not a user-facing\n * name / display name.\n */\n toString(): string;\n /**\n * Returns `tag:id`, i.e. `a:1` for persisted entities and `a#1` for new entities.\n *\n * This is meant to be used for developer-facing logging and debugging, and not a user-facing\n * name / display name.\n */\n toTaggedString(): string;\n}\n"],"mappings":";;;;;AAGA,SAAgB,SAAS,aAA6C;CACpE,OAAO,uBAAuBA,mBAAAA;AAChC"}
|
package/build/Entity.d.cts
CHANGED
|
@@ -10,6 +10,12 @@ export interface Entity {
|
|
|
10
10
|
/** The entity id that is always tagged, regardless of the idType config. */
|
|
11
11
|
idTagged: TaggedId;
|
|
12
12
|
idTaggedMaybe: TaggedId | undefined;
|
|
13
|
+
/**
|
|
14
|
+
* An immutable EM-local alias, retained after database ID assignment and flush.
|
|
15
|
+
*
|
|
16
|
+
* Created entities always have one. This is undefined for entities loaded from the database.
|
|
17
|
+
*/
|
|
18
|
+
readonly $clientId: TaggedId | undefined;
|
|
13
19
|
readonly em: EntityManager;
|
|
14
20
|
readonly isNewEntity: boolean;
|
|
15
21
|
readonly isDeletedEntity: boolean;
|
package/build/Entity.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Entity.d.cts","names":[],"sources":["../src/Entity.ts"],"mappings":";wBAGgB,SAAS,uBAAuB,eAAe;;YAKnD;;iBAGK;;EAEf,IAAI;EACJ,SAAS;;EAET,UAAU;EACV,eAAe;
|
|
1
|
+
{"version":3,"file":"Entity.d.cts","names":[],"sources":["../src/Entity.ts"],"mappings":";wBAGgB,SAAS,uBAAuB,eAAe;;YAKnD;;iBAGK;;EAEf,IAAI;EACJ,SAAS;;EAET,UAAU;EACV,eAAe;;;;;;WAMN,WAAW;WACX,IAAI;WACJ;WACA;WACA;WACA,iBAAiB;EAC1B,IAAI;EACJ,WAAW;EACX,eAAe,gBAAgB;;;;;;;;;;EAU/B;;;;;;;EAOA"}
|
package/build/Entity.d.mts
CHANGED
|
@@ -10,6 +10,12 @@ export interface Entity {
|
|
|
10
10
|
/** The entity id that is always tagged, regardless of the idType config. */
|
|
11
11
|
idTagged: TaggedId;
|
|
12
12
|
idTaggedMaybe: TaggedId | undefined;
|
|
13
|
+
/**
|
|
14
|
+
* An immutable EM-local alias, retained after database ID assignment and flush.
|
|
15
|
+
*
|
|
16
|
+
* Created entities always have one. This is undefined for entities loaded from the database.
|
|
17
|
+
*/
|
|
18
|
+
readonly $clientId: TaggedId | undefined;
|
|
13
19
|
readonly em: EntityManager;
|
|
14
20
|
readonly isNewEntity: boolean;
|
|
15
21
|
readonly isDeletedEntity: boolean;
|
package/build/Entity.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Entity.d.mts","names":[],"sources":["../src/Entity.ts"],"mappings":";wBAGgB,SAAS,uBAAuB,eAAe;;YAKnD;;iBAGK;;EAEf,IAAI;EACJ,SAAS;;EAET,UAAU;EACV,eAAe;
|
|
1
|
+
{"version":3,"file":"Entity.d.mts","names":[],"sources":["../src/Entity.ts"],"mappings":";wBAGgB,SAAS,uBAAuB,eAAe;;YAKnD;;iBAGK;;EAEf,IAAI;EACJ,SAAS;;EAET,UAAU;EACV,eAAe;;;;;;WAMN,WAAW;WACX,IAAI;WACJ;WACA;WACA;WACA,iBAAiB;EAC1B,IAAI;EACJ,WAAW;EACX,eAAe,gBAAgB;;;;;;;;;;EAU/B;;;;;;;EAOA"}
|
package/build/Entity.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Entity.js","names":[],"sources":["../src/Entity.ts"],"sourcesContent":["import { type EntityManager, type TaggedId } from \"src/EntityManager.ts\";\nimport { BaseEntity } from \"src/index.ts\";\n\nexport function isEntity(maybeEntity: unknown): maybeEntity is Entity {\n return maybeEntity instanceof BaseEntity;\n}\n\n/** All the types we support for entity `id` fields. */\nexport type IdType = number | string;\n\n/** A marker/base interface for all of our entity types. */\nexport interface Entity {\n /** The entity id, either tagged or untagged; the per-application `Entity` will specialize appropriately. */\n id: IdType;\n idMaybe: IdType | undefined;\n /** The entity id that is always tagged, regardless of the idType config. */\n idTagged: TaggedId;\n idTaggedMaybe: TaggedId | undefined;\n readonly em: EntityManager;\n readonly isNewEntity: boolean;\n readonly isDeletedEntity: boolean;\n readonly isDirtyEntity: boolean;\n readonly transientFields: Record<string, unknown>;\n set(opts: unknown): void;\n setPartial(opts: unknown): void;\n setDeepPartial(opts: unknown): Promise<void>;\n /**\n * Returns `type:id`, i.e. `Author:1` for persisted entities and `Author#1` for new entities.\n *\n * Once an entity is persisted, we'll return `Author#1:1` to denote \"the previously-new author #1\n * is now authors row id=1 in the database\".\n *\n * This is meant to be used for developer-facing logging and debugging, and not a user-facing\n * name / display name.\n */\n toString(): string;\n /**\n * Returns `tag:id`, i.e. `a:1` for persisted entities and `a#1` for new entities.\n *\n * This is meant to be used for developer-facing logging and debugging, and not a user-facing\n * name / display name.\n */\n toTaggedString(): string;\n}\n"],"mappings":";;;;AAGA,SAAgB,SAAS,aAA6C;CACpE,OAAO,uBAAuB;AAChC"}
|
|
1
|
+
{"version":3,"file":"Entity.js","names":[],"sources":["../src/Entity.ts"],"sourcesContent":["import { type EntityManager, type TaggedId } from \"src/EntityManager.ts\";\nimport { BaseEntity } from \"src/index.ts\";\n\nexport function isEntity(maybeEntity: unknown): maybeEntity is Entity {\n return maybeEntity instanceof BaseEntity;\n}\n\n/** All the types we support for entity `id` fields. */\nexport type IdType = number | string;\n\n/** A marker/base interface for all of our entity types. */\nexport interface Entity {\n /** The entity id, either tagged or untagged; the per-application `Entity` will specialize appropriately. */\n id: IdType;\n idMaybe: IdType | undefined;\n /** The entity id that is always tagged, regardless of the idType config. */\n idTagged: TaggedId;\n idTaggedMaybe: TaggedId | undefined;\n /**\n * An immutable EM-local alias, retained after database ID assignment and flush.\n *\n * Created entities always have one. This is undefined for entities loaded from the database.\n */\n readonly $clientId: TaggedId | undefined;\n readonly em: EntityManager;\n readonly isNewEntity: boolean;\n readonly isDeletedEntity: boolean;\n readonly isDirtyEntity: boolean;\n readonly transientFields: Record<string, unknown>;\n set(opts: unknown): void;\n setPartial(opts: unknown): void;\n setDeepPartial(opts: unknown): Promise<void>;\n /**\n * Returns `type:id`, i.e. `Author:1` for persisted entities and `Author#1` for new entities.\n *\n * Once an entity is persisted, we'll return `Author#1:1` to denote \"the previously-new author #1\n * is now authors row id=1 in the database\".\n *\n * This is meant to be used for developer-facing logging and debugging, and not a user-facing\n * name / display name.\n */\n toString(): string;\n /**\n * Returns `tag:id`, i.e. `a:1` for persisted entities and `a#1` for new entities.\n *\n * This is meant to be used for developer-facing logging and debugging, and not a user-facing\n * name / display name.\n */\n toTaggedString(): string;\n}\n"],"mappings":";;;;AAGA,SAAgB,SAAS,aAA6C;CACpE,OAAO,uBAAuB;AAChC"}
|
package/build/EntityManager.cjs
CHANGED
|
@@ -4,17 +4,18 @@ const require_BaseEntity = require("./BaseEntity.cjs");
|
|
|
4
4
|
const require_batchloaders_BatchLoader = require("./batchloaders/BatchLoader.cjs");
|
|
5
5
|
require("./batchloaders/enumCollectionBatchLoader.cjs");
|
|
6
6
|
const require_batchloaders_loadBatchLoader = require("./batchloaders/loadBatchLoader.cjs");
|
|
7
|
+
require("./batchloaders/manyToManyBatchLoader.cjs");
|
|
7
8
|
const require_relations_CustomCollection = require("./relations/CustomCollection.cjs");
|
|
8
9
|
const require_relations_CustomReference = require("./relations/CustomReference.cjs");
|
|
9
10
|
const require_normalizeHints = require("./normalizeHints.cjs");
|
|
10
|
-
require("./batchloaders/manyToManyBatchLoader.cjs");
|
|
11
11
|
require("./batchloaders/oneToManyBatchLoader.cjs");
|
|
12
12
|
require("./batchloaders/oneToOneBatchLoader.cjs");
|
|
13
|
-
const require_relations_RecursiveCycleError = require("./relations/RecursiveCycleError.cjs");
|
|
14
13
|
const require_batchloaders_populateBatchLoader = require("./batchloaders/populateBatchLoader.cjs");
|
|
14
|
+
const require_relations_RecursiveCycleError = require("./relations/RecursiveCycleError.cjs");
|
|
15
15
|
require("./batchloaders/recursiveChildrenBatchLoader.cjs");
|
|
16
16
|
require("./batchloaders/recursiveM2mBatchLoader.cjs");
|
|
17
17
|
require("./batchloaders/recursiveParentsBatchLoader.cjs");
|
|
18
|
+
const require_clientIds = require("./clientIds.cjs");
|
|
18
19
|
const require_config = require("./config.cjs");
|
|
19
20
|
const require_keys = require("./keys.cjs");
|
|
20
21
|
const require_configure = require("./configure.cjs");
|
|
@@ -569,6 +570,7 @@ var EntityManager = class EntityManager {
|
|
|
569
570
|
* This supports upsert behavior, i.e.:
|
|
570
571
|
*
|
|
571
572
|
* - findOrCreate the primary entity being updated (based on the `id` key being provided),
|
|
573
|
+
* - `id` accepts a database ID or an already-registered client ID,
|
|
572
574
|
* - upsert relation markers like `op`, `delete`, and `remove` are supported,
|
|
573
575
|
* - deep input is supported, i.e. `{ firstName: "Bob", books: [{ title: "b1" } ] }`, and
|
|
574
576
|
* - fields set to `undefined` will be skipped instead of unset
|
|
@@ -722,7 +724,7 @@ var EntityManager = class EntityManager {
|
|
|
722
724
|
}
|
|
723
725
|
async load(typeOrId, id, hint) {
|
|
724
726
|
const entity = await this.#load(typeOrId, id, hint);
|
|
725
|
-
if (!this.#canViewEntity(entity)) throw new NotFoundError(`${
|
|
727
|
+
if (!this.#canViewEntity(entity)) throw new NotFoundError(`${id ?? typeOrId} was not found`);
|
|
726
728
|
return entity;
|
|
727
729
|
}
|
|
728
730
|
/** Resolves identity and hydration without filtering internal relation targets. */
|
|
@@ -736,6 +738,13 @@ var EntityManager = class EntityManager {
|
|
|
736
738
|
id = id || require_utils.fail(`Invalid ${typeOrId.name} id: ${id}`);
|
|
737
739
|
}
|
|
738
740
|
const meta = require_EntityMetadata.getMetadata(type);
|
|
741
|
+
if (require_clientIds.isClientId(id)) {
|
|
742
|
+
const entity = this.#getEntity(id);
|
|
743
|
+
if (!entity) throw new NotFoundError(`${id} was not found`);
|
|
744
|
+
if (!(entity instanceof type)) throw new Error(`${id} is ${entity.constructor.name} but should be ${type.name}`);
|
|
745
|
+
if (hint) await this.populate(entity, hint);
|
|
746
|
+
return entity;
|
|
747
|
+
}
|
|
739
748
|
const taggedId = require_keys.toTaggedId(meta, id);
|
|
740
749
|
let entity = this.#getEntity(taggedId);
|
|
741
750
|
if (!entity) {
|
|
@@ -755,8 +764,8 @@ var EntityManager = class EntityManager {
|
|
|
755
764
|
async loadAll(type, _ids, hint) {
|
|
756
765
|
const entities = await this.#loadAll(type, _ids, hint);
|
|
757
766
|
if (this.#hasVisibilityHook) {
|
|
758
|
-
const hidden =
|
|
759
|
-
if (hidden.length > 0) throw new NotFoundError(`${hidden.
|
|
767
|
+
const hidden = _ids.filter((_, i) => !this.#canViewEntity(entities[i]));
|
|
768
|
+
if (hidden.length > 0) throw new NotFoundError(`${hidden.join(",")} were not found`);
|
|
760
769
|
}
|
|
761
770
|
return entities;
|
|
762
771
|
}
|
|
@@ -767,8 +776,12 @@ var EntityManager = class EntityManager {
|
|
|
767
776
|
let idsToLoad;
|
|
768
777
|
let positionsToLoad;
|
|
769
778
|
for (let i = 0; i < _ids.length; i++) {
|
|
770
|
-
const
|
|
779
|
+
const input = _ids[i];
|
|
780
|
+
const client = require_clientIds.isClientId(input);
|
|
781
|
+
const id = client ? input : require_keys.tagId(meta, input);
|
|
771
782
|
const entity = this.#getEntity(id);
|
|
783
|
+
if (client && !entity) throw new NotFoundError(`${id} was not found`);
|
|
784
|
+
if (client && entity && !(entity instanceof type)) throw new Error(`${id} is ${entity.constructor.name} but should be ${type.name}`);
|
|
772
785
|
if (entity) entities[i] = entity;
|
|
773
786
|
else {
|
|
774
787
|
(idsToLoad ??= []).push(id);
|
|
@@ -803,16 +816,21 @@ var EntityManager = class EntityManager {
|
|
|
803
816
|
const maybeEntities = new Array(_ids.length);
|
|
804
817
|
let idsToLoad;
|
|
805
818
|
let positionsToLoad;
|
|
819
|
+
let allFound = true;
|
|
806
820
|
for (let i = 0; i < _ids.length; i++) {
|
|
807
|
-
const
|
|
821
|
+
const input = _ids[i];
|
|
822
|
+
const client = require_clientIds.isClientId(input);
|
|
823
|
+
const id = client ? input : require_keys.tagId(meta, input);
|
|
808
824
|
const entity = this.#getEntity(id);
|
|
825
|
+
if (client && entity && !(entity instanceof type)) throw new Error(`${id} is ${entity.constructor.name} but should be ${type.name}`);
|
|
809
826
|
if (entity) maybeEntities[i] = entity;
|
|
810
827
|
else {
|
|
828
|
+
allFound = false;
|
|
829
|
+
if (client) continue;
|
|
811
830
|
(idsToLoad ??= []).push(id);
|
|
812
831
|
(positionsToLoad ??= []).push(i);
|
|
813
832
|
}
|
|
814
833
|
}
|
|
815
|
-
let entities;
|
|
816
834
|
if (idsToLoad) {
|
|
817
835
|
await require_batchloaders_loadBatchLoader.loadBatchLoader(this, meta).loadAll(idsToLoad.map((id) => ({
|
|
818
836
|
taggedId: id,
|
|
@@ -821,9 +839,13 @@ var EntityManager = class EntityManager {
|
|
|
821
839
|
throw appendStack(err, /* @__PURE__ */ new Error());
|
|
822
840
|
});
|
|
823
841
|
for (let j = 0; j < positionsToLoad.length; j++) maybeEntities[positionsToLoad[j]] = this.#getEntity(idsToLoad[j]);
|
|
842
|
+
}
|
|
843
|
+
let entities;
|
|
844
|
+
if (allFound) entities = maybeEntities;
|
|
845
|
+
else {
|
|
824
846
|
entities = [];
|
|
825
847
|
for (const entity of maybeEntities) if (entity !== void 0) entities.push(entity);
|
|
826
|
-
}
|
|
848
|
+
}
|
|
827
849
|
entities = this.#filterVisible(entities);
|
|
828
850
|
if (hint) await this.populate(entities, hint);
|
|
829
851
|
return entities;
|
|
@@ -1518,7 +1540,7 @@ var EntityManager = class EntityManager {
|
|
|
1518
1540
|
for (const entity of entities) {
|
|
1519
1541
|
const instanceData = entity.__data;
|
|
1520
1542
|
if (!allowPendingChanges && instanceData.pendingOperation !== "none") require_utils.fail("Cannot fork an EntityManager with pending changes");
|
|
1521
|
-
else if (instanceData.isNewEntity) newEm.create(entity.constructor, {});
|
|
1543
|
+
else if (instanceData.isNewEntity) newEm.create(entity.constructor, { $clientId: entity.$clientId });
|
|
1522
1544
|
else {
|
|
1523
1545
|
rows[i] = createRowFromEntityData(entity);
|
|
1524
1546
|
i++;
|
|
@@ -1528,9 +1550,15 @@ var EntityManager = class EntityManager {
|
|
|
1528
1550
|
newEm.#hydrateCanonical(baseCstr, rows);
|
|
1529
1551
|
}
|
|
1530
1552
|
function mapEntity(oldEntity) {
|
|
1531
|
-
if (oldEntity.isNewEntity) return newEm.#entitiesById.get(oldEntity
|
|
1553
|
+
if (oldEntity.isNewEntity) return newEm.#entitiesById.get(oldEntity.$clientId);
|
|
1532
1554
|
else return newEm.#entitiesById.get(oldEntity.idTagged);
|
|
1533
1555
|
}
|
|
1556
|
+
for (const entity of oldEm.#entitiesArray) if (entity.$clientId) {
|
|
1557
|
+
const copy = mapEntity(entity);
|
|
1558
|
+
require_BaseEntity.getInstanceData(copy).clientId = entity.$clientId;
|
|
1559
|
+
newEm.#entitiesById.set(entity.$clientId, copy);
|
|
1560
|
+
}
|
|
1561
|
+
if (oldEm.#createCounter) newEm.#createCounter = new Map(oldEm.#createCounter);
|
|
1534
1562
|
function mapEntities(v) {
|
|
1535
1563
|
if (v === void 0) return void 0;
|
|
1536
1564
|
const result = new Array(v.length);
|
|
@@ -1620,18 +1648,37 @@ var EntityManager = class EntityManager {
|
|
|
1620
1648
|
}
|
|
1621
1649
|
/** Creates a new `type` and marks it as loaded, i.e. we know its collections are all safe to access in memory. */
|
|
1622
1650
|
#doCreate(type, opts, partial) {
|
|
1623
|
-
const entity = require_newEntity.newEntity(this, type, true);
|
|
1624
1651
|
const meta = require_EntityMetadata.getMetadata(type);
|
|
1625
|
-
const
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1652
|
+
const $clientId = opts.$clientId;
|
|
1653
|
+
let fields = opts;
|
|
1654
|
+
if ("$clientId" in opts) {
|
|
1655
|
+
fields = { ...opts };
|
|
1656
|
+
delete fields.$clientId;
|
|
1657
|
+
}
|
|
1658
|
+
if (require_clientIds.isClientId(fields.id)) throw new Error("id must be a database id; use $clientId for client identity");
|
|
1659
|
+
else if ($clientId !== void 0) {
|
|
1660
|
+
require_clientIds.assertClientId(meta, $clientId);
|
|
1661
|
+
if (this.#getEntity($clientId)) throw new Error(`Client id ${$clientId} is already registered`);
|
|
1662
|
+
this.__api.checkWritesAllowed();
|
|
1663
|
+
}
|
|
1664
|
+
let clientId = $clientId;
|
|
1665
|
+
if (clientId === void 0) {
|
|
1666
|
+
const counter = this.#createCounter ??= /* @__PURE__ */ new Map();
|
|
1667
|
+
let i = counter.get(meta.tagName) ?? 0;
|
|
1668
|
+
do
|
|
1669
|
+
clientId = `${meta.tagName}#${++i}`;
|
|
1670
|
+
while (this.#getEntity(clientId));
|
|
1671
|
+
counter.set(meta.tagName, i);
|
|
1672
|
+
}
|
|
1673
|
+
const entity = require_newEntity.newEntity(this, type, true);
|
|
1674
|
+
const data = require_BaseEntity.getInstanceData(entity);
|
|
1675
|
+
data.clientId = clientId;
|
|
1676
|
+
this.#doRegister(entity, fields.id);
|
|
1630
1677
|
const baseMeta = require_EntityMetadata.getBaseMeta(require_EntityMetadata.getMetadata(entity));
|
|
1631
1678
|
if (baseMeta.timestampFields) this.#assignTimestamps(entity, baseMeta);
|
|
1632
1679
|
if (baseMeta.inheritanceType === "sti") setStiDiscriminatorValue(baseMeta, entity);
|
|
1633
1680
|
getEmInternalApi(this)?.fieldLogger.logCreate(entity);
|
|
1634
|
-
require_index.setOpts(entity,
|
|
1681
|
+
require_index.setOpts(entity, fields, {
|
|
1635
1682
|
partial,
|
|
1636
1683
|
calledFromConstructor: true
|
|
1637
1684
|
});
|
|
@@ -1652,7 +1699,9 @@ var EntityManager = class EntityManager {
|
|
|
1652
1699
|
if (maybeId) {
|
|
1653
1700
|
if (!skipDuplicateCheck && this.#getEntity(maybeId) !== void 0) throw new Error(`Entity ${entity} has a duplicate instance already loaded`);
|
|
1654
1701
|
this.#entitiesById.set(maybeId, entity);
|
|
1655
|
-
}
|
|
1702
|
+
}
|
|
1703
|
+
const clientId = require_BaseEntity.getInstanceData(entity).clientId;
|
|
1704
|
+
if (clientId) this.#entitiesById.set(clientId, entity);
|
|
1656
1705
|
require_BaseEntity.getInstanceData(entity).entityIndex = this.#entitiesArray.length;
|
|
1657
1706
|
this.#entitiesArray.push(entity);
|
|
1658
1707
|
meta ??= require_EntityMetadata.getMetadata(entity);
|