DWRecord Resource
DWRecord Resource
Schema-aware Resource base class
Declarative table definitions that hydrate into strongly typed Godot Resources.
Expose fields with `@export` to define columns, including typed Dictionaries and Arrays.
Control primary keys, unique constraints, and indices using the `schema` helper.
Call `.save_to_db()` and `.load_from_db()` to persist without writing SQL manually.
Usage patterns
Declare a player profile
@tool
class_name PlayerRecord
extends DWRecord
@export var player_id: DWGuid
@export var name: String
@export var stats: Dictionary[String, int]
func _init() -> void:
schema.table_name = "players"
schema.primary_key = "player_id"
schema.unique_keys = [["name"]]
schema.indices = [["team", "level"]]
Persist an instance
`.save_to_db()` auto-registers the schema and upserts data by default.
var player := PlayerRecord.new()
player.player_id = DWTypes.create_guid()
player.name = "Edda"
player.stats = {"level": 12}
player.save_to_db()
Key methods
- save_to_db(wait: bool = false) -> bool
Registers the schema if needed, writes current property values, and optionally blocks until complete.
- load_from_db(where_clause: String = "", params: Dictionary = {}) -> bool
Loads the first row that matches the filter into the Resource and returns whether anything was found.
- delete_from_db() -> bool
Removes the current record by primary key.
- to_dictionary() -> Dictionary
Serializes exported properties into a dictionary that matches the persisted schema.
- from_dictionary(payload: Dictionary)
Hydrates the Resource from a dictionary, respecting typed Array/Dictionary hints.
Operational tips
- Set `schema.table_name` when your Resource class name doesn't match the desired table.
- Combine `unique_keys` and `indices` to enforce constraints without writing migrations manually.