@getstrata/core 1.1.2 → 1.1.3
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/CHANGELOG.md +4 -0
- package/dist/core/database/model.d.ts +4 -3
- package/dist/entries/database/model.js +21 -2
- package/dist/index.js +21 -2
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -14,6 +14,7 @@ interface ModelConstructor<TEntity extends object, PrimaryKey extends keyof TEnt
|
|
|
14
14
|
}
|
|
15
15
|
type AnyModel = Model<Record<string, unknown>, "id">;
|
|
16
16
|
type RelatedRef<TRelated extends object, RelatedKey extends keyof TRelated & string> = RelatedModelClass<TRelated, RelatedKey> | string | (() => RelatedModelClass<TRelated, RelatedKey>);
|
|
17
|
+
type RelationNameInput = string | readonly string[];
|
|
17
18
|
type ModelObserver = {
|
|
18
19
|
retrieved?: (model: AnyModel) => unknown;
|
|
19
20
|
creating?: (model: AnyModel) => unknown;
|
|
@@ -36,7 +37,7 @@ declare class ModelQuery {
|
|
|
36
37
|
readonly query: RepositoryQuery<Record<string, unknown>, "id">;
|
|
37
38
|
private readonly eager;
|
|
38
39
|
constructor(modelClass: object, query: RepositoryQuery<Record<string, unknown>, "id">);
|
|
39
|
-
with(...relations:
|
|
40
|
+
with(...relations: RelationNameInput[]): this;
|
|
40
41
|
where(input: QueryWhere<object> | ((builder: import("./whereBuilder.ts").WhereBuilder<object>) => void)): this;
|
|
41
42
|
orWhere(input: QueryWhere<object> | ((builder: import("./whereBuilder.ts").WhereBuilder<object>) => void)): this;
|
|
42
43
|
orderBy(orderBy: QueryOptions<object>["orderBy"]): this;
|
|
@@ -124,7 +125,7 @@ declare class Model<TEntity extends object, PrimaryKey extends keyof TEntity & s
|
|
|
124
125
|
static query(this: object): ModelQuery;
|
|
125
126
|
static newFromRecord(this: object, record: object, exists?: boolean): Model<Record<string, unknown>, "id">;
|
|
126
127
|
static create(this: object, attributes: Record<string, unknown>, forced?: Record<string, unknown>): Promise<Model<Record<string, unknown>, "id">>;
|
|
127
|
-
static with(this: object, ...relations:
|
|
128
|
+
static with(this: object, ...relations: RelationNameInput[]): ModelQuery;
|
|
128
129
|
static withTrashed(this: object): ModelQuery;
|
|
129
130
|
static onlyTrashed(this: object): ModelQuery;
|
|
130
131
|
static chunk(this: object, count: number, callback: (models: Array<Model<Record<string, unknown>, "id">>) => Promise<boolean | void>): Promise<void>;
|
|
@@ -173,7 +174,7 @@ declare class Model<TEntity extends object, PrimaryKey extends keyof TEntity & s
|
|
|
173
174
|
morphMany<TRelated extends object, RelatedKey extends keyof TRelated & string>(related: RelatedRef<TRelated, RelatedKey>, morphName: string, typeKey?: keyof TRelated & string, idKey?: keyof TRelated & string, morphType?: string): MorphManyRelationQuery<TEntity, PrimaryKey, TRelated, RelatedKey>;
|
|
174
175
|
morphOne<TRelated extends object, RelatedKey extends keyof TRelated & string>(related: RelatedRef<TRelated, RelatedKey>, morphName: string, typeKey?: keyof TRelated & string, idKey?: keyof TRelated & string, morphType?: string): MorphOneRelationQuery<TEntity, PrimaryKey, TRelated, RelatedKey>;
|
|
175
176
|
morphTo(relatedByType: Record<string, RelatedRef<Record<string, unknown>, "id">>, morphName?: string, typeKey?: keyof TEntity & string, idKey?: keyof TEntity & string): MorphToRelationQuery<TEntity, PrimaryKey>;
|
|
176
|
-
load(...names:
|
|
177
|
+
load(...names: RelationNameInput[]): Promise<this>;
|
|
177
178
|
loaded<T = unknown>(name: string): T | undefined;
|
|
178
179
|
setLoaded(name: string, value: unknown): this;
|
|
179
180
|
mergeAttributes(patch: Partial<TEntity>): this;
|
|
@@ -1469,6 +1469,25 @@ var namedModels = new Map;
|
|
|
1469
1469
|
var modelGlobalScopes = new WeakMap;
|
|
1470
1470
|
var modelObservers = new WeakMap;
|
|
1471
1471
|
var modelBooted = new WeakSet;
|
|
1472
|
+
function flattenRelationNames(relations) {
|
|
1473
|
+
const names = [];
|
|
1474
|
+
for (const item of relations) {
|
|
1475
|
+
if (typeof item === "string") {
|
|
1476
|
+
if (item.length > 0) {
|
|
1477
|
+
names.push(item);
|
|
1478
|
+
}
|
|
1479
|
+
continue;
|
|
1480
|
+
}
|
|
1481
|
+
if (Array.isArray(item)) {
|
|
1482
|
+
for (const nested of item) {
|
|
1483
|
+
if (typeof nested === "string" && nested.length > 0) {
|
|
1484
|
+
names.push(nested);
|
|
1485
|
+
}
|
|
1486
|
+
}
|
|
1487
|
+
}
|
|
1488
|
+
}
|
|
1489
|
+
return names;
|
|
1490
|
+
}
|
|
1472
1491
|
async function runObservers(model, hook) {
|
|
1473
1492
|
const observers = modelObservers.get(model.constructor) ?? [];
|
|
1474
1493
|
for (const observer of observers) {
|
|
@@ -1725,7 +1744,7 @@ class ModelQuery {
|
|
|
1725
1744
|
const statics = modelStatics(this.modelClass);
|
|
1726
1745
|
ensureBooted(this.modelClass);
|
|
1727
1746
|
const dummy = statics.newFromRecord({}, false);
|
|
1728
|
-
for (const path of relations) {
|
|
1747
|
+
for (const path of flattenRelationNames(relations)) {
|
|
1729
1748
|
const name = path.split(".")[0] ?? path;
|
|
1730
1749
|
const method = dummy[name];
|
|
1731
1750
|
if (typeof method !== "function") {
|
|
@@ -2387,7 +2406,7 @@ class Model {
|
|
|
2387
2406
|
}));
|
|
2388
2407
|
}
|
|
2389
2408
|
async load(...names) {
|
|
2390
|
-
for (const name of names) {
|
|
2409
|
+
for (const name of flattenRelationNames(names)) {
|
|
2391
2410
|
if (name.includes(".")) {
|
|
2392
2411
|
await loadNested(this, name);
|
|
2393
2412
|
continue;
|
package/dist/index.js
CHANGED
|
@@ -5868,6 +5868,25 @@ var namedModels = new Map;
|
|
|
5868
5868
|
var modelGlobalScopes = new WeakMap;
|
|
5869
5869
|
var modelObservers = new WeakMap;
|
|
5870
5870
|
var modelBooted = new WeakSet;
|
|
5871
|
+
function flattenRelationNames(relations) {
|
|
5872
|
+
const names = [];
|
|
5873
|
+
for (const item of relations) {
|
|
5874
|
+
if (typeof item === "string") {
|
|
5875
|
+
if (item.length > 0) {
|
|
5876
|
+
names.push(item);
|
|
5877
|
+
}
|
|
5878
|
+
continue;
|
|
5879
|
+
}
|
|
5880
|
+
if (Array.isArray(item)) {
|
|
5881
|
+
for (const nested of item) {
|
|
5882
|
+
if (typeof nested === "string" && nested.length > 0) {
|
|
5883
|
+
names.push(nested);
|
|
5884
|
+
}
|
|
5885
|
+
}
|
|
5886
|
+
}
|
|
5887
|
+
}
|
|
5888
|
+
return names;
|
|
5889
|
+
}
|
|
5871
5890
|
async function runObservers(model, hook) {
|
|
5872
5891
|
const observers = modelObservers.get(model.constructor) ?? [];
|
|
5873
5892
|
for (const observer of observers) {
|
|
@@ -6124,7 +6143,7 @@ class ModelQuery {
|
|
|
6124
6143
|
const statics = modelStatics(this.modelClass);
|
|
6125
6144
|
ensureBooted(this.modelClass);
|
|
6126
6145
|
const dummy = statics.newFromRecord({}, false);
|
|
6127
|
-
for (const path of relations) {
|
|
6146
|
+
for (const path of flattenRelationNames(relations)) {
|
|
6128
6147
|
const name = path.split(".")[0] ?? path;
|
|
6129
6148
|
const method = dummy[name];
|
|
6130
6149
|
if (typeof method !== "function") {
|
|
@@ -6786,7 +6805,7 @@ class Model {
|
|
|
6786
6805
|
}));
|
|
6787
6806
|
}
|
|
6788
6807
|
async load(...names) {
|
|
6789
|
-
for (const name of names) {
|
|
6808
|
+
for (const name of flattenRelationNames(names)) {
|
|
6790
6809
|
if (name.includes(".")) {
|
|
6791
6810
|
await loadNested(this, name);
|
|
6792
6811
|
continue;
|