arkormx 2.12.11 → 2.12.13

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.mjs CHANGED
@@ -986,6 +986,7 @@ var KyselyDatabaseAdapter = class KyselyDatabaseAdapter {
986
986
  if (condition.type === "time") return this.buildTimeCondition(target, condition);
987
987
  if (condition.type === "day") return this.buildDayCondition(target, condition);
988
988
  if (condition.type === "exists") return this.buildExistsCondition(condition);
989
+ if (condition.type === "relation") return this.buildRelationFilterExpression(target, condition.filter);
989
990
  if (condition.type === "full-text") return this.buildFullTextCondition(target, condition);
990
991
  if (condition.type === "json") return this.buildJsonCondition(target, condition);
991
992
  if (condition.type === "expression") return sql`${this.buildExpression(target, condition.expression)}`;
@@ -5184,15 +5185,13 @@ var QueryBuilder = class QueryBuilder {
5184
5185
  return operator;
5185
5186
  }
5186
5187
  /**
5187
- * Appends a pre-built structured condition, first flushing any Prisma-like
5188
- * legacy where into the structured representation so the two combine correctly.
5188
+ * Appends a pre-built structured condition.
5189
+ *
5190
+ * @param boolean
5191
+ * @param condition
5192
+ * @returns
5189
5193
  */
5190
5194
  appendStructuredWhere(boolean, condition) {
5191
- if (this.legacyWhere) {
5192
- const existing = this.tryBuildQueryCondition(this.legacyWhere);
5193
- this.legacyWhere = void 0;
5194
- if (existing) this.queryWhere = existing;
5195
- }
5196
5195
  this.appendQueryCondition(boolean, condition);
5197
5196
  return this;
5198
5197
  }
@@ -5200,6 +5199,10 @@ var QueryBuilder = class QueryBuilder {
5200
5199
  * Appends a boolean {@link Expression} as a where predicate. When the query is
5201
5200
  * using the Prisma-like legacy where representation, the expression is merged in
5202
5201
  * as a structured condition alongside it.
5202
+ *
5203
+ * @param boolean
5204
+ * @param expression
5205
+ * @returns
5203
5206
  */
5204
5207
  appendExpressionCondition(boolean, expression) {
5205
5208
  const condition = {
@@ -5210,11 +5213,15 @@ var QueryBuilder = class QueryBuilder {
5210
5213
  }
5211
5214
  /**
5212
5215
  * Resolve a callback into a parenthesized group condition and append it.
5216
+ *
5217
+ * @param boolean
5218
+ * @param callback
5219
+ * @returns
5213
5220
  */
5214
5221
  appendNestedWhere(boolean, callback) {
5215
5222
  const nested = new QueryBuilder(this.model, this.adapter);
5216
5223
  callback(nested);
5217
- const condition = nested.getNestedWhereCondition();
5224
+ const condition = nested.withRelationConditions(nested.getNestedWhereCondition(), "query.where");
5218
5225
  if (!condition) return this;
5219
5226
  const grouped = condition.type === "group" ? condition : {
5220
5227
  type: "group",
@@ -5225,6 +5232,40 @@ var QueryBuilder = class QueryBuilder {
5225
5232
  return this;
5226
5233
  }
5227
5234
  /**
5235
+ * Fold this builder's relation filters into `condition`, in the order and
5236
+ * with the and/or they were added with, so they can be carried inside a
5237
+ * where group or another relation's constraint.
5238
+ *
5239
+ * @param condition
5240
+ * @param operation Names the caller in the exception raised when a filter
5241
+ * cannot be expressed as a condition.
5242
+ * @returns
5243
+ */
5244
+ withRelationConditions(condition, operation) {
5245
+ if (!this.hasRelationFilters()) return condition;
5246
+ const specs = this.tryBuildRelationFilterSpecs();
5247
+ if (!specs) throw new UnsupportedAdapterFeatureException("Relation filters inside a where group or relation constraint could not be compiled into conditions.", {
5248
+ operation,
5249
+ model: this.model.name,
5250
+ meta: { feature: "relationFilters" }
5251
+ });
5252
+ return specs.reduce((combined, spec) => {
5253
+ const relation = {
5254
+ type: "relation",
5255
+ filter: {
5256
+ ...spec,
5257
+ boolean: void 0
5258
+ }
5259
+ };
5260
+ if (!combined) return relation;
5261
+ return {
5262
+ type: "group",
5263
+ operator: spec.boolean === "OR" ? "or" : "and",
5264
+ conditions: [combined, relation]
5265
+ };
5266
+ }, condition);
5267
+ }
5268
+ /**
5228
5269
  * Returns the user-authored where condition for nesting, excluding any
5229
5270
  * soft-delete predicate (the parent query owns that).
5230
5271
  */
@@ -5818,6 +5859,10 @@ var QueryBuilder = class QueryBuilder {
5818
5859
  };
5819
5860
  return this;
5820
5861
  }
5862
+ if (!this.legacyWhere && this.queryWhere && !this.isSchemaWhereRepresentable(this.queryWhere)) throw new UnsupportedAdapterFeatureException("This where clause cannot be combined with the raw, JSON, relation, or expression constraints already on the query.", {
5863
+ operation: "query.where",
5864
+ model: this.model.name
5865
+ });
5821
5866
  const existingWhere = this.legacyWhere ?? this.toQuerySchemaWhere(this.queryWhere);
5822
5867
  this.queryWhere = void 0;
5823
5868
  if (!existingWhere) {
@@ -7347,42 +7392,38 @@ var QueryBuilder = class QueryBuilder {
7347
7392
  return this.insertOrIgnore(rows);
7348
7393
  }
7349
7394
  /**
7350
- * Updates records matching the current query constraints with the
7351
- * specified data and returns the updated record(s) as model instance(s).
7395
+ * Updates the first record matching the current query constraints and
7396
+ * returns it as a model. Use {@link updateFrom} to update every match.
7352
7397
  *
7353
7398
  * @param data
7354
7399
  * @returns
7355
7400
  */
7356
7401
  async update(data) {
7357
- const where = this.buildWhere();
7358
- if (!where) throw new QueryConstraintException("Update requires a where clause.", {
7359
- operation: "update",
7360
- model: this.model.name
7361
- });
7362
- const directSpec = this.tryBuildUpdateSpec(where, data);
7402
+ const condition = this.buildWriteCondition("update", "Update");
7403
+ const uniqueWhere = this.uniqueWhereFor(condition);
7363
7404
  const adapter = this.requireAdapter();
7364
- if (!this.isUniqueWhere(where) && directSpec && typeof adapter.updateFirst === "function") {
7365
- const updated = await adapter.updateFirst(directSpec);
7405
+ if (!uniqueWhere && typeof adapter.updateFirst === "function") {
7406
+ const updated = await adapter.updateFirst({
7407
+ target: this.buildQueryTarget(),
7408
+ where: condition,
7409
+ values: data
7410
+ });
7366
7411
  if (!updated) throw new ModelNotFoundException(this.model.name, "Record not found for update operation.", { operation: "update" });
7367
7412
  return this.model.hydrate(updated);
7368
7413
  }
7369
- const uniqueWhere = await this.resolveUniqueWhere(where);
7370
- const updated = await this.executeUpdateRow(uniqueWhere, data);
7414
+ const updated = await this.executeUpdateRow(uniqueWhere ?? await this.resolveUniqueWhere(condition), data);
7371
7415
  return this.model.hydrate(updated);
7372
7416
  }
7373
7417
  /**
7374
- * Update records using update-many semantics when available.
7418
+ * Update every record matching the current query constraints and return
7419
+ * the number of affected rows.
7375
7420
  *
7376
7421
  * @param data
7377
7422
  * @returns
7378
7423
  */
7379
7424
  async updateFrom(data) {
7380
- const where = this.buildWhere();
7381
- if (!where) throw new QueryConstraintException("Update requires a where clause.", {
7382
- operation: "updateFrom",
7383
- model: this.model.name
7384
- });
7385
- return await this.executeUpdateManyRows(where, data);
7425
+ const condition = this.buildWriteCondition("updateFrom", "Update");
7426
+ return await this.executeUpdateManyRows(condition, data);
7386
7427
  }
7387
7428
  /**
7388
7429
  * Insert a record when no match exists, otherwise update the matching record.
@@ -7471,12 +7512,7 @@ var QueryBuilder = class QueryBuilder {
7471
7512
  * @returns the count of deleted rows (0 when nothing matched)
7472
7513
  */
7473
7514
  async delete() {
7474
- const where = this.buildWhere();
7475
- if (!where) throw new QueryConstraintException("Delete requires a where clause.", {
7476
- operation: "delete",
7477
- model: this.model.name
7478
- });
7479
- return this.executeDeleteManyRows(where);
7515
+ return this.executeDeleteManyRows(this.buildWriteCondition("delete", "Delete"));
7480
7516
  }
7481
7517
  /**
7482
7518
  * Deletes every record matching the current query constraints and throws when
@@ -7524,9 +7560,7 @@ var QueryBuilder = class QueryBuilder {
7524
7560
  values
7525
7561
  };
7526
7562
  }
7527
- tryBuildUpdateManySpec(where, values) {
7528
- const condition = this.tryBuildQueryCondition(where);
7529
- if (condition === null) return null;
7563
+ tryBuildUpdateManySpec(condition, values) {
7530
7564
  return {
7531
7565
  target: this.buildQueryTarget(),
7532
7566
  where: condition,
@@ -8138,7 +8172,23 @@ var QueryBuilder = class QueryBuilder {
8138
8172
  }
8139
8173
  return plans;
8140
8174
  }
8175
+ /**
8176
+ * Appends a structured condition, first flushing any Prisma-like legacy
8177
+ * where into the structured representation so that neither is lost.
8178
+ *
8179
+ * @param operator
8180
+ * @param condition
8181
+ */
8141
8182
  appendQueryCondition(operator, condition) {
8183
+ if (this.legacyWhere) {
8184
+ const existing = this.tryBuildQueryCondition(this.legacyWhere);
8185
+ if (existing === null) throw new UnsupportedAdapterFeatureException("This constraint cannot be combined with the where clause already on the query.", {
8186
+ operation: "query.where",
8187
+ model: this.model.name
8188
+ });
8189
+ this.legacyWhere = void 0;
8190
+ if (existing) this.queryWhere = existing;
8191
+ }
8142
8192
  if (!this.queryWhere) {
8143
8193
  this.queryWhere = condition;
8144
8194
  return;
@@ -8149,6 +8199,19 @@ var QueryBuilder = class QueryBuilder {
8149
8199
  conditions: [this.queryWhere, condition]
8150
8200
  };
8151
8201
  }
8202
+ /**
8203
+ * Whether a condition survives conversion to the Prisma-like where shape;
8204
+ * only comparisons and groups or negations of them do.
8205
+ *
8206
+ * @param condition
8207
+ * @returns
8208
+ */
8209
+ isSchemaWhereRepresentable(condition) {
8210
+ if (condition.type === "comparison") return true;
8211
+ if (condition.type === "group") return condition.conditions.every((entry) => this.isSchemaWhereRepresentable(entry));
8212
+ if (condition.type === "not") return this.isSchemaWhereRepresentable(condition.condition);
8213
+ return false;
8214
+ }
8152
8215
  toQuerySchemaWhere(condition) {
8153
8216
  if (!condition) return void 0;
8154
8217
  if (condition.type === "comparison") {
@@ -8529,14 +8592,10 @@ var QueryBuilder = class QueryBuilder {
8529
8592
  if (!updated) throw new ModelNotFoundException(this.model.name, "Record not found for update operation.", { operation: "update" });
8530
8593
  return updated;
8531
8594
  }
8532
- async executeUpdateManyRows(where, values) {
8595
+ async executeUpdateManyRows(condition, values) {
8533
8596
  const adapter = this.requireAdapter();
8534
8597
  const normalizedValues = this.normalizeUpdatePayload(values);
8535
- const spec = this.tryBuildUpdateManySpec(where, normalizedValues);
8536
- if (!spec) throw new UnsupportedAdapterFeatureException("Update-many could not be compiled into an Arkorm update specification.", {
8537
- operation: "query.updateMany",
8538
- model: this.model.name
8539
- });
8598
+ const spec = this.tryBuildUpdateManySpec(condition, normalizedValues);
8540
8599
  if (typeof adapter.updateMany === "function") return await adapter.updateMany(spec);
8541
8600
  const rows = await adapter.select({
8542
8601
  target: spec.target,
@@ -8554,13 +8613,8 @@ var QueryBuilder = class QueryBuilder {
8554
8613
  }
8555
8614
  return updated;
8556
8615
  }
8557
- async executeDeleteManyRows(where) {
8616
+ async executeDeleteManyRows(condition) {
8558
8617
  const adapter = this.requireAdapter();
8559
- const condition = this.tryBuildQueryCondition(where);
8560
- if (condition === null) throw new UnsupportedAdapterFeatureException("Delete could not be compiled into an Arkorm delete specification.", {
8561
- operation: "query.delete",
8562
- model: this.model.name
8563
- });
8564
8618
  const spec = {
8565
8619
  target: this.buildQueryTarget(),
8566
8620
  where: condition
@@ -8582,6 +8636,39 @@ var QueryBuilder = class QueryBuilder {
8582
8636
  return deleted;
8583
8637
  }
8584
8638
  /**
8639
+ * The complete where condition for a write, built the same way reads build
8640
+ * theirs so that every constraint (raw, JSON, relation, expression, and the
8641
+ * rest) narrows the write. A write without constraints is refused.
8642
+ *
8643
+ * @param operation
8644
+ * @param label
8645
+ * @returns
8646
+ */
8647
+ buildWriteCondition(operation, label) {
8648
+ const condition = this.buildQueryWhereCondition();
8649
+ if (condition === null) throw new UnsupportedAdapterFeatureException(`${label} could not be compiled into an Arkorm ${label.toLowerCase()} specification.`, {
8650
+ operation: `query.${operation}`,
8651
+ model: this.model.name
8652
+ });
8653
+ if (!condition) throw new QueryConstraintException(`${label} requires a where clause.`, {
8654
+ operation,
8655
+ model: this.model.name
8656
+ });
8657
+ return condition;
8658
+ }
8659
+ /**
8660
+ * The primary-key lookup a condition amounts to, when it is nothing more
8661
+ * than an equality on the primary key.
8662
+ *
8663
+ * @param condition
8664
+ * @returns
8665
+ */
8666
+ uniqueWhereFor(condition) {
8667
+ const primaryKey = this.model.getPrimaryKey();
8668
+ if (condition.type !== "comparison" || condition.column !== primaryKey || condition.operator !== "=" || condition.value == null) return null;
8669
+ return { [primaryKey]: condition.value };
8670
+ }
8671
+ /**
8585
8672
  * Builds the where clause for the query, taking into account soft delete
8586
8673
  * settings if applicable.
8587
8674
  *
@@ -8604,17 +8691,12 @@ var QueryBuilder = class QueryBuilder {
8604
8691
  /**
8605
8692
  * Resolves a unique where clause for update and delete operations.
8606
8693
  *
8607
- * @param where
8694
+ * @param condition
8608
8695
  * @returns
8609
8696
  */
8610
- async resolveUniqueWhere(where, failIfMissing = true) {
8611
- if (this.isUniqueWhere(where)) return where;
8612
- const condition = this.tryBuildQueryCondition(where);
8613
- if (!condition) throw new UniqueConstraintResolutionException("Unable to resolve a unique identifier for update/delete operation from the current query shape.", {
8614
- operation: "resolveUniqueWhere",
8615
- model: this.model.name,
8616
- meta: { where }
8617
- });
8697
+ async resolveUniqueWhere(condition, failIfMissing = true) {
8698
+ const unique = this.uniqueWhereFor(condition);
8699
+ if (unique) return unique;
8618
8700
  const row = await this.requireAdapter().selectOne({
8619
8701
  target: this.buildQueryTarget(),
8620
8702
  columns: [{ column: this.model.getPrimaryKey() }],
@@ -8624,28 +8706,17 @@ var QueryBuilder = class QueryBuilder {
8624
8706
  if (!row) return failIfMissing ? (() => {
8625
8707
  throw new ModelNotFoundException(this.model.name, "Record not found for update/delete operation.", {
8626
8708
  operation: "resolveUniqueWhere",
8627
- meta: { where }
8709
+ meta: { condition }
8628
8710
  });
8629
8711
  })() : null;
8630
8712
  const primaryKey = this.model.getPrimaryKey();
8631
8713
  if (!Object.prototype.hasOwnProperty.call(row, primaryKey)) throw new UniqueConstraintResolutionException(`Unable to resolve a unique identifier for update/delete operation. Include [${primaryKey}] in the query constraints.`, {
8632
8714
  operation: "resolveUniqueWhere",
8633
8715
  model: this.model.name,
8634
- meta: { where }
8716
+ meta: { condition }
8635
8717
  });
8636
8718
  return { [primaryKey]: row[primaryKey] };
8637
8719
  }
8638
- /**
8639
- * Checks if the provided where clause is already a unique
8640
- * identifier (i.e., contains only an 'id' field).
8641
- *
8642
- * @param where
8643
- * @returns
8644
- */
8645
- isUniqueWhere(where) {
8646
- const primaryKey = this.model.getPrimaryKey();
8647
- return Object.keys(where).length === 1 && Object.prototype.hasOwnProperty.call(where, primaryKey);
8648
- }
8649
8720
  shuffleRows(rows) {
8650
8721
  const shuffled = [...rows];
8651
8722
  for (let index = shuffled.length - 1; index > 0; index--) {
@@ -8852,8 +8923,11 @@ var QueryBuilder = class QueryBuilder {
8852
8923
  return this.tryBuildRelationQueryConstraintWhere(relatedQuery);
8853
8924
  }
8854
8925
  tryBuildRelationQueryConstraintWhere(relatedQuery) {
8855
- if (relatedQuery.hasRelationFilters() || relatedQuery.hasRelationAggregates() || relatedQuery.queryRelationLoads || relatedQuery.querySelect || relatedQuery.queryOrderBy || relatedQuery.queryDistinct || relatedQuery.queryGroupBy || relatedQuery.offsetValue !== void 0 || relatedQuery.limitValue !== void 0 || relatedQuery.randomOrderEnabled) return null;
8856
- return relatedQuery.buildQueryWhereCondition(false);
8926
+ if (relatedQuery.hasRelationAggregates() || relatedQuery.queryRelationLoads || relatedQuery.querySelect || relatedQuery.queryOrderBy || relatedQuery.queryDistinct || relatedQuery.queryGroupBy || relatedQuery.offsetValue !== void 0 || relatedQuery.limitValue !== void 0 || relatedQuery.randomOrderEnabled) return null;
8927
+ const where = relatedQuery.buildQueryWhereCondition(false);
8928
+ if (where === null || !relatedQuery.hasRelationFilters()) return where;
8929
+ if (!relatedQuery.tryBuildRelationFilterSpecs()) return null;
8930
+ return relatedQuery.withRelationConditions(where, "query.relations");
8857
8931
  }
8858
8932
  isSqlRelationFeatureMetadata(metadata) {
8859
8933
  if (!metadata) return false;
@@ -1,2 +1,2 @@
1
- import { Cs as Relation, Ss as BelongsToManyRelation, _s as HasOneRelation, bs as BelongsToRelation, ds as MorphToRelation, fs as MorphedByManyRelation, gs as HasOneThroughRelation, hs as MorphManyRelation, ms as MorphOneRelation, ps as MorphToManyRelation, us as SetBasedEagerLoader, vs as HasManyThroughRelation, ws as RelationTableLoader, xs as SingleResultRelation, ys as HasManyRelation } from "../index-DdMKdS88.cjs";
1
+ import { Cs as BelongsToManyRelation, Ss as SingleResultRelation, Ts as RelationTableLoader, _s as HasOneThroughRelation, bs as HasManyRelation, ds as SetBasedEagerLoader, fs as MorphToRelation, gs as MorphManyRelation, hs as MorphOneRelation, ms as MorphToManyRelation, ps as MorphedByManyRelation, vs as HasOneRelation, ws as Relation, xs as BelongsToRelation, ys as HasManyThroughRelation } from "../index-BbKMvoiA.cjs";
2
2
  export { BelongsToManyRelation, BelongsToRelation, HasManyRelation, HasManyThroughRelation, HasOneRelation, HasOneThroughRelation, MorphManyRelation, MorphOneRelation, MorphToManyRelation, MorphToRelation, MorphedByManyRelation, Relation, RelationTableLoader, SetBasedEagerLoader, SingleResultRelation };
@@ -1,2 +1,2 @@
1
- import { Cs as Relation, Ss as BelongsToManyRelation, _s as HasOneRelation, bs as BelongsToRelation, ds as MorphToRelation, fs as MorphedByManyRelation, gs as HasOneThroughRelation, hs as MorphManyRelation, ms as MorphOneRelation, ps as MorphToManyRelation, us as SetBasedEagerLoader, vs as HasManyThroughRelation, ws as RelationTableLoader, xs as SingleResultRelation, ys as HasManyRelation } from "../index-Cot4hmHA.mjs";
1
+ import { Cs as BelongsToManyRelation, Ss as SingleResultRelation, Ts as RelationTableLoader, _s as HasOneThroughRelation, bs as HasManyRelation, ds as SetBasedEagerLoader, fs as MorphToRelation, gs as MorphManyRelation, hs as MorphOneRelation, ms as MorphToManyRelation, ps as MorphedByManyRelation, vs as HasOneRelation, ws as Relation, xs as BelongsToRelation, ys as HasManyThroughRelation } from "../index-ChLGDqKz.mjs";
2
2
  export { BelongsToManyRelation, BelongsToRelation, HasManyRelation, HasManyThroughRelation, HasOneRelation, HasOneThroughRelation, MorphManyRelation, MorphOneRelation, MorphToManyRelation, MorphToRelation, MorphedByManyRelation, Relation, RelationTableLoader, SetBasedEagerLoader, SingleResultRelation };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "arkormx",
3
- "version": "2.12.11",
3
+ "version": "2.12.13",
4
4
  "description": "Modern TypeScript-first ORM for Node.js.",
5
5
  "keywords": [
6
6
  "orm",