@bleco/ds-user-updatable
TypeScript icon, indicating that this package has built-in type declarations

0.5.11 • Public • Published

@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

Readme

Keywords

none

Package Sidebar

Install

npm i @bleco/ds-user-updatable

Weekly Downloads

64

Version

0.5.11

License

MIT

Unpacked Size

26.6 kB

Total Files

19

Last publish

Collaborators

  • towyuan