arkormx 2.2.3 → 2.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{index-DbtnN_Yb.d.mts → index-DV9QDB5H.d.mts} +266 -79
- package/dist/{index-nSC0udqX.d.cts → index-XyE0Kf2W.d.cts} +266 -79
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +1 -1
- package/dist/relationship/index.cjs +1 -1
- package/dist/relationship/index.d.cts +1 -1
- package/dist/relationship/index.d.mts +1 -1
- package/dist/relationship/index.mjs +1 -1
- package/dist/{relationship-CRlJHS90.cjs → relationship-55Hpemxh.cjs} +268 -0
- package/dist/{relationship-CJaPnw92.mjs → relationship-k7kdsCor.mjs} +268 -0
- package/package.json +1 -1
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { An as RelationTableLoader, a as HasOneRelation, c as BelongsToRelation, d as Relation, i as HasOneThroughRelation, kn as SetBasedEagerLoader, l as SingleResultRelation, n as MorphOneRelation, o as HasManyThroughRelation, r as MorphManyRelation, s as HasManyRelation, t as MorphToManyRelation, u as BelongsToManyRelation } from "../relationship-
|
|
1
|
+
import { An as RelationTableLoader, a as HasOneRelation, c as BelongsToRelation, d as Relation, i as HasOneThroughRelation, kn as SetBasedEagerLoader, l as SingleResultRelation, n as MorphOneRelation, o as HasManyThroughRelation, r as MorphManyRelation, s as HasManyRelation, t as MorphToManyRelation, u as BelongsToManyRelation } from "../relationship-k7kdsCor.mjs";
|
|
2
2
|
|
|
3
3
|
export { BelongsToManyRelation, BelongsToRelation, HasManyRelation, HasManyThroughRelation, HasOneRelation, HasOneThroughRelation, MorphManyRelation, MorphOneRelation, MorphToManyRelation, Relation, RelationTableLoader, SetBasedEagerLoader, SingleResultRelation };
|
|
@@ -3362,9 +3362,34 @@ var Relation = class {
|
|
|
3362
3362
|
getRelatedModel() {
|
|
3363
3363
|
return this.related;
|
|
3364
3364
|
}
|
|
3365
|
+
getRelatedModelConstructor() {
|
|
3366
|
+
return this.related;
|
|
3367
|
+
}
|
|
3365
3368
|
createRelationTableLoader() {
|
|
3366
3369
|
return new RelationTableLoader(this.getRelationAdapter());
|
|
3367
3370
|
}
|
|
3371
|
+
getCreationAttributes() {
|
|
3372
|
+
return {};
|
|
3373
|
+
}
|
|
3374
|
+
mergeCreationAttributes(attributes = {}) {
|
|
3375
|
+
return {
|
|
3376
|
+
...attributes,
|
|
3377
|
+
...this.getCreationAttributes()
|
|
3378
|
+
};
|
|
3379
|
+
}
|
|
3380
|
+
applyCreationAttributesToModel(model) {
|
|
3381
|
+
const attributes = this.getCreationAttributes();
|
|
3382
|
+
const fillable = model;
|
|
3383
|
+
if (Object.keys(attributes).length === 0) return model;
|
|
3384
|
+
if (typeof fillable.fill === "function") {
|
|
3385
|
+
fillable.fill(attributes);
|
|
3386
|
+
return model;
|
|
3387
|
+
}
|
|
3388
|
+
if (typeof fillable.setAttribute === "function") Object.entries(attributes).forEach(([key, value]) => {
|
|
3389
|
+
fillable.setAttribute?.(key, value);
|
|
3390
|
+
});
|
|
3391
|
+
return model;
|
|
3392
|
+
}
|
|
3368
3393
|
/**
|
|
3369
3394
|
* Apply a constraint to the relationship query.
|
|
3370
3395
|
*
|
|
@@ -3560,6 +3585,30 @@ var Relation = class {
|
|
|
3560
3585
|
return results;
|
|
3561
3586
|
}
|
|
3562
3587
|
/**
|
|
3588
|
+
* Execute the relationship query and return the first related model or throw an error if not found.
|
|
3589
|
+
*
|
|
3590
|
+
* @returns
|
|
3591
|
+
*/
|
|
3592
|
+
async firstOrFail() {
|
|
3593
|
+
return (await this.getQuery()).firstOrFail();
|
|
3594
|
+
}
|
|
3595
|
+
/**
|
|
3596
|
+
* Execute the relationship query and return the first related model or the result of
|
|
3597
|
+
* the callback if not found.
|
|
3598
|
+
*
|
|
3599
|
+
* @param callback
|
|
3600
|
+
* @returns
|
|
3601
|
+
*/
|
|
3602
|
+
async firstOr(callback) {
|
|
3603
|
+
const result = await this.first();
|
|
3604
|
+
if (result) return result;
|
|
3605
|
+
return callback();
|
|
3606
|
+
}
|
|
3607
|
+
async firstWhere(key, operatorOrValue, maybeValue) {
|
|
3608
|
+
const query = await this.getQuery();
|
|
3609
|
+
return maybeValue === void 0 ? query.firstWhere(key, operatorOrValue) : query.firstWhere(key, operatorOrValue, maybeValue);
|
|
3610
|
+
}
|
|
3611
|
+
/**
|
|
3563
3612
|
* Count records that match the relationship query.
|
|
3564
3613
|
*
|
|
3565
3614
|
* @returns
|
|
@@ -3583,6 +3632,207 @@ var Relation = class {
|
|
|
3583
3632
|
async doesntExist() {
|
|
3584
3633
|
return !await this.exists();
|
|
3585
3634
|
}
|
|
3635
|
+
/**
|
|
3636
|
+
* Create a new instance of the related model with the given attributes and
|
|
3637
|
+
* relationship creation attributes applied, but do not save it.
|
|
3638
|
+
*
|
|
3639
|
+
* @param attributes
|
|
3640
|
+
* @returns
|
|
3641
|
+
*/
|
|
3642
|
+
make(attributes = {}) {
|
|
3643
|
+
return this.getRelatedModelConstructor().hydrate(this.mergeCreationAttributes(attributes));
|
|
3644
|
+
}
|
|
3645
|
+
/**
|
|
3646
|
+
* Create new instances of the related model with the given attributes and relationship
|
|
3647
|
+
* creation attributes applied, but do not save them.
|
|
3648
|
+
*
|
|
3649
|
+
* @param attributes
|
|
3650
|
+
* @returns
|
|
3651
|
+
*/
|
|
3652
|
+
makeMany(attributes = []) {
|
|
3653
|
+
return attributes.map((item) => this.make(item));
|
|
3654
|
+
}
|
|
3655
|
+
/**
|
|
3656
|
+
* Create a new instance of the related model with the given attributes and relationship
|
|
3657
|
+
* creation attributes applied, and save it to the database.
|
|
3658
|
+
*
|
|
3659
|
+
* @param attributes
|
|
3660
|
+
* @returns
|
|
3661
|
+
*/
|
|
3662
|
+
async create(attributes = {}) {
|
|
3663
|
+
return await this.getRelatedModelConstructor().query().create(this.mergeCreationAttributes(attributes));
|
|
3664
|
+
}
|
|
3665
|
+
/**
|
|
3666
|
+
* Create new instances of the related model with the given attributes and relationship
|
|
3667
|
+
* creation attributes applied, and save them to the database.
|
|
3668
|
+
*
|
|
3669
|
+
* @param values
|
|
3670
|
+
* @returns
|
|
3671
|
+
*/
|
|
3672
|
+
async createMany(values = []) {
|
|
3673
|
+
if (values.length === 0) return [];
|
|
3674
|
+
return await Promise.all(values.map(async (value) => await this.create(value)));
|
|
3675
|
+
}
|
|
3676
|
+
/**
|
|
3677
|
+
* Save the given model instance by applying relationship creation attributes and calling save() on it.
|
|
3678
|
+
*
|
|
3679
|
+
* @param model
|
|
3680
|
+
* @returns
|
|
3681
|
+
*/
|
|
3682
|
+
async save(model) {
|
|
3683
|
+
const saveable = this.applyCreationAttributesToModel(model);
|
|
3684
|
+
if (typeof saveable.save !== "function") throw new UnsupportedAdapterFeatureException("Related model does not support save().", { operation: "relation.save" });
|
|
3685
|
+
try {
|
|
3686
|
+
return await saveable.save();
|
|
3687
|
+
} catch (error) {
|
|
3688
|
+
if (!this.shouldCreateAfterSaveMiss(error)) throw error;
|
|
3689
|
+
const attributes = typeof saveable.getRawAttributes === "function" ? saveable.getRawAttributes() : {};
|
|
3690
|
+
return await this.create(attributes);
|
|
3691
|
+
}
|
|
3692
|
+
}
|
|
3693
|
+
/**
|
|
3694
|
+
* Save the given model instance by applying relationship creation attributes and
|
|
3695
|
+
* calling saveQuietly() on it if supported, otherwise falling back to save().
|
|
3696
|
+
*
|
|
3697
|
+
* @param model
|
|
3698
|
+
* @returns
|
|
3699
|
+
*/
|
|
3700
|
+
async saveQuietly(model) {
|
|
3701
|
+
const saveable = this.applyCreationAttributesToModel(model);
|
|
3702
|
+
if (typeof saveable.saveQuietly === "function") try {
|
|
3703
|
+
return await saveable.saveQuietly();
|
|
3704
|
+
} catch (error) {
|
|
3705
|
+
if (!this.shouldCreateAfterSaveMiss(error)) throw error;
|
|
3706
|
+
const attributes = typeof saveable.getRawAttributes === "function" ? saveable.getRawAttributes() : {};
|
|
3707
|
+
return await this.create(attributes);
|
|
3708
|
+
}
|
|
3709
|
+
if (typeof saveable.save === "function") return await saveable.save();
|
|
3710
|
+
throw new UnsupportedAdapterFeatureException("Related model does not support saveQuietly().", { operation: "relation.saveQuietly" });
|
|
3711
|
+
}
|
|
3712
|
+
shouldCreateAfterSaveMiss(error) {
|
|
3713
|
+
return error instanceof Error && (error.name === "ModelNotFoundException" || error.message.includes("Record not found"));
|
|
3714
|
+
}
|
|
3715
|
+
/**
|
|
3716
|
+
* Create new instances of the related model with the given attributes and
|
|
3717
|
+
* relationship * creation attributes applied, and save them to the database.
|
|
3718
|
+
*
|
|
3719
|
+
* @param models
|
|
3720
|
+
* @returns
|
|
3721
|
+
*/
|
|
3722
|
+
async saveMany(models = []) {
|
|
3723
|
+
return await Promise.all(models.map(async (model) => await this.save(model)));
|
|
3724
|
+
}
|
|
3725
|
+
/**
|
|
3726
|
+
* Create new instances of the related model with the given attributes and relationship
|
|
3727
|
+
* creation attributes applied, and save them to the database.
|
|
3728
|
+
*
|
|
3729
|
+
* @param models
|
|
3730
|
+
* @returns
|
|
3731
|
+
*/
|
|
3732
|
+
async saveManyQuietly(models = []) {
|
|
3733
|
+
return await Promise.all(models.map(async (model) => await this.saveQuietly(model)));
|
|
3734
|
+
}
|
|
3735
|
+
async find(value, key) {
|
|
3736
|
+
return (await this.getQuery()).find(value, key);
|
|
3737
|
+
}
|
|
3738
|
+
async findMany(values, key) {
|
|
3739
|
+
const related = this.getRelatedModelConstructor();
|
|
3740
|
+
const resolvedKey = key ?? related.getPrimaryKey();
|
|
3741
|
+
return (await this.getQuery()).where({ [resolvedKey]: { in: values } }).get();
|
|
3742
|
+
}
|
|
3743
|
+
async findOr(value, keyOrCallback, maybeCallback) {
|
|
3744
|
+
const query = await this.getQuery();
|
|
3745
|
+
return typeof keyOrCallback === "function" ? query.findOr(value, keyOrCallback) : query.findOr(value, keyOrCallback, maybeCallback);
|
|
3746
|
+
}
|
|
3747
|
+
async findOrFail(value, key) {
|
|
3748
|
+
const found = await this.find(value, key);
|
|
3749
|
+
if (found) return found;
|
|
3750
|
+
return (await this.getQuery()).where({ [key ?? this.getRelatedModelConstructor().getPrimaryKey()]: value }).firstOrFail();
|
|
3751
|
+
}
|
|
3752
|
+
/**
|
|
3753
|
+
* Find the first related model by a specific key and value, or create a new instance if not found.
|
|
3754
|
+
*
|
|
3755
|
+
* @param attributes
|
|
3756
|
+
* @param values
|
|
3757
|
+
* @returns
|
|
3758
|
+
*/
|
|
3759
|
+
async firstOrNew(attributes, values = {}) {
|
|
3760
|
+
const found = await (await this.getQuery()).clone().where(attributes).first();
|
|
3761
|
+
if (found) return found;
|
|
3762
|
+
return this.make({
|
|
3763
|
+
...attributes,
|
|
3764
|
+
...values
|
|
3765
|
+
});
|
|
3766
|
+
}
|
|
3767
|
+
/**
|
|
3768
|
+
* Find the first related model by a specific key and value, or create and save a new instance
|
|
3769
|
+
* if not found.
|
|
3770
|
+
*
|
|
3771
|
+
* @param attributes
|
|
3772
|
+
* @param values
|
|
3773
|
+
* @returns
|
|
3774
|
+
*/
|
|
3775
|
+
async firstOrCreate(attributes, values = {}) {
|
|
3776
|
+
const found = await (await this.getQuery()).clone().where(attributes).first();
|
|
3777
|
+
if (found) return found;
|
|
3778
|
+
return await this.create({
|
|
3779
|
+
...attributes,
|
|
3780
|
+
...values
|
|
3781
|
+
});
|
|
3782
|
+
}
|
|
3783
|
+
/**
|
|
3784
|
+
* Find the first related model by a specific key and value, update the first matching record with
|
|
3785
|
+
* the given values, or create and save a new instance if no matching record is found.
|
|
3786
|
+
*
|
|
3787
|
+
* @param attributes
|
|
3788
|
+
* @param values
|
|
3789
|
+
* @returns
|
|
3790
|
+
*/
|
|
3791
|
+
async updateOrCreate(attributes, values = {}) {
|
|
3792
|
+
const query = await this.getQuery();
|
|
3793
|
+
const found = await query.clone().where(attributes).first();
|
|
3794
|
+
if (!found) return await this.create({
|
|
3795
|
+
...attributes,
|
|
3796
|
+
...values
|
|
3797
|
+
});
|
|
3798
|
+
const updatable = found;
|
|
3799
|
+
if (typeof updatable.fill === "function" && typeof updatable.save === "function") return await updatable.fill(values).save();
|
|
3800
|
+
return await query.clone().where(attributes).update(values);
|
|
3801
|
+
}
|
|
3802
|
+
/**
|
|
3803
|
+
* Find related models by specific attributes, update matching records with the given values, or
|
|
3804
|
+
* create and save new instances if no matching records are found.
|
|
3805
|
+
*
|
|
3806
|
+
* @param values
|
|
3807
|
+
* @param uniqueBy
|
|
3808
|
+
* @param update
|
|
3809
|
+
* @returns
|
|
3810
|
+
*/
|
|
3811
|
+
async upsert(values, uniqueBy, update = null) {
|
|
3812
|
+
return await (await this.getQuery()).upsert(values.map((value) => this.mergeCreationAttributes(value)), uniqueBy, update);
|
|
3813
|
+
}
|
|
3814
|
+
/**
|
|
3815
|
+
* Paginate the relationship query results.
|
|
3816
|
+
*
|
|
3817
|
+
* @param perPage
|
|
3818
|
+
* @param page
|
|
3819
|
+
* @param options
|
|
3820
|
+
* @returns
|
|
3821
|
+
*/
|
|
3822
|
+
async paginate(perPage = 15, page, options = {}) {
|
|
3823
|
+
return (await this.getQuery()).paginate(perPage, page, options);
|
|
3824
|
+
}
|
|
3825
|
+
/**
|
|
3826
|
+
* Paginate the relationship query results without total count optimization.
|
|
3827
|
+
*
|
|
3828
|
+
* @param perPage
|
|
3829
|
+
* @param page
|
|
3830
|
+
* @param options
|
|
3831
|
+
* @returns
|
|
3832
|
+
*/
|
|
3833
|
+
async simplePaginate(perPage = 15, page, options = {}) {
|
|
3834
|
+
return (await this.getQuery()).simplePaginate(perPage, page, options);
|
|
3835
|
+
}
|
|
3586
3836
|
};
|
|
3587
3837
|
|
|
3588
3838
|
//#endregion
|
|
@@ -4269,6 +4519,9 @@ var HasManyRelation = class extends Relation {
|
|
|
4269
4519
|
const localValue = this.parent.getAttribute(this.localKey);
|
|
4270
4520
|
return this.applyConstraint(this.related.query().where({ [this.foreignKey]: localValue }));
|
|
4271
4521
|
}
|
|
4522
|
+
getCreationAttributes() {
|
|
4523
|
+
return { [this.foreignKey]: this.parent.getAttribute(this.localKey) };
|
|
4524
|
+
}
|
|
4272
4525
|
getMetadata() {
|
|
4273
4526
|
return {
|
|
4274
4527
|
type: "hasMany",
|
|
@@ -4372,6 +4625,9 @@ var HasOneRelation = class extends SingleResultRelation {
|
|
|
4372
4625
|
const localValue = this.parent.getAttribute(this.localKey);
|
|
4373
4626
|
return this.applyConstraint(this.related.query().where({ [this.foreignKey]: localValue }));
|
|
4374
4627
|
}
|
|
4628
|
+
getCreationAttributes() {
|
|
4629
|
+
return { [this.foreignKey]: this.parent.getAttribute(this.localKey) };
|
|
4630
|
+
}
|
|
4375
4631
|
getMetadata() {
|
|
4376
4632
|
return {
|
|
4377
4633
|
type: "hasOne",
|
|
@@ -4480,6 +4736,12 @@ var MorphManyRelation = class extends Relation {
|
|
|
4480
4736
|
[`${this.morphName}Type`]: type
|
|
4481
4737
|
}));
|
|
4482
4738
|
}
|
|
4739
|
+
getCreationAttributes() {
|
|
4740
|
+
return {
|
|
4741
|
+
[`${this.morphName}Id`]: this.parent.getAttribute(this.localKey),
|
|
4742
|
+
[`${this.morphName}Type`]: this.parent.constructor.name
|
|
4743
|
+
};
|
|
4744
|
+
}
|
|
4483
4745
|
getMetadata() {
|
|
4484
4746
|
return {
|
|
4485
4747
|
type: "morphMany",
|
|
@@ -4527,6 +4789,12 @@ var MorphOneRelation = class extends SingleResultRelation {
|
|
|
4527
4789
|
[`${this.morphName}Type`]: type
|
|
4528
4790
|
}));
|
|
4529
4791
|
}
|
|
4792
|
+
getCreationAttributes() {
|
|
4793
|
+
return {
|
|
4794
|
+
[`${this.morphName}Id`]: this.parent.getAttribute(this.localKey),
|
|
4795
|
+
[`${this.morphName}Type`]: this.parent.constructor.name
|
|
4796
|
+
};
|
|
4797
|
+
}
|
|
4530
4798
|
getMetadata() {
|
|
4531
4799
|
return {
|
|
4532
4800
|
type: "morphOne",
|
|
@@ -3334,9 +3334,34 @@ var Relation = class {
|
|
|
3334
3334
|
getRelatedModel() {
|
|
3335
3335
|
return this.related;
|
|
3336
3336
|
}
|
|
3337
|
+
getRelatedModelConstructor() {
|
|
3338
|
+
return this.related;
|
|
3339
|
+
}
|
|
3337
3340
|
createRelationTableLoader() {
|
|
3338
3341
|
return new RelationTableLoader(this.getRelationAdapter());
|
|
3339
3342
|
}
|
|
3343
|
+
getCreationAttributes() {
|
|
3344
|
+
return {};
|
|
3345
|
+
}
|
|
3346
|
+
mergeCreationAttributes(attributes = {}) {
|
|
3347
|
+
return {
|
|
3348
|
+
...attributes,
|
|
3349
|
+
...this.getCreationAttributes()
|
|
3350
|
+
};
|
|
3351
|
+
}
|
|
3352
|
+
applyCreationAttributesToModel(model) {
|
|
3353
|
+
const attributes = this.getCreationAttributes();
|
|
3354
|
+
const fillable = model;
|
|
3355
|
+
if (Object.keys(attributes).length === 0) return model;
|
|
3356
|
+
if (typeof fillable.fill === "function") {
|
|
3357
|
+
fillable.fill(attributes);
|
|
3358
|
+
return model;
|
|
3359
|
+
}
|
|
3360
|
+
if (typeof fillable.setAttribute === "function") Object.entries(attributes).forEach(([key, value]) => {
|
|
3361
|
+
fillable.setAttribute?.(key, value);
|
|
3362
|
+
});
|
|
3363
|
+
return model;
|
|
3364
|
+
}
|
|
3340
3365
|
/**
|
|
3341
3366
|
* Apply a constraint to the relationship query.
|
|
3342
3367
|
*
|
|
@@ -3532,6 +3557,30 @@ var Relation = class {
|
|
|
3532
3557
|
return results;
|
|
3533
3558
|
}
|
|
3534
3559
|
/**
|
|
3560
|
+
* Execute the relationship query and return the first related model or throw an error if not found.
|
|
3561
|
+
*
|
|
3562
|
+
* @returns
|
|
3563
|
+
*/
|
|
3564
|
+
async firstOrFail() {
|
|
3565
|
+
return (await this.getQuery()).firstOrFail();
|
|
3566
|
+
}
|
|
3567
|
+
/**
|
|
3568
|
+
* Execute the relationship query and return the first related model or the result of
|
|
3569
|
+
* the callback if not found.
|
|
3570
|
+
*
|
|
3571
|
+
* @param callback
|
|
3572
|
+
* @returns
|
|
3573
|
+
*/
|
|
3574
|
+
async firstOr(callback) {
|
|
3575
|
+
const result = await this.first();
|
|
3576
|
+
if (result) return result;
|
|
3577
|
+
return callback();
|
|
3578
|
+
}
|
|
3579
|
+
async firstWhere(key, operatorOrValue, maybeValue) {
|
|
3580
|
+
const query = await this.getQuery();
|
|
3581
|
+
return maybeValue === void 0 ? query.firstWhere(key, operatorOrValue) : query.firstWhere(key, operatorOrValue, maybeValue);
|
|
3582
|
+
}
|
|
3583
|
+
/**
|
|
3535
3584
|
* Count records that match the relationship query.
|
|
3536
3585
|
*
|
|
3537
3586
|
* @returns
|
|
@@ -3555,6 +3604,207 @@ var Relation = class {
|
|
|
3555
3604
|
async doesntExist() {
|
|
3556
3605
|
return !await this.exists();
|
|
3557
3606
|
}
|
|
3607
|
+
/**
|
|
3608
|
+
* Create a new instance of the related model with the given attributes and
|
|
3609
|
+
* relationship creation attributes applied, but do not save it.
|
|
3610
|
+
*
|
|
3611
|
+
* @param attributes
|
|
3612
|
+
* @returns
|
|
3613
|
+
*/
|
|
3614
|
+
make(attributes = {}) {
|
|
3615
|
+
return this.getRelatedModelConstructor().hydrate(this.mergeCreationAttributes(attributes));
|
|
3616
|
+
}
|
|
3617
|
+
/**
|
|
3618
|
+
* Create new instances of the related model with the given attributes and relationship
|
|
3619
|
+
* creation attributes applied, but do not save them.
|
|
3620
|
+
*
|
|
3621
|
+
* @param attributes
|
|
3622
|
+
* @returns
|
|
3623
|
+
*/
|
|
3624
|
+
makeMany(attributes = []) {
|
|
3625
|
+
return attributes.map((item) => this.make(item));
|
|
3626
|
+
}
|
|
3627
|
+
/**
|
|
3628
|
+
* Create a new instance of the related model with the given attributes and relationship
|
|
3629
|
+
* creation attributes applied, and save it to the database.
|
|
3630
|
+
*
|
|
3631
|
+
* @param attributes
|
|
3632
|
+
* @returns
|
|
3633
|
+
*/
|
|
3634
|
+
async create(attributes = {}) {
|
|
3635
|
+
return await this.getRelatedModelConstructor().query().create(this.mergeCreationAttributes(attributes));
|
|
3636
|
+
}
|
|
3637
|
+
/**
|
|
3638
|
+
* Create new instances of the related model with the given attributes and relationship
|
|
3639
|
+
* creation attributes applied, and save them to the database.
|
|
3640
|
+
*
|
|
3641
|
+
* @param values
|
|
3642
|
+
* @returns
|
|
3643
|
+
*/
|
|
3644
|
+
async createMany(values = []) {
|
|
3645
|
+
if (values.length === 0) return [];
|
|
3646
|
+
return await Promise.all(values.map(async (value) => await this.create(value)));
|
|
3647
|
+
}
|
|
3648
|
+
/**
|
|
3649
|
+
* Save the given model instance by applying relationship creation attributes and calling save() on it.
|
|
3650
|
+
*
|
|
3651
|
+
* @param model
|
|
3652
|
+
* @returns
|
|
3653
|
+
*/
|
|
3654
|
+
async save(model) {
|
|
3655
|
+
const saveable = this.applyCreationAttributesToModel(model);
|
|
3656
|
+
if (typeof saveable.save !== "function") throw new UnsupportedAdapterFeatureException("Related model does not support save().", { operation: "relation.save" });
|
|
3657
|
+
try {
|
|
3658
|
+
return await saveable.save();
|
|
3659
|
+
} catch (error) {
|
|
3660
|
+
if (!this.shouldCreateAfterSaveMiss(error)) throw error;
|
|
3661
|
+
const attributes = typeof saveable.getRawAttributes === "function" ? saveable.getRawAttributes() : {};
|
|
3662
|
+
return await this.create(attributes);
|
|
3663
|
+
}
|
|
3664
|
+
}
|
|
3665
|
+
/**
|
|
3666
|
+
* Save the given model instance by applying relationship creation attributes and
|
|
3667
|
+
* calling saveQuietly() on it if supported, otherwise falling back to save().
|
|
3668
|
+
*
|
|
3669
|
+
* @param model
|
|
3670
|
+
* @returns
|
|
3671
|
+
*/
|
|
3672
|
+
async saveQuietly(model) {
|
|
3673
|
+
const saveable = this.applyCreationAttributesToModel(model);
|
|
3674
|
+
if (typeof saveable.saveQuietly === "function") try {
|
|
3675
|
+
return await saveable.saveQuietly();
|
|
3676
|
+
} catch (error) {
|
|
3677
|
+
if (!this.shouldCreateAfterSaveMiss(error)) throw error;
|
|
3678
|
+
const attributes = typeof saveable.getRawAttributes === "function" ? saveable.getRawAttributes() : {};
|
|
3679
|
+
return await this.create(attributes);
|
|
3680
|
+
}
|
|
3681
|
+
if (typeof saveable.save === "function") return await saveable.save();
|
|
3682
|
+
throw new UnsupportedAdapterFeatureException("Related model does not support saveQuietly().", { operation: "relation.saveQuietly" });
|
|
3683
|
+
}
|
|
3684
|
+
shouldCreateAfterSaveMiss(error) {
|
|
3685
|
+
return error instanceof Error && (error.name === "ModelNotFoundException" || error.message.includes("Record not found"));
|
|
3686
|
+
}
|
|
3687
|
+
/**
|
|
3688
|
+
* Create new instances of the related model with the given attributes and
|
|
3689
|
+
* relationship * creation attributes applied, and save them to the database.
|
|
3690
|
+
*
|
|
3691
|
+
* @param models
|
|
3692
|
+
* @returns
|
|
3693
|
+
*/
|
|
3694
|
+
async saveMany(models = []) {
|
|
3695
|
+
return await Promise.all(models.map(async (model) => await this.save(model)));
|
|
3696
|
+
}
|
|
3697
|
+
/**
|
|
3698
|
+
* Create new instances of the related model with the given attributes and relationship
|
|
3699
|
+
* creation attributes applied, and save them to the database.
|
|
3700
|
+
*
|
|
3701
|
+
* @param models
|
|
3702
|
+
* @returns
|
|
3703
|
+
*/
|
|
3704
|
+
async saveManyQuietly(models = []) {
|
|
3705
|
+
return await Promise.all(models.map(async (model) => await this.saveQuietly(model)));
|
|
3706
|
+
}
|
|
3707
|
+
async find(value, key) {
|
|
3708
|
+
return (await this.getQuery()).find(value, key);
|
|
3709
|
+
}
|
|
3710
|
+
async findMany(values, key) {
|
|
3711
|
+
const related = this.getRelatedModelConstructor();
|
|
3712
|
+
const resolvedKey = key ?? related.getPrimaryKey();
|
|
3713
|
+
return (await this.getQuery()).where({ [resolvedKey]: { in: values } }).get();
|
|
3714
|
+
}
|
|
3715
|
+
async findOr(value, keyOrCallback, maybeCallback) {
|
|
3716
|
+
const query = await this.getQuery();
|
|
3717
|
+
return typeof keyOrCallback === "function" ? query.findOr(value, keyOrCallback) : query.findOr(value, keyOrCallback, maybeCallback);
|
|
3718
|
+
}
|
|
3719
|
+
async findOrFail(value, key) {
|
|
3720
|
+
const found = await this.find(value, key);
|
|
3721
|
+
if (found) return found;
|
|
3722
|
+
return (await this.getQuery()).where({ [key ?? this.getRelatedModelConstructor().getPrimaryKey()]: value }).firstOrFail();
|
|
3723
|
+
}
|
|
3724
|
+
/**
|
|
3725
|
+
* Find the first related model by a specific key and value, or create a new instance if not found.
|
|
3726
|
+
*
|
|
3727
|
+
* @param attributes
|
|
3728
|
+
* @param values
|
|
3729
|
+
* @returns
|
|
3730
|
+
*/
|
|
3731
|
+
async firstOrNew(attributes, values = {}) {
|
|
3732
|
+
const found = await (await this.getQuery()).clone().where(attributes).first();
|
|
3733
|
+
if (found) return found;
|
|
3734
|
+
return this.make({
|
|
3735
|
+
...attributes,
|
|
3736
|
+
...values
|
|
3737
|
+
});
|
|
3738
|
+
}
|
|
3739
|
+
/**
|
|
3740
|
+
* Find the first related model by a specific key and value, or create and save a new instance
|
|
3741
|
+
* if not found.
|
|
3742
|
+
*
|
|
3743
|
+
* @param attributes
|
|
3744
|
+
* @param values
|
|
3745
|
+
* @returns
|
|
3746
|
+
*/
|
|
3747
|
+
async firstOrCreate(attributes, values = {}) {
|
|
3748
|
+
const found = await (await this.getQuery()).clone().where(attributes).first();
|
|
3749
|
+
if (found) return found;
|
|
3750
|
+
return await this.create({
|
|
3751
|
+
...attributes,
|
|
3752
|
+
...values
|
|
3753
|
+
});
|
|
3754
|
+
}
|
|
3755
|
+
/**
|
|
3756
|
+
* Find the first related model by a specific key and value, update the first matching record with
|
|
3757
|
+
* the given values, or create and save a new instance if no matching record is found.
|
|
3758
|
+
*
|
|
3759
|
+
* @param attributes
|
|
3760
|
+
* @param values
|
|
3761
|
+
* @returns
|
|
3762
|
+
*/
|
|
3763
|
+
async updateOrCreate(attributes, values = {}) {
|
|
3764
|
+
const query = await this.getQuery();
|
|
3765
|
+
const found = await query.clone().where(attributes).first();
|
|
3766
|
+
if (!found) return await this.create({
|
|
3767
|
+
...attributes,
|
|
3768
|
+
...values
|
|
3769
|
+
});
|
|
3770
|
+
const updatable = found;
|
|
3771
|
+
if (typeof updatable.fill === "function" && typeof updatable.save === "function") return await updatable.fill(values).save();
|
|
3772
|
+
return await query.clone().where(attributes).update(values);
|
|
3773
|
+
}
|
|
3774
|
+
/**
|
|
3775
|
+
* Find related models by specific attributes, update matching records with the given values, or
|
|
3776
|
+
* create and save new instances if no matching records are found.
|
|
3777
|
+
*
|
|
3778
|
+
* @param values
|
|
3779
|
+
* @param uniqueBy
|
|
3780
|
+
* @param update
|
|
3781
|
+
* @returns
|
|
3782
|
+
*/
|
|
3783
|
+
async upsert(values, uniqueBy, update = null) {
|
|
3784
|
+
return await (await this.getQuery()).upsert(values.map((value) => this.mergeCreationAttributes(value)), uniqueBy, update);
|
|
3785
|
+
}
|
|
3786
|
+
/**
|
|
3787
|
+
* Paginate the relationship query results.
|
|
3788
|
+
*
|
|
3789
|
+
* @param perPage
|
|
3790
|
+
* @param page
|
|
3791
|
+
* @param options
|
|
3792
|
+
* @returns
|
|
3793
|
+
*/
|
|
3794
|
+
async paginate(perPage = 15, page, options = {}) {
|
|
3795
|
+
return (await this.getQuery()).paginate(perPage, page, options);
|
|
3796
|
+
}
|
|
3797
|
+
/**
|
|
3798
|
+
* Paginate the relationship query results without total count optimization.
|
|
3799
|
+
*
|
|
3800
|
+
* @param perPage
|
|
3801
|
+
* @param page
|
|
3802
|
+
* @param options
|
|
3803
|
+
* @returns
|
|
3804
|
+
*/
|
|
3805
|
+
async simplePaginate(perPage = 15, page, options = {}) {
|
|
3806
|
+
return (await this.getQuery()).simplePaginate(perPage, page, options);
|
|
3807
|
+
}
|
|
3558
3808
|
};
|
|
3559
3809
|
|
|
3560
3810
|
//#endregion
|
|
@@ -4241,6 +4491,9 @@ var HasManyRelation = class extends Relation {
|
|
|
4241
4491
|
const localValue = this.parent.getAttribute(this.localKey);
|
|
4242
4492
|
return this.applyConstraint(this.related.query().where({ [this.foreignKey]: localValue }));
|
|
4243
4493
|
}
|
|
4494
|
+
getCreationAttributes() {
|
|
4495
|
+
return { [this.foreignKey]: this.parent.getAttribute(this.localKey) };
|
|
4496
|
+
}
|
|
4244
4497
|
getMetadata() {
|
|
4245
4498
|
return {
|
|
4246
4499
|
type: "hasMany",
|
|
@@ -4344,6 +4597,9 @@ var HasOneRelation = class extends SingleResultRelation {
|
|
|
4344
4597
|
const localValue = this.parent.getAttribute(this.localKey);
|
|
4345
4598
|
return this.applyConstraint(this.related.query().where({ [this.foreignKey]: localValue }));
|
|
4346
4599
|
}
|
|
4600
|
+
getCreationAttributes() {
|
|
4601
|
+
return { [this.foreignKey]: this.parent.getAttribute(this.localKey) };
|
|
4602
|
+
}
|
|
4347
4603
|
getMetadata() {
|
|
4348
4604
|
return {
|
|
4349
4605
|
type: "hasOne",
|
|
@@ -4452,6 +4708,12 @@ var MorphManyRelation = class extends Relation {
|
|
|
4452
4708
|
[`${this.morphName}Type`]: type
|
|
4453
4709
|
}));
|
|
4454
4710
|
}
|
|
4711
|
+
getCreationAttributes() {
|
|
4712
|
+
return {
|
|
4713
|
+
[`${this.morphName}Id`]: this.parent.getAttribute(this.localKey),
|
|
4714
|
+
[`${this.morphName}Type`]: this.parent.constructor.name
|
|
4715
|
+
};
|
|
4716
|
+
}
|
|
4455
4717
|
getMetadata() {
|
|
4456
4718
|
return {
|
|
4457
4719
|
type: "morphMany",
|
|
@@ -4499,6 +4761,12 @@ var MorphOneRelation = class extends SingleResultRelation {
|
|
|
4499
4761
|
[`${this.morphName}Type`]: type
|
|
4500
4762
|
}));
|
|
4501
4763
|
}
|
|
4764
|
+
getCreationAttributes() {
|
|
4765
|
+
return {
|
|
4766
|
+
[`${this.morphName}Id`]: this.parent.getAttribute(this.localKey),
|
|
4767
|
+
[`${this.morphName}Type`]: this.parent.constructor.name
|
|
4768
|
+
};
|
|
4769
|
+
}
|
|
4502
4770
|
getMetadata() {
|
|
4503
4771
|
return {
|
|
4504
4772
|
type: "morphOne",
|