Skip to content

Commit 0fa68a3

Browse files
committed
feat: add Typescript support using deno runtime.
Add package.json support.
1 parent 0418228 commit 0fa68a3

28 files changed

+392
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/
+77
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 `deno (1.42)` 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).
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const databaseFilePath: string = Deno.args[0];
2+
const command: string = Deno.args[1];
3+
4+
if (command === ".dbinfo") {
5+
const databaseFileHandler = await Deno.open(databaseFilePath, { read: true });
6+
const buffer = new Uint8Array(100);
7+
await databaseFileHandler.read(buffer);
8+
9+
// You can use print statements as follows for debugging, they'll be visible when running tests.
10+
console.log("Logs from your program will appear here!");
11+
12+
// Uncomment this to pass the first stage
13+
// const pageSize = new DataView(buffer.buffer, 0, buffer.byteLength).getUint16(16);
14+
// console.log(`database page size: ${pageSize}`);
15+
16+
databaseFileHandler.close();
17+
} else {
18+
throw new Error(`Unknown command ${command}`);
19+
}
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: deno-1.42
11+
language_pack: deno-1.42
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."
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "@codecrafters/build-your-own-sqlite",
3+
"description": "Build your own SQLite challenge, from CodeCrafters",
4+
"type": "module",
5+
"scripts": {
6+
"dev": "deno run --allow-all app/main.ts"
7+
},
8+
"dependencies": {}
9+
}
16 KB
Binary file not shown.
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 deno run --allow-all ./app/main.ts "$@"

course-definition.yml

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ languages:
4444
- slug: "zig"
4545
- slug: "java"
4646
release_status: "beta"
47+
- slug: "typescript"
48+
release_status: "beta"
4749

4850
marketing:
4951
difficulty: hard

dockerfiles/deno-1.42.Dockerfile

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
FROM denoland/deno:alpine-1.42.0
2+
3+
ENV CODECRAFTERS_DEPENDENCY_FILE_PATHS="package.json"
4+
5+
WORKDIR /app
6+
7+
COPY package.json ./
8+
# If users import dependencies in files foo.ts and bar.ts, unless those files are present while running deno cache, these dependencies will not be resolved.
9+
# Even if they are present in package.json#dependencies.
10+
COPY app/ ./app/
11+
RUN deno cache app/main.ts
12+
RUN rm -rf ./app/
13+
14+
RUN mkdir -p /app-cached
15+
# If the node_modules directory exists, move it to /app-cached
16+
RUN if [ -d "/app/node_modules" ]; then mv /app/node_modules /app-cached; fi
17+
18+
RUN printf "cd \${CODECRAFTERS_SUBMISSION_DIR} && deno cache app/main.ts" > /codecrafters-precompile.sh
19+
RUN chmod +x /codecrafters-precompile.sh
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/
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 `deno (1.42)` 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).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const databaseFilePath: string = Deno.args[0];
2+
const command: string = Deno.args[1];
3+
4+
if (command === ".dbinfo") {
5+
const databaseFileHandler = await Deno.open(databaseFilePath, { read: true });
6+
const buffer = new Uint8Array(100);
7+
await databaseFileHandler.read(buffer);
8+
9+
const pageSize = new DataView(buffer.buffer, 0, buffer.byteLength).getUint16(16);
10+
console.log(`database page size: ${pageSize}`);
11+
12+
databaseFileHandler.close();
13+
} else {
14+
throw new Error(`Unknown command ${command}`);
15+
}
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: deno-1.42
11+
language_pack: deno-1.42
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."
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "@codecrafters/build-your-own-sqlite",
3+
"description": "Build your own SQLite challenge, from CodeCrafters",
4+
"type": "module",
5+
"scripts": {
6+
"dev": "deno run --allow-all app/main.ts"
7+
},
8+
"dependencies": {}
9+
}
16 KB
Binary file not shown.
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 deno run --allow-all ./app/main.ts "$@"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
@@ -1,19 +1,15 @@
2+
const databaseFilePath: string = Deno.args[0];
3+
const command: string = Deno.args[1];
4+
5+
if (command === ".dbinfo") {
6+
const databaseFileHandler = await Deno.open(databaseFilePath, { read: true });
7+
const buffer = new Uint8Array(100);
8+
await databaseFileHandler.read(buffer);
9+
10+
- // You can use print statements as follows for debugging, they'll be visible when running tests.
11+
- console.log("Logs from your program will appear here!");
12+
-
13+
- // Uncomment this to pass the first stage
14+
- // const pageSize = new DataView(buffer.buffer, 0, buffer.byteLength).getUint16(16);
15+
- // console.log(`database page size: ${pageSize}`);
16+
+ const pageSize = new DataView(buffer.buffer, 0, buffer.byteLength).getUint16(16);
17+
+ console.log(`database page size: ${pageSize}`);
18+
19+
databaseFileHandler.close();
20+
} else {
21+
throw new Error(`Unknown command ${command}`);
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
The entry point for your SQLite implementation is in `app/main.ts`.
2+
3+
Study and uncomment the relevant code:
4+
5+
```typescript
6+
// Uncomment this to pass the first stage
7+
const pageSize = new DataView(buffer.buffer, 0, buffer.byteLength).getUint16(16);
8+
console.log(`database page size: ${pageSize}`);
9+
```
10+
11+
Push your changes to pass the first stage:
12+
13+
```
14+
git add .
15+
git commit -m "pass 1st stage" # any msg
16+
git push origin master
17+
```

starter-repository-definitions.yml

+25
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,28 @@
255255
template_attributes:
256256
required_executable: "java (21)"
257257
user_editable_file: "src/main/java/Main.java"
258+
259+
- language: typescript
260+
file_mappings:
261+
- source: starter_templates/README.md
262+
target: README.md
263+
- source: starter_templates/codecrafters.yml
264+
target: codecrafters.yml
265+
- source: starter_templates/sample.db
266+
target: sample.db
267+
should_skip_template_evaluation: true
268+
- source: starter_templates/download_sample_databases.sh
269+
target: download_sample_databases.sh
270+
- source: starter_templates/typescript/app/main.ts
271+
target: app/main.ts
272+
- source: starter_templates/typescript/package.json
273+
target: package.json
274+
- source: starter_templates/typescript/your_sqlite3.sh
275+
target: your_sqlite3.sh
276+
- source: starter_templates/.gitattributes
277+
target: .gitattributes
278+
- source: starter_templates/typescript/.gitignore
279+
target: .gitignore
280+
template_attributes:
281+
required_executable: "deno (1.42)"
282+
user_editable_file: "app/main.ts"

starter_templates/codecrafters.yml

+4
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,7 @@ language_pack: cpp-20
7979
# Available versions: zig-0.11
8080
language_pack: zig-0.11
8181
{{/language_is_zig}}
82+
{{#language_is_typescript}}
83+
# Available versions: deno-1.42
84+
language_pack: deno-1.42
85+
{{/language_is_typescript}}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

0 commit comments

Comments
 (0)