Database
self:define
Server
Defines the schema for a table, registering it for use with the database instance
Syntax
local status = self:define(table, columns)Defining a schema does not create the table in the database. Call self:sync after all schemas have been defined to synchronise and create any tables that do not already exist.
- Before sync — the schema is registered locally on the database instance only
- After sync — the table is created in the database if it does not already exist
Parameters
| Type | Name | Description |
|---|---|---|
string | table | Name of the table to be defined |
table | columns | Map of column names to their column definition Refer Definitions section |
Returns
| Type | Name | Description |
|---|---|---|
bool | status | true on successful execution, or false on failure |
Definitions
Each entry in the columns table is a key-value pair where the key is the column name and the value is a definition table with the following fields:
| Field | Type | Default | Description |
|---|---|---|---|
type | string | "VARCHAR(255)" | SQL column type: • "INT" / "INT UNSIGNED" / "BIGINT"• "FLOAT" / "DOUBLE"• "BOOLEAN"• "VARCHAR(n)" / "TEXT"• "DATETIME" / "TIMESTAMP" |
primary | bool | false | Whether this column is the primary key |
autoincrement | bool | false | Whether this column auto-increments Typically used alongside "INT UNSIGNED" primary keys |
nullable | bool | true | Whether this column allows NULL values |
Examples
local db = database.create("127.0.0.1", "root", "", "vital_sandbox")
--Define the players table schema
db:define("players", {
id = { type = "INT UNSIGNED", primary = true, autoincrement = true, nullable = false },
name = { type = "VARCHAR(64)", nullable = false },
score = { type = "INT", nullable = true }
})
--Sync all defined schemas to the database
db:sync()