Basic ComponentsDatabase TablesVersion: 4.58On this pageDatabase Tables mos. offers a build-in SQLite based solution to persist data. Furthermore it allows you to define sources for tables that the app will automatically keep up to date. In the ./db_table directory each file represents a database table with optional static data or an optional source. noteWhile mos. makes it easy to create and interact with persisent database tables basic knowledge about relational databases and SQLite are recommended to fully understand and make use of this feature. Database Table Definition The following example contains the definition for a persistent database table with the name users and the columns id (of type integer) and name (of type text). { "table": { "name": "users", "fields": [ { "name": "id", "type": "INTEGER" }, { "name": "name", "type": "TEXT" } ], "primaryKey": [ "id" ], "queries": { "byId": { "where": "id = ?" } } }} Lets go over the example and explain each field. name: The name of the database table used to reference the table from layers that allow you to render your data in the UI or from other queries. fields: A list of all columns the table will contain. A field consists of a name and a type. name: The name the column will be created with. type: The type the column will be assigned. Possible values are TEXT, INTEGER, REAL and DATETIME primaryKey: A list of all columns that will be used as the primary key for the table. If empty or not defined no primary key will be used. queries: See Queries cautionBy default mos. will drop and re-create a database table when it detects that its file changed leading to potential loss of data if the table is not making use of static data or a source. Queries In the queries map, named queries can be defined that can later be used in other parts of the config, such as getting data to display for the grid layer. You define queries by adding a new object to the queries map where the key of the object is the name of the query. When defining a new query you have two options: Use pre-defined fields to filter, order and limit your result and let mos. build the actual query: { "table": { "name": "users", "fields": [ { "name": "name", "type": "TEXT" }, { "name": "isAdmin", "type": "INTEGER" } ], "queries": { "admins": { "where": "isAdmin = 1", "orderBy": "name ASC", "limit": 5 } } }} Use a completly custom query: { "queries": { "admins": { "custom": "SELECT * FROM users WHERE isAdmin = 1 ORDER BY name ASC LIMIT 5;" } }} All queries may be parametrized using the typical ? syntax allowing you to pass values to the query when executing it as shown in the following example. Definition of a query with 2 parameters. { "queries": { "admins": { "custom": "SELECT * FROM users WHERE isAdmin = ? ORDER BY name ASC LIMIT ?;" } }} The parameters can be passed using the queryParams parameter when referencing the query from other parts of the configuration later. The parameters are bound in their respective order in the array. It is also possbible to re-use a single parameter multiple times in the same query by indexing the ? like ?1, ?2. { "table": "users", "query": "admins", "queryParams": [ "1", "10" ]} Custom Functions