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