npm.io
0.5.11 • Published 1 year ago

@bleco/ds-user-updatable

Licence
MIT
Version
0.5.11
Deps
7
Size
26 kB
Vulns
0
Weekly
0

@bleco/ds-user-updatable

A loopback next extension for automatically adding and managing createdBy and updatedBy attributes to Model

Usage

For example, if you have a model called Book:

// book.model.ts
import {UserUpdatableModelMixin} from '';
import {Entity, model, property} from '@loopback/repository';

@model()
@mixin(UserUpdatableModelMixin)
class Book extends Entity {
  @property({
    type: 'number',
    id: true,
  })
  id: number;

  @property()
  name: string;

  @property()
  type: string;

  constructor(data?: Partial<Book>) {
    super(data);
  }
}

interface Book extends UserUpdatableModel {}

And then, you define a repository with UserUpdatableRepositoryMixin:

// book.repository.ts
import {UserUpdatableRepositoryMixin} from '';
import {mixin} from '@bleco/mixin';
import {DefaultCrudRepository} from '@loopback/repository';

@mixin(UserUpdatableRepositoryMixin({throwIfNoUser: true, userIdKey: ['userTenantId', 'id']}))
class BookRepository extends DefaultCrudRepository<Book, typeof Book.prototype.id> {
  constructor(dataSource: juggler.DataSource) {
    super(Book, dataSource);
  }
}

interface BookRepository extends UserUpdatableRepository<Book, typeof Book.prototype.id, Book, string> {}

UserUpdatableRepositoryMixinOptions is an optional parameter for @mixin(UserUpdatableRepositoryMixin(options))

type UserUpdatableRepositoryMixinOptions = {
  // Throw InvalidCredentials error if no getCurrentUser function provided or no user signed in
  throwIfNoUser?: boolean;
  // The keys of user id in user object. It will try to get user id from user object by keys in order until got a first non null value.
  userIdKey?: string | string[];
};

License

MIT