pinia-orm
Version:
The Pinia plugin to enable Object-Relational Mapping access to the Pinia Store.
37 lines (32 loc) • 918 B
JavaScript
;
const nonSecure = require('nanoid/non-secure');
const CastAttribute = require('../shared/pinia-orm.DWjxIbAJ.cjs');
class UidCast extends CastAttribute.CastAttribute {
static parameters;
/**
* Create a new String attribute instance.
*/
constructor(attributes) {
super(attributes);
}
static withParameters(parameters) {
this.parameters = parameters;
return this;
}
/**
* Make the value for the attribute.
*/
set(value) {
const nanoid = this.$parameters?.alphabet ? nonSecure.customAlphabet(this.$parameters.alphabet) : nonSecure.nanoid;
return value ?? nanoid(this.$parameters?.size);
}
}
function Uid(options) {
return (target, propertyKey) => {
const self = target.$self();
self.setCast(propertyKey, UidCast.withParameters(options));
self.setRegistry(propertyKey, () => self.uid());
};
}
exports.Uid = Uid;
exports.UidCast = UidCast;