Skip to content

Commit

Permalink
Merge 6859485 into 0c5cead
Browse files Browse the repository at this point in the history
  • Loading branch information
tranhl committed Apr 25, 2024
2 parents 0c5cead + 6859485 commit 7ae624f
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 18 deletions.
2 changes: 2 additions & 0 deletions docs/docs_src/getting_started/TypeScript.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ In order to get started using Dynamoose in your TypeScript project, simply [inst

There is no need to install any additional `@types` package in order to use Dynamoose with TypeScript since the typings are included with the Dynamoose package.

We highly recommend enabling `strict: true` in your [tsconfig.json](https://www.typescriptlang.org/tsconfig#strict), as some typings will not work correctly unless this option is enabled.

After that, so long as you have TypeScript already setup on your project and text editor you should be ready to start using Dynamoose with TypeScript.

## FAQ
Expand Down
14 changes: 7 additions & 7 deletions packages/dynamoose/lib/Model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1004,15 +1004,15 @@ export class Model<T extends ItemCarrier = AnyItem> extends InternalPropertiesCl
}

// Get
get (key: InputKey): Promise<T>;
get (key: InputKey, callback: CallbackType<T, any>): void;
get (key: InputKey): Promise<T | undefined>;
get (key: InputKey, callback: CallbackType<T | undefined, any>): void;
get (key: InputKey, settings: ModelGetSettings & {return: "request"}): Promise<DynamoDB.GetItemInput>;
get (key: InputKey, settings: ModelGetSettings & {return: "request"}, callback: CallbackType<DynamoDB.GetItemInput, any>): void;
get (key: InputKey, settings: ModelGetSettings): Promise<T>;
get (key: InputKey, settings: ModelGetSettings, callback: CallbackType<T, any>): void;
get (key: InputKey, settings: ModelGetSettings & {return: "item"}): Promise<T>;
get (key: InputKey, settings: ModelGetSettings & {return: "item"}, callback: CallbackType<T, any>): void;
get (key: InputKey, settings?: ModelGetSettings | CallbackType<T, any> | CallbackType<DynamoDB.GetItemInput, any>, callback?: CallbackType<T, any> | CallbackType<DynamoDB.GetItemInput, any>): void | Promise<DynamoDB.GetItemInput> | Promise<T> {
get (key: InputKey, settings: ModelGetSettings): Promise<T | undefined>;
get (key: InputKey, settings: ModelGetSettings, callback: CallbackType<T | undefined, any>): void;
get (key: InputKey, settings: ModelGetSettings & {return: "item"}): Promise<T | undefined>;
get (key: InputKey, settings: ModelGetSettings & {return: "item"}, callback: CallbackType<T | undefined, any>): void;
get (key: InputKey, settings?: ModelGetSettings | CallbackType<T | undefined, any> | CallbackType<DynamoDB.GetItemInput, any>, callback?: CallbackType<T | undefined, any> | CallbackType<DynamoDB.GetItemInput, any>): void | Promise<DynamoDB.GetItemInput> | Promise<T | undefined> {
if (typeof settings === "function") {
callback = settings;
settings = {"return": "item"};
Expand Down
8 changes: 4 additions & 4 deletions packages/dynamoose/test/types/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ const shouldFailWithInvalidConditionTransaction = model.transaction.condition(0,

// Typed Models
export class User extends Item {
id: string;
name: string;
age: number;
id!: string;
name!: string;
age!: number;
}
const userSchema = new dynamoose.Schema({
"id": String,
Expand Down Expand Up @@ -108,5 +108,5 @@ UserTypedModel.update({"id": "foo"}, {
});

UserTypedModel.update({"id": "foo"}, {
"$REMOVE":{"age":null}
"$REMOVE":{"age":undefined}
});
2 changes: 1 addition & 1 deletion packages/dynamoose/test/types/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ const shouldSucceedWithAsyncSetMethodSchema = new dynamoose.Schema({
const shouldSucceedWithSetMethodSecondArgSchema = new dynamoose.Schema({
"id": {
"type": String,
"set": (value, oldValue) => oldValue
"set": (value, oldValue) => oldValue ? oldValue : value
}
});
const shouldSucceedWithAsyncValidateMethodSchema = new dynamoose.Schema({
Expand Down
12 changes: 12 additions & 0 deletions packages/dynamoose/test/types/model/Get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* eslint @typescript-eslint/no-unused-vars: 0 */

import {UserTypedModel} from "../Model";

const shouldGetSuccessfully = await UserTypedModel.get("1");
const user = shouldGetSuccessfully;
// @ts-expect-error
const shouldFailImmediatePropertyAccess = user.id;

if (user) {
const shouldPassPropertyAccessAfterNullishCheck = user.id;
}
7 changes: 5 additions & 2 deletions packages/dynamoose/test/types/model/Query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ let isAssignableToQuery : Query<User>;
isAssignableToQuery = UserTypedModel.query("name").eq("Will").limit(5);

// query.startAt(key)
async function queryStartAt (): Promise<Query<User>> {
async function queryStartAt (): Promise<Query<User> | undefined> {
const response = await UserTypedModel.query("name").eq("Will").exec();
return UserTypedModel.query("name").eq("Will").startAt(response.lastKey);

if (response.lastKey) {
return UserTypedModel.query("name").eq("Will").startAt(response.lastKey);
}
}

// query.attributes(attributes)
Expand Down
6 changes: 4 additions & 2 deletions packages/dynamoose/test/types/model/Scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ let isAssignableToScan : Scan<User>;
isAssignableToScan = UserTypedModel.scan().limit(5);

// scan.startAt(key)
async function scanStartAt (): Promise<Scan<User>> {
async function scanStartAt (): Promise<Scan<User> | undefined> {
const response = await UserTypedModel.scan().exec();
return UserTypedModel.scan().startAt(response.lastKey);
if (response.lastKey) {
return UserTypedModel.scan().startAt(response.lastKey);
}
}

// scan.attributes(attributes)
Expand Down
5 changes: 3 additions & 2 deletions packages/dynamoose/test/types/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"moduleResolution": "node",
"lib": ["es2015"],
"target": "es2017",
"noEmit": true
},
"noEmit": true,
"strict": true
},
"include": ["./**/*"]
}

0 comments on commit 7ae624f

Please sign in to comment.