@@ -6,10 +6,10 @@ title: >
6
6
import { Code } from " @astrojs/starlight/components" ;
7
7
import { Aside } from " @astrojs/starlight/components" ;
8
8
9
- In this tutorial, we'll set up a coffee database, implement a custom TypeScript
10
- HTTP handler for performing vector searches, and a simple production-ready web app
11
- all in ~ 100 lines of code.
12
-
9
+ This tutorial is a short introduction to TrailBase and some of its features.
10
+ We'll bootstrap a database with coffee data, implement a custom TypeScript HTTP
11
+ handler for finding the best coffee matches using vector search, and deploy a
12
+ simple production-ready web app all in ~ 100 lines of code.
13
13
14
14
<div class = " flex justify-center" >
15
15
<div class = " w-[80%] shadow-lg " >
@@ -19,8 +19,8 @@ all in ~100 lines of code.
19
19
20
20
<div class = " h-[24px]" />
21
21
22
- This introductory tutorial is part of the main TrailBase code repository, which
23
- you can download to follow along running:
22
+ This introductory tutorial is part of TrailBase's main code repository, which
23
+ can be downloaded to follow along by running:
24
24
25
25
``` bash
26
26
$ git clone https://github.com/trailbaseio/trailbase.git
@@ -36,7 +36,8 @@ import GettingTrailBase from "./_getting_trailbase.md";
36
36
## Importing Data
37
37
38
38
We'll use the ` sqlite3 ` CLI directly to import
39
- ` examples/coffeesearch/arabica_data_cleaned.csv ` with the following SQL script[ ^ 1 ] :
39
+ ` examples/coffeesearch/arabica_data_cleaned.csv ` with the following SQL
40
+ script[ ^ 1 ] :
40
41
41
42
``` sql
42
43
-- First create the strictly typed "coffee" table.
@@ -72,47 +73,47 @@ FROM temporary;
72
73
DROP TABLE temporary;
73
74
```
74
75
75
- Note that we didn't initialize the vector ` embedding ` . This is because the
76
- ` sqlite3 ` CLI doesn't have the necessary extensions built-in.
77
- We'll update the entries to add the embedding later as part of a TrailBase
78
- migration .
76
+ Note that we didn't initialize the vector ` embedding ` . This is merely because
77
+ ` sqlite3 ` doesn't have the necessary extensions built-in.
78
+ We'll update the entries to add the embedding later as part of our initial
79
+ database migrations shortly [ ^ 2 ] .
79
80
80
- While in ` example/coffeesearch ` , you can run
81
+ From within the ` example/coffeesearch ` directory, you can execute the script
82
+ above and import the coffee data by running:
81
83
82
84
``` bash
83
85
$ cat import.sql | sqlite3 traildepot/data/main.db -
84
86
```
85
87
86
- to execute above script.
87
-
88
- After the initial data import and still in the same directory, let's start the
89
- ` trail ` binary for the first time:
88
+ After importing the data while still in the same directory, we can start the
89
+ ` trail ` server:
90
90
91
91
``` bash
92
92
$ trail run
93
93
```
94
94
95
- This will apply the migrations under ` examples/coffeesearch/traildepot/migrations ` , basically
95
+ Because ` trail ` starts for the first time the migrations in
96
+ ` traildepot/migrations ` will be applied, which are essentially:
96
97
97
98
``` sql
98
99
UPDATE coffee SET embedding = VECTOR(FORMAT(" [%f, %f, %f, %f]" , Aroma, Flavor, Acidity, Sweetness));
99
100
```
100
101
101
- to initialize the previously missing ` coffee.embedding ` for all records.
102
+ initializing the previously skipped ` coffee.embedding ` for all records.
102
103
103
104
104
105
## Custom TypeScript Endpoint
105
106
106
- Starting ` trail ` also executes JavaScript and TypeScript files under
107
- ` traildepot/scripts ` .
107
+ Any time you start ` trail run ` [ ^ 3 ] , JavaScript and TypeScript files under
108
+ ` traildepot/scripts ` will be executed .
108
109
109
110
<Aside type = " note" >
110
111
TrailBase will automatically transpile TypeScript to JavaScript which can
111
112
then execute on the underlying V8 engine. You don't need a separate build
112
113
step.
113
114
</Aside >
114
115
115
- We can use this to register custom HTTP endpoints among other things.
116
+ We can use this to register custom HTTP API routes among other things.
116
117
Let's have a quick look at ` examples/coffeesearch/traildepot/scripts/main.ts ` ,
117
118
which defines a ` /search ` API route we'll later use in our application to
118
119
find coffees most closely matching our desired coffee notes:
@@ -200,7 +201,7 @@ You can now check out your fuly self-contained app under
200
201
[ http://localhost:4000/ ] ( http://localhost:4000/ ) or browse the coffee data in
201
202
the [ admin dashboard] ( http://localhost:4000/_/admin ) .
202
203
203
- All we need to serve our application in production is[ ^ 2 ] :
204
+ All[ ^ 4 ] we need to serve our application in production is:
204
205
205
206
- the static ` trail ` binary,
206
207
- the ` traildepot ` folder containing the data and endpoints,
@@ -221,9 +222,11 @@ $ docker build -t coffee . && docker run -p 4000:4000 coffee
221
222
will speed-run this entire tutorial by building and starting the app listening
222
223
at [ http://localhost:4000/ ] ( http://localhost:4000/ ) .
223
224
224
- That's it. We hope this was a fun little introduction showcasing some of
225
- TrailBase's features. If you have any feedback, don't hesitate and reach
226
- out on [ GitHub] ( https://github.com/trailbaseio/trailbase ) .
225
+ That's it. We hope this was a fun little intro to some of TrailBase's features.
226
+ There's more we haven't touched on: CRUD APIs, auth, admin dash, file uploads,
227
+ just to name a few.
228
+ If you have any feedback, don't hesitate and reach out on
229
+ [ GitHub] ( https://github.com/trailbaseio/trailbase ) .
227
230
228
231
<div class = " h-[50px]" />
229
232
@@ -235,6 +238,17 @@ out on [GitHub](https://github.com/trailbaseio/trailbase).
235
238
[ download] ( https://www.sqlite.org/download.html ) pre-built binaries
236
239
237
240
[ ^ 2 ] :
241
+ Migrations are versioned SQL scripts that will be executed by the database
242
+ on first encounter to programmatically and consistently evolve your database
243
+ schema and data along with your code.
244
+ For example, when you add a new column you'll likely want all your
245
+ integration tests, development setups, and production deployments to add
246
+ the column so your application logic has a consistent schema to target.
247
+
248
+ [ ^ 3 ] :
249
+ Unless explicitly disabled.
250
+
251
+ [ ^ 4 ] :
238
252
To serve HTTPS you'll either need a reverse proxy in front to terminate TLS
239
253
or if you don't require end-to-end encryption (e.g. you're not using auth
240
254
or handling sensitive data) you can fall back to TLS termination via a CDN
0 commit comments