You have been redirected from an outdated version of the article. Below is the content available on this topic. To view the old article click here.

Advanced find()

The find() function can be used to execute database queries in StructrScript and server-side JavaScript environments such as Repeaters.

Usage

find(type, predicates..., options...)
find(type, uuid)
find(type)

Description
This method is one of the most important and frequently used built-in functions. It returns a collection of entities, which can be empty if none of the existing nodes or relationships matches the given search parameters.

find() accepts several different parameter combinations, whereas the first parameter is always the name of the type (a string) to retrieve from the database. The second parameter can either be a UUID (string), a map (e.g. a result from nested function calls), a list of predicates and/or (key, value) pairs or other query options like sort order or pagination controls. See the examples below for an overview of all the possible parameter combinations.

Note
Calling find() with only a single parameter will return all the nodes of the given type (which might be dangerous if there are many of them in the database).

Predicates
The following predicates can be specified. Predicates can be combined and mixed to build complex queries. Some predicates and property keys need to be combined in a different way than others, please refer to the examples below for an overview.

Predicate Description
and(...) Logical AND
or(...) Logical OR
not(...) Logical NOT
equals(key, value) Returns only those nodes that match the given (key, value) pair
contains(key, text) Returns only those nodes whose value contains the given text
empty('key') Returns only those nodes that don’t have a value set for the given key
range(start, end) Returns only those nodes that match the given (key, value) pair
range(start, end, withStart, withEnd) Like range(start, end) above but with flags for the endpoints
range(null, end) Unbounded range() to emulate “less than”
range(start, null) Unbounded range() to emulate “greater than”

Options
The following options can be specified:

Option Description
sort(key) Sorts the result according to the given property key (ascending)
sort(key, true) Sorts the result according to the given property key (descending)
page(page, pageSize) Limnits the result size to pageSize, returning the given page

StructrScript Examples

${find('User')}
=> returns all User entities in the database

${find('User', sort('name'))}
=> returns all User entities in the database, sorted by name

