@getstrata/core 0.5.10 → 0.5.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.
@@ -873,6 +873,66 @@ function indexBelongsToManyRelation(parents, pivotRows, relatedRows, relation) {
873
873
  }
874
874
  return groups;
875
875
  }
876
+ function morphMany(definition) {
877
+ return {
878
+ type: "morphMany",
879
+ ...definition
880
+ };
881
+ }
882
+ function morphOne(definition) {
883
+ return {
884
+ type: "morphOne",
885
+ ...definition
886
+ };
887
+ }
888
+ function morphTo(definition) {
889
+ return {
890
+ type: "morphTo",
891
+ ...definition
892
+ };
893
+ }
894
+ function indexMorphManyRelation(parents, children, relation) {
895
+ const groups = new Map;
896
+ for (const parent of parents) {
897
+ groups.set(parent[relation.localKey], []);
898
+ }
899
+ for (const child of children) {
900
+ if (child[relation.morphTypeKey] !== relation.morphType) {
901
+ continue;
902
+ }
903
+ const key = child[relation.morphIdKey];
904
+ const group = groups.get(key);
905
+ if (!group) {
906
+ continue;
907
+ }
908
+ group.push(child);
909
+ }
910
+ return groups;
911
+ }
912
+ function indexMorphOneRelation(parents, children, relation) {
913
+ const grouped = indexMorphManyRelation(parents, children, relation);
914
+ const result = new Map;
915
+ for (const parent of parents) {
916
+ const matches = grouped.get(parent[relation.localKey]) ?? [];
917
+ result.set(parent[relation.localKey], matches[0]);
918
+ }
919
+ return result;
920
+ }
921
+ function indexMorphToRelation(children, parentsByType, relation) {
922
+ const result = new Map;
923
+ for (const child of children) {
924
+ const morphType = String(child[relation.morphTypeKey]);
925
+ const parents = parentsByType.get(morphType);
926
+ if (!parents) {
927
+ continue;
928
+ }
929
+ const parent = parents.get(child[relation.morphIdKey]);
930
+ if (parent) {
931
+ result.set(child[relation.morphIdKey], parent);
932
+ }
933
+ }
934
+ return result;
935
+ }
876
936
 
877
937
  // ../../src/config/database.ts
878
938
  function readInteger(name, fallback) {
@@ -1076,6 +1136,37 @@ class RepositoryQuery {
1076
1136
  });
1077
1137
  return this;
1078
1138
  }
1139
+ withMorphMany(as, relation, childRepository, options = {}) {
1140
+ this.eagerLoads.push({
1141
+ kind: "morphMany",
1142
+ as,
1143
+ relation,
1144
+ repository: childRepository,
1145
+ options
1146
+ });
1147
+ return this;
1148
+ }
1149
+ withMorphOne(as, relation, childRepository, options = {}) {
1150
+ this.eagerLoads.push({
1151
+ kind: "morphOne",
1152
+ as,
1153
+ relation,
1154
+ repository: childRepository,
1155
+ options
1156
+ });
1157
+ return this;
1158
+ }
1159
+ withMorphTo(as, relation, repositoriesByType, options = {}) {
1160
+ this.eagerLoads.push({
1161
+ kind: "morphTo",
1162
+ as,
1163
+ relation,
1164
+ repository: this.repository,
1165
+ morphRepositories: repositoriesByType,
1166
+ options
1167
+ });
1168
+ return this;
1169
+ }
1079
1170
  async get() {
1080
1171
  const rows = await this.repository.findAll(this.buildOptions());
1081
1172
  return await this.attach(rows);
@@ -1136,6 +1227,33 @@ class RepositoryQuery {
1136
1227
  }));
1137
1228
  continue;
1138
1229
  }
1230
+ if (load.kind === "morphMany") {
1231
+ const relation2 = load.relation;
1232
+ const grouped2 = await load.repository.withConnection(this.repository.getConnection()).loadMorphManyForParents(rows, relation2, load.options);
1233
+ result = result.map((row) => ({
1234
+ ...row,
1235
+ [load.as]: grouped2.get(row[relation2.localKey]) ?? []
1236
+ }));
1237
+ continue;
1238
+ }
1239
+ if (load.kind === "morphOne") {
1240
+ const relation2 = load.relation;
1241
+ const grouped2 = await load.repository.withConnection(this.repository.getConnection()).loadMorphOneForParents(rows, relation2, load.options);
1242
+ result = result.map((row) => ({
1243
+ ...row,
1244
+ [load.as]: grouped2.get(row[relation2.localKey])
1245
+ }));
1246
+ continue;
1247
+ }
1248
+ if (load.kind === "morphTo") {
1249
+ const relation2 = load.relation;
1250
+ const grouped2 = await this.repository.loadMorphToForChildren(rows, relation2, load.morphRepositories ?? new Map, load.options);
1251
+ result = result.map((row) => ({
1252
+ ...row,
1253
+ [load.as]: grouped2.get(row[relation2.morphIdKey])
1254
+ }));
1255
+ continue;
1256
+ }
1139
1257
  const relation = load.relation;
