{"record":{"id":"a90638101d6700fe","repo":"mastra-ai/mastra","slug":"factory-storage-domain-domain-name-is-already","errorCode":null,"errorMessage":"Factory storage domain '${domain.name}' is already registered","messagePattern":"Factory storage domain '(.+?)' is already registered","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/core/src/storage/factory-storage.ts","lineNumber":292,"sourceCode":"   * Agent-state store (threads, messages, memory, OM) for this database,\n   * sharing this backend's connection. Callers pass the result to the Mastra\n   * instance and all agent-related wiring. Lazily constructed; returns the\n   * same instance on repeat calls.\n   */\n  abstract getMastraStorage(): MastraCompositeStore;\n\n  /** Open/validate the backend, then initialize registered domains fail-soft. */\n  async init(): Promise<void> {\n    await this.#ensureStorageReady();\n    await Promise.all([...this.#domains.keys()].map(name => this.#initDomain(name).catch(() => undefined)));\n  }\n\n  /** Backend-specific connection initialization. */\n  protected abstract initStorage(): Promise<void>;\n\n  registerDomain<T extends FactoryStorageDomain>(domain: T): T {\n    if (this.#domains.has(domain.name)) {\n      throw new Error(`Factory storage domain '${domain.name}' is already registered`);\n    }\n    domain.__bindFactoryStorage(this);\n    this.#domains.set(domain.name, domain);\n    return domain;\n  }\n\n  getDomain<T extends FactoryStorageDomain = FactoryStorageDomain>(name: string): T {\n    const domain = this.#domains.get(name);\n    if (!domain) {\n      throw new Error(`Factory storage domain '${name}' is not registered`);\n    }\n    return domain as T;\n  }\n\n  hasDomain(name: string): boolean {\n    return this.#domains.has(name);\n  }\n","sourceCodeStart":274,"sourceCodeEnd":310,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/storage/factory-storage.ts#L274-L310","documentation":"registerDomain() rejects adding a second domain whose name matches one already present in the storage's #domains map. Each factory storage domain name must be unique per storage instance, so duplicate registration is treated as a programming error and thrown immediately (before the duplicate would silently shadow the original).","triggerScenarios":"Calling `storage.registerDomain(domain)` twice for domains with the same `name`, or calling registerDomain for a domain after re-running initStorage() (e.g. calling `init()` twice on the storage instance).","commonSituations":"Calling storage.init() more than once (hot reload, multiple bootstrap paths); two custom domains accidentally given the same name; subclass re-registering a parent's domain during re-initialization.","solutions":["Guard registration: check `storage.hasDomain(name)` before calling registerDomain, or reuse the existing domain via `storage.getDomain(name)`.","Ensure storage init runs only once (memoize/flag the init path, e.g. `if (inited) return`).","Give custom domains distinct `name` values.","If a backend re-registers domains in initStorage(), check hasDomain() first and skip."],"exampleFix":"// before\nawait storage.init();\nawait storage.init(); // throws: already registered\n\n// after\nif (!storage.hasDomain('memorySettings')) {\n  storage.registerDomain(new InMemoryMemorySettingsStorage());\n}","handlingStrategy":"validation","validationCode":"if (!storage.hasDomain(domain.name)) {\n  storage.registerDomain(domain);\n}","typeGuard":null,"tryCatchPattern":"try {\n  storage.registerDomain(domain);\n} catch (e) {\n  if (!e.message.includes('is already registered')) throw e;\n  // reuse existing registration\n  return storage.getDomain(domain.name);\n}","preventionTips":["Make storage initialization idempotent (init-once flag or memoized promise).","Check hasDomain() before registerDomain in custom backends.","Ensure each custom domain has a unique name constant.","Avoid calling init() from multiple bootstrap paths (hot reload, workers)."],"tags":["storage","duplicate-registration","lifecycle"],"backgroundTag":"duplicate-registration","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}