Skip to content

Commit

Permalink
Feature/complex types for real (tywalch#73)
Browse files Browse the repository at this point in the history
* Working tests but complex types still need to be modified for `required`, `hidden`, and `readOnly`

* Updates work I think

* Update progress

* update progress!!!

* put improvements

* Seemingly making progress but I gotta be honest: I'm not really sure I am...

* Adding additional type types

* Removing Defaulted because it was not accurate here

* Improvements to match new `set` definition style

* Attribute improvements to include only relevant fields. No nested enums.

* Overall improvements, before splitting Attribute into more specialized attributes, adding some typing refactors

* get/set return type improvements!

* Example and type improvements

* Working tests, broken out Attribute classes

* Type improvements!

* Working set!

* Consistency to getter/setters

* type tests for type changes: required properties should not be able to be removed via remove method

* Further test improvements for complex attributes

* Type and test improvements

* I have to figure out how to apply required/not required constraints to nested maps through the update chain methods

* Cleanup

* Defaults before required

* Using the client's createSet functionality if provided

* createSet without a client test

* README and type improvements

* Test improvements

* value operation bug fixes

* Implementation of Issue#71

* Complex attribute validation improvements

* Cleanup

* README Cleanup

* Type improvements for GET, readme improvements, new type tests for GET

* Example improvements

* Package bump, readme improvements

* Fixing typing issues with adding `null` as a possible response type from the `get` method

* Adding changes to CHANGELOG.md
  • Loading branch information
tywalch authored Aug 10, 2021
1 parent 41a6748 commit e58919e
Show file tree
Hide file tree
Showing 29 changed files with 6,088 additions and 735 deletions.
23 changes: 18 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ All notable changes to this project will be documented in this file. Breaking ch
## Changing
- Bulk Operations to return the original object passed to the operation if that object was returned by DynamoDB as unprocessed.

## Adding
- Additional return options for `update`, `patch`, `create` and `put` methods.
- Complex Attribute support for "list", "set", and "map".

## [1.0.0] - 2021-06-27
### Added
- new `.match()` method to replace original Find method functionality. [[read more]](./README.md#match-records)
Expand Down Expand Up @@ -47,4 +43,21 @@ All notable changes to this project will be documented in this file. Breaking ch
- Added new update methods `append`, `add`, `subtract`, `data`, `remove`, `delete`, and `data` for improved support of all DynamoDB update methods. [[read more]](./README.md#update-record)

### Changed
- The property names of `ExpressionAttributeValues` underwent some change in this release due to the addition of new update operations. This is not a breaking change but if you have tests to match on the exact params returned from ElectroDB these will likely break. [[read more]](./RELEASE.md#expressionattributevalues-properties)
- The property names of `ExpressionAttributeValues` underwent some change in this release due to the addition of new update operations. This is not a breaking change but if you have tests to match on the exact params returned from ElectroDB these will likely break. [[read more]](./RELEASE.md#expressionattributevalues-properties)

## [1.3.0] - 2021-08-09
### Added
- New Attribute types `map`, `list`, `set` [[read more]](./README.md#expanded-syntax)
- New Query Options, and support for, `ReturnValues` as requested in Issue#71 [[read more]](./README.md#query-options)
- New type definitions for recently released update methods `append`, `add`, `subtract`, `data`, `remove`, and `delete`. [[read more]](./README.md#exported-types)

### Changed
- Attributes that have been flagged as `required` are now not possible to be removed (using the update method `remove()`) from a stored Item. This was an oversight from the last release.
- Attributes that have been flagged as `hidden` now skips invoking that attribute's getter method.

### Fixed
- Issues that prevented the nesting of update `value()` operation.
- TypeScript type definitions for `get()` method now incorporate potential for `null` response.
- Type definitions for `value()` and `name()` where clause operations.


412 changes: 290 additions & 122 deletions README.md

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions examples/versioncontrol/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {subscriptions} from "./subscriptions";
import {users} from "./users";
import {pullRequests, pullRequestComments} from "./pullrequests";
import {issues, issueComments} from "./issues";
import {CreateEntityItem, UpdateEntityItem, EntityItem} from "../../../";
import {CreateEntityItem, UpdateEntityItem, EntityItem, EntityRecord, CollectionItem} from "../../../";
import {Status, SubscriptionTypes, IsNotTicket, isIssueCommentIds, isPullRequestCommentIds, NotYetViewed} from "./types"

const table = "electro";
Expand Down Expand Up @@ -34,6 +34,7 @@ export type CreatePullRequest = CreateEntityItem<typeof pullRequests>;
export type UpdatePullRequest = UpdateEntityItem<typeof pullRequests>;
export type PullRequestItem = EntityItem<typeof pullRequests>;
export type PullRequestIds = Parameters<typeof pullRequests.get>[0][0];
export type PullRequest = EntityRecord<typeof pullRequests>;

export type CreatePullRequestComment = CreateEntityItem<typeof pullRequestComments>;
export type UpdatePullRequestComment = UpdateEntityItem<typeof pullRequestComments>;
Expand All @@ -55,4 +56,7 @@ export type UpdateSubscription = UpdateEntityItem<typeof subscriptions>;
export type SubscriptionItem = EntityItem<typeof subscriptions>;
export type SubscriptionIds = Parameters<typeof subscriptions.get>[0][0];

export { NotYetViewed, Status, SubscriptionTypes, IsNotTicket, isIssueCommentIds, isPullRequestCommentIds };
export type OwnedItems = CollectionItem<typeof store, "owned">;

export { NotYetViewed, Status, SubscriptionTypes, IsNotTicket, isIssueCommentIds, isPullRequestCommentIds };

21 changes: 15 additions & 6 deletions examples/versioncontrol/database/issues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,14 @@ export const issues = new Entity({
},
createdAt: {
type: "string",
default: () => moment.utc().format()
default: () => moment.utc().format(),
readOnly: true,
},
updatedAt: {
type: "string",
watch: ["status"],
set: () => moment.utc().format()
watch: ["*"],
set: () => moment.utc().format(),
readOnly: true,
},
},
indexes: {
Expand All @@ -68,15 +70,15 @@ export const issues = new Entity({
}
},
created: {
collection: "owned",
collection: ["owned", "managed"],
index: "gsi1pk-gsi1sk-index",
pk: {
field: "gsi1pk",
composite: ["username"]
},
sk: {
field: "gsi1sk",
composite: ["status", "repoOwner", "repoName",]
composite: ["status", "createdAt"]
}
},
todos: {
Expand Down Expand Up @@ -151,7 +153,14 @@ export const issueComments = new Entity({
},
createdAt: {
type: "string",
default: () => moment.utc().format()
default: () => moment.utc().format(),
readOnly: true
},
updatedAt: {
type: "string",
watch: ["*"],
set: () => moment.utc().format(),
readOnly: true,
},
},
indexes: {
Expand Down
26 changes: 24 additions & 2 deletions examples/versioncontrol/database/pullrequests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,28 @@ export const pullRequests = new Entity({
set: (val) => toStatusCode(val),
get: (val) => toStatusString(val)
},
reviewers: {
type: "list",
items: {
type: "map",
properties: {
username: {
type: "string",
required: true,
},
approved: {
type: "boolean",
required: true,
},
createdAt: {
type: "string",
default: () => moment.utc().format(),
readOnly: true,
required: true,
}
}
}
},
createdAt: {
type: "string",
default: () => moment.utc().format()
Expand All @@ -61,15 +83,15 @@ export const pullRequests = new Entity({
}
},
created: {
collection: "owned",
collection: ["owned", "managed"],
index: "gsi1pk-gsi1sk-index",
pk: {
field: "gsi1pk",
composite: ["username"]
},
sk: {
field: "gsi1sk",
composite: ["status", "repoOwner", "repoName"]
composite: ["status", "createdAt"]
}
},
enhancements: {
Expand Down
12 changes: 12 additions & 0 deletions examples/versioncontrol/database/repositories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ export const repositories = new Entity({
type: "string",
default: "main"
},
topics: {
type: "set",
items: "string"
},
followers: {
type: "set",
items: "string"
},
stars: {
type: "set",
items: "string"
},
createdAt: {
type: "string",
default: () => moment.utc().format()
Expand Down
21 changes: 18 additions & 3 deletions examples/versioncontrol/database/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,25 @@ export const users = new Entity({
pinned: {
type: "any"
},
following: {
type: "set",
items: "string"
},
followers: {
type: "set",
items: "string"
},
createdAt: {
type: "string",
default: () => moment.utc().format()
}
default: () => moment.utc().format(),
readOnly: true,
},
updatedAt: {
type: "string",
watch: ["*"],
set: () => moment.utc().format(),
readOnly: true,
},
},
indexes: {
user: {
Expand Down Expand Up @@ -68,4 +83,4 @@ export const users = new Entity({
}
}
}
});
});
66 changes: 66 additions & 0 deletions examples/versioncontrol/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Status,
IsNotTicket,
NotYetViewed,
OwnedItems,
isIssueCommentIds,
isPullRequestCommentIds
} from "./database";
Expand Down Expand Up @@ -52,6 +53,28 @@ export async function closePullRequest(user: string, pr: PullRequestIds) {
.go()
}

// Get all user info, repos, pull requests, and issues in one query
export async function getFirstPageLoad(username: string) {
const results: OwnedItems = {
issues: [],
pullRequests: [],
repositories: [],
users: [],
};
let page = null;

do {
const [next, data] = await store.collections.owned({username}).page();
results.issues = results.issues.concat(data.issues);
results.pullRequests = results.pullRequests.concat(data.pullRequests);
results.repositories = results.repositories.concat(data.repositories);
results.users = results.users.concat(data.users);
page = next;
} while (page !== null);

return results;
}

// Get Subscriptions for a given Repository, PullRequest, or Issue.
export async function getSubscribed(repoOwner: string, repoName: string, ticketNumber: string = IsNotTicket) {
return store.collections.subscribers({repoOwner, repoName, ticketNumber}).go();
Expand Down Expand Up @@ -115,3 +138,46 @@ async function readReply(user: string, comment: any): Promise<boolean> {
return false;
}
}

async function approvePullRequest(repoOwner: string, repoName: string, pullRequestNumber: string, username: string) {
const pullRequest = await store.entities.pullRequests
.get({repoOwner, repoName, pullRequestNumber})
.go();

if (!pullRequest || !pullRequest.reviewers) {
return false;
}

let index: number = -1;

for (let i = 0; i < pullRequest.reviewers.length; i++) {
const reviewer = pullRequest.reviewers[i];
if (reviewer.username === username) {
index = i;
}
}

if (index === -1) {
return false;
}

return store.entities.pullRequests
.update({repoOwner, repoName, pullRequestNumber})
.data(({reviewers}, {set}) => {
set(reviewers[index].approved, true);
})
.where(({reviewers}, {eq}) => `
${eq(reviewers[index].username, username)};
`)
.go()
.then(() => true)
.catch(() => false);
}

async function followRepository(repoOwner: string, repoName: string, follower: string) {
await store.entities
.repositories.update({repoOwner, repoName})
.add({followers: [follower]})
.go()
}

Loading

0 comments on commit e58919e

Please sign in to comment.