UNPKG

@accounts/mikro-orm

Version:
78 lines 2.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getUserSchema = exports.getUserCtor = void 0; const core_1 = require("@mikro-orm/core"); const Service_1 = require("./Service"); const Email_1 = require("./Email"); const Session_1 = require("./Session"); const getUserCtor = ({ abstract = false, EmailEntity = Email_1.Email, ServiceEntity = Service_1.Service, getCustomEmailArgs = () => ({}), getCustomServiceArgs = () => ({}), } = {}) => { class User { id; username; services = new core_1.Collection(this); emails = new core_1.Collection(this); sessions = new core_1.Collection(this); deactivated = false; createdAt = new Date(); updatedAt = new Date(); constructor(args) { const { email, password, username } = args; if (username) { this.username = username; } if (email) { this.emails.add(new EmailEntity({ address: email, ...getCustomEmailArgs(args) })); } if (password) { this.services.add(new ServiceEntity({ name: 'password', password, ...getCustomServiceArgs(args), })); } } } if (abstract) { Object.defineProperty(User, 'name', { value: 'AccountsUser' }); } return User; }; exports.getUserCtor = getUserCtor; const getUserSchema = ({ AccountsUser, EmailEntity = Email_1.Email, SessionEntity = Session_1.Session, ServiceEntity = Service_1.Service, abstract = false, }) => { if (abstract) { Object.defineProperty(AccountsUser, 'name', { value: 'AccountsUser' }); } return new core_1.EntitySchema({ class: AccountsUser, abstract, properties: { id: { type: 'number', primary: true }, createdAt: { type: 'Date', onCreate: () => new Date(), nullable: true }, updatedAt: { type: 'Date', onCreate: () => new Date(), onUpdate: () => new Date(), nullable: true, }, username: { type: 'string', nullable: true }, deactivated: { type: 'boolean', default: false, onCreate: () => false }, services: { kind: '1:m', entity: () => ServiceEntity?.name ?? Service_1.Service.name, mappedBy: (service) => service.user, }, emails: { kind: '1:m', entity: () => EmailEntity?.name ?? Email_1.Email.name, mappedBy: (email) => email.user, }, sessions: { kind: '1:m', entity: () => SessionEntity?.name ?? Session_1.Session.name, mappedBy: (session) => session.user, }, }, }); }; exports.getUserSchema = getUserSchema; //# sourceMappingURL=User.js.map