@hivehub/rulebook 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +191 -0
- package/README.md +539 -0
- package/dist/agents/claude-code.d.ts +69 -0
- package/dist/agents/claude-code.d.ts.map +1 -0
- package/dist/agents/claude-code.js +180 -0
- package/dist/agents/claude-code.js.map +1 -0
- package/dist/agents/cursor-agent.d.ts +184 -0
- package/dist/agents/cursor-agent.d.ts.map +1 -0
- package/dist/agents/cursor-agent.js +299 -0
- package/dist/agents/cursor-agent.js.map +1 -0
- package/dist/agents/gemini-cli.d.ts +69 -0
- package/dist/agents/gemini-cli.d.ts.map +1 -0
- package/dist/agents/gemini-cli.js +180 -0
- package/dist/agents/gemini-cli.js.map +1 -0
- package/dist/cli/commands.d.ts +57 -0
- package/dist/cli/commands.d.ts.map +1 -0
- package/dist/cli/commands.js +1370 -0
- package/dist/cli/commands.js.map +1 -0
- package/dist/cli/docs-prompts.d.ts +3 -0
- package/dist/cli/docs-prompts.d.ts.map +1 -0
- package/dist/cli/docs-prompts.js +45 -0
- package/dist/cli/docs-prompts.js.map +1 -0
- package/dist/cli/prompts.d.ts +6 -0
- package/dist/cli/prompts.d.ts.map +1 -0
- package/dist/cli/prompts.js +376 -0
- package/dist/cli/prompts.js.map +1 -0
- package/dist/core/agent-manager.d.ts +89 -0
- package/dist/core/agent-manager.d.ts.map +1 -0
- package/dist/core/agent-manager.js +546 -0
- package/dist/core/agent-manager.js.map +1 -0
- package/dist/core/auto-fixer.d.ts +14 -0
- package/dist/core/auto-fixer.d.ts.map +1 -0
- package/dist/core/auto-fixer.js +207 -0
- package/dist/core/auto-fixer.js.map +1 -0
- package/dist/core/changelog-generator.d.ts +44 -0
- package/dist/core/changelog-generator.d.ts.map +1 -0
- package/dist/core/changelog-generator.js +222 -0
- package/dist/core/changelog-generator.js.map +1 -0
- package/dist/core/cli-bridge.d.ts +113 -0
- package/dist/core/cli-bridge.d.ts.map +1 -0
- package/dist/core/cli-bridge.js +1094 -0
- package/dist/core/cli-bridge.js.map +1 -0
- package/dist/core/config-manager.d.ts +65 -0
- package/dist/core/config-manager.d.ts.map +1 -0
- package/dist/core/config-manager.js +266 -0
- package/dist/core/config-manager.js.map +1 -0
- package/dist/core/coverage-checker.d.ts +14 -0
- package/dist/core/coverage-checker.d.ts.map +1 -0
- package/dist/core/coverage-checker.js +176 -0
- package/dist/core/coverage-checker.js.map +1 -0
- package/dist/core/custom-templates.d.ts +27 -0
- package/dist/core/custom-templates.d.ts.map +1 -0
- package/dist/core/custom-templates.js +122 -0
- package/dist/core/custom-templates.js.map +1 -0
- package/dist/core/dependency-checker.d.ts +21 -0
- package/dist/core/dependency-checker.d.ts.map +1 -0
- package/dist/core/dependency-checker.js +247 -0
- package/dist/core/dependency-checker.js.map +1 -0
- package/dist/core/detector.d.ts +3 -0
- package/dist/core/detector.d.ts.map +1 -0
- package/dist/core/detector.js +1443 -0
- package/dist/core/detector.js.map +1 -0
- package/dist/core/docs-generator.d.ts +9 -0
- package/dist/core/docs-generator.d.ts.map +1 -0
- package/dist/core/docs-generator.js +531 -0
- package/dist/core/docs-generator.js.map +1 -0
- package/dist/core/generator.d.ts +16 -0
- package/dist/core/generator.d.ts.map +1 -0
- package/dist/core/generator.js +561 -0
- package/dist/core/generator.js.map +1 -0
- package/dist/core/gitignore-generator.d.ts +13 -0
- package/dist/core/gitignore-generator.d.ts.map +1 -0
- package/dist/core/gitignore-generator.js +307 -0
- package/dist/core/gitignore-generator.js.map +1 -0
- package/dist/core/health-scorer.d.ts +22 -0
- package/dist/core/health-scorer.d.ts.map +1 -0
- package/dist/core/health-scorer.js +395 -0
- package/dist/core/health-scorer.js.map +1 -0
- package/dist/core/logger.d.ts +116 -0
- package/dist/core/logger.d.ts.map +1 -0
- package/dist/core/logger.js +289 -0
- package/dist/core/logger.js.map +1 -0
- package/dist/core/merger.d.ts +6 -0
- package/dist/core/merger.d.ts.map +1 -0
- package/dist/core/merger.js +131 -0
- package/dist/core/merger.js.map +1 -0
- package/dist/core/migrator.d.ts +19 -0
- package/dist/core/migrator.d.ts.map +1 -0
- package/dist/core/migrator.js +102 -0
- package/dist/core/migrator.js.map +1 -0
- package/dist/core/minimal-scaffolder.d.ts +8 -0
- package/dist/core/minimal-scaffolder.d.ts.map +1 -0
- package/dist/core/minimal-scaffolder.js +51 -0
- package/dist/core/minimal-scaffolder.js.map +1 -0
- package/dist/core/modern-console-new.d.ts +81 -0
- package/dist/core/modern-console-new.d.ts.map +1 -0
- package/dist/core/modern-console-new.js +340 -0
- package/dist/core/modern-console-new.js.map +1 -0
- package/dist/core/modern-console.d.ts +99 -0
- package/dist/core/modern-console.d.ts.map +1 -0
- package/dist/core/modern-console.js +568 -0
- package/dist/core/modern-console.js.map +1 -0
- package/dist/core/openspec-manager.d.ts +133 -0
- package/dist/core/openspec-manager.d.ts.map +1 -0
- package/dist/core/openspec-manager.js +605 -0
- package/dist/core/openspec-manager.js.map +1 -0
- package/dist/core/openspec-migrator.d.ts +27 -0
- package/dist/core/openspec-migrator.d.ts.map +1 -0
- package/dist/core/openspec-migrator.js +255 -0
- package/dist/core/openspec-migrator.js.map +1 -0
- package/dist/core/task-manager.d.ts +65 -0
- package/dist/core/task-manager.d.ts.map +1 -0
- package/dist/core/task-manager.js +318 -0
- package/dist/core/task-manager.js.map +1 -0
- package/dist/core/test-task-manager.d.ts +49 -0
- package/dist/core/test-task-manager.d.ts.map +1 -0
- package/dist/core/test-task-manager.js +121 -0
- package/dist/core/test-task-manager.js.map +1 -0
- package/dist/core/validator.d.ts +21 -0
- package/dist/core/validator.d.ts.map +1 -0
- package/dist/core/validator.js +177 -0
- package/dist/core/validator.js.map +1 -0
- package/dist/core/version-bumper.d.ts +19 -0
- package/dist/core/version-bumper.d.ts.map +1 -0
- package/dist/core/version-bumper.js +180 -0
- package/dist/core/version-bumper.js.map +1 -0
- package/dist/core/watcher.d.ts +9 -0
- package/dist/core/watcher.d.ts.map +1 -0
- package/dist/core/watcher.js +22 -0
- package/dist/core/watcher.js.map +1 -0
- package/dist/core/workflow-generator.d.ts +10 -0
- package/dist/core/workflow-generator.d.ts.map +1 -0
- package/dist/core/workflow-generator.js +279 -0
- package/dist/core/workflow-generator.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +159 -0
- package/dist/index.js.map +1 -0
- package/dist/mcp/handlers/archive-task.d.ts +17 -0
- package/dist/mcp/handlers/archive-task.d.ts.map +1 -0
- package/dist/mcp/handlers/archive-task.js +36 -0
- package/dist/mcp/handlers/archive-task.js.map +1 -0
- package/dist/mcp/handlers/create-task.d.ts +17 -0
- package/dist/mcp/handlers/create-task.d.ts.map +1 -0
- package/dist/mcp/handlers/create-task.js +56 -0
- package/dist/mcp/handlers/create-task.js.map +1 -0
- package/dist/mcp/handlers/list-tasks.d.ts +22 -0
- package/dist/mcp/handlers/list-tasks.d.ts.map +1 -0
- package/dist/mcp/handlers/list-tasks.js +42 -0
- package/dist/mcp/handlers/list-tasks.js.map +1 -0
- package/dist/mcp/handlers/show-task.d.ts +25 -0
- package/dist/mcp/handlers/show-task.d.ts.map +1 -0
- package/dist/mcp/handlers/show-task.js +43 -0
- package/dist/mcp/handlers/show-task.js.map +1 -0
- package/dist/mcp/handlers/update-task.d.ts +17 -0
- package/dist/mcp/handlers/update-task.d.ts.map +1 -0
- package/dist/mcp/handlers/update-task.js +35 -0
- package/dist/mcp/handlers/update-task.js.map +1 -0
- package/dist/mcp/handlers/validate-task.d.ts +15 -0
- package/dist/mcp/handlers/validate-task.d.ts.map +1 -0
- package/dist/mcp/handlers/validate-task.js +27 -0
- package/dist/mcp/handlers/validate-task.js.map +1 -0
- package/dist/mcp/rulebook-config.d.ts +22 -0
- package/dist/mcp/rulebook-config.d.ts.map +1 -0
- package/dist/mcp/rulebook-config.js +65 -0
- package/dist/mcp/rulebook-config.js.map +1 -0
- package/dist/mcp/rulebook-server.d.ts +4 -0
- package/dist/mcp/rulebook-server.d.ts.map +1 -0
- package/dist/mcp/rulebook-server.js +246 -0
- package/dist/mcp/rulebook-server.js.map +1 -0
- package/dist/types.d.ts +190 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/file-system.d.ts +9 -0
- package/dist/utils/file-system.d.ts.map +1 -0
- package/dist/utils/file-system.js +51 -0
- package/dist/utils/file-system.js.map +1 -0
- package/dist/utils/git-hooks.d.ts +8 -0
- package/dist/utils/git-hooks.d.ts.map +1 -0
- package/dist/utils/git-hooks.js +440 -0
- package/dist/utils/git-hooks.js.map +1 -0
- package/dist/utils/rulesignore.d.ts +9 -0
- package/dist/utils/rulesignore.d.ts.map +1 -0
- package/dist/utils/rulesignore.js +42 -0
- package/dist/utils/rulesignore.js.map +1 -0
- package/package.json +106 -0
- package/templates/cli/AIDER.md +49 -0
- package/templates/cli/AMAZON_Q.md +25 -0
- package/templates/cli/AUGGIE.md +32 -0
- package/templates/cli/CLAUDE.md +32 -0
- package/templates/cli/CLAUDE_CODE.md +35 -0
- package/templates/cli/CLINE.md +32 -0
- package/templates/cli/CODEBUDDY.md +20 -0
- package/templates/cli/CODEIUM.md +20 -0
- package/templates/cli/CODEX.md +21 -0
- package/templates/cli/CONTINUE.md +34 -0
- package/templates/cli/CURSOR_CLI.md +28 -0
- package/templates/cli/FACTORY.md +18 -0
- package/templates/cli/GEMINI.md +35 -0
- package/templates/cli/KILOCODE.md +18 -0
- package/templates/cli/OPENCODE.md +18 -0
- package/templates/cli/_GENERIC_TEMPLATE.md +29 -0
- package/templates/commands/rulebook-task-apply.md +67 -0
- package/templates/commands/rulebook-task-archive.md +70 -0
- package/templates/commands/rulebook-task-create.md +93 -0
- package/templates/commands/rulebook-task-list.md +42 -0
- package/templates/commands/rulebook-task-show.md +52 -0
- package/templates/commands/rulebook-task-validate.md +53 -0
- package/templates/core/AGENT_AUTOMATION.md +184 -0
- package/templates/core/DAG.md +304 -0
- package/templates/core/DOCUMENTATION_RULES.md +37 -0
- package/templates/core/QUALITY_ENFORCEMENT.md +68 -0
- package/templates/core/RULEBOOK.md +1874 -0
- package/templates/frameworks/ANGULAR.md +36 -0
- package/templates/frameworks/DJANGO.md +83 -0
- package/templates/frameworks/ELECTRON.md +147 -0
- package/templates/frameworks/FLASK.md +38 -0
- package/templates/frameworks/FLUTTER.md +55 -0
- package/templates/frameworks/JQUERY.md +32 -0
- package/templates/frameworks/LARAVEL.md +38 -0
- package/templates/frameworks/NESTJS.md +43 -0
- package/templates/frameworks/NEXTJS.md +127 -0
- package/templates/frameworks/NUXT.md +40 -0
- package/templates/frameworks/RAILS.md +66 -0
- package/templates/frameworks/REACT.md +38 -0
- package/templates/frameworks/REACT_NATIVE.md +47 -0
- package/templates/frameworks/SPRING.md +39 -0
- package/templates/frameworks/SYMFONY.md +36 -0
- package/templates/frameworks/VUE.md +36 -0
- package/templates/frameworks/ZEND.md +35 -0
- package/templates/git/CI_CD_PATTERNS.md +661 -0
- package/templates/git/GITHUB_ACTIONS.md +728 -0
- package/templates/git/GITLAB_CI.md +730 -0
- package/templates/git/GIT_WORKFLOW.md +1157 -0
- package/templates/git/SECRETS_MANAGEMENT.md +585 -0
- package/templates/hooks/COMMIT_MSG.md +530 -0
- package/templates/hooks/POST_CHECKOUT.md +546 -0
- package/templates/hooks/PREPARE_COMMIT_MSG.md +619 -0
- package/templates/hooks/PRE_COMMIT.md +414 -0
- package/templates/hooks/PRE_PUSH.md +601 -0
- package/templates/hooks/csharp-pre-commit.sh +23 -0
- package/templates/hooks/csharp-pre-push.sh +23 -0
- package/templates/hooks/dart-pre-commit.sh +30 -0
- package/templates/hooks/dart-pre-push.sh +25 -0
- package/templates/hooks/elixir-pre-commit.sh +32 -0
- package/templates/hooks/elixir-pre-push.sh +31 -0
- package/templates/hooks/erlang-pre-commit.sh +30 -0
- package/templates/hooks/erlang-pre-push.sh +37 -0
- package/templates/hooks/go-pre-commit.sh +40 -0
- package/templates/hooks/go-pre-push.sh +31 -0
- package/templates/hooks/haskell-pre-commit.sh +41 -0
- package/templates/hooks/haskell-pre-push.sh +37 -0
- package/templates/hooks/java-pre-commit.sh +34 -0
- package/templates/hooks/java-pre-push.sh +24 -0
- package/templates/hooks/kotlin-pre-commit.sh +32 -0
- package/templates/hooks/kotlin-pre-push.sh +16 -0
- package/templates/hooks/php-pre-commit.sh +36 -0
- package/templates/hooks/php-pre-push.sh +26 -0
- package/templates/hooks/python-pre-commit.sh +51 -0
- package/templates/hooks/python-pre-push.sh +25 -0
- package/templates/hooks/ruby-pre-commit.sh +33 -0
- package/templates/hooks/ruby-pre-push.sh +32 -0
- package/templates/hooks/rust-pre-commit.sh +30 -0
- package/templates/hooks/rust-pre-push.sh +30 -0
- package/templates/hooks/scala-pre-commit.sh +32 -0
- package/templates/hooks/scala-pre-push.sh +24 -0
- package/templates/hooks/swift-pre-commit.sh +25 -0
- package/templates/hooks/swift-pre-push.sh +23 -0
- package/templates/hooks/typescript-pre-commit.sh +37 -0
- package/templates/hooks/typescript-pre-push.sh +36 -0
- package/templates/ides/COPILOT.md +37 -0
- package/templates/ides/CURSOR.md +43 -0
- package/templates/ides/JETBRAINS_AI.md +35 -0
- package/templates/ides/REPLIT.md +36 -0
- package/templates/ides/TABNINE.md +29 -0
- package/templates/ides/VSCODE.md +40 -0
- package/templates/ides/WINDSURF.md +36 -0
- package/templates/ides/ZED.md +32 -0
- package/templates/languages/ADA.md +58 -0
- package/templates/languages/C.md +333 -0
- package/templates/languages/CPP.md +743 -0
- package/templates/languages/CSHARP.md +417 -0
- package/templates/languages/DART.md +332 -0
- package/templates/languages/ELIXIR.md +454 -0
- package/templates/languages/ERLANG.md +361 -0
- package/templates/languages/GO.md +645 -0
- package/templates/languages/HASKELL.md +177 -0
- package/templates/languages/JAVA.md +607 -0
- package/templates/languages/JAVASCRIPT.md +631 -0
- package/templates/languages/JULIA.md +97 -0
- package/templates/languages/KOTLIN.md +511 -0
- package/templates/languages/LISP.md +100 -0
- package/templates/languages/LUA.md +74 -0
- package/templates/languages/OBJECTIVEC.md +90 -0
- package/templates/languages/PHP.md +416 -0
- package/templates/languages/PYTHON.md +682 -0
- package/templates/languages/R.md +350 -0
- package/templates/languages/RUBY.md +421 -0
- package/templates/languages/RUST.md +477 -0
- package/templates/languages/SAS.md +73 -0
- package/templates/languages/SCALA.md +348 -0
- package/templates/languages/SOLIDITY.md +580 -0
- package/templates/languages/SQL.md +137 -0
- package/templates/languages/SWIFT.md +466 -0
- package/templates/languages/TYPESCRIPT.md +591 -0
- package/templates/languages/ZIG.md +265 -0
- package/templates/modules/ATLASSIAN.md +255 -0
- package/templates/modules/CONTEXT7.md +54 -0
- package/templates/modules/FIGMA.md +267 -0
- package/templates/modules/GITHUB_MCP.md +64 -0
- package/templates/modules/GRAFANA.md +328 -0
- package/templates/modules/NOTION.md +247 -0
- package/templates/modules/PLAYWRIGHT.md +90 -0
- package/templates/modules/RULEBOOK_MCP.md +156 -0
- package/templates/modules/SERENA.md +337 -0
- package/templates/modules/SUPABASE.md +223 -0
- package/templates/modules/SYNAP.md +69 -0
- package/templates/modules/VECTORIZER.md +63 -0
- package/templates/services/AZURE_BLOB.md +184 -0
- package/templates/services/CASSANDRA.md +239 -0
- package/templates/services/DYNAMODB.md +308 -0
- package/templates/services/ELASTICSEARCH.md +347 -0
- package/templates/services/GCS.md +178 -0
- package/templates/services/INFLUXDB.md +265 -0
- package/templates/services/KAFKA.md +341 -0
- package/templates/services/MARIADB.md +183 -0
- package/templates/services/MEMCACHED.md +242 -0
- package/templates/services/MINIO.md +201 -0
- package/templates/services/MONGODB.md +268 -0
- package/templates/services/MYSQL.md +358 -0
- package/templates/services/NEO4J.md +247 -0
- package/templates/services/ORACLE.md +290 -0
- package/templates/services/POSTGRESQL.md +326 -0
- package/templates/services/RABBITMQ.md +286 -0
- package/templates/services/REDIS.md +292 -0
- package/templates/services/S3.md +298 -0
- package/templates/services/SQLITE.md +294 -0
- package/templates/services/SQLSERVER.md +294 -0
- package/templates/workflows/codespell.yml +31 -0
- package/templates/workflows/cpp-lint.yml +47 -0
- package/templates/workflows/cpp-publish.yml +119 -0
- package/templates/workflows/cpp-test.yml +77 -0
- package/templates/workflows/dotnet-lint.yml +29 -0
- package/templates/workflows/dotnet-publish.yml +40 -0
- package/templates/workflows/dotnet-test.yml +41 -0
- package/templates/workflows/elixir-lint.yml +45 -0
- package/templates/workflows/elixir-publish.yml +49 -0
- package/templates/workflows/elixir-test.yml +54 -0
- package/templates/workflows/erlang-lint.yml +47 -0
- package/templates/workflows/erlang-test.yml +62 -0
- package/templates/workflows/go-lint.yml +39 -0
- package/templates/workflows/go-publish.yml +95 -0
- package/templates/workflows/go-test.yml +59 -0
- package/templates/workflows/java-lint.yml +60 -0
- package/templates/workflows/java-publish.yml +120 -0
- package/templates/workflows/java-test.yml +85 -0
- package/templates/workflows/kotlin-lint.yml +34 -0
- package/templates/workflows/kotlin-publish.yml +56 -0
- package/templates/workflows/kotlin-test.yml +48 -0
- package/templates/workflows/php-lint.yml +39 -0
- package/templates/workflows/php-publish.yml +50 -0
- package/templates/workflows/php-test.yml +54 -0
- package/templates/workflows/python-lint.yml +47 -0
- package/templates/workflows/python-publish.yml +91 -0
- package/templates/workflows/python-test.yml +59 -0
- package/templates/workflows/rust-lint.yml +54 -0
- package/templates/workflows/rust-publish.yml +66 -0
- package/templates/workflows/rust-test.yml +75 -0
- package/templates/workflows/solidity-lint.yml +41 -0
- package/templates/workflows/solidity-test.yml +47 -0
- package/templates/workflows/swift-lint.yml +32 -0
- package/templates/workflows/swift-publish.yml +58 -0
- package/templates/workflows/swift-test.yml +44 -0
- package/templates/workflows/typescript-lint.yml +61 -0
- package/templates/workflows/typescript-publish.yml +60 -0
- package/templates/workflows/typescript-test.yml +73 -0
- package/templates/workflows/zig-lint.yml +27 -0
- package/templates/workflows/zig-test.yml +40 -0
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
<!-- AZURE_BLOB:START -->
|
|
2
|
+
# Azure Blob Storage Instructions
|
|
3
|
+
|
|
4
|
+
**CRITICAL**: Use Azure Blob Storage for object storage, file uploads, static assets, and backup storage in Azure cloud.
|
|
5
|
+
|
|
6
|
+
## Core Features
|
|
7
|
+
|
|
8
|
+
### Connection
|
|
9
|
+
```typescript
|
|
10
|
+
// Using @azure/storage-blob
|
|
11
|
+
import { BlobServiceClient, StorageSharedKeyCredential } from '@azure/storage-blob'
|
|
12
|
+
|
|
13
|
+
const accountName = process.env.AZURE_STORAGE_ACCOUNT_NAME || ''
|
|
14
|
+
const accountKey = process.env.AZURE_STORAGE_ACCOUNT_KEY || ''
|
|
15
|
+
|
|
16
|
+
const sharedKeyCredential = new StorageSharedKeyCredential(accountName, accountKey)
|
|
17
|
+
const blobServiceClient = new BlobServiceClient(
|
|
18
|
+
`https://${accountName}.blob.core.windows.net`,
|
|
19
|
+
sharedKeyCredential
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
const containerClient = blobServiceClient.getContainerClient(
|
|
23
|
+
process.env.AZURE_CONTAINER_NAME || 'mycontainer'
|
|
24
|
+
)
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### Basic Operations
|
|
28
|
+
```typescript
|
|
29
|
+
// Create container
|
|
30
|
+
await containerClient.createIfNotExists({
|
|
31
|
+
access: 'blob', // 'blob', 'container', or 'private'
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
// Upload blob
|
|
35
|
+
const blockBlobClient = containerClient.getBlockBlobClient('path/to/file.jpg')
|
|
36
|
+
await blockBlobClient.upload(fileBuffer, fileBuffer.length, {
|
|
37
|
+
blobHTTPHeaders: {
|
|
38
|
+
blobContentType: 'image/jpeg',
|
|
39
|
+
},
|
|
40
|
+
metadata: {
|
|
41
|
+
userId: '123',
|
|
42
|
+
originalName: 'photo.jpg',
|
|
43
|
+
},
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
// Download blob
|
|
47
|
+
const downloadResponse = await blockBlobClient.download()
|
|
48
|
+
const fileContent = await streamToBuffer(downloadResponse.readableStreamBody)
|
|
49
|
+
|
|
50
|
+
// Delete blob
|
|
51
|
+
await blockBlobClient.delete()
|
|
52
|
+
|
|
53
|
+
// List blobs
|
|
54
|
+
for await (const blob of containerClient.listBlobsFlat()) {
|
|
55
|
+
console.log(blob.name, blob.properties.contentLength)
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Advanced Features
|
|
60
|
+
```typescript
|
|
61
|
+
// Generate SAS URL
|
|
62
|
+
import { generateBlobSASQueryParameters, BlobSASPermissions, StorageSharedKeyCredential } from '@azure/storage-blob'
|
|
63
|
+
|
|
64
|
+
const sasToken = generateBlobSASQueryParameters(
|
|
65
|
+
{
|
|
66
|
+
containerName: 'mycontainer',
|
|
67
|
+
blobName: 'path/to/file.jpg',
|
|
68
|
+
permissions: BlobSASPermissions.parse('r'), // read
|
|
69
|
+
startsOn: new Date(),
|
|
70
|
+
expiresOn: new Date(new Date().valueOf() + 3600 * 1000), // 1 hour
|
|
71
|
+
},
|
|
72
|
+
sharedKeyCredential
|
|
73
|
+
).toString()
|
|
74
|
+
|
|
75
|
+
const sasUrl = `https://${accountName}.blob.core.windows.net/mycontainer/path/to/file.jpg?${sasToken}`
|
|
76
|
+
|
|
77
|
+
// Copy blob
|
|
78
|
+
const sourceBlobClient = containerClient.getBlockBlobClient('source/file.jpg')
|
|
79
|
+
const destBlobClient = containerClient.getBlockBlobClient('dest/file.jpg')
|
|
80
|
+
await destBlobClient.beginCopyFromURL(sourceBlobClient.url)
|
|
81
|
+
|
|
82
|
+
// Set blob metadata
|
|
83
|
+
await blockBlobClient.setMetadata({
|
|
84
|
+
category: 'profile',
|
|
85
|
+
uploadedBy: 'user-123',
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
// Get blob properties
|
|
89
|
+
const properties = await blockBlobClient.getProperties()
|
|
90
|
+
console.log(properties.contentType, properties.contentLength, properties.metadata)
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Common Patterns
|
|
94
|
+
|
|
95
|
+
### File Upload Handler
|
|
96
|
+
```typescript
|
|
97
|
+
async function uploadFile(file: Buffer, filename: string, userId: string) {
|
|
98
|
+
const blobName = `users/${userId}/${Date.now()}-${filename}`
|
|
99
|
+
const blobClient = containerClient.getBlockBlobClient(blobName)
|
|
100
|
+
|
|
101
|
+
await blobClient.upload(file, file.length, {
|
|
102
|
+
blobHTTPHeaders: {
|
|
103
|
+
blobContentType: getContentType(filename),
|
|
104
|
+
},
|
|
105
|
+
metadata: {
|
|
106
|
+
userId,
|
|
107
|
+
originalName: filename,
|
|
108
|
+
uploadedAt: new Date().toISOString(),
|
|
109
|
+
},
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
return {
|
|
113
|
+
blobName,
|
|
114
|
+
url: blobClient.url,
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### Temporary Access URL
|
|
120
|
+
```typescript
|
|
121
|
+
async function generateTemporaryUrl(blobName: string, expiresInMinutes: number = 60) {
|
|
122
|
+
const blobClient = containerClient.getBlockBlobClient(blobName)
|
|
123
|
+
const sasToken = generateBlobSASQueryParameters(
|
|
124
|
+
{
|
|
125
|
+
containerName: containerClient.containerName,
|
|
126
|
+
blobName,
|
|
127
|
+
permissions: BlobSASPermissions.parse('r'),
|
|
128
|
+
expiresOn: new Date(Date.now() + expiresInMinutes * 60 * 1000),
|
|
129
|
+
},
|
|
130
|
+
sharedKeyCredential
|
|
131
|
+
).toString()
|
|
132
|
+
|
|
133
|
+
return `${blobClient.url}?${sasToken}`
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## Best Practices
|
|
138
|
+
|
|
139
|
+
✅ **DO:**
|
|
140
|
+
- Use appropriate access tiers (Hot, Cool, Archive)
|
|
141
|
+
- Set blob metadata for organization
|
|
142
|
+
- Use SAS tokens for temporary access
|
|
143
|
+
- Implement proper error handling
|
|
144
|
+
- Use appropriate content types
|
|
145
|
+
- Enable soft delete for important data
|
|
146
|
+
- Monitor storage usage and costs
|
|
147
|
+
- Use lifecycle management policies
|
|
148
|
+
- Implement retry logic
|
|
149
|
+
- Use connection strings or managed identity
|
|
150
|
+
|
|
151
|
+
❌ **DON'T:**
|
|
152
|
+
- Store sensitive data without encryption
|
|
153
|
+
- Use public access unnecessarily
|
|
154
|
+
- Hardcode credentials
|
|
155
|
+
- Ignore error handling
|
|
156
|
+
- Skip content-type validation
|
|
157
|
+
- Ignore lifecycle policies
|
|
158
|
+
- Skip access logging
|
|
159
|
+
- Use default access levels
|
|
160
|
+
- Ignore cost optimization
|
|
161
|
+
- Store large files without chunking
|
|
162
|
+
|
|
163
|
+
## Configuration
|
|
164
|
+
|
|
165
|
+
### Environment Variables
|
|
166
|
+
```bash
|
|
167
|
+
AZURE_STORAGE_ACCOUNT_NAME=myaccount
|
|
168
|
+
AZURE_STORAGE_ACCOUNT_KEY=your-account-key
|
|
169
|
+
AZURE_CONTAINER_NAME=mycontainer
|
|
170
|
+
AZURE_STORAGE_CONNECTION_STRING=DefaultEndpointsProtocol=https;AccountName=...
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### Managed Identity (Recommended)
|
|
174
|
+
```typescript
|
|
175
|
+
import { DefaultAzureCredential } from '@azure/identity'
|
|
176
|
+
|
|
177
|
+
const blobServiceClient = new BlobServiceClient(
|
|
178
|
+
`https://${accountName}.blob.core.windows.net`,
|
|
179
|
+
new DefaultAzureCredential()
|
|
180
|
+
)
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
<!-- AZURE_BLOB:END -->
|
|
184
|
+
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
<!-- CASSANDRA:START -->
|
|
2
|
+
# Apache Cassandra Database Instructions
|
|
3
|
+
|
|
4
|
+
**CRITICAL**: Use Cassandra for distributed NoSQL database with high availability, linear scalability, and eventual consistency.
|
|
5
|
+
|
|
6
|
+
## Core Features
|
|
7
|
+
|
|
8
|
+
### Connection
|
|
9
|
+
```typescript
|
|
10
|
+
// Using cassandra-driver
|
|
11
|
+
import { Client } from 'cassandra-driver'
|
|
12
|
+
|
|
13
|
+
const client = new Client({
|
|
14
|
+
contactPoints: (process.env.CASSANDRA_HOSTS || 'localhost').split(','),
|
|
15
|
+
localDataCenter: process.env.CASSANDRA_DATACENTER || 'datacenter1',
|
|
16
|
+
keyspace: process.env.CASSANDRA_KEYSPACE || 'myapp',
|
|
17
|
+
credentials: {
|
|
18
|
+
username: process.env.CASSANDRA_USER || 'cassandra',
|
|
19
|
+
password: process.env.CASSANDRA_PASSWORD || 'cassandra',
|
|
20
|
+
},
|
|
21
|
+
queryOptions: {
|
|
22
|
+
consistency: 1, // ONE, QUORUM, ALL
|
|
23
|
+
prepare: true,
|
|
24
|
+
},
|
|
25
|
+
})
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### Basic Operations
|
|
29
|
+
```typescript
|
|
30
|
+
// Create keyspace
|
|
31
|
+
await client.execute(`
|
|
32
|
+
CREATE KEYSPACE IF NOT EXISTS myapp
|
|
33
|
+
WITH REPLICATION = {
|
|
34
|
+
'class': 'SimpleStrategy',
|
|
35
|
+
'replication_factor': 1
|
|
36
|
+
}
|
|
37
|
+
`)
|
|
38
|
+
|
|
39
|
+
// Use keyspace
|
|
40
|
+
await client.execute('USE myapp')
|
|
41
|
+
|
|
42
|
+
// Create table
|
|
43
|
+
await client.execute(`
|
|
44
|
+
CREATE TABLE IF NOT EXISTS users (
|
|
45
|
+
id UUID PRIMARY KEY,
|
|
46
|
+
email TEXT,
|
|
47
|
+
name TEXT,
|
|
48
|
+
created_at TIMESTAMP
|
|
49
|
+
)
|
|
50
|
+
`)
|
|
51
|
+
|
|
52
|
+
// Create index
|
|
53
|
+
await client.execute('CREATE INDEX IF NOT EXISTS ON users (email)')
|
|
54
|
+
|
|
55
|
+
// Insert
|
|
56
|
+
await client.execute(
|
|
57
|
+
'INSERT INTO users (id, email, name, created_at) VALUES (?, ?, ?, ?)',
|
|
58
|
+
[cassandra.types.Uuid.random(), 'john@example.com', 'John Doe', new Date()],
|
|
59
|
+
{ prepare: true }
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
// Select
|
|
63
|
+
const result = await client.execute(
|
|
64
|
+
'SELECT * FROM users WHERE id = ?',
|
|
65
|
+
[userId],
|
|
66
|
+
{ prepare: true }
|
|
67
|
+
)
|
|
68
|
+
const user = result.first()
|
|
69
|
+
|
|
70
|
+
// Update
|
|
71
|
+
await client.execute(
|
|
72
|
+
'UPDATE users SET name = ? WHERE id = ?',
|
|
73
|
+
['Jane Doe', userId],
|
|
74
|
+
{ prepare: true }
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
// Delete
|
|
78
|
+
await client.execute(
|
|
79
|
+
'DELETE FROM users WHERE id = ?',
|
|
80
|
+
[userId],
|
|
81
|
+
{ prepare: true }
|
|
82
|
+
)
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### Advanced Features
|
|
86
|
+
```typescript
|
|
87
|
+
// Batch operations
|
|
88
|
+
const queries = [
|
|
89
|
+
{
|
|
90
|
+
query: 'INSERT INTO users (id, email, name) VALUES (?, ?, ?)',
|
|
91
|
+
params: [id1, 'user1@example.com', 'User 1'],
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
query: 'INSERT INTO users (id, email, name) VALUES (?, ?, ?)',
|
|
95
|
+
params: [id2, 'user2@example.com', 'User 2'],
|
|
96
|
+
},
|
|
97
|
+
]
|
|
98
|
+
|
|
99
|
+
await client.batch(queries, { prepare: true })
|
|
100
|
+
|
|
101
|
+
// Collections
|
|
102
|
+
await client.execute(`
|
|
103
|
+
CREATE TABLE IF NOT EXISTS products (
|
|
104
|
+
id UUID PRIMARY KEY,
|
|
105
|
+
name TEXT,
|
|
106
|
+
tags SET<TEXT>,
|
|
107
|
+
metadata MAP<TEXT, TEXT>
|
|
108
|
+
)
|
|
109
|
+
`)
|
|
110
|
+
|
|
111
|
+
await client.execute(
|
|
112
|
+
'UPDATE products SET tags = tags + ? WHERE id = ?',
|
|
113
|
+
[['electronics', 'gadgets'], productId],
|
|
114
|
+
{ prepare: true }
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
// Time-to-Live (TTL)
|
|
118
|
+
await client.execute(
|
|
119
|
+
'INSERT INTO sessions (id, data) VALUES (?, ?) USING TTL 3600',
|
|
120
|
+
[sessionId, sessionData],
|
|
121
|
+
{ prepare: true }
|
|
122
|
+
)
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## Common Patterns
|
|
126
|
+
|
|
127
|
+
### Data Modeling
|
|
128
|
+
```typescript
|
|
129
|
+
// Design tables for query patterns
|
|
130
|
+
// Query: Get users by email
|
|
131
|
+
await client.execute(`
|
|
132
|
+
CREATE TABLE users_by_email (
|
|
133
|
+
email TEXT PRIMARY KEY,
|
|
134
|
+
id UUID,
|
|
135
|
+
name TEXT,
|
|
136
|
+
created_at TIMESTAMP
|
|
137
|
+
)
|
|
138
|
+
`)
|
|
139
|
+
|
|
140
|
+
// Query: Get posts by user and date
|
|
141
|
+
await client.execute(`
|
|
142
|
+
CREATE TABLE posts_by_user (
|
|
143
|
+
user_id UUID,
|
|
144
|
+
created_at TIMESTAMP,
|
|
145
|
+
post_id UUID,
|
|
146
|
+
title TEXT,
|
|
147
|
+
content TEXT,
|
|
148
|
+
PRIMARY KEY (user_id, created_at, post_id)
|
|
149
|
+
) WITH CLUSTERING ORDER BY (created_at DESC)
|
|
150
|
+
`)
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### Consistency Levels
|
|
154
|
+
```typescript
|
|
155
|
+
// Read with QUORUM consistency
|
|
156
|
+
const result = await client.execute(
|
|
157
|
+
'SELECT * FROM users WHERE id = ?',
|
|
158
|
+
[userId],
|
|
159
|
+
{
|
|
160
|
+
consistency: 2, // QUORUM
|
|
161
|
+
prepare: true,
|
|
162
|
+
}
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
// Write with ALL consistency (strongest)
|
|
166
|
+
await client.execute(
|
|
167
|
+
'INSERT INTO users (id, email, name) VALUES (?, ?, ?)',
|
|
168
|
+
[id, email, name],
|
|
169
|
+
{
|
|
170
|
+
consistency: 3, // ALL
|
|
171
|
+
prepare: true,
|
|
172
|
+
}
|
|
173
|
+
)
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
## Best Practices
|
|
177
|
+
|
|
178
|
+
✅ **DO:**
|
|
179
|
+
- Design tables for query patterns (denormalize)
|
|
180
|
+
- Use appropriate partition keys
|
|
181
|
+
- Use clustering keys for sorting
|
|
182
|
+
- Create secondary indexes sparingly
|
|
183
|
+
- Use prepared statements
|
|
184
|
+
- Set appropriate consistency levels
|
|
185
|
+
- Use TTL for time-based data
|
|
186
|
+
- Monitor cluster health
|
|
187
|
+
- Use batch operations carefully
|
|
188
|
+
- Implement retry logic
|
|
189
|
+
|
|
190
|
+
❌ **DON'T:**
|
|
191
|
+
- Use secondary indexes on high-cardinality columns
|
|
192
|
+
- Create too many secondary indexes
|
|
193
|
+
- Use ALL consistency for all operations
|
|
194
|
+
- Store large values (> 1MB)
|
|
195
|
+
- Skip error handling
|
|
196
|
+
- Ignore cluster topology
|
|
197
|
+
- Hardcode contact points
|
|
198
|
+
- Use SELECT * in production
|
|
199
|
+
- Ignore data modeling best practices
|
|
200
|
+
- Skip monitoring
|
|
201
|
+
|
|
202
|
+
## Configuration
|
|
203
|
+
|
|
204
|
+
### Environment Variables
|
|
205
|
+
```bash
|
|
206
|
+
CASSANDRA_HOSTS=localhost
|
|
207
|
+
CASSANDRA_HOSTS=node1:9042,node2:9042,node3:9042
|
|
208
|
+
CASSANDRA_DATACENTER=datacenter1
|
|
209
|
+
CASSANDRA_KEYSPACE=myapp
|
|
210
|
+
CASSANDRA_USER=cassandra
|
|
211
|
+
CASSANDRA_PASSWORD=securepassword
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
### Docker Compose
|
|
215
|
+
```yaml
|
|
216
|
+
services:
|
|
217
|
+
cassandra:
|
|
218
|
+
image: cassandra:4
|
|
219
|
+
ports:
|
|
220
|
+
- "9042:9042"
|
|
221
|
+
environment:
|
|
222
|
+
CASSANDRA_CLUSTER_NAME: my-cluster
|
|
223
|
+
CASSANDRA_DC: datacenter1
|
|
224
|
+
CASSANDRA_RACK: rack1
|
|
225
|
+
CASSANDRA_ENDPOINT_SNITCH: GossipingPropertyFileSnitch
|
|
226
|
+
volumes:
|
|
227
|
+
- cassandra_data:/var/lib/cassandra
|
|
228
|
+
healthcheck:
|
|
229
|
+
test: ["CMD-SHELL", "nodetool status | grep -E '^UN' || exit 1"]
|
|
230
|
+
interval: 30s
|
|
231
|
+
timeout: 10s
|
|
232
|
+
retries: 5
|
|
233
|
+
|
|
234
|
+
volumes:
|
|
235
|
+
cassandra_data:
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
<!-- CASSANDRA:END -->
|
|
239
|
+
|
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
<!-- DYNAMODB:START -->
|
|
2
|
+
# AWS DynamoDB Instructions
|
|
3
|
+
|
|
4
|
+
**CRITICAL**: Use DynamoDB for serverless NoSQL database with automatic scaling, high performance, and pay-per-use pricing.
|
|
5
|
+
|
|
6
|
+
## Core Features
|
|
7
|
+
|
|
8
|
+
### Connection
|
|
9
|
+
```typescript
|
|
10
|
+
// Using @aws-sdk/client-dynamodb
|
|
11
|
+
import { DynamoDBClient } from '@aws-sdk/client-dynamodb'
|
|
12
|
+
import { DynamoDBDocumentClient, PutCommand, GetCommand, QueryCommand, ScanCommand } from '@aws-sdk/lib-dynamodb'
|
|
13
|
+
|
|
14
|
+
const client = new DynamoDBClient({
|
|
15
|
+
region: process.env.AWS_REGION || 'us-east-1',
|
|
16
|
+
credentials: {
|
|
17
|
+
accessKeyId: process.env.AWS_ACCESS_KEY_ID || '',
|
|
18
|
+
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY || '',
|
|
19
|
+
},
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
const docClient = DynamoDBDocumentClient.from(client)
|
|
23
|
+
|
|
24
|
+
// Using AWS SDK v2
|
|
25
|
+
import AWS from 'aws-sdk'
|
|
26
|
+
|
|
27
|
+
const dynamodb = new AWS.DynamoDB.DocumentClient({
|
|
28
|
+
region: process.env.AWS_REGION || 'us-east-1',
|
|
29
|
+
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
|
|
30
|
+
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
|
|
31
|
+
})
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### Basic Operations
|
|
35
|
+
```typescript
|
|
36
|
+
// Put item
|
|
37
|
+
await docClient.send(new PutCommand({
|
|
38
|
+
TableName: 'Users',
|
|
39
|
+
Item: {
|
|
40
|
+
id: 'user-1',
|
|
41
|
+
email: 'john@example.com',
|
|
42
|
+
name: 'John Doe',
|
|
43
|
+
createdAt: new Date().toISOString(),
|
|
44
|
+
},
|
|
45
|
+
}))
|
|
46
|
+
|
|
47
|
+
// Get item
|
|
48
|
+
const result = await docClient.send(new GetCommand({
|
|
49
|
+
TableName: 'Users',
|
|
50
|
+
Key: {
|
|
51
|
+
id: 'user-1',
|
|
52
|
+
},
|
|
53
|
+
}))
|
|
54
|
+
const user = result.Item
|
|
55
|
+
|
|
56
|
+
// Update item
|
|
57
|
+
await docClient.send(new UpdateCommand({
|
|
58
|
+
TableName: 'Users',
|
|
59
|
+
Key: { id: 'user-1' },
|
|
60
|
+
UpdateExpression: 'SET #name = :name, updatedAt = :updatedAt',
|
|
61
|
+
ExpressionAttributeNames: {
|
|
62
|
+
'#name': 'name',
|
|
63
|
+
},
|
|
64
|
+
ExpressionAttributeValues: {
|
|
65
|
+
':name': 'Jane Doe',
|
|
66
|
+
':updatedAt': new Date().toISOString(),
|
|
67
|
+
},
|
|
68
|
+
}))
|
|
69
|
+
|
|
70
|
+
// Delete item
|
|
71
|
+
await docClient.send(new DeleteCommand({
|
|
72
|
+
TableName: 'Users',
|
|
73
|
+
Key: { id: 'user-1' },
|
|
74
|
+
}))
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Query Operations
|
|
78
|
+
```typescript
|
|
79
|
+
// Query by partition key
|
|
80
|
+
const result = await docClient.send(new QueryCommand({
|
|
81
|
+
TableName: 'Posts',
|
|
82
|
+
KeyConditionExpression: 'userId = :userId',
|
|
83
|
+
ExpressionAttributeValues: {
|
|
84
|
+
':userId': 'user-1',
|
|
85
|
+
},
|
|
86
|
+
ScanIndexForward: false, // Sort descending
|
|
87
|
+
Limit: 10,
|
|
88
|
+
}))
|
|
89
|
+
|
|
90
|
+
// Query with GSI (Global Secondary Index)
|
|
91
|
+
const result = await docClient.send(new QueryCommand({
|
|
92
|
+
TableName: 'Users',
|
|
93
|
+
IndexName: 'EmailIndex',
|
|
94
|
+
KeyConditionExpression: 'email = :email',
|
|
95
|
+
ExpressionAttributeValues: {
|
|
96
|
+
':email': 'john@example.com',
|
|
97
|
+
},
|
|
98
|
+
}))
|
|
99
|
+
|
|
100
|
+
// Scan (use sparingly)
|
|
101
|
+
const result = await docClient.send(new ScanCommand({
|
|
102
|
+
TableName: 'Users',
|
|
103
|
+
FilterExpression: '#status = :status',
|
|
104
|
+
ExpressionAttributeNames: {
|
|
105
|
+
'#status': 'status',
|
|
106
|
+
},
|
|
107
|
+
ExpressionAttributeValues: {
|
|
108
|
+
':status': 'active',
|
|
109
|
+
},
|
|
110
|
+
Limit: 100,
|
|
111
|
+
}))
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### Advanced Features
|
|
115
|
+
```typescript
|
|
116
|
+
// Batch operations
|
|
117
|
+
import { BatchWriteCommand } from '@aws-sdk/lib-dynamodb'
|
|
118
|
+
|
|
119
|
+
await docClient.send(new BatchWriteCommand({
|
|
120
|
+
RequestItems: {
|
|
121
|
+
Users: [
|
|
122
|
+
{
|
|
123
|
+
PutRequest: {
|
|
124
|
+
Item: { id: 'user-1', name: 'User 1' },
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
PutRequest: {
|
|
129
|
+
Item: { id: 'user-2', name: 'User 2' },
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
],
|
|
133
|
+
},
|
|
134
|
+
}))
|
|
135
|
+
|
|
136
|
+
// Transactions
|
|
137
|
+
import { TransactWriteCommand } from '@aws-sdk/lib-dynamodb'
|
|
138
|
+
|
|
139
|
+
await docClient.send(new TransactWriteCommand({
|
|
140
|
+
TransactItems: [
|
|
141
|
+
{
|
|
142
|
+
Put: {
|
|
143
|
+
TableName: 'Accounts',
|
|
144
|
+
Item: { userId: 'user-1', balance: 1000 },
|
|
145
|
+
},
|
|
146
|
+
},
|
|
147
|
+
{
|
|
148
|
+
Put: {
|
|
149
|
+
TableName: 'Transactions',
|
|
150
|
+
Item: { id: 'tx-1', userId: 'user-1', amount: 1000 },
|
|
151
|
+
},
|
|
152
|
+
},
|
|
153
|
+
],
|
|
154
|
+
}))
|
|
155
|
+
|
|
156
|
+
// Conditional writes
|
|
157
|
+
await docClient.send(new PutCommand({
|
|
158
|
+
TableName: 'Users',
|
|
159
|
+
Item: { id: 'user-1', email: 'john@example.com' },
|
|
160
|
+
ConditionExpression: 'attribute_not_exists(id)',
|
|
161
|
+
}))
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
## Common Patterns
|
|
165
|
+
|
|
166
|
+
### Single Table Design
|
|
167
|
+
```typescript
|
|
168
|
+
// Store different entity types in one table
|
|
169
|
+
await docClient.send(new PutCommand({
|
|
170
|
+
TableName: 'AppData',
|
|
171
|
+
Item: {
|
|
172
|
+
PK: 'USER#user-1',
|
|
173
|
+
SK: 'METADATA',
|
|
174
|
+
EntityType: 'User',
|
|
175
|
+
email: 'john@example.com',
|
|
176
|
+
name: 'John Doe',
|
|
177
|
+
},
|
|
178
|
+
}))
|
|
179
|
+
|
|
180
|
+
await docClient.send(new PutCommand({
|
|
181
|
+
TableName: 'AppData',
|
|
182
|
+
Item: {
|
|
183
|
+
PK: 'USER#user-1',
|
|
184
|
+
SK: 'POST#post-1',
|
|
185
|
+
EntityType: 'Post',
|
|
186
|
+
title: 'My Post',
|
|
187
|
+
content: 'Content here',
|
|
188
|
+
},
|
|
189
|
+
}))
|
|
190
|
+
|
|
191
|
+
// Query all posts for a user
|
|
192
|
+
const result = await docClient.send(new QueryCommand({
|
|
193
|
+
TableName: 'AppData',
|
|
194
|
+
KeyConditionExpression: 'PK = :pk AND begins_with(SK, :sk)',
|
|
195
|
+
ExpressionAttributeValues: {
|
|
196
|
+
':pk': 'USER#user-1',
|
|
197
|
+
':sk': 'POST#',
|
|
198
|
+
},
|
|
199
|
+
}))
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### Time-to-Live (TTL)
|
|
203
|
+
```typescript
|
|
204
|
+
// Items automatically deleted after TTL
|
|
205
|
+
await docClient.send(new PutCommand({
|
|
206
|
+
TableName: 'Sessions',
|
|
207
|
+
Item: {
|
|
208
|
+
sessionId: 'session-1',
|
|
209
|
+
userId: 'user-1',
|
|
210
|
+
ttl: Math.floor(Date.now() / 1000) + 3600, // 1 hour from now
|
|
211
|
+
},
|
|
212
|
+
}))
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
## Best Practices
|
|
216
|
+
|
|
217
|
+
✅ **DO:**
|
|
218
|
+
- Design for access patterns
|
|
219
|
+
- Use appropriate partition keys
|
|
220
|
+
- Create GSIs for alternative access patterns
|
|
221
|
+
- Use batch operations when possible
|
|
222
|
+
- Implement exponential backoff for throttling
|
|
223
|
+
- Use TTL for temporary data
|
|
224
|
+
- Monitor provisioned capacity
|
|
225
|
+
- Use transactions for atomic operations
|
|
226
|
+
- Implement proper error handling
|
|
227
|
+
- Use single table design when appropriate
|
|
228
|
+
|
|
229
|
+
❌ **DON'T:**
|
|
230
|
+
- Use Scan for frequent queries
|
|
231
|
+
- Create too many GSIs (affects write performance)
|
|
232
|
+
- Store large items (> 400KB)
|
|
233
|
+
- Ignore throttling errors
|
|
234
|
+
- Hardcode table names
|
|
235
|
+
- Skip error handling
|
|
236
|
+
- Use eventually consistent reads when strong consistency needed
|
|
237
|
+
- Ignore capacity planning
|
|
238
|
+
- Store sensitive data without encryption
|
|
239
|
+
- Skip monitoring
|
|
240
|
+
|
|
241
|
+
## Configuration
|
|
242
|
+
|
|
243
|
+
### Environment Variables
|
|
244
|
+
```bash
|
|
245
|
+
AWS_REGION=us-east-1
|
|
246
|
+
AWS_ACCESS_KEY_ID=your-access-key
|
|
247
|
+
AWS_SECRET_ACCESS_KEY=your-secret-key
|
|
248
|
+
DYNAMODB_TABLE_PREFIX=myapp_
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
### IAM Policy
|
|
252
|
+
```json
|
|
253
|
+
{
|
|
254
|
+
"Version": "2012-10-17",
|
|
255
|
+
"Statement": [
|
|
256
|
+
{
|
|
257
|
+
"Effect": "Allow",
|
|
258
|
+
"Action": [
|
|
259
|
+
"dynamodb:PutItem",
|
|
260
|
+
"dynamodb:GetItem",
|
|
261
|
+
"dynamodb:UpdateItem",
|
|
262
|
+
"dynamodb:DeleteItem",
|
|
263
|
+
"dynamodb:Query",
|
|
264
|
+
"dynamodb:Scan"
|
|
265
|
+
],
|
|
266
|
+
"Resource": "arn:aws:dynamodb:*:*:table/myapp-*"
|
|
267
|
+
}
|
|
268
|
+
]
|
|
269
|
+
}
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
## Integration with Development
|
|
273
|
+
|
|
274
|
+
### Testing
|
|
275
|
+
```typescript
|
|
276
|
+
// Use DynamoDB Local
|
|
277
|
+
const localClient = new DynamoDBClient({
|
|
278
|
+
endpoint: 'http://localhost:8000',
|
|
279
|
+
region: 'local',
|
|
280
|
+
credentials: {
|
|
281
|
+
accessKeyId: 'local',
|
|
282
|
+
secretAccessKey: 'local',
|
|
283
|
+
},
|
|
284
|
+
})
|
|
285
|
+
|
|
286
|
+
// Clean up after tests
|
|
287
|
+
afterEach(async () => {
|
|
288
|
+
// Delete test items or use separate test table
|
|
289
|
+
})
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
### Health Checks
|
|
293
|
+
```typescript
|
|
294
|
+
async function checkDynamoDBHealth(): Promise<boolean> {
|
|
295
|
+
try {
|
|
296
|
+
await docClient.send(new GetCommand({
|
|
297
|
+
TableName: 'HealthCheck',
|
|
298
|
+
Key: { id: 'health' },
|
|
299
|
+
}))
|
|
300
|
+
return true
|
|
301
|
+
} catch {
|
|
302
|
+
return false
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
<!-- DYNAMODB:END -->
|
|
308
|
+
|