Skip to content

Commit c1085f4

Browse files
committed
docs cleanup
1 parent 0a95fb8 commit c1085f4

File tree

1 file changed

+72
-28
lines changed

1 file changed

+72
-28
lines changed

docs/database.md

Lines changed: 72 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -537,12 +537,18 @@ using `pairs`,and converted to an SQL fragment similar to
537537
`db.escape_identifier(key) = db.escape_literal(value)`, then concatenated with
538538
the `AND` SQL operator.
539539

540-
$dual_code{[[
540+
$dual_code{lua=[[
541+
print(db.encode_clause({
542+
name = "Garf",
543+
color = db.list({"orange", "ginger"}),
544+
processed_at = db.NULL
545+
})) -- "color" IN ('orange', 'ginger') AND "processed_at" IS NULL AND "name" = 'Garf'
546+
]], moon=[[
541547
print db.encode_clause {
542548
name: "Garf"
543549
color: db.list {"orange", "ginger"}
544550
processed_at: db.NULL
545-
} --> "color" IN ('orange', 'ginger') AND "processed_at" IS NULL AND "name" = 'Garf'
551+
} -- "color" IN ('orange', 'ginger') AND "processed_at" IS NULL AND "name" = 'Garf'
546552
]]}
547553

548554

@@ -554,6 +560,27 @@ the mistake of accidentally providing `nil` in place of a value of `db.NULL`
554560
that results in generating a clause that matches a much wider range of data
555561
than desired.
556562

563+
$dual_code{lua=[[
564+
-- Example of incorrect usage that leads to an error
565+
local clause = {
566+
age = nil -- Mistakenly using nil instead of db.NULL, resulting in empty object
567+
}
568+
569+
-- This will throw an error because 'age' is nil, and the clause is an empty object
570+
local sql_fragment = db.encode_clause(clause)
571+
]], moon=[[
572+
-- Example of incorrect usage that leads to an error
573+
clause = {
574+
age: nil -- Mistakenly using nil instead of db.NULL, resulting in empty object
575+
}
576+
577+
-- This will throw an error because 'age' is nil, and the clause is an empty object
578+
sql_fragment = db.encode_clause clause
579+
]]}
580+
581+
582+
583+
557584
## Database Primitives
558585

559586
To make writing queries easier and safer, Lapis provides a set of basic
@@ -1146,30 +1173,47 @@ defaults of 0 and boolean false.
11461173
When a type is called like a function it takes one argument, a table of
11471174
options. The options include:
11481175

1149-
* `default: value` -- sets default value
1150-
* `null: boolean` -- determines if the column is `NOT NULL`
1151-
* `unique: boolean` -- determines if the column has a unique index
1152-
* `primary_key: boolean` -- determines if the column is the primary key
1153-
* `array: bool|number` -- makes the type an array (PostgreSQL Only), pass number to set how many dimensions the array is, `true` == `1`
1176+
$options_table{
1177+
{
1178+
name = "default",
1179+
description = "Sets default value. Use `db.NULL` to set a `NULL` default."
1180+
}, {
1181+
name = "null",
1182+
description = "Determines if the column is `NOT NULL`.",
1183+
default = '`false`'
1184+
}, {
1185+
name = "unique",
1186+
description = "Determines if the column has a unique index.",
1187+
default = '`false`'
1188+
}, {
1189+
name = "primary_key",
1190+
description = "Determines if the column is the primary key."
1191+
}, {
1192+
name = "array",
1193+
description = [[
1194+
Makes the type an array (PostgreSQL Only), pass number to set how many dimensions the array is, or set to `true` to make a 1 dimensional array.
1195+
]]
1196+
}
1197+
}
11541198

11551199
Here are some examples:
11561200

1157-
```lua
1201+
$dual_code{
1202+
lua=[[
11581203
types.integer({ default = 1, null = true }) --> integer DEFAULT 1
11591204
types.integer({ primary_key = true }) --> integer NOT NULL DEFAULT 0 PRIMARY KEY
11601205
types.text({ null = true }) --> text
11611206
types.varchar({ primary_key = true }) --> character varying(255) NOT NULL PRIMARY KEY
11621207
types.real({ array = true }) --> real[]
1163-
```
1164-
1165-
```moon
1208+
]],
1209+
moon=[[
11661210
types.integer default: 1, null: true --> integer DEFAULT 1
11671211
types.integer primary_key: true --> integer NOT NULL DEFAULT 0 PRIMARY KEY
11681212
types.text null: true --> text
11691213
types.varchar primary_key: true --> character varying(255) NOT NULL PRIMARY KEY
11701214
types.real array: true --> real[]
11711215
types.text array: 2 --> text[][]
1172-
```
1216+
]]}
11731217