${find('User', sort('name'), page(1, 10)}
=> returns the first 10 User entities in the database, sorted by name

${find('User', 'name', 'admin')}
=> returns all User entities with the name "admin"

${find('User', '7379af469cd645aebe1a3f8d52b105bd')}
=> returns the user entity with the given UUID

${find('User', '7379af469cd645aebe1a3f8d52b105bd')}
=> returns the user entity with the given UUID

${find('User', 'name', contains('e'))}
=> returns all user entities whose name property contains the letter 'e'

${find('User', contains('name', 'e'))}
=> returns all user entities whose name property contains the letter 'e' (same as above)

${find('User', 'age', range(0, 18))}
=> returns all user entities whose age property is between 0 and 18 (inclusive)

${find('User', 'age', range(0, 18, false, false))}
=> returns all user entities whose age property is between 0 and 18 (exclusive)

${find('User', and(equals('name', 'Tester), equals('age', range(0, 18))))}
=> returns all user entities whose name is 'Tester' and whose age is between 0 and 18 (inclusive)

JavaScript Example

${{
    let users = $.find('Project',
        {
            $and: {
                'name1': 'structr',
                'age': $.range(30, 50)
            }
        },
        $.sort('name', true),
        $.page(1, 10)
    );

    return users;
}}

Search results for "Advanced find()"

Advanced find()

This method is one of the most important and frequently used built-in functions. It returns a collection of entities, which can be empty if none of the existing nodes or relationships matches the given search parameters.
find() accepts several different predicates (key, value pairs) and other query options like sort order or pagination controls. See the examples below for an overview of the possible parameter combinations for an advanced find() query. The simpler usage of the find() function is documented below in the article for find()

Predicates

The following predicates can be specified. Predicates can be combined and mixed to build complex queries. Some predicates and property keys need to be combined in a different way than others, please refer to the examples below for an overview.

Predicate Description
and(...) Logical AND
or(...) Logical OR
not(...) Logical NOT
equals(key, value) Returns only those nodes that match the given (key, value) pair
contains(key, text) Returns only those nodes whose value contains the given text
empty('key') Returns only those nodes that don’t have a value set for the given key
range(start, end [, withStart = true [, withEnd = true ]] ) Returns only those nodes where the given propertyKey is in the range between start and end
range(null, end) Unbounded range() to emulate “less than”
range(start, null) Unbounded range() to emulate “greater than”
within_distance(latitude, longitude, distance) Returns only those nodes that are within distance meters around the given coordinates. The type that is being searched for needs to extend the built-in type Location

Options
The following options can be specified:

Option Description
sort(key)Sorts the result according to the given property key (ascending)
sort(key, true)Sorts the result according to the given property key (descending)
page(page, pageSize)Limits the result size to pageSize, returning the given page
find(type, predicates..., options...)

Advanced

From the Advanced properties dialog you can access Structr’s automatic content extraction. The content extraction takes structured documents such as PDF files or images and splits them into structural elements such as pages and sentences, which can be accessed via the Schema types StructuredDocument and StructuredTextNode. You can trigger this content extraction via the Extract document content button (Extract document content).

Advanced Search Capabilities

Besides simple value-based filters, Structr also supports other search methods:

  • inexact search
  • geographical distance search
  • range queries
  • empty / non-empty values

Advanced Example

Since all parts of a Structr application are stored in the database, the JavaScript console can even be used to modify the schema.

find()

This method is one of the most important and frequently used built-in functions. It returns a collection of entities, which can be empty if none of the existing nodes or relationships matches the given search parameters.
find() accepts several different parameter combinations, whereas the first parameter is always the name of the type to retrieve from the database. The second parameter can either be a UUID (string), a map (e.g. a result from nested function calls) or a list of (key, value) pairs.

The given query parameters are combined with an AND predicate. For node attributes an exact match is searched for the given value. For remote attribute collections the given search values are combined with an OR predicate (see examples).

find(type [, key1, value1 [, ... ]] )
find(type [, map ])
find(type [, uuid ])

Advanced Settings

The Advanced Settings tab contains settings controlling specialized aspects of a Structr application, such as Geocoding. It is subdivided into the following sections:

Dynamic Content / Repeater

This concludes the development of the most basic functionalities of this simple demo application. The following four sections cover more advanced topics by extending our application in the following ways:

  1. extending the user interface with imported styles from existing projects,
  2. adding images to projects via built-in types,
  3. adding editing functionality to the table and
  4. extending the data model with tasks and deadlines.

These sections are largely independent of one another, as they only assume the functionality described in this section.

Page Title

This pattern makes sure that the page’s title is automatically displayed correctly when changing the page’s name. To do so, click on “Basic” or “Advanced” and set the name to “overview” (this page will later be the overview page for projects and tasks). To save the name, we click anywhere outside of the input field. The text field should flash green when the data was successfully updated in the database. The page’s title should now be shown as “Overview”.

For advanced users, there are many options to precisely control the security and visibility of pages and individual elements, how to handle HTTP errors like 404 (Not Found) or 403 (Forbidden) in your application, etc. Like all other built-in types in Structr, Pages and other HTML elements can be extended with custom properties, relationships to other types, and even inherited from, to allow a very tight integration in your application.

StructrScript

The above example shows the use of three different built-in functions. The find() function executes a database query and returns the result collection. extract() converts the collection of database objects into a collection of strings and join() concatenates the collection of strings into a single string, using the given separator between the elements.

Dynamic Content / Repeater

The result returned by the find() function is not sorted, so in order to get a sorted project list, we need to add the system function sort() which requires two parameters: A a collection and the name of a property to sort by. Modify the function query of the repeater element as follows:

The find() function is another Structr built-in function, just like capitalize() we used in the page’s title and heading above. The find() function takes as argument the name of a type and returns a collection of all instances of this type.

Finally, we cover simple extending the data model by adding additional data types such as Task and Deadline to the project. This section focuses on more advanced functionalities of the Schema Editor.

Shared Components

Let’s change the name of the shared component we created. Click on “Advanced” and set the name to “Navigation (top)”. See how the name changes in the shared component area and in the page tree simultaneously.

Advanced examples

Script expressions inside the REST query and the Cypher Query are evaluated for more flexibility. Using this functionality a more advanced repeater configuration can be created.

Scripting Migration

Only occurrences which are used in advanced find need to be prefixed. Some of those predicates also exist as regular functions ($.sort(), $.empty(), …) or keywords ($.page).