@goodrequest/user-repository-sequelize
TypeScript icon, indicating that this package has built-in type declarations

0.4.0 • Public • Published

Build and run tests Publish package to GitHub Packages

User Repository implemented using Sequelize

This is implementation of user repository interface specified in passport-jwt-wrapper library. It uses sequelize library for the data storage.

Installation

npm i --save @goodrequest/user-repository-sequelize

Usage

import { UserRepository } from '@goodrequest/user-repository-sequelize'
import { models } from './db/models'

initAuth(passport, {
	userRepository: new UserRepository(models.User),
	refreshTokenRepository
})

Models Templates

Library also exports basic Sequelize models:

  • BaseUserModel: BaseUserModel - id is not defined in the attributes, needs to expanded
  • UUIDUserModel: UUIDUserModel - Uses uuid as id
  • BigIntUserModel: BigIntUserModel- Uses BIGINT with autoincrement as id

Model definition is divided into:

  • UserModelClass
  • UserModelAttributes
  • UserModelOptions

These parts are used in the sequelize init method: UserModel.init(UserModelAttributes, UserModelOptions)

Model needs to be defined and can be expanded using these parts. Examples are in the tests folder:

export class UuidUserModel extends UUIDUserModel {}

/* eslint-disable no-param-reassign */
export default (sequelize: Sequelize, modelName: string) => {
	UuidUserModel.init(
		{
			...UUIDUserModelAttributes,
			// overload createdBy column definition
			createdBy: {
				type: DataTypes.UUID,
				allowNull: true
			},
			// virtual attributes
			fullName: {
				type: DataTypes.VIRTUAL,
				get() {
					return `${this.name ?? ''} ${this.surname ?? ''}`.trim()
				}
			}
		},
		{
			...UUIDUserModelOptions,
			sequelize,
			modelName,
			tableName: 'uuid-users'
		}
	)

	return UuidUserModel
}

Readme

Keywords

none

Package Sidebar

Install

npm i @goodrequest/user-repository-sequelize

Weekly Downloads

15

Version

0.4.0

License

ISC

Unpacked Size

107 kB

Total Files

67

Last publish

Collaborators

  • logingoodrequest
  • lubomirigonda