1140
1258
  const grouped = await this.repository.loadBelongsToForParents(rows, relation, load.repository, load.options);
1141
1259
  result = result.map((row) => ({
@@ -1412,6 +1530,56 @@ class BaseRepository {
1412
1530
  }, options);
1413
1531
  return indexBelongsToRelation(children, parents, relation);
1414
1532
  }
1533
+ async loadMorphManyForParents(parents, relation, options = {}) {
1534
+ if (parents.length === 0) {
1535
+ return indexMorphManyRelation(parents, [], relation);
1536
+ }
1537
+ const parentIds = [...new Set(parents.map((parent) => parent[relation.localKey]))];
1538
+ const children = await this.findWhere({
1539
+ [relation.morphTypeKey]: relation.morphType,
1540
+ [relation.morphIdKey]: parentIds
1541
+ }, options);
1542
+ return indexMorphManyRelation(parents, children, relation);
1543
+ }
1544
+ async loadMorphOneForParents(parents, relation, options = {}) {
1545
+ const grouped = await this.loadMorphManyForParents(parents, relation, options);
1546
+ const result = new Map;
1547
+ for (const parent of parents) {
1548
+ const matches = grouped.get(parent[relation.localKey]) ?? [];
1549
+ result.set(parent[relation.localKey], matches[0]);
1550
+ }
1551
+ return result;
1552
+ }
1553
+ async loadMorphToForChildren(children, relation, repositoriesByType, options = {}) {
1554
+ if (children.length === 0) {
1555
+ return new Map;
1556
+ }
1557
+ const idsByType = new Map;
1558
+ for (const child of children) {
1559
+ const morphType = String(child[relation.morphTypeKey]);
1560
+ const morphId = child[relation.morphIdKey];
1561
+ const ids = idsByType.get(morphType) ?? new Set;
1562
+ ids.add(morphId);
1563
+ idsByType.set(morphType, ids);
1564
+ }
1565
+ const parentsByType = new Map;
1566
+ for (const [morphType, ids] of idsByType) {
1567
+ const repository = repositoriesByType.get(morphType);
1568
+ if (!repository) {
1569
+ continue;
1570
+ }
1571
+ const ownerKey = repository.getTable().primaryKey;
1572
+ const parents = await repository.withConnection(this.connection).findWhere({
1573
+ [ownerKey]: [...ids]
1574
+ }, options);
1575
+ const indexed = new Map;
1576
+ for (const parent of parents) {
1577
+ indexed.set(parent[ownerKey], parent);
1578
+ }
1579
+ parentsByType.set(morphType, indexed);
1580
+ }
1581
+ return indexMorphToRelation(children, parentsByType, relation);
1582
+ }
1415
1583
  }
1416
1584
  var baseRepository_default = BaseRepository;
1417
1585
  // ../../src/core/database/connection.ts
@@ -5,6 +5,8 @@
5
5
  export { resolveApplicationAuth, resolveApplicationCache, resolveApplicationConfig, resolveApplicationDependencies, resolveApplicationLogger, resolveApplicationPolicyGate, resolveApplicationQueue, setActiveApplicationContext, } from "../bootstrap/applicationRegistry.ts";
6
6
  export type { ServiceProvider } from "../bootstrap/contracts.ts";
7
7
  export { ConfigStore, resolveService, ServiceContainer, } from "../bootstrap/contracts.ts";
8
+ export type { AdminColumn, AdminColumnType, AdminResource, AdminResourceDefinition, AdminResourceHandlers, } from "../core/admin/index.ts";
9
+ export { AdminResourceRegistry, formatAdminValue, } from "../core/admin/index.ts";
8
10
  export type { AbilityChecker } from "../core/auth/abilityChecker.ts";
9
11
  export type { AuthUser } from "../core/auth/authContext.ts";
10
12
  export { currentAuthUser, runWithAuthUser } from "../core/auth/authContext.ts";
