Skip to content

Commit

Permalink
feat: support more configs on forFeatureAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
hardyscc committed Apr 6, 2023
1 parent 9e1c69c commit d3cedbd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
28 changes: 19 additions & 9 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.name,
model.tableName || model.name,
model.schema,
model.options,
);
Expand All @@ -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<ModelDefinition, 'name'> | undefined;
let schema: ModelDefinition['schema'] | undefined;
if (object.hasOwnProperty('schema')) {
modelDefinition = object as Omit<ModelDefinition, 'name'>;
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);
});
}
Expand Down
8 changes: 6 additions & 2 deletions lib/interfaces/async-model-factory.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ import { ModelDefinition } from './model-definition.interface';

export interface AsyncModelFactory
extends Pick<ModuleMetadata, 'imports'>,
Pick<ModelDefinition, 'name' | 'options' | 'serializers'> {
Pick<ModelDefinition, 'name' | 'tableName' | 'options' | 'serializers'> {
useFactory: (
...args: any[]
) => ModelDefinition['schema'] | Promise<ModelDefinition['schema']>;
) =>
| ModelDefinition['schema']
| Promise<ModelDefinition['schema']>
| Omit<ModelDefinition, 'name'>
| Promise<Omit<ModelDefinition, 'name'>>;
inject?: any[];
}
3 changes: 2 additions & 1 deletion lib/interfaces/model-definition.interface.ts
Original file line number Diff line number Diff line change
@@ -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 };
};

0 comments on commit d3cedbd

Please sign in to comment.