Skip to content

Commit

Permalink
define model structure
Browse files Browse the repository at this point in the history
  • Loading branch information
ebisbe committed Jul 6, 2024
1 parent 5998ee6 commit 6d74723
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 16 deletions.
54 changes: 38 additions & 16 deletions models/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,60 @@ import { Entity, Table } from "dynamodb-onetable";
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
const client = new DynamoDBClient();

const { ddb_table } = process.env;

const MySchema = {
version: "0.0.1",
indexes: {
primary: { hash: "pk", sort: "sk" },
},
params: {
typeField: "__typename",
isoDates: true,
separator: "#",
timestamps: true,
},
models: {
list: {
pk: { type: String, value: "account#${name}" },
name: { type: String },
List: {
pk: { type: String, value: "USER#${userId}" },
sk: { type: String, value: "LIST#${id}" },
id: { type: String, generate: "ulid" },
userId: { type: String, required: true },
name: { type: String, required: true },
description: { type: String },
totalValue: { type: Number, default: 0 },
totalItems: { type: Number, default: 0 },
},
item: {
pk: { type: String },
sk: { type: String },
Item: {
pk: { type: String, value: "LIST#${listId}" },
sk: { type: String, value: "ITEM#${id}" },
listId: { type: String },
id: { type: String, generate: "ulid" },
name: { type: String },
description: { type: String },
value: { type: Number, default: 0 },
},
buyer: {
pk: { type: String },
sk: { type: String },
Buyer: {
pk: { type: String, value: "ITEM#${itemId}" },
sk: { type: String, value: "BUYER#${userId}" },
userId: { type: String, required: true },
name: { type: String, required: true },
itemId: { type: String, required: true },
},
} as const,
};

const table = new Table({
client: client,
name: "MyTable",
name: ddb_table,
schema: MySchema,
});

export type List = Entity<typeof MySchema.models.list>;
export const List = table.getModel("list");
export type List = Entity<typeof MySchema.models.List>;
export const List = table.getModel("List");

export type Item = Entity<typeof MySchema.models.item>;
export const Item = table.getModel("item");
export type Item = Entity<typeof MySchema.models.Item>;
export const Item = table.getModel("Item");

export type Buyer = Entity<typeof MySchema.models.buyer>;
export const Buyer = table.getModel("buyer");
export type Buyer = Entity<typeof MySchema.models.Buyer>;
export const Buyer = table.getModel("Buyer");
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"devDependencies": {
"@eslint/js": "^9.6.0",
"@types/node": "^20.14.10",
"eslint": "9.x",
"globals": "^15.8.0",
"prettier": "3.3.2",
Expand Down
15 changes: 15 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions serverless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ const serverlessConfig: Serverless = {
functions: {
hello: {
handler: "functions/handler.hello",
environment: {
ddb_table: { Ref: "DdbTable" },
},
events: [
{
httpApi: {
Expand All @@ -30,6 +33,36 @@ const serverlessConfig: Serverless = {
],
},
},

resources: {
Resources: {
DdbTable: {
Type: "AWS::DynamoDB::Table",
Properties: {
AttributeDefinitions: [
{
AttributeName: "pk",
AttributeType: "S",
},
{
AttributeName: "sk",
AttributeType: "S",
},
],
KeySchema: [
{
AttributeName: "pk",
KeyType: "HASH",
},
{
AttributeName: "sk",
KeyType: "RANGE",
},
],
},
},
},
},
};

export default serverlessConfig;

0 comments on commit 6d74723

Please sign in to comment.