Skip to content

Commit

Permalink
feat: move tableName from model to options
Browse files Browse the repository at this point in the history
  • Loading branch information
hardyscc committed Apr 26, 2023
1 parent 9d1a5ba commit 15a06f2
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 13 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,11 @@ import { UserService } from './user.service';
imports: [
DynamooseModule.forFeature([{
name: 'User',
tableName: 'user',
schema: UserSchema }]),
schema: UserSchema,
options: {
tableName: 'user',
},
}]),
],
providers: [
UserService,
Expand All @@ -125,7 +128,7 @@ import { UserService } from './user.service';
export class UserModule {}
```

> `tableName` is optional. If it is not provided, `name` will be used as the table name.
> `options.tableName` is optional. If it is not provided, `name` will be used as the table name.

There is also `forFeatureAsync(factories?: AsyncModelFactory[])` if you want to use a factory with dependency injection. Notes that the first parameter of the `useFactory` callback is reserved for future use, so please just add `_,` to ignore it.

Expand All @@ -144,8 +147,10 @@ import { UserService } from './user.service';
name: 'User',
useFactory: (_, configService: ConfigService) => {
return {
tableName: configService.get<string>('USER_TABLE_NAME'),
schema: UserSchema,
options: {
tableName: configService.get<string>('USER_TABLE_NAME'),
},
};
},
inject: [ConfigService],
Expand Down
7 changes: 3 additions & 4 deletions lib/dynamoose.providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function createDynamooseProviders(models: ModelDefinition[] = []) {
provide: getModelToken(model.name),
useFactory: () => {
const modelInstance = dynamoose.model(
model.tableName || model.name,
model.name,
model.schema,
model.options,
);
Expand Down Expand Up @@ -43,12 +43,11 @@ export function createDynamooseAsyncProviders(
} else {
schema = object as ModelDefinition['schema'];
}
const tableName =
modelDefinition?.tableName || model.tableName || model.name;

const options = modelDefinition?.options || model.options;
const serializers = modelDefinition?.serializers || model.serializers;

const modelInstance = dynamoose.model(tableName, schema, options);
const modelInstance = dynamoose.model(model.name, schema, options);
if (serializers) {
Object.entries(serializers).forEach(([key, value]) => {
modelInstance.serializer.add(key, value);
Expand Down
2 changes: 1 addition & 1 deletion lib/interfaces/async-model-factory.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ModelDefinition } from './model-definition.interface';

export interface AsyncModelFactory
extends Pick<ModuleMetadata, 'imports'>,
Pick<ModelDefinition, 'name' | 'tableName' | 'options' | 'serializers'> {
Pick<ModelDefinition, 'name' | 'options' | 'serializers'> {
useFactory: (
...args: any[]
) =>
Expand Down
5 changes: 2 additions & 3 deletions lib/interfaces/model-definition.interface.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { ModelTableOptions } from 'dynamoose/dist/Model';
import { Schema, SchemaDefinition } from 'dynamoose/dist/Schema';
import { SerializerOptions } from 'dynamoose/dist/Serializer';
import { TableOptionsOptional } from 'dynamoose/dist/Table';

export type ModelDefinition = {
name: string;
schema: Schema | SchemaDefinition | (Schema | SchemaDefinition)[];
tableName?: string;
options?: TableOptionsOptional;
options?: ModelTableOptions;
serializers?: { [key: string]: SerializerOptions };
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"@aws-sdk/client-dynamodb": "^3.0.0",
"@nestjs/common": "^6.0.0 || ^7.0.0 || ^8.0.0 || ^9.0.0",
"@nestjs/core": "^6.0.0 || ^7.0.0 || ^8.0.0 || ^9.0.0",
"dynamoose": "^3.0.0",
"dynamoose": "^3.2.0",
"reflect-metadata": "^0.1.12",
"rxjs": "^6.0.0 || ^7.0.0"
},
Expand Down

0 comments on commit 15a06f2

Please sign in to comment.