Jump to content

Lua Data Access

From NeoWiki Demo
Revision as of 11:27, 20 April 2026 by NeoWiki (talk | contribs) (Importing NeoWiki demo data)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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) Acme Anvil Acme Anvil, Acme Rocket, Acme TNT

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
Founded at number 2019
World domination progress number 42
Status select o1demo1aaaaaaa1

ACME Inc

Property Type Value(s)
Websites url https://acme.example
Products relation Acme Anvil, Acme Rocket, Acme TNT
Is public boolean true
Offices relation ACME Amsterdam HQ, ACME Vienna Office
Departments relation ACME Engineering, ACME Sales, ACME Operations
Headquarters relation ACME Amsterdam HQ
Status select o1demo1aaaaaaa1
World domination progress number 42
Founded at number 2005

Child Subjects

mw.neowiki.getChildSubjects() returns all non-main Subjects on a page.

Child subjects on ACME Inc: No child subjects

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
s1demo5sssssss1 Professional Wiki GmbH
s1demo1aaaaaaa1 ACME Inc
s1demo5ssssssz1 Hallo Welt! 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
Acme Rocket 2010
Foo 2010
Acme TNT 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
Main product relation No targetSchema: Product, relation: Has main product
Products relation No targetSchema: Product, relation: Has product
Departments relation No targetSchema: Department, relation: Has department
Offices relation No targetSchema: Office, relation: Has office
Headquarters relation No targetSchema: Office, relation: Has headquarters
Status select Yes options: Active, Inactive, Acquired, Dissolved
World domination progress number No
Is public boolean 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.