Skip to Content

DynamoDB

Introduction

A library providing typed access to the shared DynamoDB tables (cache, configs) used across frontend projects. Mostly used internally by the Cache and Configs bundles, but can be installed directly when a project needs its own table access.


Installation

To use @phpcreation/frontend-dynamodb-react-nextjs-bundle in your project, follow these steps:

Open a terminal in your project root

Create a Github Personnal Access Token

How to create a Github PAT :

Create a Github Personal Access Token

Login to NPM registry

npm login --scope=@phpcreation --auth-type=legacy --registry=https://npm.pkg.github.com
  • username: your github username in lowercase
  • password: The PAT you just created

Install the Package

npm install @phpcreation/frontend-dynamodb-react-nextjs-bundle

Setup

Environment Variables

Follow these steps to manage environment variables correctly :

Environment Variables Management

.env.local

These variables should be sent to the secrets of the deployed app, and not committed to version control.

# DynamoDB DYNAMODB_DSN_APP_CACHE= # DSN connection string to the app's cache table DYNAMODB_DSN_CONFIG= # DSN connection string to the shared configs table

src/utils/dynamodb.ts is project-specific — it must not be re-exported as part of a bundle’s index.ts. Each app copies it and instantiates its own singletons.

Copy src/utils/dynamodb.ts

/** * Project specific util file. Do not expose as part of bundle in index.ts files */ import { CacheTable, ConfigTable, } from "@phpcreation/frontend-dynamodb-react-nextjs-bundle"; let cacheInstance: CacheTable | null = null; export async function getCache(): Promise<CacheTable> { if (!cacheInstance) { const dsn = process.env.DYNAMODB_DSN_APP_CACHE; if (!dsn) { throw new Error("Missing DYNAMODB_DSN_APP_CACHE environment variable"); } cacheInstance = await CacheTable.fromDSN(dsn); } return cacheInstance; } let configInstance: ConfigTable | null = null; export async function getConfigCache(): Promise<ConfigTable> { if (!configInstance) { const dsn = process.env.DYNAMODB_DSN_CONFIG; if (!dsn) { throw new Error("Missing DYNAMODB_DSN_CONFIG environment variable"); } configInstance = await ConfigTable.fromDSN(dsn); } return configInstance; }

Usage

import { getCache } from "@/utils/dynamodb"; const cache = await getCache(); const value = await cache.get("some-key");

Common Issues

Common Issues
Last updated on