Files
metabuilder/packages/schema_editor/seed/scripts/relations.lua
T
git 9d27207fbc Refactor package validation and quick guide modules
- Simplified structure validation by re-exporting functions from structure_config and validate_structure modules for better maintainability.
- Consolidated validation logic in validate.lua, delegating to validate_package module for clearer orchestration.
- Introduced new quick guide functionalities including step management (add, remove, update, reset ordering) and media handling (thumbnail and video URL validation).
- Added utility functions for URL validation and step creation, enhancing the quick guide's usability.
- Established type definitions for steps and media states to improve code clarity and type safety.
- Enhanced schema editor with new field and relation definitions, providing a more robust structure for database schema management.
2025-12-30 11:17:40 +00:00

76 lines
2.1 KiB
Lua

-- Table relationships
---@alias RelationType "one_to_one" | "one_to_many" | "many_to_many"
---@class RelationEndpoint
---@field table string Table name
---@field field string Field name
---@class RelationOptions
---@field cascade? boolean Whether to cascade deletes
---@class Relation
---@field type RelationType Relation type
---@field from_table string Source table name
---@field from_field string Source field name
---@field to_table string Target table name
---@field to_field string Target field name
---@field cascade boolean Whether to cascade deletes
---@field pivot_table? string Pivot table for many-to-many
---@class RelationsModule
---@field ONE_TO_ONE string
---@field ONE_TO_MANY string
---@field MANY_TO_MANY string
local M = {}
M.ONE_TO_ONE = "one_to_one"
M.ONE_TO_MANY = "one_to_many"
M.MANY_TO_MANY = "many_to_many"
---Define a table relationship
---@param type RelationType Relation type
---@param from RelationEndpoint Source endpoint
---@param to RelationEndpoint Target endpoint
---@param options? RelationOptions Relation options
---@return Relation
function M.define(type, from, to, options)
return {
type = type,
from_table = from.table,
from_field = from.field,
to_table = to.table,
to_field = to.field,
cascade = options and options.cascade or false
}
end
---Create a one-to-one relationship
---@param from RelationEndpoint Source endpoint
---@param to RelationEndpoint Target endpoint
---@return Relation
function M.has_one(from, to)
return M.define(M.ONE_TO_ONE, from, to)
end
---Create a one-to-many relationship
---@param from RelationEndpoint Source endpoint
---@param to RelationEndpoint Target endpoint
---@return Relation
function M.has_many(from, to)
return M.define(M.ONE_TO_MANY, from, to)
end
---Create a many-to-many relationship
---@param from RelationEndpoint Source endpoint
---@param to RelationEndpoint Target endpoint
---@param pivot string Pivot table name
---@return Relation
function M.belongs_to_many(from, to, pivot)
local rel = M.define(M.MANY_TO_MANY, from, to)
rel.pivot_table = pivot
return rel
end
return M