@getstrata/core 0.5.98 → 0.5.99

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.
@@ -74,7 +74,7 @@ function buildOperatorClauses(column, operator, params) {
74
74
  clauses.push(`${column} <= ${pushParam(params, operator.lte)}`);
75
75
  }
76
76
  if (operator.ilike !== undefined) {
77
- clauses.push(`${column} ILIKE ${pushParam(params, `%${operator.ilike}%`)}`);
77
+ clauses.push(`${column} ILIKE ${pushParam(params, operator.ilike)}`);
78
78
  }
79
79
  if (operator.tsMatch !== undefined) {
80
80
  clauses.push(`${column} @@ plainto_tsquery('english', ${pushParam(params, operator.tsMatch)})`);
@@ -74,7 +74,7 @@ function buildOperatorClauses(column, operator, params) {
74
74
  clauses.push(`${column} <= ${pushParam(params, operator.lte)}`);
75
75
  }
76
76
  if (operator.ilike !== undefined) {
77
- clauses.push(`${column} ILIKE ${pushParam(params, `%${operator.ilike}%`)}`);
77
+ clauses.push(`${column} ILIKE ${pushParam(params, operator.ilike)}`);
78
78
  }
79
79
  if (operator.tsMatch !== undefined) {
80
80
  clauses.push(`${column} @@ plainto_tsquery('english', ${pushParam(params, operator.tsMatch)})`);
@@ -579,13 +579,30 @@ class RepositoryQuery {
579
579
  });
580
580
  return this;
581
581
  }
582
+ withBelongsToMany(as, relation, relatedRepository, options = {}) {
583
+ this.eagerLoads.push({
584
+ kind: "belongsToMany",
585
+ as,
586
+ relation,
587
+ repository: relatedRepository,
588
+ options
589
+ });
590
+ return this;
591
+ }
582
592
  async get() {
583
593
  const rows = await this.repository.findAll(this.buildOptions());
584
594
  return await this.attach(rows);
585
595
  }
586
596
  async first() {
587
- const rows = await this.get();
588
- return rows[0] ?? null;
597
+ const rows = await this.repository.findAll({ ...this.buildOptions(), limit: 1 });
598
+ const attached = await this.attach(rows);
599
+ return attached[0] ?? null;
600
+ }
601
+ async count() {
602
+ return await this.repository.count(this.buildOptions());
603
+ }
604
+ async attachToRows(rows) {
605
+ return await this.attach(rows);
589
606
  }
590
607
  async paginate(options) {
591
608
  return await this.repository.paginate({
@@ -657,6 +674,15 @@ class RepositoryQuery {
657
674
  }));
658
675
  continue;
659
676
  }
677
+ if (load.kind === "belongsToMany") {
678
+ const relation2 = load.relation;
679
+ const grouped2 = await this.repository.loadBelongsToManyForParents(rows, relation2, load.repository, load.options);
680
+ result = result.map((row) => ({
681
+ ...row,
682
+ [load.as]: grouped2.get(row[relation2.parentKey]) ?? []
683
+ }));
684
+ continue;
685
+ }
660
686
  if (load.kind === "morphTo") {
661
687
  const relation2 = load.relation;
662
688
  const grouped2 = await this.repository.loadMorphToForChildren(rows, relation2, load.morphRepositories ?? new Map, load.options);
@@ -341,7 +341,7 @@ function buildOperatorClauses(column, operator, params) {
341
341
  clauses.push(`${column} <= ${pushParam(params, operator.lte)}`);
342
342
  }
343
343
  if (operator.ilike !== undefined) {
344
- clauses.push(`${column} ILIKE ${pushParam(params, `%${operator.ilike}%`)}`);
344
+ clauses.push(`${column} ILIKE ${pushParam(params, operator.ilike)}`);
345
345
  }
346
346
  if (operator.tsMatch !== undefined) {
347
347
  clauses.push(`${column} @@ plainto_tsquery('english', ${pushParam(params, operator.tsMatch)})`);
@@ -56,9 +56,15 @@ class JsonResource {
56
56
 
57
57
  class ResourceCollection extends JsonResource {
58
58
  toArray() {
59
- return {
60
- data: this.resource.map((item) => item instanceof JsonResource ? item.toArray() : { ...item })
61
- };
59
+ const items = this.resource.map((item) => item instanceof JsonResource ? item.toArray() : { ...item });
60
+ const wrap = this.constructor.wrap;
61
+ if (wrap === null) {
62
+ return { data: items };
63
+ }
64
+ return { [wrap]: items };
65
+ }
66
+ toResponse() {
67
+ return { ...this.toArray(), ...this.extra };
62
68
  }
63
69
  }
64
70
  function toResourceCollection(items, transformer) {
@@ -35,7 +35,7 @@ export { withMigrationLock } from "../core/database/migrations/advisoryLock.ts";
35
35
  export { freshDatabase, getMigrationStatus, loadMigrationsFromDirectory, migrateDatabase, rollbackDatabase, } from "../core/database/migrations/runner.ts";
36
36
  export type { Migration, MigrationDatabase, MigrationStatus, } from "../core/database/migrations/types.ts";
37
37
  export type { CastType, GlobalScopeFn, ModelConstructor } from "../core/database/model.ts";
38
- export { applyCasts, BelongsToManyRelationQuery, BelongsToRelationQuery, dehydrateValue, filterMassAssignable, HasManyRelationQuery, HasOneRelationQuery, hydrateValue, Model, MorphManyRelationQuery, MorphOneRelationQuery, MorphToRelationQuery, registerModelRepository, } from "../core/database/model.ts";
38
+ export { applyCasts, BelongsToManyRelationQuery, BelongsToRelationQuery, dehydrateValue, filterMassAssignable, HasManyRelationQuery, HasOneRelationQuery, hydrateValue, Model, ModelQuery, MorphManyRelationQuery, MorphOneRelationQuery, MorphToRelationQuery, registerModelClass, registerModelRepository, } from "../core/database/model.ts";
39
39
  export { createDatabaseQueryProxy } from "../core/database/queryProxy.ts";
40
40
  export type { BelongsToManyRelation, BelongsToRelation, HasManyRelation, HasOneRelation, MorphManyRelation, MorphOneRelation, MorphToRelation, } from "../core/database/relationships.ts";
41
41
  export { belongsTo, belongsToMany, hasMany, hasOne, indexBelongsToManyRelation, indexBelongsToRelation, indexHasManyRelation, indexHasOneRelation, indexMorphManyRelation, indexMorphOneRelation, indexMorphToRelation, morphMany, morphOne, morphTo, } from "../core/database/relationships.ts";