Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,29 @@ export class UserRepository extends DefaultUserModifyCrudRepository<
)
```

The repository by default allows to pass back dated or future dates for createdOn and modifiedOn values but to restrict the manual date modification we can pass overridingOptions like this
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The repository by default allows to pass back dated or future dates for createdOn and modifiedOn values but to restrict the manual date modification we can pass overridingOptions like this
The repository by default does not restrict setting up of createdOn and modifiedOn through API or external sources. However, you can restrict it using the overridingOptions like this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


```ts
export class UsersRepository extends DefaultUserModifyCrudRepository<
Users,
typeof Users.prototype.id,
UsersRelations
> {
constructor(
@inject(`datasources.AuditDB`) dataSource: juggler.DataSource,
@inject.getter(AuthenticationBindings.CURRENT_USER)
protected readonly getCurrentUser: Getter<
IAuthUserWithPermissions | undefined
>,
) {
super(Users, dataSource, getCurrentUser);
}
public overridingOptions = {
restrictDateModification: true,
};
}
```

![Connector](https://loopback.io/images/9830486.png)

#### SequelizeUserModifyCrudRepository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {AuthErrorKeys} from 'loopback4-authentication';
import {DefaultTransactionSoftCrudRepository} from 'loopback4-soft-delete';
import {IAuthUserWithPermissions} from '../components';
import {UserModifiableEntity} from '../models';
import {RepositoryOverridingOptions} from '../types';

export abstract class DefaultTransactionalUserModifyRepository<
T extends UserModifiableEntity,
Expand All @@ -34,6 +35,8 @@ export abstract class DefaultTransactionalUserModifyRepository<
super(entityClass, dataSource);
}

public overridingOptions?: RepositoryOverridingOptions;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont like this approach. This will mean they will have to do it for all the repo files. Why ? We need to support both mechanism. One using options and another using a global config.

Copy link
Contributor Author

@yeshamavani yeshamavani Oct 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried passing an optional config to the repository but it still requires it from the derived class no mater what
making it a Breaking change - hence used this approach

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now using the coreConfig for decision making


async create(entity: DataObject<T>, options?: Options): Promise<T> {
let currentUser = await this.getCurrentUser();
currentUser = currentUser ?? options?.currentUser;
Expand All @@ -43,6 +46,10 @@ export abstract class DefaultTransactionalUserModifyRepository<
const uid = currentUser?.userTenantId ?? currentUser?.id;
entity.createdBy = uid;
entity.modifiedBy = uid;
if (this.overridingOptions?.restrictDateModification) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why cant we use the second parameter of the method options?: Options. That ways its consistent and easy to implement.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the method's option parameter user will have to make changes in all the method calls
but with this only one change at repo level

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this introduces a new param whereas we already have such options in method param. The second param is for that purpose only. Moreover, using the method options param gives more flexibility at runtime.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now using the coreConfig for decision making

delete entity.createdOn;
delete entity.modifiedOn;
}
return super.create(entity, options);
}

Expand All @@ -56,6 +63,10 @@ export abstract class DefaultTransactionalUserModifyRepository<
entities.forEach(entity => {
entity.createdBy = uid ?? '';
entity.modifiedBy = uid ?? '';
if (this.overridingOptions?.restrictDateModification) {
delete entity.createdOn;
delete entity.modifiedOn;
}
});
return super.createAll(entities, options);
}
Expand All @@ -67,6 +78,10 @@ export abstract class DefaultTransactionalUserModifyRepository<
}
const uid = currentUser?.userTenantId ?? currentUser?.id;
entity.modifiedBy = uid;
if (this.overridingOptions?.restrictDateModification) {
delete entity.createdOn;
delete entity.modifiedOn;
}
return super.save(entity, options);
}

Expand All @@ -77,6 +92,11 @@ export abstract class DefaultTransactionalUserModifyRepository<
}
const uid = currentUser?.userTenantId ?? currentUser?.id;
entity.modifiedBy = uid;
if (this.overridingOptions?.restrictDateModification) {
/**not deleting the createdOn as it can be a use case where
* we want to update the modifiedOn but not the createdOn */
delete entity.modifiedOn;
}
return super.update(entity, options);
}

Expand All @@ -92,6 +112,11 @@ export abstract class DefaultTransactionalUserModifyRepository<
}
const uid = currentUser?.userTenantId ?? currentUser?.id;
data.modifiedBy = uid;
if (this.overridingOptions?.restrictDateModification) {
/**not deleting the createdOn as it can be a use case where
* we want to update the modifiedOn but not the createdOn */
delete data.modifiedOn;
}
return super.updateAll(data, where, options);
}

Expand All @@ -107,6 +132,11 @@ export abstract class DefaultTransactionalUserModifyRepository<
}
const uid = currentUser?.userTenantId ?? currentUser?.id;
data.modifiedBy = uid;
if (this.overridingOptions?.restrictDateModification) {
/**not deleting the createdOn as it can be a use case where
* we want to update the modifiedOn but not the createdOn */
delete data.modifiedOn;
}
return super.updateById(id, data, options);
}

Expand All @@ -121,6 +151,11 @@ export abstract class DefaultTransactionalUserModifyRepository<
}
const uid = currentUser?.userTenantId ?? currentUser?.id;
data.modifiedBy = uid;
if (this.overridingOptions?.restrictDateModification) {
/**not deleting the createdOn as it can be a use case where
* we want to update the modifiedOn but not the createdOn */
delete data.modifiedOn;
}
return super.replaceById(id, data, options);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {SoftCrudRepository} from 'loopback4-soft-delete';

import {IAuthUserWithPermissions} from '../components';
import {UserModifiableEntity} from '../models';
import {RepositoryOverridingOptions} from '../types';

export class DefaultUserModifyCrudRepository<
T extends UserModifiableEntity,
Expand All @@ -34,6 +35,7 @@ export class DefaultUserModifyCrudRepository<
) {
super(entityClass, dataSource);
}
public overridingOptions?: RepositoryOverridingOptions;

async create(entity: DataObject<T>, options?: Options): Promise<T> {
let currentUser = await this.getCurrentUser();
Expand All @@ -44,6 +46,10 @@ export class DefaultUserModifyCrudRepository<
const uid = currentUser?.userTenantId ?? currentUser?.id;
entity.createdBy = uid;
entity.modifiedBy = uid;
if (this.overridingOptions?.restrictDateModification) {
delete entity.createdOn;
delete entity.modifiedOn;
}
return super.create(entity, options);
}

Expand All @@ -57,6 +63,10 @@ export class DefaultUserModifyCrudRepository<
entities.forEach(entity => {
entity.createdBy = uid ?? '';
entity.modifiedBy = uid ?? '';
if (this.overridingOptions?.restrictDateModification) {
delete entity.createdOn;
delete entity.modifiedOn;
}
});
return super.createAll(entities, options);
}
Expand All @@ -68,6 +78,10 @@ export class DefaultUserModifyCrudRepository<
}
const uid = currentUser?.userTenantId ?? currentUser?.id;
entity.modifiedBy = uid;
if (this.overridingOptions?.restrictDateModification) {
delete entity.createdOn;
delete entity.modifiedOn;
}
return super.save(entity, options);
}

Expand All @@ -78,6 +92,11 @@ export class DefaultUserModifyCrudRepository<
}
const uid = currentUser?.userTenantId ?? currentUser?.id;
entity.modifiedBy = uid;
if (this.overridingOptions?.restrictDateModification) {
/**not deleting the createdOn as it can be a use case where
* we want to update the modifiedOn but not the createdOn */
delete entity.modifiedOn;
}
return super.update(entity, options);
}

Expand All @@ -93,6 +112,11 @@ export class DefaultUserModifyCrudRepository<
}
const uid = currentUser?.userTenantId ?? currentUser?.id;
data.modifiedBy = uid;
if (this.overridingOptions?.restrictDateModification) {
/**not deleting the createdOn as it can be a use case where
* we want to update the modifiedOn but not the createdOn */
delete data.modifiedOn;
}
return super.updateAll(data, where, options);
}

Expand All @@ -108,6 +132,11 @@ export class DefaultUserModifyCrudRepository<
}
const uid = currentUser?.userTenantId ?? currentUser?.id;
data.modifiedBy = uid;
if (this.overridingOptions?.restrictDateModification) {
/**not deleting the createdOn as it can be a use case where
* we want to update the modifiedOn but not the createdOn */
delete data.modifiedOn;
}
return super.updateById(id, data, options);
}
async replaceById(
Expand All @@ -121,6 +150,11 @@ export class DefaultUserModifyCrudRepository<
}
const uid = currentUser?.userTenantId ?? currentUser?.id;
data.modifiedBy = uid;
if (this.overridingOptions?.restrictDateModification) {
/**not deleting the createdOn as it can be a use case where
* we want to update the modifiedOn but not the createdOn */
delete data.modifiedOn;
}
return super.replaceById(id, data, options);
}
}
9 changes: 8 additions & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ export interface IServiceConfig {
useCustomSequence: boolean;
useSequelize?: boolean;
}

/**This type is used to override the default behaviour of our repo classes */
export interface RepositoryOverridingOptions {
/**restricts user to pass createdOn and modifiedOn fields in the request body
* and only current date will be set in the database */
restrictDateModification: boolean;
//eslint-disable-next-line @typescript-eslint/no-explicit-any
[property: string]: any; //NOSONAR
}
export type OASPathDefinition = AnyObject;

export interface CoreConfig {
Expand Down
Loading