Skip to content

Commit 5fd4f74

Browse files
authored
Merge pull request #65 from codecrafters-io/CC-1122-ts-support
feat: Add Typescript support using bun runtime.
2 parents e45f23d + 14843e0 commit 5fd4f74

31 files changed

+415
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/sqlite.png)
2+
3+
This is a starting point for TypeScript solutions to the
4+
["Build Your Own SQLite" Challenge](https://codecrafters.io/challenges/sqlite).
5+
6+
In this challenge, you'll build a barebones SQLite implementation that supports
7+
basic SQL queries like `SELECT`. Along the way we'll learn about
8+
[SQLite's file format](https://www.sqlite.org/fileformat.html), how indexed data
9+
is
10+
[stored in B-trees](https://jvns.ca/blog/2014/10/02/how-does-sqlite-work-part-2-btrees/)
11+
and more.
12+
13+
**Note**: If you're viewing this repo on GitHub, head over to
14+
[codecrafters.io](https://codecrafters.io) to try the challenge.
15+
16+
# Passing the first stage
17+
18+
The entry point for your SQLite implementation is in `app/main.ts`. Study and
19+
uncomment the relevant code, and push your changes to pass the first stage:
20+
21+
```sh
22+
git add .
23+
git commit -m "pass 1st stage" # any msg
24+
git push origin master
25+
```
26+
27+
Time to move on to the next stage!
28+
29+
# Stage 2 & beyond
30+
31+
Note: This section is for stages 2 and beyond.
32+
33+
1. Ensure you have `bun (1.1)` installed locally
34+
1. Run `./your_sqlite3.sh` to run your program, which is implemented in
35+
`app/main.ts`.
36+
1. Commit your changes and run `git push origin master` to submit your solution
37+
to CodeCrafters. Test output will be streamed to your terminal.
38+
39+
# Sample Databases
40+
41+
To make it easy to test queries locally, we've added a sample database in the
42+
root of this repository: `sample.db`.
43+
44+
This contains two tables: `apples` & `oranges`. You can use this to test your
45+
implementation for the first 6 stages.
46+
47+
You can explore this database by running queries against it like this:
48+
49+
```sh
50+
$ sqlite3 sample.db "select id, name from apples"
51+
1|Granny Smith
52+
2|Fuji
53+
3|Honeycrisp
54+
4|Golden Delicious
55+
```
56+
57+
There are two other databases that you can use:
58+
59+
1. `superheroes.db`:
60+
- This is a small version of the test database used in the table-scan stage.
61+
- It contains one table: `superheroes`.
62+
- It is ~1MB in size.
63+
1. `companies.db`:
64+
- This is a small version of the test database used in the index-scan stage.
65+
- It contains one table: `companies`, and one index: `idx_companies_country`
66+
- It is ~7MB in size.
67+
68+
These aren't included in the repository because they're large in size. You can
69+
download them by running this script:
70+
71+
```sh
72+
./download_sample_databases.sh
73+
```
74+
75+
If the script doesn't work for some reason, you can download the databases
76+
directly from
77+
[codecrafters-io/sample-sqlite-databases](https://github.com/codecrafters-io/sample-sqlite-databases).
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { open } from 'fs/promises';
2+
import { constants } from 'fs';
3+
4+
const args = process.argv;
5+
const databaseFilePath: string = args[2]
6+
const command: string = args[3];
7+
8+
if (command === ".dbinfo") {
9+
const databaseFileHandler = await open(databaseFilePath, constants.O_RDONLY);
10+
const buffer: Uint8Array = new Uint8Array(100);
11+
await databaseFileHandler.read(buffer, 0, buffer.length, 0);
12+
13+
// You can use print statements as follows for debugging, they'll be visible when running tests.
14+
console.log("Logs from your program will appear here!");
15+
16+
// Uncomment this to pass the first stage
17+
// const pageSize = new DataView(buffer.buffer, 0, buffer.byteLength).getUint16(16);
18+
// console.log(`database page size: ${pageSize}`);
19+
20+
await databaseFileHandler.close();
21+
} else {
22+
throw new Error(`Unknown command ${command}`);
23+
}
2.33 KB
Binary file not shown.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Set this to true if you want debug logs.
2+
#
3+
# These can be VERY verbose, so we suggest turning them off
4+
# unless you really need them.
5+
debug: false
6+
7+
# Use this to change the TypeScript version used to run your code
8+
# on Codecrafters.
9+
#
10+
# Available versions: bun-1.1
11+
language_pack: bun-1.1
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/sh
2+
3+
echo "Downloading superheroes.db: ~1MB (used in stage 7)"
4+
curl -Lo superheroes.db https://raw.githubusercontent.com/codecrafters-io/sample-sqlite-databases/master/superheroes.db
5+
6+
echo "Downloading companies.db: ~7MB (used in stage 8)"
7+
curl -Lo companies.db https://raw.githubusercontent.com/codecrafters-io/sample-sqlite-databases/master/companies.db
8+
9+
echo "Sample databases downloaded."
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "@codecrafters/build-your-own-sqlite",
3+
"description": "Build your own SQLite challenge, from CodeCrafters",
4+
"type": "module",
5+
"scripts": {
6+
"dev": "bun run app/main.ts"
7+
},
8+
"dependencies": {
9+
"fs-extra": "^11.2.0"
10+
}
11+
}
16 KB
Binary file not shown.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/sh
2+
#
3+
# DON'T EDIT THIS!
4+
#
5+
# CodeCrafters uses this file to test your code. Don't make any changes here!
6+
#
7+
# DON'T EDIT THIS!
8+
exec bun run ./app/main.ts "$@"

0 commit comments

Comments
 (0)