@@ -21,8 +23,8 @@ export { freshDatabase, getMigrationStatus, loadMigrationsFromDirectory, migrate
21
23
  export type { Migration, MigrationDatabase, MigrationStatus, } from "../core/database/migrations/types.ts";
22
24
  export type { CastType, GlobalScopeFn, ModelConstructor } from "../core/database/model.ts";
23
25
  export { applyCasts, dehydrateValue, filterMassAssignable, hydrateValue, Model, registerModelRepository, } from "../core/database/model.ts";
24
- export type { BelongsToManyRelation, BelongsToRelation, HasManyRelation, HasOneRelation, } from "../core/database/relationships.ts";
25
- export { belongsTo, belongsToMany, hasMany, hasOne, indexBelongsToManyRelation, indexBelongsToRelation, indexHasManyRelation, indexHasOneRelation, } from "../core/database/relationships.ts";
26
+ export type { BelongsToManyRelation, BelongsToRelation, HasManyRelation, HasOneRelation, MorphManyRelation, MorphOneRelation, MorphToRelation, } from "../core/database/relationships.ts";
27
+ export { belongsTo, belongsToMany, hasMany, hasOne, indexBelongsToManyRelation, indexBelongsToRelation, indexHasManyRelation, indexHasOneRelation, indexMorphManyRelation, indexMorphOneRelation, indexMorphToRelation, morphMany, morphOne, morphTo, } from "../core/database/relationships.ts";
26
28
  export { RepositoryQuery } from "../core/database/repositoryQuery.ts";
27
29
  export type { BlueprintAction, BlueprintCallback, ColumnKind, DatabaseDriver, ForeignKeyOptions, Grammar, IndexDefinition, IndexKind, ResolveDatabaseDriverOptions, SchemaBuilder, } from "../core/database/schema/index.ts";
28
30
  export { Blueprint, ColumnDefinition, compileBlueprint, createSchemaBuilder, ForeignIdColumnDefinition, grammarForDriver, inferReferencedTable, MySqlGrammar, PostgresGrammar, resolveDatabaseDriver, Schema, SqliteGrammar, UnsupportedSchemaFeatureError, } from "../core/database/schema/index.ts";
@@ -56,13 +58,19 @@ export { createThrottleMiddleware } from "../core/http/throttleMiddleware.ts";
56
58
  export { WebFormRequest } from "../core/http/webFormRequest.ts";
57
59
  export { installGracefulShutdownSignals, registerShutdownHandler, runGracefulShutdown, } from "../core/lifecycle/gracefulShutdown.ts";
58
60
  export type { MailDriver, MailMessage } from "../core/mail/mailer.ts";
59
- export { LogMailDriver, Mailer, mailer } from "../core/mail/mailer.ts";
61
+ export { buildSmtpPayload, LogMailDriver, Mailer, mailer, } from "../core/mail/mailer.ts";
62
+ export type { MarkdownMailLayoutOptions, RenderedMarkdownMail } from "../core/mail/markdownMail.ts";
63
+ export { markdownToHtml, renderMarkdownMail, stripMarkdown, wrapMarkdownMailLayout, } from "../core/mail/markdownMail.ts";
64
+ export type { MarkdownMailableInput } from "../core/mail/markdownMailable.ts";
65
+ export { buildMarkdownMailMessage, sendMarkdownMail } from "../core/mail/markdownMailable.ts";
60
66
  export type { MetricLabels } from "../core/metrics/prometheus.ts";
61
67
  export { PrometheusRegistry, prometheusRegistry } from "../core/metrics/prometheus.ts";
68
+ export type { DatabaseNotificationPayload, DatabaseNotificationStore, MailNotificationMessage, Notifiable, NotificationChannelName, } from "../core/notifications/index.ts";
69
+ export { createNotificationDispatcher, Notification, NotificationDispatcher, } from "../core/notifications/index.ts";
62
70
  export type { CursorPaginatedResult, PaginatedResult, PaginationMeta, } from "../core/pagination/index.ts";
63
71
  export type { Queue, QueuePriority } from "../core/queue/index.ts";
64
72
  export { AsyncQueue, createQueue, Job, SyncQueue } from "../core/queue/index.ts";
65
- export { createFailedJobService, createProductionQueue, createQueueWorker, createTrackedJob, FailedJobRepository, FailedJobService, jobRegistry, QueueWorker, RedisQueue, ResilientQueue, } from "../core/queue/publicQueue.ts";
73
+ export { createFailedJobService, createProductionQueue, createQueueWorker, createTrackedJob, FailedJobRepository, FailedJobService, jobRegistry, QueueWorker, RedisQueue, ResilientQueue, runQueueJob, } from "../core/queue/publicQueue.ts";
66
74
  export type { ScheduledTask } from "../core/scheduler/schedule.ts";
67
75
  export { appSchedule, runDueScheduledTasks, Schedule } from "../core/scheduler/schedule.ts";
68
76
  export type { StorageDriver } from "../core/storage/storage.ts";