Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
Example: SQlite on EFS
Browse files Browse the repository at this point in the history
  • Loading branch information
fwang committed Oct 18, 2024
1 parent dc4d359 commit d2d1445
Show file tree
Hide file tree
Showing 6 changed files with 264 additions and 0 deletions.
175 changes: 175 additions & 0 deletions examples/aws-efs-sqlite/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore

# Logs

logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Caches

.cache

# Diagnostic reports (https://nodejs.org/api/report.html)

report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json

# Runtime data

pids
_.pid
_.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover

lib-cov

# Coverage directory used by tools like istanbul

coverage
*.lcov

# nyc test coverage

.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)

.grunt

# Bower dependency directory (https://bower.io/)

bower_components

# node-waf configuration

.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)

build/Release

# Dependency directories

node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)

web_modules/

# TypeScript cache

*.tsbuildinfo

# Optional npm cache directory

.npm

# Optional eslint cache

.eslintcache

# Optional stylelint cache

.stylelintcache

# Microbundle cache

.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history

.node_repl_history

# Output of 'npm pack'

*.tgz

# Yarn Integrity file

.yarn-integrity

# dotenv environment variable files

.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)

.parcel-cache

# Next.js build output

.next
out

# Nuxt.js build / generate output

.nuxt
dist

# Gatsby files

# Comment in the public line in if your project uses Gatsby and not Next.js

# https://nextjs.org/blog/next-9-1#public-directory-support

# public

# vuepress build output

.vuepress/dist

# vuepress v2.x temp and cache directory

.temp

# Docusaurus cache and generated files

.docusaurus

# Serverless directories

.serverless/

# FuseBox cache

.fusebox/

# DynamoDB Local files

.dynamodb/

# TernJS port file

.tern-port

# Stores VSCode versions used for testing VSCode extensions

.vscode-test

# yarn v2

.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# IntelliJ based IDEs
.idea

# Finder (MacOS) folder config
.DS_Store
24 changes: 24 additions & 0 deletions examples/aws-efs-sqlite/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import sqlite3 from "better-sqlite3";
const db = sqlite3("/mnt/efs/mydb.sqlite");

export const handler = async () => {
db.exec(`
CREATE TABLE IF NOT EXISTS visits (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp TEXT NOT NULL
);
`);

// Record a visit
db.prepare("INSERT INTO visits (timestamp) VALUES (CURRENT_TIMESTAMP)").run();

// Get recent visits
const visits = db
.prepare("SELECT * FROM visits ORDER BY timestamp DESC LIMIT 10")
.all();

return {
statusCode: 200,
body: JSON.stringify({ "10 Most recent visits": visits }, null, 2),
};
};
15 changes: 15 additions & 0 deletions examples/aws-efs-sqlite/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "aws-efs-sqlite",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"better-sqlite3": "^11.4.0"
}
}
17 changes: 17 additions & 0 deletions examples/aws-efs-sqlite/sst-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* This file is auto-generated by SST. Do not edit. */
/* tslint:disable */
/* eslint-disable */
import "sst"
export {}
declare module "sst" {
export interface Resource {
"MyFunction": {
"name": string
"type": "sst.aws.Function"
"url": string
}
"MyVpc": {
"type": "sst.aws.Vpc"
}
}
}
32 changes: 32 additions & 0 deletions examples/aws-efs-sqlite/sst.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/// <reference path="./.sst/platform/config.d.ts" />

export default $config({
app(input) {
return {
name: "aws-efs-sqlite",
removal: input?.stage === "production" ? "retain" : "remove",
home: "aws",
};
},
async run() {
// NAT Gateways are required for Lambda functions
const vpc = new sst.aws.Vpc("MyVpc", { nat: "managed" });

// Create an EFS file system to store the SQLite database
const efs = new sst.aws.Efs("MyEfs", { vpc });

// Create a Lambda function that queries the database
new sst.aws.Function("MyFunction", {
handler: "index.handler",
url: true,
vpc,
volume: {
efs,
path: "/mnt/efs",
},
nodejs: {
install: ["better-sqlite3"],
},
});
},
});
1 change: 1 addition & 0 deletions examples/aws-efs-sqlite/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}

0 comments on commit d2d1445

Please sign in to comment.