UNPKG

pinia-orm

Version:

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

148 lines (125 loc) 5.09 kB
import { c as createFieldDecorator, a as createRegistrationDecorator, o as ownMetadataRecord, F as FIELDS_ON_DELETE, C as CASTS, M as MUTATORS, H as HIDDEN } from './shared/pinia-orm.DhuaEUVi.mjs'; import { t as throwError } from './shared/pinia-orm.pROBaphR.mjs'; function Attr(value) { return createFieldDecorator((model) => model.attr(value)); } function Str(value, options = {}) { return createFieldDecorator((model) => { const attr = model.string(value); if (options.notNullable) { attr.notNullable(); } return attr; }); } function Num(value, options = {}) { return createFieldDecorator((model) => { const attr = model.number(value); if (options.notNullable) { attr.notNullable(); } return attr; }); } function Bool(value, options = {}) { return createFieldDecorator((model) => { const attr = model.boolean(value); if (options.notNullable) { attr.notNullable(); } return attr; }); } function Uid(options) { return createFieldDecorator((model) => model.uid(options)); } function HasOne(related, foreignKey, localKey) { return createFieldDecorator((model) => model.hasOne(related(), foreignKey, localKey)); } function BelongsTo(related, foreignKey, ownerKey) { return createFieldDecorator((model) => model.belongsTo(related(), foreignKey, ownerKey)); } function BelongsToMany(related, pivot, foreignPivotKey, relatedPivotKey, parentKey, relatedKey) { return 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 createFieldDecorator((model) => model.hasMany(related(), foreignKey, localKey)); } function HasManyBy(related, foreignKey, ownerKey) { return createFieldDecorator((model) => model.hasManyBy(related(), foreignKey, ownerKey)); } function HasManyThrough(related, through, firstKey, secondKey, localKey, secondLocalKey) { return createFieldDecorator((model) => model.hasManyThrough(related(), through(), firstKey, secondKey, localKey, secondLocalKey)); } function MorphOne(related, id, type, localKey) { return createFieldDecorator((model) => model.morphOne(related(), id, type, localKey)); } function MorphTo(related, id, type, ownerKey) { return createFieldDecorator((model) => model.morphTo(related(), id, type, ownerKey)); } function MorphToMany(related, pivot, relatedId, id, type, parentKey, relatedKey) { return 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 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 createFieldDecorator((model) => model.morphMany(related(), id, type, localKey)); } function OnDelete(mode) { return createRegistrationDecorator((context) => { ownMetadataRecord(context.metadata, FIELDS_ON_DELETE)[String(context.name)] = mode; }); } function Cast(to) { return createRegistrationDecorator((context) => { ownMetadataRecord(context.metadata, CASTS)[String(context.name)] = to(); }); } function Mutate(get, set) { return createRegistrationDecorator((context) => { ownMetadataRecord(context.metadata, MUTATORS)[String(context.name)] = { get, set }; }); } function Hidden() { return createRegistrationDecorator((context) => { ownMetadataRecord(context.metadata, HIDDEN)[String(context.name)] = true; }); } function NonEnumerable(_target, context) { if (!context || typeof context !== "object" || context.kind !== "accessor") { 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); } }); } export { Attr, BelongsTo, BelongsToMany, Bool, Cast, HasMany, HasManyBy, HasManyThrough, HasOne, Hidden, MorphMany, MorphOne, MorphTo, MorphToMany, MorphedByMany, Mutate, NonEnumerable, Num, OnDelete, Str, Uid };