goqux icon indicating copy to clipboard operation
goqux copied to clipboard

Better CTE support

Open Smithx10 opened this issue 6 months ago • 0 comments

I currently have:

Resource struct {
	Id           uuid.UUID    `db:"id" json:"uuid" goqux:"skip_insert"`
	CreatedAt    *time.Time   `db:"created_at" json:"created_at" goqux:"skip_insert"`
	UpdatedAt    *time.Time   `db:"updated_at" json:"updated_at" goqux:"skip_insert"`
	DeletedAt    *time.Time   `db:"deleted_at" json:"deleted_at" goqux:"skip_insert"`
	Name         string       `db:"name" json:"name"`
	Description  *string      `db:"description" json:"description"`
	ParentId     *uuid.UUID   `db:"parent_id" json:"parent_id"`
	ResourceType ResourceType `db:"resource_type" json:"resource_type"`
}

type Organization struct {
	Resource
	Foo string `db:"foo" json:"foo"`
}

type Folder struct {
	Resource
	Bar string `db:"bar" json:"bar"`
}

type Project struct {
	Resource
	Baz string `db:"baz" json:"baz"`
}

Which have the following:

-- resource manager
CREATE TYPE resman_resource_type AS ENUM (
    'organization',
    'project',
    'folder'
);

CREATE TABLE resman_resource (
    -- Entitiy fields are on all tables.
    -- Norad will expect these fields on almost all entities
	id
		UUID DEFAULT gen_random_uuid() NOT NULL PRIMARY KEY,
	name
		TEXT NOT NULL,
	description
		TEXT,
	created_at
		TIMESTAMPTZ DEFAULT now(),
	updated_at
		TIMESTAMPTZ DEFAULT now(),
	deleted_at
		TIMESTAMPTZ DEFAULT NULL,
    -- Theses fields are local to resman.Resource
	parent_id
		UUID REFERENCES resman_resource(id),
	resource_type
		resman_resource_type NOT NULL,
    -- Organizations do not have parents.
    CHECK (
        NOT (resource_type = 'organization' AND parent_id IS NOT NULL)
    )
);

CREATE TABLE resman_organization (
	id
		UUID REFERENCES resman_resource(id) ON DELETE CASCADE PRIMARY KEY,
    foo
        TEXT
);
CREATE TABLE resman_folder (
	id
        UUID REFERENCES resman_resource(id) ON DELETE CASCADE PRIMARY KEY,
    bar
        TEXT
);
CREATE TABLE resman_project (
	id
        UUID REFERENCES resman_resource(id) ON DELETE CASCADE PRIMARY KEY,
    baz
        TEXT
);

It would be nice to be able to do a goqx.$NewQuery[Organization]

That would take n Structs as input, and join on the pk

Smithx10 avatar Aug 06 '25 23:08 Smithx10