11741218
> MySQL has a complete different type set than PostgreSQL, see [MySQL types](https://github.com/leafo/lapis/blob/master/lapis/db/mysql/schema.moon#L162)
11751219
@@ -1183,6 +1227,7 @@ We define migrations in our code as a table of functions where the key of each
11831227
function in the table is the name of the migration. You are free to name the
11841228
migrations anything but it's suggested to give them Unix timestamps as names:
11851229

1230+
11861231
$dual_code{
11871232
moon=[[
11881233
import add_column, create_index, types from require "lapis.db.schema"
@@ -1228,7 +1273,8 @@ migrations in the format described above.
12281273

12291274
Let's create this file with a single migration as an example.
12301275

1231-
```lua
1276+
$dual_code{
1277+
lua=[[
12321278
-- migrations.lua
12331279

12341280
local schema = require("lapis.db.schema")
@@ -1245,9 +1291,8 @@ return {
12451291
})
12461292
end
12471293
}
1248-
```
1249-
1250-
```moon
1294+
]],
1295+
moon=[[
12511296
-- migrations.moon
12521297

12531298
import create_table, types from require "lapis.db.schema"
@@ -1262,7 +1307,7 @@ import create_table, types from require "lapis.db.schema"
12621307
"PRIMARY KEY (id)"
12631308
}
12641309
}
1265-
```
1310+
]]}
12661311

12671312
After creating the file, ensure that it is compiled to Lua and run `lapis
12681313
migrate`. The command will first create the migrations table if it doesn't
@@ -1274,15 +1319,15 @@ Read more about [the migrate command](command_line.html#command-reference/lapis-
12741319

12751320
We can manually create the migrations table using the following code:
12761321

1277-
```lua
1322+
$dual_code{
1323+
lua=[[
12781324
local migrations = require("lapis.db.migrations")
12791325
migrations.create_migrations_table()
1280-
```
1281-
1282-
```moon
1326+
]],
1327+
moon=[[
12831328
migrations = require "lapis.db.migrations"
12841329
migrations.create_migrations_table!
1285-
```
1330+
]]}
12861331

12871332
It will execute the following SQL:
12881333

@@ -1296,15 +1341,15 @@ CREATE TABLE IF NOT EXISTS "lapis_migrations" (
12961341
Then we can manually run migrations with the following code:
12971342

12981343

1299-
```lua
1344+
$dual_code{
1345+
lua=[[
13001346
local migrations = require("lapis.db.migrations")
13011347
migrations.run_migrations(require("migrations"))
1302-
```
1303-
1304-
```moon
1348+
]],
1349+
moon=[[
13051350
import run_migrations from require "lapis.db.migrations"
13061351
run_migrations require "migrations"
1307-
```
1352+
]]}
13081353

13091354
## Database Helpers
13101355

@@ -1317,7 +1362,6 @@ Returns a date string formatted properly for insertion in the database.
13171362

13181363
The `time` argument is optional, will default to the current UTC time.
13191364

1320-
13211365
$dual_code{[[
13221366
date = db.format_date!
13231367
db.query "update things set published_at = ?", date

0 commit comments

Comments
 (0)