Skip to content

Commit

Permalink
Adjust the return of save/create to conform to schema
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmyn committed Jul 15, 2024
1 parent 1f7e433 commit 76f3924
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 66 deletions.
33 changes: 0 additions & 33 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]
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 }}
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
27 changes: 9 additions & 18 deletions packages/dynamoose/lib/Item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,12 @@ export class Item extends InternalPropertiesClass<ItemInternalProperties> {

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
Expand Down Expand Up @@ -478,26 +483,12 @@ export class Item extends InternalPropertiesClass<ItemInternalProperties> {
return ddb(table.getInternalProperties(internalProperties).instance, "putItem", putItemObj);
});


if (callback) {
const localCallback: CallbackType<Item, any> = callback as CallbackType<Item, any>;
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<Item> => {
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);
}
}

Expand Down
13 changes: 7 additions & 6 deletions packages/dynamoose/test/Item.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({});
Expand All @@ -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"
}]);
});
Expand All @@ -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}`}},
Expand Down Expand Up @@ -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"
});
Expand Down Expand Up @@ -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();
Expand Down
13 changes: 7 additions & 6 deletions packages/dynamoose/test/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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);
});
});
});
Expand Down

0 comments on commit 76f3924

Please sign in to comment.