From 6d74723c174e6d283b2f0ff7f398accb4e731c2c Mon Sep 17 00:00:00 2001 From: Enric Bisbe Gil Date: Sat, 6 Jul 2024 11:15:55 +0200 Subject: [PATCH] define model structure --- models/table.ts | 54 ++++++++++++++++++++++++++++++++++--------------- package.json | 1 + pnpm-lock.yaml | 15 ++++++++++++++ serverless.ts | 33 ++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 16 deletions(-) diff --git a/models/table.ts b/models/table.ts index 3d90065..8bdbc9e 100644 --- a/models/table.ts +++ b/models/table.ts @@ -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; -export const List = table.getModel("list"); +export type List = Entity; +export const List = table.getModel("List"); -export type Item = Entity; -export const Item = table.getModel("item"); +export type Item = Entity; +export const Item = table.getModel("Item"); -export type Buyer = Entity; -export const Buyer = table.getModel("buyer"); +export type Buyer = Entity; +export const Buyer = table.getModel("Buyer"); diff --git a/package.json b/package.json index f0e4fab..3c780fc 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7ed96b0..8b3733c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -24,6 +24,9 @@ importers: '@eslint/js': specifier: ^9.6.0 version: 9.6.0 + '@types/node': + specifier: ^20.14.10 + version: 20.14.10 eslint: specifier: 9.x version: 9.6.0 @@ -392,6 +395,9 @@ packages: resolution: {integrity: sha512-4pP0EV3iTsexDx+8PPGAKCQpd/6hsQBaQhqWzU4hqKPHN5epPsxKbvUTIiYIHTxaKt6/kEaqPBpu/ufvfbrRzw==} engines: {node: '>=16.0.0'} + '@types/node@20.14.10': + resolution: {integrity: sha512-MdiXf+nDuMvY0gJKxyfZ7/6UFsETO7mGKF54MVD/ekJS6HdFtpZFBgrh6Pseu64XTb2MLyFPlbW6hj8HYRQNOQ==} + '@types/serverless@3.12.22': resolution: {integrity: sha512-h6U8jv22T0N+9mcKBK2p/gVmcrKW7trUdVNhGemm8bct0lx3i/7svt1x0o/q87nMtOJMcqSvqFz63c7jM1X2Ew==} @@ -980,6 +986,9 @@ packages: engines: {node: '>=14.17'} hasBin: true + undici-types@5.26.5: + resolution: {integrity: sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==} + uri-js@4.4.1: resolution: {integrity: sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==} @@ -1739,6 +1748,10 @@ snapshots: '@smithy/types': 3.3.0 tslib: 2.6.3 + '@types/node@20.14.10': + dependencies: + undici-types: 5.26.5 + '@types/serverless@3.12.22': {} '@typescript-eslint/eslint-plugin@7.15.0(@typescript-eslint/parser@7.15.0(eslint@9.6.0)(typescript@5.5.3))(eslint@9.6.0)(typescript@5.5.3)': @@ -2321,6 +2334,8 @@ snapshots: typescript@5.5.3: {} + undici-types@5.26.5: {} + uri-js@4.4.1: dependencies: punycode: 2.3.1 diff --git a/serverless.ts b/serverless.ts index 34e7b19..fc7f734 100644 --- a/serverless.ts +++ b/serverless.ts @@ -20,6 +20,9 @@ const serverlessConfig: Serverless = { functions: { hello: { handler: "functions/handler.hello", + environment: { + ddb_table: { Ref: "DdbTable" }, + }, events: [ { httpApi: { @@ -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;