UNPKG

pinia-orm

Version:

The Pinia plugin to enable Object-Relational Mapping access to the Pinia Store.

170 lines (146 loc) 5.6 kB
'use strict'; const Metadata = require('./shared/pinia-orm.uuzKYJ-M.cjs'); const Utils = require('./shared/pinia-orm.DiWsQ6gA.cjs'); function Attr(value) { return Metadata.createFieldDecorator((model) => model.attr(value)); } function Str(value, options = {}) { return Metadata.createFieldDecorator((model) => { const attr = model.string(value); if (options.notNullable) { attr.notNullable(); } return attr; }); } function Num(value, options = {}) { return Metadata.createFieldDecorator((model) => { const attr = model.number(value); if (options.notNullable) { attr.notNullable(); } return attr; }); } function Bool(value, options = {}) { return Metadata.createFieldDecorator((model) => { const attr = model.boolean(value); if (options.notNullable) { attr.notNullable(); } return attr; }); } function Uid(options) { return Metadata.createFieldDecorator((model) => model.uid(options)); } function HasOne(related, foreignKey, localKey) { return Metadata.createFieldDecorator((model) => model.hasOne(related(), foreignKey, localKey)); } function BelongsTo(related, foreignKey, ownerKey) { return Metadata.createFieldDecorator((model) => model.belongsTo(related(), foreignKey, ownerKey)); } function BelongsToMany(related, pivot, foreignPivotKey, relatedPivotKey, parentKey, relatedKey) { return Metadata.createFieldDecorator((model) => { if (typeof pivot === "function") { return model.belongsToMany(related(), pivot(), foreignPivotKey, relatedPivotKey, parentKey, relatedKey); } return model.belongsToMany(related(), pivot.model(), foreignPivotKey, relatedPivotKey, parentKey, relatedKey).as(pivot.as); }); } function HasMany(related, foreignKey, localKey) { return Metadata.createFieldDecorator((model) => model.hasMany(related(), foreignKey, localKey)); } function HasManyBy(related, foreignKey, ownerKey) { return Metadata.createFieldDecorator((model) => model.hasManyBy(related(), foreignKey, ownerKey)); } function HasManyThrough(related, through, firstKey, secondKey, localKey, secondLocalKey) { return Metadata.createFieldDecorator((model) => model.hasManyThrough(related(), through(), firstKey, secondKey, localKey, secondLocalKey)); } function MorphOne(related, id, type, localKey) { return Metadata.createFieldDecorator((model) => model.morphOne(related(), id, type, localKey)); } function MorphTo(related, id, type, ownerKey) { return Metadata.createFieldDecorator((model) => model.morphTo(related(), id, type, ownerKey)); } function MorphToMany(related, pivot, relatedId, id, type, parentKey, relatedKey) { return Metadata.createFieldDecorator((model) => { if (typeof pivot === "function") { return model.morphToMany(related(), pivot(), relatedId, id, type, parentKey, relatedKey); } return model.morphToMany(related(), pivot.model(), relatedId, id, type, parentKey, relatedKey).as(pivot.as); }); } function MorphedByMany(related, pivot, relatedId, id, type, parentKey, relatedKey) { return Metadata.createFieldDecorator((model) => { if (typeof pivot === "function") { return model.morphedByMany(related(), pivot(), relatedId, id, type, parentKey, relatedKey); } return model.morphedByMany(related(), pivot.model(), relatedId, id, type, parentKey, relatedKey).as(pivot.as); }); } function MorphMany(related, id, type, localKey) { return Metadata.createFieldDecorator((model) => model.morphMany(related(), id, type, localKey)); } function OnDelete(mode) { return Metadata.createRegistrationDecorator((context) => { Metadata.ownMetadataRecord(context.metadata, Metadata.FIELDS_ON_DELETE)[String(context.name)] = mode; }); } function Cast(to) { return Metadata.createRegistrationDecorator((context) => { Metadata.ownMetadataRecord(context.metadata, Metadata.CASTS)[String(context.name)] = to(); }); } function Mutate(get, set) { return Metadata.createRegistrationDecorator((context) => { Metadata.ownMetadataRecord(context.metadata, Metadata.MUTATORS)[String(context.name)] = { get, set }; }); } function Hidden() { return Metadata.createRegistrationDecorator((context) => { Metadata.ownMetadataRecord(context.metadata, Metadata.HIDDEN)[String(context.name)] = true; }); } function NonEnumerable(_target, context) { if (!context || typeof context !== "object" || context.kind !== "accessor") { Utils.throwError([ "The NonEnumerable decorator requires the `accessor` keyword,", "e.g. `@NonEnumerable accessor hidden!: string`." ]); } context.addInitializer(function() { let proto = Object.getPrototypeOf(this); while (proto) { const descriptor = Object.getOwnPropertyDescriptor(proto, context.name); if (descriptor) { if (descriptor.enumerable) { Object.defineProperty(proto, context.name, { ...descriptor, enumerable: false }); } break; } proto = Object.getPrototypeOf(proto); } }); } exports.Attr = Attr; exports.BelongsTo = BelongsTo; exports.BelongsToMany = BelongsToMany; exports.Bool = Bool; exports.Cast = Cast; exports.HasMany = HasMany; exports.HasManyBy = HasManyBy; exports.HasManyThrough = HasManyThrough; exports.HasOne = HasOne; exports.Hidden = Hidden; exports.MorphMany = MorphMany; exports.MorphOne = MorphOne; exports.MorphTo = MorphTo; exports.MorphToMany = MorphToMany; exports.MorphedByMany = MorphedByMany; exports.Mutate = Mutate; exports.NonEnumerable = NonEnumerable; exports.Num = Num; exports.OnDelete = OnDelete; exports.Str = Str; exports.Uid = Uid;