Lua Data Access: Difference between revisions
Importing NeoWiki demo data |
Importing NeoWiki demo data |
||
| Line 34: | Line 34: | ||
Child subjects on [[ACME Inc]]: {{#invoke:NeoWikiDemo|children|ACME Inc}} | Child subjects on [[ACME Inc]]: {{#invoke:NeoWikiDemo|children|ACME Inc}} | ||
== Running Cypher Queries == | |||
<code>mw.neowiki.query()</code> runs a read-only Cypher query and returns each row as a Lua table keyed | |||
by the <code>RETURN</code> aliases. Use it when a single-property lookup is not enough — for example | |||
to join Subjects, sort results, or list all Subjects of a given type. | |||
=== Listing all Companies === | |||
<syntaxhighlight lang="cypher"> | |||
MATCH (n:Company) RETURN n.name, n.id | |||
</syntaxhighlight> | |||
{{#invoke:NeoWikiDemo|query|MATCH (n:Company) RETURN n.name, n.id}} | |||
=== Parameterised query: Products available since a given year === | |||
Templates should always pass values via the <code>params</code> table instead of concatenating into the query string. | |||
<syntaxhighlight lang="lua"> | |||
nw.query( | |||
'MATCH (n:Product) WHERE n.`Available since` >= $year ' .. | |||
'RETURN n.name AS name, n.`Available since` AS year ORDER BY year', | |||
{ year = 2010 } | |||
) | |||
</syntaxhighlight> | |||
Products available since 2010: {{#invoke:NeoWikiDemo|productsFoundedSince|2010}} | |||
== Inspecting a Schema == | |||
<code>mw.neowiki.getSchema()</code> returns a Schema definition as a Lua table, including all property names, types, and type-specific attributes. This enables templates to render or validate any schema without hardcoding property names. | |||
=== Company === | |||
{{#invoke:NeoWikiDemo|schema|Company}} | |||
=== Employee === | |||
{{#invoke:NeoWikiDemo|schema|Employee}} | |||
== Module Source == | == Module Source == | ||
| Line 55: | Line 95: | ||
-- Get child subjects | -- Get child subjects | ||
local children = nw.getChildSubjects( 'ACME Inc' ) | local children = nw.getChildSubjects( 'ACME Inc' ) | ||
-- Run a read-only Cypher query | |||
local rows = nw.query( | |||
'MATCH (n:Product) WHERE n.`Available since` >= $year RETURN n.name, n.`Available since`', | |||
{ year = 2010 } | |||
) | |||
</syntaxhighlight> | </syntaxhighlight> | ||
See [[Module:NeoWikiDemo]] for the full source. | See [[Module:NeoWikiDemo]] for the full source. | ||
Latest revision as of 11:27, 20 April 2026
This page demonstrates the mw.neowiki Lua API for accessing NeoWiki structured data from templates and modules.
The examples use Module:NeoWikiDemo, a demo module included with NeoWiki.
Reading Property Values
mw.neowiki.getValue() returns a single scalar value. mw.neowiki.getAll() returns all values as a table.
| Property | getValue | getAll |
|---|---|---|
| Founded at | 2005 | 2005 |
| Websites | https://acme.example | https://acme.example |
| Products (relations) | Foo | Foo, Bar, Baz |
Full Subject Data
mw.neowiki.getMainSubject() returns all data for a Subject as a Lua table, enabling templates to render complete data views.
Professional Wiki
| Property | Type | Value(s) |
|---|---|---|
| Websites | url | https://professional.wiki, https://wikibase.consulting |
| Products | relation | NeoWiki, ProWiki |
| Brand Color | color | #32739b |
| Founded at | number | 2019 |
| World domination progress | number | 42 |
| Status | select | o1demo1aaaaaaa1 |
ACME Inc
| Property | Type | Value(s) |
|---|---|---|
| World domination progress | number | 42 |
| Products | relation | Foo, Bar, Baz |
| Founded at | number | 2005 |
| Websites | url | https://acme.example |
| Status | select | o1demo1aaaaaaa1 |
Child Subjects
mw.neowiki.getChildSubjects() returns all non-main Subjects on a page.
Child subjects on ACME Inc: Foo (Product), Bar (Product), Baz (Product)
Running Cypher Queries
mw.neowiki.query() runs a read-only Cypher query and returns each row as a Lua table keyed
by the RETURN aliases. Use it when a single-property lookup is not enough — for example
to join Subjects, sort results, or list all Subjects of a given type.
Listing all Companies
MATCH (n:Company) RETURN n.name, n.id
| n.id | n.name |
|---|---|
| s1demo1aaaaaaa1 | ACME Inc. |
| s1demo5ssssssz1 | Hallo Welt! GmbH |
| s1demo5sssssss1 | Professional Wiki GmbH |
| sEpjeGi3EjWM9dw | Main Pagegfsdgt |
| sEq1P1iwqG1s4UP | Fggxftgx |
| sEr2axw2QTXhVLk | KMA Knowledge Management Associates |
| s1demo1aaa2aaa1 | ACME Inc. 2 |
Parameterised query: Products available since a given year
Templates should always pass values via the params table instead of concatenating into the query string.
nw.query(
'MATCH (n:Product) WHERE n.`Available since` >= $year ' ..
'RETURN n.name AS name, n.`Available since` AS year ORDER BY year',
{ year = 2010 }
)
Products available since 2010:
| name | year |
|---|---|
| Bar | 2010 |
| Foo | 2010 |
| Baz | 2015 |
| Foo | 2015 |
| ProWiki | 2022 |
| NeoWiki | 2026 |
| Test Subject | 2026 |
| Exampleproduct | 2026 |
Inspecting a Schema
mw.neowiki.getSchema() returns a Schema definition as a Lua table, including all property names, types, and type-specific attributes. This enables templates to render or validate any schema without hardcoding property names.
Company
| Name | Type | Required | Details |
|---|---|---|---|
| Founded at | number | No | |
| Websites | url | No | multiple: true, uniqueItems: true |
| Main product | relation | No | targetSchema: Product, relation: Has main product |
| Products | relation | No | targetSchema: Product, relation: Has product |
| Status | select | Yes | options: Active, Inactive, Acquired, Dissolved |
| World domination progress | number | No | |
| Brand Color | color | No |
Employee
| Name | Type | Required | Details |
|---|---|---|---|
| LegalName | text | No | |
| WorkEmail | text | No | |
| EmploymentFte | number | No | min: 0, max: 100 |
| Skills | text | No | multiple: true, uniqueItems: true |
| Employer | relation | No | targetSchema: Company, relation: Employer |
Module Source
The demo module uses these mw.neowiki functions:
local nw = require( 'mw.neowiki' )
-- Read a single value (always returns a scalar or nil)
nw.getValue( 'Founded at' ) --> 2005
nw.getValue( 'Products' ) --> "Foo" (first value only)
-- Read all values (always returns a 1-indexed table or nil)
nw.getAll( 'Products' ) --> { "Foo", "Bar", "Baz" }
-- Get full subject data
local s = nw.getMainSubject( 'ACME Inc' )
s.id, s.label, s.schema, s.statements
-- Get child subjects
local children = nw.getChildSubjects( 'ACME Inc' )
-- Run a read-only Cypher query
local rows = nw.query(
'MATCH (n:Product) WHERE n.`Available since` >= $year RETURN n.name, n.`Available since`',
{ year = 2010 }
)
See Module:NeoWikiDemo for the full source.