-
-
Save Tezza48/2618a1d52e894d68e79161afcfa098cf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { CasualDB } from "https://deno.land/x/casualdb/mod.ts"; | |
// create an interface to describe the structure of your JSON | |
interface Schema { | |
posts: Array<{ | |
id: number; | |
title: string; | |
views: number; | |
}>; | |
user: { | |
name: string; | |
}; | |
} | |
const db = new CasualDB<Schema>(); // instantiate the db, casually 🤓 | |
await db.connect("./test-db.json"); // "connect" to the db (JSON file) | |
// (optional) seed it with data, if starting with an empty db | |
await db.seed({ | |
posts: [ | |
{ id: 1, title: "Post 1", views: 99 }, | |
{ id: 2, title: "Post 2", views: 30 }, | |
], | |
user: { name: "Camp Vanilla" }, | |
}); | |
const posts = await db.get<Schema['posts']>('posts'); // pass the interface key in order for type-checking to work | |
const postTitlesByViews = ( | |
posts | |
.sort(['views']) // sort by views (ascending) | |
.pick(['title']) // pick the title of every post | |
.value() // => ['Post 2', 'Post 1'] | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment