Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Api additions #375

Merged
merged 5 commits into from
Feb 9, 2024
Merged

Api additions #375

merged 5 commits into from
Feb 9, 2024

Conversation

rallisf1
Copy link
Contributor

I went ahead and added what was discussed on #373

Even more efficient page API

You can now get exactly the sections you need via the page API via a query parameter: &sections=id1,id2...

Search endpoint

Fancy a search widget for your primo website that uses no 3rd party search service? So did I! Go crazy with /api/site_url/websearch?q=your search terms

Existing installations need to run the following query in their supabase dashboard:

CREATE OR REPLACE FUNCTION page_search(search_terms text, site_url text)
RETURNS TABLE(id uuid, name text, url text, created_at timestamp with time zone) AS $$
BEGIN
    RETURN QUERY
    WITH RECURSIVE parent_urls AS (
        SELECT
            p.id,
            ARRAY[p.url] AS urls
        FROM pages p
        INNER JOIN sites s ON p.site = s.id 
        WHERE p.parent IS NULL AND s.url = site_url

        UNION ALL

        SELECT
            p.id,
            pu.urls || p.url
        FROM pages p
        INNER JOIN sites s ON p.site = s.id 
        INNER JOIN parent_urls pu ON p.parent = pu.id
        WHERE s.url = site_url
    )
    SELECT DISTINCT
        p.id,
        p.name,
        ARRAY_TO_STRING(parent_urls.urls, '/', '/') AS url,
        p.created_at
    FROM pages p
    INNER JOIN sites s ON p.site = s.id
    INNER JOIN sections se ON p.id = se.page
    INNER JOIN parent_urls ON p.id = parent_urls.id
    WHERE s.url = site_url AND to_tsvector(se.content) @@ to_tsquery(search_terms);
END;
$$ LANGUAGE plpgsql;

P.S. Since most people use search for their search results page url I thought websearch would be the next best candidate. Feel free to change it.

P.S.2. I couldn't figure out a better way to get the full url of the result posts. Any improvements are welcome.

@mateomorris
Copy link
Collaborator

Nice! Will review this week. What do you think of just a search param instead of /websearch?q? That way we wouldn't have to worry about /websearch resembling a page endpoint, and you could run a search against the whole site, or a specific page.

Like: /api/site_url?search=your search terms or /api/site_url/page_url?search=your search terms
Searching only within a page's subpages could be useful for things like searching blog posts

@rallisf1
Copy link
Contributor Author

Separate endpoints keep the code cleaner. We can still add parent_url query param to limit the search on subpages.

It's your baby though, so as long as it can still do a whole site search I don't really mind :D

@mateomorris
Copy link
Collaborator

Okay thanks - switched to using a query param isntead and exported the page_search function to keep things clean. I tried modifying it to work on pages but couldn't finish, so that'll have to wait for another day.

Besides that, I also fixed an issue with fetching from root pages (sections was showing up empty) - caused by querying select('page(parent!inner(url)' when parent is null.

@mateomorris mateomorris merged commit 041c77b into primocms:master Feb 9, 2024
@rallisf1 rallisf1 deleted the api_additions branch February 9, 2024 20:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants