From 76f3924e9f714dc3f471901f571af9fcf93281a4 Mon Sep 17 00:00:00 2001 From: Dmitriy Date: Mon, 15 Jul 2024 15:31:06 +0200 Subject: [PATCH] Adjust the return of save/create to conform to schema --- .github/workflows/publish.yml | 33 -------------------------------- package-lock.json | 4 ++-- package.json | 2 +- packages/dynamoose/lib/Item.ts | 27 +++++++++----------------- packages/dynamoose/test/Item.js | 13 +++++++------ packages/dynamoose/test/Model.js | 13 +++++++------ 6 files changed, 26 insertions(+), 66 deletions(-) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 28a6a2b67..85824ac1b 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -34,36 +34,3 @@ jobs: TAG: ${{ needs.getinfo.outputs.npmtag }} NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} - publishsite: - needs: getinfo - runs-on: ubuntu-latest - if: needs.getinfo.outputs.websites3bucket != '' - steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v2 - with: - node-version: lts/* - - run: npm install - - run: npm run site:install - - run: npm run site:crowdin:sync - env: - CROWDIN_PERSONAL_TOKEN: ${{ secrets.CROWDIN_PERSONAL_TOKEN }} - - run: npm run site:build - - uses: jakejarvis/s3-sync-action@v0.5.1 - with: - args: --follow-symlinks --delete --cache-control 's-maxage=604800, max-age=0' - env: - AWS_S3_BUCKET: ${{ needs.getinfo.outputs.websites3bucket }} - AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} - AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} - AWS_REGION: 'us-west-2' - SOURCE_DIR: 'docs/build' - - name: Purge Cloudflare Cache - run: | - curl -X POST "https://api.cloudflare.com/client/v4/zones/$ZONE/purge_cache" \ - -H "Authorization: Bearer $TOKEN" \ - -H "Content-Type: application/json" \ - --data '{"purge_everything":true}' - env: - TOKEN: ${{ secrets.CLOUDFLARE_TOKEN }} - ZONE: ${{ secrets.CLOUDFLARE_ZONE }} diff --git a/package-lock.json b/package-lock.json index 462709906..c6001bcc7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { - "name": "dynamoose", + "name": "@monei-js/dynamoose", "version": "3.0.0-beta.2", "lockfileVersion": 2, "requires": true, "packages": { "": { - "name": "dynamoose", + "name": "@monei-js/dynamoose", "funding": [ { "type": "github-sponsors", diff --git a/package.json b/package.json index d4520e9b6..215c18dce 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "dynamoose", + "name": "@monei-js/dynamoose", "description": "Dynamoose is a modeling tool for Amazon's DynamoDB (inspired by Mongoose)", "homepage": "https://dynamoosejs.com", "directories": { diff --git a/packages/dynamoose/lib/Item.ts b/packages/dynamoose/lib/Item.ts index 3fd3cf334..bd4eedf3b 100644 --- a/packages/dynamoose/lib/Item.ts +++ b/packages/dynamoose/lib/Item.ts @@ -440,7 +440,12 @@ export class Item extends InternalPropertiesClass { const localSettings: ItemSaveSettings = settings; const paramsPromise = this.toDynamo({"defaults": true, "validate": true, "required": true, "enum": true, "forceDefault": true, "combine": true, "saveUnknown": true, "customTypesDynamo": true, "updateTimestamps": true, "modifiers": ["set"], "mapAttributes": true}).then(async (item) => { - savedItem = item; + const conformToSchemaSettings : ItemObjectFromSchemaSettings = {"combine": true, "customTypesDynamo": true, "saveUnknown": true, "type": "fromDynamo", "modifiers": ["get"], "mapAttributes": true}; + savedItem = await new (this.getInternalProperties(internalProperties).model).Item(item as any).conformToSchema(conformToSchemaSettings); + Object.keys(savedItem).forEach((key) => this[key] = savedItem[key]); + savedItem.getInternalProperties(internalProperties).storedInDynamo = true; + this.getInternalProperties(internalProperties).storedInDynamo = true; + let putItemObj: DynamoDB.PutItemInput = { "Item": item, "TableName": table.getInternalProperties(internalProperties).name @@ -478,26 +483,12 @@ export class Item extends InternalPropertiesClass { return ddb(table.getInternalProperties(internalProperties).instance, "putItem", putItemObj); }); + if (callback) { const localCallback: CallbackType = callback as CallbackType; - promise.then(() => { - this.getInternalProperties(internalProperties).storedInDynamo = true; - - const returnItem = new (this.getInternalProperties(internalProperties).model).Item(savedItem as any); - returnItem.getInternalProperties(internalProperties).storedInDynamo = true; - - localCallback(null, returnItem); - }).catch((error) => callback(error)); + promise.then(() => localCallback(null, savedItem), (error) => callback(error)); } else { - return (async (): Promise => { - await promise; - this.getInternalProperties(internalProperties).storedInDynamo = true; - - const returnItem = new (this.getInternalProperties(internalProperties).model).Item(savedItem as any); - returnItem.getInternalProperties(internalProperties).storedInDynamo = true; - - return returnItem; - })(); + return promise.then(() => savedItem); } } diff --git a/packages/dynamoose/test/Item.js b/packages/dynamoose/test/Item.js index 889de4ec5..ea509da55 100644 --- a/packages/dynamoose/test/Item.js +++ b/packages/dynamoose/test/Item.js @@ -121,9 +121,9 @@ describe("Item", () => { return putItemFunction(); } }); - User = dynamoose.model("User", {"id": Number, "name": String}); + User = dynamoose.model("User", {"id": Number, "name": String, "birthday": Date}); new dynamoose.Table("User", [User]); - user = new User({"id": 1, "name": "Charlie"}); + user = new User({"id": 1, "name": "Charlie", "birthday": new Date(10000)}); }); afterEach(() => { dynamoose.Table.defaults.set({}); @@ -148,7 +148,7 @@ describe("Item", () => { putItemFunction = () => Promise.resolve(); await callType.func(user).bind(user)(); expect(putParams).toEqual([{ - "Item": {"id": {"N": "1"}, "name": {"S": "Charlie"}}, + "Item": {"id": {"N": "1"}, "name": {"S": "Charlie"}, "birthday": {"N": `${new Date(10000).getTime()}`}}, "TableName": "User" }]); }); @@ -165,7 +165,7 @@ describe("Item", () => { expect(resultA).toEqual(user); expect(resultB).toEqual(robot); expect(putParams).toEqual([{ - "Item": {"id": {"N": "1"}, "name": {"S": "Charlie"}}, + "Item": {"id": {"N": "1"}, "name": {"S": "Charlie"}, "birthday": {"N": `${new Date(10000).getTime()}`}}, "TableName": "User" }, { "Item": {"id": {"N": "2"}, "built": {"N": `${date}`}}, @@ -209,7 +209,8 @@ describe("Item", () => { expect(result).toEqual({ "Item": { "id": {"N": "1"}, - "name": {"S": "Charlie"} + "name": {"S": "Charlie"}, + "birthday": {"N": `${new Date(10000).getTime()}`} }, "TableName": "User" }); @@ -1721,7 +1722,7 @@ describe("Item", () => { error = e; } expect(putParams).toEqual([{ - "Item": {"id": {"N": "1"}, "name": {"S": "Charlie"}}, + "Item": {"id": {"N": "1"}, "name": {"S": "Charlie"}, "birthday": {"N": `${new Date(10000).getTime()}`}}, "TableName": "User" }]); expect(result).not.toBeDefined(); diff --git a/packages/dynamoose/test/Model.js b/packages/dynamoose/test/Model.js index 5400c341d..a71e334c4 100644 --- a/packages/dynamoose/test/Model.js +++ b/packages/dynamoose/test/Model.js @@ -1998,9 +1998,9 @@ describe("Model", () => { // Check original timestamps expect(result.id).toEqual(1); expect(result.name).toEqual("Charlie"); - expect(typeof result.createdAt).toEqual("number"); + expect(result.createdAt.constructor).toEqual(Date); expect(result.createdAt).toBeWithinRange(date1 - 10, date1 + 10); - expect(typeof result.updatedAt).toEqual("number"); + expect(result.updatedAt.constructor).toEqual(Date); expect(result.updatedAt).toBeWithinRange(date1 - 10, date1 + 10); await new Promise((resolve) => setTimeout(resolve, 20)); @@ -2010,15 +2010,16 @@ describe("Model", () => { // Mutate document and re-save result.name = "Charlie 2"; const result2 = await result.save(); + const date3 = Date.now(); expect(result.toJSON()).toEqual(result2.toJSON()); [result, result2].forEach((r) => { expect(r.id).toEqual(1); expect(r.name).toEqual("Charlie 2"); - expect(typeof r.createdAt).toEqual("number"); - expect(r.createdAt).toBeWithinRange(date1 - 10, date1 + 10); - expect(typeof r.updatedAt).toEqual("number"); - expect(r.updatedAt).toBeWithinRange(date2 - 10, date2 + 10); + expect(r.createdAt.constructor).toEqual(Date); + expect(r.createdAt.getTime()).toBeWithinRange(date1, date3); + expect(r.updatedAt.constructor).toEqual(Date); + expect(r.updatedAt.getTime()).toBeWithinRange(date2, date3); }); }); });