Skip to content

Commit

Permalink
feat(migration): support async hooks in migration nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
binoy14 committed Jan 26, 2024
1 parent b60c02d commit d5d846f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
26 changes: 15 additions & 11 deletions packages/@sanity/migrate/src/runner/normalizeMigrateDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,34 @@ export function createAsyncIterableMutation(
return async function* run(docs, context) {
for await (const doc of docs) {
if (doc._type !== opts.documentType) continue
const documentMutations = collectDocumentMutations(migration, doc, context)
const documentMutations = await collectDocumentMutations(migration, doc, context)
if (documentMutations.length > 0) {
yield documentMutations
}
}
}
}

function collectDocumentMutations(
async function collectDocumentMutations(
migration: NodeMigration,
doc: SanityDocument,
context: NodeMigrationContext,
) {
const documentMutations = migration.document?.(doc, context)
const nodeMigrations = flatMapDeep(doc as JsonValue, (value, path) => {
return [
...arrify(migration.node?.(value, path, context)),
...arrify(migrateNodeType(migration, value, path, context)),
].map((change) => normalizeNodeMutation(path, change))
const documentMutations = Promise.resolve(migration.document?.(doc, context))
const nodeMigrations = flatMapDeep(doc as JsonValue, async (value, path) => {
const [nodeReturnValues, nodeTypeReturnValues] = await Promise.all([
Promise.resolve(migration.node?.(value, path, context)),
Promise.resolve(migrateNodeType(migration, value, path, context)),
])

return [...arrify(nodeReturnValues), ...arrify(nodeTypeReturnValues)].map((change) =>
normalizeNodeMutation(path, change),
)
})

return [...arrify(documentMutations), ...nodeMigrations].map((change) =>
normalizeDocumentMutation(doc._id, change),
)
return (await Promise.all([...arrify(await documentMutations), ...nodeMigrations]))
.flat()
.map((change) => normalizeDocumentMutation(doc._id, change))
}

/**
Expand Down
16 changes: 8 additions & 8 deletions packages/@sanity/migrate/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,40 +51,40 @@ export interface NodeMigration {
document?: <Doc extends SanityDocument>(
doc: Doc,
context: NodeMigrationContext,
) => DocumentMigrationReturnValue
) => DocumentMigrationReturnValue | Promise<DocumentMigrationReturnValue>
node?: <Node extends JsonValue>(
node: Node,
path: Path,
context: NodeMigrationContext,
) => NodeMigrationReturnValue
) => NodeMigrationReturnValue | Promise<NodeMigrationReturnValue>
object?: <Node extends JsonObject>(
node: Node,
path: Path,
context: NodeMigrationContext,
) => NodeMigrationReturnValue
) => NodeMigrationReturnValue | Promise<NodeMigrationReturnValue>
array?: <Node extends JsonArray>(
node: Node,
path: Path,
context: NodeMigrationContext,
) => NodeMigrationReturnValue
) => NodeMigrationReturnValue | Promise<NodeMigrationReturnValue>
string?: <Node extends string>(
node: Node,
path: Path,
context: NodeMigrationContext,
) => NodeMigrationReturnValue
) => NodeMigrationReturnValue | Promise<NodeMigrationReturnValue>
number?: <Node extends number>(
node: Node,
path: Path,
context: NodeMigrationContext,
) => NodeMigrationReturnValue
) => NodeMigrationReturnValue | Promise<NodeMigrationReturnValue>
boolean?: <Node extends boolean>(
node: Node,
path: Path,
context: NodeMigrationContext,
) => NodeMigrationReturnValue
) => NodeMigrationReturnValue | Promise<NodeMigrationReturnValue>
null?: <Node extends null>(
node: Node,
path: Path,
context: NodeMigrationContext,
) => NodeMigrationReturnValue
) => NodeMigrationReturnValue | Promise<NodeMigrationReturnValue>
}

0 comments on commit d5d846f

Please sign in to comment.