UNPKG

@iredium/butterfly

Version:
54 lines (53 loc) 1.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.UUID = exports.registerType = exports.getter = void 0; var mongoose = require("mongoose"); var util = require("util"); var uuid_1 = require("../../helpers/uuid"); function getter(binary) { if (binary == null) return undefined; // @ts-ignore if (!(binary instanceof mongoose.Types.Buffer.Binary)) { return binary; } return uuid_1.UUID.bufferToBase62(binary.buffer); } exports.getter = getter; function SchemaUUID(path, options) { // @ts-ignore mongoose.SchemaTypes.Buffer.call(this, path, options); // @ts-ignore this.getters.push(getter); } util.inherits(SchemaUUID, mongoose.SchemaTypes.Buffer); SchemaUUID.schemaName = 'UUID'; SchemaUUID.prototype.checkRequired = function (value) { // @ts-ignore return value instanceof mongoose.Types.Buffer.Binary; }; SchemaUUID.prototype.cast = function (value, doc, init) { // @ts-ignore if (value instanceof mongoose.Types.Buffer.Binary) return value; if (typeof value === 'string') { return uuid_1.UUID.stringToBuffer(value); } throw new Error('Could not cast ' + value + ' to UUID.'); }; SchemaUUID.prototype.castForQuery = function ($conditional, val) { var handler; if (arguments.length === 2) { handler = this.$conditionalHandlers[$conditional]; if (!handler) { throw new Error("Can't use " + $conditional + " with UUID."); } return handler.call(this, val); } return this.cast($conditional); }; function registerType(mongoose) { mongoose.Types.UUID = mongoose.SchemaTypes.UUID = SchemaUUID; } exports.registerType = registerType; exports.UUID = SchemaUUID;