Svelte's compile-time model

Svelte is a free, open-source front-end JavaScript framework aimed at highly performant applications with small bundles. Its distinguishing trait is where the work happens: React and Vue do most of their work in the browser at runtime, while Svelte moves that work into a compile step that runs when you build the app, emitting optimized vanilla JavaScript.

The payoff is smaller bundles and better performance, plus a lower barrier for developers with limited exposure to modern tooling. Svelte stays close to the classic HTML, CSS and JS model, adding only a few extensions to HTML and JavaScript, and arguably asks you to learn fewer concepts than other framework options.

Scaffolding the project

The recommended way to initialize a Svelte app is degit, which sets everything up automatically. You need either yarn or npm installed.

# for Rollup
npx degit "sveltejs/sapper-template#rollup" hn-clone
# for webpack
npx degit "sveltejs/sapper-template#webpack" hn-clone
cd hn-clone
yarn #or just npm install
── package.json
├── README.md
├── rollup.config.js
├── scripts
│   └── setupTypeScript.js
├── src
│   ├── ambient.d.ts
│   ├── client.js
│   ├── components
│   │   └── Nav.svelte
│   ├── node_modules
│   │   └── images
│   │   	└── successkid.jpg
│   ├── routes
│   │   ├── about.svelte
│   │   ├── blog
│   │   │   ├── index.json.js
│   │   │   ├── index.svelte
│   │   │   ├── _posts.js
│   │   │   ├── [slug].json.js
│   │   │   └── [slug].svelte
│   │   ├── _error.svelte
│   │   ├── index.svelte
│   │   └── _layout.svelte
│   ├── server.js
│   ├── service-worker.js
│   └── template.html
├── static
│   ├── favicon.png
│   ├── global.css
│   ├── logo-192.png
│   ├── logo-512.png
│   └── manifest.json
└── yarn.lock

What we're building

The application is a basic HN clone that can create a post and comment on that post.

Working with Fauna

yarn add faunadb

Creating the database

All application data lives in a database, and creating one takes a single command or line of code. You'll need a Fauna account before continuing.

The Fauna Shell

Fauna's API is reachable through many interfaces: drivers for JS, GO, Java and others, a cloud console, local and cloud shells, and even a VS Code extension. We'll start with the local Fauna Shell, which is almost 100% interchangeable with the other interfaces.

npm install -g fauna-shell

After installing the shell with npm, log in with your Fauna credentials:

$ fauna cloud-login
Email: [email protected]: **********

You can then create the database:

fauna create-database hn-clone

Collections and documents

A Fauna database is made up of one or more collections. Data is stored as documents inside a collection, which is roughly the equivalent of an SQL table:

FaunaDB TerminologySQL Terminology
DatabaseDatabase
CollectionTable
DocumentRow
IndexIndex

The clone uses two collections: posts and comments.

To query the new database interactively, start a shell session:

fauna shell hn-clone

From there the database is fully operable:

$ fauna shell hn-clone

Starting shell for database hn-clone
Connected to https://db.fauna.com
Type Ctrl+D or .exit to exit the shell
hn-clone>

Create the posts collection with the default collection configuration by running:

hn-clone> CreateCollection({ name: "posts" })