UNPKG

appwrite-utils-cli

Version:

Appwrite Utility Functions to help with database management, data conversion, data import, migrations, and much more. Meant to be used as a CLI tool, I do not recommend installing this in frontend environments.

58 lines (57 loc) 3.19 kB
// Helper function to categorize collections based on relationship sides export const categorizeCollectionByRelationshipSide = (attributes) => { let hasParent = false; let hasChild = false; for (const attr of attributes) { if (attr.type === "relationship") { if (attr.side === "parent") { hasParent = true; } else if (attr.side === "child") { hasChild = true; } } } if (hasParent && hasChild) return "mixed"; if (hasParent) return "parent"; return "child"; }; // Helper function to get all dependencies of a collection export const getDependencies = (attributes) => { return attributes .filter((attr) => attr.type === "relationship" && attr.relatedCollection !== undefined) .map((attr) => attr.relatedCollection); }; // Function to sort collections based on dependencies and relationship sides export const sortCollections = (configCollections) => { if (!configCollections) { return []; } // Categorize collections based on their relationship sides const parentCollections = configCollections.filter(({ attributes }) => categorizeCollectionByRelationshipSide(attributes) === "parent"); const mixedCollections = configCollections.filter(({ attributes }) => categorizeCollectionByRelationshipSide(attributes) === "mixed"); const childCollections = configCollections.filter(({ attributes }) => categorizeCollectionByRelationshipSide(attributes) === "child"); // Sort mixedCollections to ensure parents are processed before children within the mixed category // This might involve more sophisticated logic if you need to order mixed collections based on specific parent-child relationships mixedCollections.sort((a, b) => { // Example sorting logic for mixed collections; adjust based on your specific needs const aDependencies = getDependencies(a.attributes).length; const bDependencies = getDependencies(b.attributes).length; return aDependencies - bDependencies; }); // Combine them back into a single array with the desired order // Children first because they have no dependencies and parents will create the relationship if it's twoWay return [...childCollections, ...parentCollections, ...mixedCollections]; }; // Function to sort attributes within a collection based on relationship sides export const sortAttributesByRelationshipSide = (attributes) => { // Separate attributes into parent and child based on their relationship side const parentAttributes = attributes.filter((attr) => attr.type === "relationship" && attr.side === "parent"); const childAttributes = attributes.filter((attr) => attr.type === "relationship" && attr.side === "child"); const otherAttributes = attributes.filter((attr) => attr.type !== "relationship"); // Combine them back into a single array with child attributes first, then other attributes, and parent attributes last // as parent attributes will create the relationship with the child if needed return [...childAttributes, ...otherAttributes, ...parentAttributes]; };