@sanity/migrate
Version:
Tooling for running data migrations on Sanity.io projects
101 lines (100 loc) • 3.04 kB
JavaScript
function _define_property(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function _object_spread(target) {
for(var i = 1; i < arguments.length; i++){
var source = arguments[i] != null ? arguments[i] : {};
var ownKeys = Object.keys(source);
if (typeof Object.getOwnPropertySymbols === "function") {
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
}));
}
ownKeys.forEach(function(key) {
_define_property(target, key, source[key]);
});
}
return target;
}
import { fromString } from '@sanity/util/paths';
import arrify from 'arrify';
/**
* Creates a new document.
* @param document - The document to be created.
* @returns The mutation to create the document.
*/ export function create(document) {
return {
document: document,
type: 'create'
};
}
/**
* Applies a patch to a document.
* @param id - The ID of the document to be patched.
* @param patches - The patches to be applied.
* @param options - Optional patch options.
* @returns The mutation to patch the document.
*/ export function patch(id, patches, options) {
return _object_spread({
id: id,
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- arrify loses type information, cast needed for tuple type preservation
patches: arrify(patches),
type: 'patch'
}, options ? {
options: options
} : {});
}
/**
* Creates a {@link NodePatch} at a specific path.
* @param path - The path where the operation should be applied.
* @param operation - The operation to be applied.
* @returns The node patch.
*/ export function at(path, operation) {
return {
op: operation,
path: typeof path === 'string' ? fromString(path) : path
};
}
/**
* Creates a document if it does not exist.
* @param document - The document to be created.
* @returns The mutation operation to create the document if it does not exist.
*/ export function createIfNotExists(document) {
return {
document: document,
type: 'createIfNotExists'
};
}
/**
* Creates or replaces a document.
* @param document - The document to be created or replaced.
* @returns The mutation operation to create or replace the document.
*/ export function createOrReplace(document) {
return {
document: document,
type: 'createOrReplace'
};
}
/**
* Deletes a document.
* @param id - The id of the document to be deleted.
* @returns The mutation operation to delete the document.
*/ export function delete_(id) {
return {
id: id,
type: 'delete'
};
}
/**
* Alias for delete
*/ export var del = delete_;