{
  "version": 3,
  "sources": ["../../../src/Migration.ts"],
  "sourcesContent": ["//\n// Copyright 2024 DXOS.org\n//\n\n// @import-as-namespace\n\nimport type * as Schema from 'effect/Schema';\n\nimport { type URI } from '@dxos/keys';\n\nimport type * as Database from './Database';\nimport type * as Entity from './Entity';\nimport { MetaId, type EntityMeta, getSchemaURI } from './internal';\nimport * as Type from './Type';\n\n/**\n * Result returned by a migration's `transform` callback.\n * The data shape matches the target schema; the optional `[Obj.Meta]` symbol key lets the\n * transform update the object's meta (e.g. `key` / `version`) atomically with the data swap.\n */\ntype MigrationSchemaInput = Type.AnyEntity;\n\ntype MigrationInstanceType<S> = S extends Type.AnyEntity\n  ? Type.InstanceType<S>\n  : S extends Schema.Schema.AnyNoContext\n    ? Schema.Schema.Type<S>\n    : never;\n\nexport type TransformResult<To> = Omit<MigrationInstanceType<To>, 'id' | Entity.KindId> & {\n  [MetaId]?: Partial<EntityMeta>;\n};\n\ntype DefineObjectMigrationOptions<From extends MigrationSchemaInput, To extends MigrationSchemaInput> = {\n  from: From;\n  to: To;\n  /**\n   * Pure function that converts the old object data to the new object data.\n   *\n   * The returned object may include an optional `[Obj.Meta]` entry to update the object's meta\n   * (e.g. registry `key` / `version`) atomically with the data swap.\n   */\n  // TODO(dmaretskyi): `id` should not be a part of the schema.\n  transform: (from: MigrationInstanceType<From>, context: ObjectMigrationContext) => Promise<TransformResult<To>>;\n\n  /**\n   * Callback that is called after the object is migrated. Called for every object that is migrated.\n   *\n   * NOTE: Database mutations performed in this callback are not guaranteed to be idempotent.\n   *       If multiple peers run the migration separately, the effects may be applied multiple times.\n   */\n  onMigration?: (params: OnMigrateProps<From, To>) => Promise<void>;\n};\n\n/**\n * Context passed to object migration callbacks.\n */\nexport type ObjectMigrationContext = {\n  db: Database.Database;\n};\n\ntype OnMigrateProps<From, To> = {\n  before: MigrationInstanceType<From>;\n  object: MigrationInstanceType<To>;\n  db: Database.Database;\n};\n\n/**\n * Definition of a migration from one object schema version to another.\n */\nexport type ObjectMigration = {\n  fromType: URI.URI;\n  toType: URI.URI;\n  fromSchema: Schema.Schema.AnyNoContext;\n  toSchema: Schema.Schema.AnyNoContext;\n  transform: (from: unknown, context: ObjectMigrationContext) => Promise<unknown>;\n  onMigration?: (params: OnMigrateProps<any, any>) => Promise<void>;\n};\n\n/**\n * Define a migration between two object schemas.\n *\n * @example\n * ```ts\n * const migration = Migration.define({\n *   from: ContactV1,\n *   to: ContactV2,\n *   transform: async (from) => ({ name: `${from.firstName} ${from.lastName}` }),\n *   onMigration: async () => {},\n * });\n * ```\n */\nexport const define = <From extends MigrationSchemaInput, To extends MigrationSchemaInput>(\n  options: DefineObjectMigrationOptions<From, To>,\n): ObjectMigration => {\n  const fromSchema = Type.getSchema(options.from);\n  const toSchema = Type.getSchema(options.to);\n  const fromType = getSchemaURI(fromSchema);\n  if (!fromType) {\n    throw new Error('Invalid from schema');\n  }\n  const toType = getSchemaURI(toSchema);\n  if (!toType) {\n    throw new Error('Invalid to schema');\n  }\n\n  return {\n    fromType,\n    toType,\n    fromSchema,\n    toSchema,\n    transform: options.transform as any,\n    onMigration: options.onMigration as any,\n  };\n};\n"],
  "mappings": ";;;;;;;;;;;AAAA;;;;AA2FO,IAAMA,SAAS,CACpBC,YAAAA;AAEA,QAAMC,aAAkBC,UAAUF,QAAQG,IAAI;AAC9C,QAAMC,WAAgBF,UAAUF,QAAQK,EAAE;AAC1C,QAAMC,WAAWC,aAAaN,UAAAA;AAC9B,MAAI,CAACK,UAAU;AACb,UAAM,IAAIE,MAAM,qBAAA;EAClB;AACA,QAAMC,SAASF,aAAaH,QAAAA;AAC5B,MAAI,CAACK,QAAQ;AACX,UAAM,IAAID,MAAM,mBAAA;EAClB;AAEA,SAAO;IACLF;IACAG;IACAR;IACAG;IACAM,WAAWV,QAAQU;IACnBC,aAAaX,QAAQW;EACvB;AACF;",
  "names": ["define", "options", "fromSchema", "getSchema", "from", "toSchema", "to", "fromType", "getSchemaURI", "Error", "toType", "transform", "onMigration"]
}
