diff --git a/lib/dynamoose.providers.ts b/lib/dynamoose.providers.ts index 3d781026..779513aa 100644 --- a/lib/dynamoose.providers.ts +++ b/lib/dynamoose.providers.ts @@ -10,7 +10,7 @@ export function createDynamooseProviders(models: ModelDefinition[] = []) { provide: getModelToken(model.name), useFactory: () => { const modelInstance = dynamoose.model( - model.name, + model.tableName || model.name, model.schema, model.options, ); @@ -33,14 +33,24 @@ export function createDynamooseAsyncProviders( { provide: getModelToken(model.name), useFactory: async (...args: unknown[]) => { - const schema = await model.useFactory(...args); - const modelInstance = dynamoose.model( - model.name, - schema, - model.options, - ); - if (model.serializers) { - Object.entries(model.serializers).forEach(([key, value]) => { + const object = await model.useFactory(...args); + + let modelDefinition: Omit | undefined; + let schema: ModelDefinition['schema'] | undefined; + if (object.hasOwnProperty('schema')) { + modelDefinition = object as Omit; + schema = modelDefinition.schema; + } 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); + if (serializers) { + Object.entries(serializers).forEach(([key, value]) => { modelInstance.serializer.add(key, value); }); } diff --git a/lib/interfaces/async-model-factory.interface.ts b/lib/interfaces/async-model-factory.interface.ts index a2519e77..6b97bae3 100644 --- a/lib/interfaces/async-model-factory.interface.ts +++ b/lib/interfaces/async-model-factory.interface.ts @@ -3,9 +3,13 @@ import { ModelDefinition } from './model-definition.interface'; export interface AsyncModelFactory extends Pick, - Pick { + Pick { useFactory: ( ...args: any[] - ) => ModelDefinition['schema'] | Promise; + ) => + | ModelDefinition['schema'] + | Promise + | Omit + | Promise>; inject?: any[]; } diff --git a/lib/interfaces/model-definition.interface.ts b/lib/interfaces/model-definition.interface.ts index 760817d8..55320db4 100644 --- a/lib/interfaces/model-definition.interface.ts +++ b/lib/interfaces/model-definition.interface.ts @@ -1,10 +1,11 @@ -import { TableOptionsOptional } from 'dynamoose/dist/Table'; 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; serializers?: { [key: string]: SerializerOptions }; };