@getstrata/core 0.5.97 → 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.
- package/CHANGELOG.md +30 -0
- package/dist/core/contracts/container.d.ts +2 -0
- package/dist/core/database/baseRepository.d.ts +3 -1
- package/dist/core/database/factory.d.ts +47 -5
- package/dist/core/database/index.d.ts +3 -1
- package/dist/core/database/inflection.d.ts +4 -0
- package/dist/core/database/model.d.ts +88 -4
- package/dist/core/database/relationQuery.d.ts +172 -0
- package/dist/core/database/repositoryQuery.d.ts +9 -1
- package/dist/core/database/whereBuilder.d.ts +9 -1
- package/dist/core/events/eventBus.d.ts +2 -0
- package/dist/core/http/resources.d.ts +21 -1
- package/dist/entries/contracts/container.js +6 -0
- package/dist/entries/database/factory.js +159 -6
- package/dist/entries/database/model.js +1066 -23
- package/dist/entries/database/query.js +10 -1
- package/dist/entries/database/repositoryQuery.js +55 -3
- package/dist/entries/database/schema.js +10 -1
- package/dist/entries/http/resources.js +68 -1
- package/dist/framework/public-api.d.ts +4 -2
- package/dist/index.js +1374 -25
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,35 @@
|
|
|
1
1
|
# @getstrata/core changelog
|
|
2
2
|
|
|
3
|
+
## 0.5.99
|
|
4
|
+
|
|
5
|
+
HiroApp dogfood of 0.5.98 found lookalike APIs. This release matches Laravel call shape and SQL, not just export names.
|
|
6
|
+
|
|
7
|
+
- Relation queries are thenable: `await user.applications()` delegates to `get()`.
|
|
8
|
+
- `Model.with()` / `where()` / `whereHas()` return a `ModelQuery` that hydrates models and chains into `where` / `find` / `findOrFail` / `first` / `get`. `first()` / `find()` use `LIMIT` / PK lookup.
|
|
9
|
+
- `belongsTo.where()` threads constraints into `whereHas` EXISTS (HiroApp application search).
|
|
10
|
+
- `{ ilike }` uses the value as-is (Laravel). Pass `%term%` yourself; the operator no longer wraps extra `%`.
|
|
11
|
+
- Morph type defaults to `$morphClass` / class name, not `table.name`. Override with `$morphClass = "App\\Models\\User"` or the last `morphMany` argument. `morphTo()` no longer silently defaults to `imageable_*`.
|
|
12
|
+
- `primaryKey()` defaults to the table PK (`id`).
|
|
13
|
+
- Nested `load("a.b")` / `with("a.b")` skip already-loaded heads and batch the next level.
|
|
14
|
+
- `count()` is `COUNT(*)` (relations and `RepositoryQuery`).
|
|
15
|
+
- `belongsToMany` eager-loads in two queries; `toggle()` and `withPivotValues()` exist. `hasMany.save($model)` sets the FK and saves.
|
|
16
|
+
- Factory `for(parent)` infers `user_id` from a Model parent. Bound `model` uses `Model.create()` so observers fire.
|
|
17
|
+
- `JsonResource.collection().toResponse()` wraps once: `{ data: [...] }`.
|
|
18
|
+
- Observers: `saving` / `saved` / `retrieved`. Integer casts: `integer` / `int`.
|
|
19
|
+
- `hasMany("Application")` / `() => Application` / `registerModelClass()` for ESM cycles.
|
|
20
|
+
|
|
21
|
+
Still non-conforming (honest): Bun cannot infer `morphTo()` method names (`debug_backtrace` equivalent is empty). `hashed` cast is a no-op (bcrypt is async). `Factory.has()` without a model class still needs an explicit FK.
|
|
22
|
+
|
|
23
|
+
## 0.5.98
|
|
24
|
+
|
|
25
|
+
- Eloquent-shaped relations: `this.hasMany(Related)` returns a relation query (`get`/`where`/`create`/`attach`). `load()` / `loaded()` and `Model.with()` replace PHP `__get`. `whereHas`/`has`/`doesntHave`, morph* methods, and nested `with("a.b")` are supported.
|
|
26
|
+
- Model `$hidden`/`$visible`/`$appends`, `toArray()`/`toJSON()`, `makeHidden`/`makeVisible`/`append`, and `observe()`.
|
|
27
|
+
- `Model.where()`, `firstOrNew()`, `firstOrCreate()`, and `updateOrCreate()`.
|
|
28
|
+
- Factory `count`/`state`/`sequence`/`for`/`has`/`recycle` plus `afterMaking`/`afterCreating`.
|
|
29
|
+
- `JsonResource` (`wrap`, `whenLoaded`, `additional`, `collection`).
|
|
30
|
+
- Laravel aliases: container `make`/`instance`, EventBus `on`/`emit`, query `whereNull`/`whereIn`/`whereExists`.
|
|
31
|
+
- Parity audit reports design score separately. Horizon/Nova/CLI stay Bun-native stand-ins.
|
|
32
|
+
|
|
3
33
|
## 0.5.97
|
|
4
34
|
|
|
5
35
|
- `mapDatabaseError()` recognizes HTTP errors by `status` + `message`, not only `instanceof HttpError`. Subpath builds duplicate the class, so a thrown `ForbiddenError` was remapped to `400 Bad Request`.
|
|
@@ -12,6 +12,8 @@ declare class ServiceContainer implements ServiceContainerLike {
|
|
|
12
12
|
bind<T>(key: string, factory: ServiceFactory<T>): void;
|
|
13
13
|
get<T>(key: string): T;
|
|
14
14
|
resolve<T>(key: string): T;
|
|
15
|
+
make<T>(key: string): T;
|
|
16
|
+
instance<T>(key: string, value: T): T;
|
|
15
17
|
has(key: string): boolean;
|
|
16
18
|
}
|
|
17
19
|
interface ConfigStoreLike {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type CursorPaginatedResult, type PaginatedResult } from "../pagination/index.ts";
|
|
2
|
-
import { type BelongsToRelation, type HasManyRelation, type MorphManyRelation, type MorphOneRelation, type MorphToRelation } from "./relationships.ts";
|
|
2
|
+
import { type BelongsToManyRelation, type BelongsToRelation, type HasManyRelation, type MorphManyRelation, type MorphOneRelation, type MorphToRelation } from "./relationships.ts";
|
|
3
3
|
import { RepositoryQuery } from "./repositoryQuery.ts";
|
|
4
4
|
import type { TableDefinition } from "./table.ts";
|
|
5
5
|
import type { MutationValues, QueryOptions, QueryWhere, UpdateValues } from "./types.ts";
|
|
@@ -20,6 +20,7 @@ declare class BaseRepository<TEntity extends object, PrimaryKey extends keyof TE
|
|
|
20
20
|
protected readonly table: TableDefinition<TEntity, PrimaryKey>;
|
|
21
21
|
protected readonly connection: DatabaseConnection;
|
|
22
22
|
constructor(table: TableDefinition<TEntity, PrimaryKey>, connection?: DatabaseConnection);
|
|
23
|
+
count(options?: ExtendedQueryOptions<TEntity>): Promise<number>;
|
|
23
24
|
findAll(options?: ExtendedQueryOptions<TEntity>): Promise<TEntity[]>;
|
|
24
25
|
paginate(options: {
|
|
25
26
|
page: number;
|
|
@@ -62,6 +63,7 @@ declare class BaseRepository<TEntity extends object, PrimaryKey extends keyof TE
|
|
|
62
63
|
loadMorphManyForParents<TParent extends object, LocalKey extends keyof TParent & string, MorphTypeKey extends keyof TEntity & string, MorphIdKey extends keyof TEntity & string>(parents: readonly TParent[], relation: MorphManyRelation<TParent, TEntity, LocalKey, MorphTypeKey, MorphIdKey>, options?: Omit<QueryOptions<TEntity>, "where">): Promise<Map<TParent[LocalKey], TEntity[]>>;
|
|
63
64
|
loadMorphOneForParents<TParent extends object, LocalKey extends keyof TParent & string, MorphTypeKey extends keyof TEntity & string, MorphIdKey extends keyof TEntity & string>(parents: readonly TParent[], relation: MorphOneRelation<TParent, TEntity, LocalKey, MorphTypeKey, MorphIdKey>, options?: Omit<QueryOptions<TEntity>, "where">): Promise<Map<TParent[LocalKey], TEntity | undefined>>;
|
|
64
65
|
loadMorphToForChildren<TChild extends object, TParent extends object, MorphTypeKey extends keyof TChild & string, MorphIdKey extends keyof TChild & string, OwnerKey extends keyof TParent & string>(children: readonly TChild[], relation: MorphToRelation<TChild, MorphTypeKey, MorphIdKey>, repositoriesByType: ReadonlyMap<string, BaseRepository<TParent, OwnerKey>>, options?: Omit<QueryOptions<TParent>, "where">): Promise<Map<TChild[MorphIdKey], TParent>>;
|
|
66
|
+
loadBelongsToManyForParents<TParent extends object, TRelated extends object, Pivot extends object, ParentKey extends keyof TParent & string, RelatedKey extends keyof TRelated & string, ForeignPivotKey extends keyof Pivot & string, RelatedPivotKey extends keyof Pivot & string>(parents: readonly TParent[], relation: BelongsToManyRelation<TParent, TRelated, Pivot, ParentKey, RelatedKey, ForeignPivotKey, RelatedPivotKey>, relatedRepository: BaseRepository<TRelated, RelatedKey>, options?: Omit<QueryOptions<TRelated>, "where">): Promise<Map<TParent[ParentKey], TRelated[]>>;
|
|
65
67
|
}
|
|
66
68
|
export { BaseRepository };
|
|
67
69
|
export default BaseRepository;
|
|
@@ -1,9 +1,51 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
type FactoryState<TRecord extends object> = Partial<TRecord> | ((record: TRecord) => Partial<TRecord>);
|
|
2
|
+
type FactorySequence<TRecord extends object> = Partial<TRecord> | ((index: number) => Partial<TRecord>);
|
|
3
|
+
type FactoryMakeResult<TRecord extends object, Counted extends boolean> = Counted extends true ? TRecord[] : TRecord;
|
|
4
|
+
declare class Factory<TRecord extends object, Counted extends boolean = false> {
|
|
5
|
+
private quantity;
|
|
6
|
+
private counted;
|
|
7
|
+
private sequenceIndex;
|
|
8
|
+
private stateTransforms;
|
|
9
|
+
private sequenceItems;
|
|
10
|
+
private parentAssociations;
|
|
11
|
+
private children;
|
|
12
|
+
private afterMakingCallbacks;
|
|
13
|
+
private afterCreatingCallbacks;
|
|
14
|
+
protected model?: {
|
|
15
|
+
create(attributes: Record<string, unknown>): Promise<{
|
|
16
|
+
toObject(): object;
|
|
17
|
+
}>;
|
|
18
|
+
};
|
|
3
19
|
protected definition(): TRecord;
|
|
4
|
-
|
|
5
|
-
|
|
20
|
+
protected clone(): this;
|
|
21
|
+
count(quantity: number): Factory<TRecord, true>;
|
|
22
|
+
state(state: FactoryState<TRecord>): Factory<TRecord, Counted>;
|
|
23
|
+
sequence(...items: Array<FactorySequence<TRecord>>): Factory<TRecord, Counted>;
|
|
24
|
+
for(parent: {
|
|
25
|
+
id?: unknown;
|
|
26
|
+
getRepository?: () => {
|
|
27
|
+
getTable(): {
|
|
28
|
+
name: string;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
}, foreignKey?: keyof TRecord & string): Factory<TRecord, Counted>;
|
|
32
|
+
recycle(parent: {
|
|
33
|
+
id?: unknown;
|
|
34
|
+
getRepository?: () => {
|
|
35
|
+
getTable(): {
|
|
36
|
+
name: string;
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
}, foreignKey?: keyof TRecord & string): Factory<TRecord, Counted>;
|
|
40
|
+
afterMaking(callback: (record: TRecord) => void): Factory<TRecord, Counted>;
|
|
41
|
+
afterCreating(callback: (record: TRecord) => void | Promise<void>): Factory<TRecord, Counted>;
|
|
42
|
+
has<TRelated extends object, RelatedCounted extends boolean>(factory: Factory<TRelated, RelatedCounted>, foreignKey?: keyof TRelated & string): Factory<TRecord, Counted>;
|
|
43
|
+
make(overrides?: Partial<TRecord>): FactoryMakeResult<TRecord, Counted>;
|
|
44
|
+
create(overrides?: Partial<TRecord>): Promise<FactoryMakeResult<TRecord, Counted>>;
|
|
45
|
+
protected makeOne(overrides?: Partial<TRecord>): TRecord;
|
|
46
|
+
protected createOne(overrides?: Partial<TRecord>): Promise<TRecord>;
|
|
6
47
|
protected insertable(record: TRecord): Partial<TRecord>;
|
|
7
|
-
protected persist(
|
|
48
|
+
protected persist(values: Partial<TRecord>): Promise<TRecord>;
|
|
8
49
|
}
|
|
50
|
+
export type { FactoryMakeResult };
|
|
9
51
|
export { Factory };
|
|
@@ -2,8 +2,10 @@ export type { DatabaseConnection } from "./baseRepository.ts";
|
|
|
2
2
|
export { default as BaseRepository } from "./baseRepository.ts";
|
|
3
3
|
export { createDatabaseConnection } from "./connection.ts";
|
|
4
4
|
export { mapDatabaseError, withDatabaseErrorHandling } from "./errors.ts";
|
|
5
|
+
export { Factory } from "./factory.ts";
|
|
6
|
+
export { foreignKeyFromTable, pivotTableName, singularize } from "./inflection.ts";
|
|
5
7
|
export type { CastType, GlobalScopeFn, ModelConstructor } from "./model.ts";
|
|
6
|
-
export { applyCasts, dehydrateValue, filterMassAssignable, hydrateValue, Model, registerModelRepository, } from "./model.ts";
|
|
8
|
+
export { applyCasts, BelongsToManyRelationQuery, BelongsToRelationQuery, dehydrateValue, filterMassAssignable, HasManyRelationQuery, HasOneRelationQuery, hydrateValue, Model, ModelQuery, MorphManyRelationQuery, MorphOneRelationQuery, MorphToRelationQuery, registerModelClass, registerModelRepository, } from "./model.ts";
|
|
7
9
|
export { buildAdvancedWhereClause, buildCountQuery, buildDeleteByIdQuery, buildGroupedCountQuery, buildInsertQuery, buildJoinClause, buildOrderByClause, buildProjectionQuery, buildQueryWhereClause, buildRestoreByIdQuery, buildSelectQuery, buildSoftDeleteByIdQuery, buildUpdateQuery, buildWhereClause, parseQualifiedColumn, qualifyColumn, quoteIdentifier, resolveQualifiedColumn, resolveSoftDeleteColumn, } from "./query.ts";
|
|
8
10
|
export type { BelongsToManyRelation, BelongsToRelation, HasManyRelation, HasOneRelation, MorphManyRelation, MorphOneRelation, MorphToRelation, } from "./relationships.ts";
|
|
9
11
|
export { belongsTo, belongsToMany, hasMany, hasOne, indexBelongsToManyRelation, indexBelongsToRelation, indexHasManyRelation, indexHasOneRelation, indexMorphManyRelation, indexMorphOneRelation, indexMorphToRelation, morphMany, morphOne, morphTo, } from "./relationships.ts";
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import type BaseRepository from "./baseRepository.ts";
|
|
2
|
+
import type { AnyRelationQuery, RelatedModelClass } from "./relationQuery.ts";
|
|
3
|
+
import { BelongsToManyRelationQuery, BelongsToRelationQuery, HasManyRelationQuery, HasOneRelationQuery, MorphManyRelationQuery, MorphOneRelationQuery, MorphToRelationQuery } from "./relationQuery.ts";
|
|
2
4
|
import type { BelongsToManyRelation, BelongsToRelation, HasManyRelation, HasOneRelation } from "./relationships.ts";
|
|
3
5
|
import type { RepositoryQuery } from "./repositoryQuery.ts";
|
|
4
6
|
import type { QueryOptions, QueryWhere } from "./types.ts";
|
|
5
|
-
type CastType = "date" | "datetime" | "json" | "bool" | "boolean";
|
|
7
|
+
type CastType = "date" | "datetime" | "json" | "bool" | "boolean" | "integer" | "int" | "hashed";
|
|
6
8
|
type LoadedAttributes = Record<string, unknown>;
|
|
7
9
|
type ModelCasts = Partial<Record<string, CastType>>;
|
|
8
10
|
type GlobalScopeFn<TEntity extends object, PrimaryKey extends keyof TEntity & string> = (query: RepositoryQuery<TEntity, PrimaryKey>) => RepositoryQuery<TEntity, PrimaryKey>;
|
|
@@ -10,10 +12,56 @@ type ModelClassType<TEntity extends object, PrimaryKey extends keyof TEntity & s
|
|
|
10
12
|
interface ModelConstructor<TEntity extends object, PrimaryKey extends keyof TEntity & string, TModel extends Model<TEntity, PrimaryKey>> extends ModelClassType<TEntity, PrimaryKey> {
|
|
11
13
|
new (attributes: TEntity, repository: BaseRepository<TEntity, PrimaryKey>, exists?: boolean): TModel;
|
|
12
14
|
}
|
|
15
|
+
type AnyModel = Model<Record<string, unknown>, "id">;
|
|
16
|
+
type RelatedRef<TRelated extends object, RelatedKey extends keyof TRelated & string> = RelatedModelClass<TRelated, RelatedKey> | string | (() => RelatedModelClass<TRelated, RelatedKey>);
|
|
17
|
+
type ModelObserver = {
|
|
18
|
+
retrieved?: (model: AnyModel) => unknown;
|
|
19
|
+
creating?: (model: AnyModel) => unknown;
|
|
20
|
+
created?: (model: AnyModel) => unknown;
|
|
21
|
+
updating?: (model: AnyModel) => unknown;
|
|
22
|
+
updated?: (model: AnyModel) => unknown;
|
|
23
|
+
saving?: (model: AnyModel) => unknown;
|
|
24
|
+
saved?: (model: AnyModel) => unknown;
|
|
25
|
+
deleting?: (model: AnyModel) => unknown;
|
|
26
|
+
deleted?: (model: AnyModel) => unknown;
|
|
27
|
+
};
|
|
28
|
+
declare function registerModelClass(name: string, model: object): void;
|
|
13
29
|
declare function hydrateValue(value: unknown, cast: CastType): unknown;
|
|
14
30
|
declare function dehydrateValue(value: unknown, cast: CastType): unknown;
|
|
15
31
|
declare function filterMassAssignable(fillable: readonly string[] | undefined, guarded: readonly string[] | true | undefined, input: LoadedAttributes): LoadedAttributes;
|
|
16
32
|
declare function applyCasts(values: LoadedAttributes, casts: ModelCasts, direction: "hydrate" | "dehydrate"): LoadedAttributes;
|
|
33
|
+
declare class ModelQuery {
|
|
34
|
+
private readonly modelClass;
|
|
35
|
+
readonly query: RepositoryQuery<Record<string, unknown>, "id">;
|
|
36
|
+
private readonly eager;
|
|
37
|
+
constructor(modelClass: object, query: RepositoryQuery<Record<string, unknown>, "id">);
|
|
38
|
+
with(...relations: string[]): this;
|
|
39
|
+
where(input: QueryWhere<object> | ((builder: import("./whereBuilder.ts").WhereBuilder<object>) => void)): this;
|
|
40
|
+
orWhere(input: QueryWhere<object> | ((builder: import("./whereBuilder.ts").WhereBuilder<object>) => void)): this;
|
|
41
|
+
orderBy(orderBy: QueryOptions<object>["orderBy"]): this;
|
|
42
|
+
limit(limit: number): this;
|
|
43
|
+
offset(offset: number): this;
|
|
44
|
+
whereNull(column: string): this;
|
|
45
|
+
whereIn(column: string, values: readonly unknown[]): this;
|
|
46
|
+
whereExists(sql: string, params?: readonly unknown[]): this;
|
|
47
|
+
whereNotExists(sql: string, params?: readonly unknown[]): this;
|
|
48
|
+
whereHas(name: string, constrain?: (query: AnyRelationQuery) => void): this;
|
|
49
|
+
has(name: string): this;
|
|
50
|
+
doesntHave(name: string): this;
|
|
51
|
+
whereDoesntHave(name: string, constrain?: (query: AnyRelationQuery) => void): this;
|
|
52
|
+
withHasMany(...args: Parameters<RepositoryQuery<Record<string, unknown>, "id">["withHasMany"]>): this;
|
|
53
|
+
withBelongsTo(...args: Parameters<RepositoryQuery<Record<string, unknown>, "id">["withBelongsTo"]>): this;
|
|
54
|
+
withBelongsToMany(...args: Parameters<RepositoryQuery<Record<string, unknown>, "id">["withBelongsToMany"]>): this;
|
|
55
|
+
withMorphMany(...args: Parameters<RepositoryQuery<Record<string, unknown>, "id">["withMorphMany"]>): this;
|
|
56
|
+
withMorphOne(...args: Parameters<RepositoryQuery<Record<string, unknown>, "id">["withMorphOne"]>): this;
|
|
57
|
+
withMorphTo(...args: Parameters<RepositoryQuery<Record<string, unknown>, "id">["withMorphTo"]>): this;
|
|
58
|
+
get(): Promise<Array<Model<Record<string, unknown>, "id">>>;
|
|
59
|
+
first(): Promise<Model<Record<string, unknown>, "id"> | null>;
|
|
60
|
+
find(id: unknown): Promise<Model<Record<string, unknown>, "id"> | null>;
|
|
61
|
+
findOrFail(id: unknown, errorFactory?: (id: unknown) => Error): Promise<Model<Record<string, unknown>, "id">>;
|
|
62
|
+
then(onfulfilled?: ((value: Array<Model<Record<string, unknown>, "id">>) => unknown) | null, onrejected?: ((reason: unknown) => unknown) | null): Promise<unknown>;
|
|
63
|
+
private constrainExists;
|
|
64
|
+
}
|
|
17
65
|
declare class Model<TEntity extends object, PrimaryKey extends keyof TEntity & string> {
|
|
18
66
|
protected attributes: TEntity;
|
|
19
67
|
protected readonly repository: BaseRepository<TEntity, PrimaryKey>;
|
|
@@ -21,26 +69,51 @@ declare class Model<TEntity extends object, PrimaryKey extends keyof TEntity & s
|
|
|
21
69
|
static $guarded?: readonly string[] | true;
|
|
22
70
|
static $casts: ModelCasts;
|
|
23
71
|
static $timestamps: boolean;
|
|
72
|
+
static $hidden?: readonly string[];
|
|
73
|
+
static $visible?: readonly string[];
|
|
74
|
+
static $appends?: readonly string[];
|
|
75
|
+
static $morphClass?: string;
|
|
24
76
|
private _exists;
|
|
77
|
+
private readonly loadedRelations;
|
|
78
|
+
private hiddenOverrides;
|
|
79
|
+
private visibleOverrides;
|
|
80
|
+
private appended;
|
|
25
81
|
constructor(attributes: TEntity, repository: BaseRepository<TEntity, PrimaryKey>, exists?: boolean);
|
|
82
|
+
getRepository(): BaseRepository<TEntity, PrimaryKey>;
|
|
26
83
|
get $exists(): boolean;
|
|
27
84
|
get<K extends keyof TEntity>(key: K): TEntity[K];
|
|
28
85
|
get id(): TEntity[PrimaryKey];
|
|
29
86
|
toObject(): TEntity;
|
|
87
|
+
toArray(): Record<string, unknown>;
|
|
88
|
+
toJSON(): Record<string, unknown>;
|
|
89
|
+
makeHidden(...keys: string[]): this;
|
|
90
|
+
makeVisible(...keys: string[]): this;
|
|
91
|
+
append(...keys: string[]): this;
|
|
30
92
|
protected primaryKey(): PrimaryKey;
|
|
31
93
|
protected static primaryKeyField(this: object): string;
|
|
32
94
|
protected static hydrateAttributes<TEntity extends object>(this: object, attributes: TEntity): TEntity;
|
|
33
95
|
protected static dehydrateAttributes(this: object, attributes: LoadedAttributes): LoadedAttributes;
|
|
34
96
|
protected static fromRecord<TEntity extends object, PrimaryKey extends keyof TEntity & string>(this: object, record: TEntity, repository: BaseRepository<TEntity, PrimaryKey>, exists?: boolean): Model<TEntity, PrimaryKey>;
|
|
35
97
|
static boot(): void;
|
|
98
|
+
static observe(this: object, observer: ModelObserver): void;
|
|
36
99
|
static addGlobalScope<TEntity extends object, PrimaryKey extends keyof TEntity & string>(this: object, _name: string, scope: GlobalScopeFn<TEntity, PrimaryKey>): void;
|
|
37
100
|
static repository<TEntity extends object, PrimaryKey extends keyof TEntity & string>(this: object): BaseRepository<TEntity, PrimaryKey>;
|
|
38
|
-
static query
|
|
39
|
-
static
|
|
101
|
+
static query(this: object): ModelQuery;
|
|
102
|
+
static newFromRecord(this: object, record: object, exists?: boolean): Model<Record<string, unknown>, "id">;
|
|
103
|
+
static create(this: object, attributes: Record<string, unknown>, forced?: Record<string, unknown>): Promise<Model<Record<string, unknown>, "id">>;
|
|
104
|
+
static with(this: object, ...relations: string[]): ModelQuery;
|
|
105
|
+
static whereHas(this: object, name: string, constrain?: (query: AnyRelationQuery) => void): ModelQuery;
|
|
106
|
+
static has(this: object, name: string): ModelQuery;
|
|
107
|
+
static doesntHave(this: object, name: string): ModelQuery;
|
|
108
|
+
static whereDoesntHave(this: object, name: string, constrain?: (query: AnyRelationQuery) => void): ModelQuery;
|
|
40
109
|
static find(this: object, id: unknown): Promise<Model<Record<string, unknown>, "id"> | null>;
|
|
41
110
|
static findOrFail(this: object, id: unknown, errorFactory?: (id: unknown) => Error): Promise<Model<Record<string, unknown>, "id">>;
|
|
42
111
|
static all(this: object, options?: Omit<QueryOptions<object>, "where">): Promise<Array<Model<Record<string, unknown>, "id">>>;
|
|
112
|
+
static where(this: object, where: QueryWhere<object>): ModelQuery;
|
|
43
113
|
static firstWhere(this: object, where: QueryWhere<object>, options?: Omit<QueryOptions<object>, "where">): Promise<Model<Record<string, unknown>, "id"> | null>;
|
|
114
|
+
static firstOrNew(this: object, where: QueryWhere<object>, values?: Record<string, unknown>): Promise<Model<Record<string, unknown>, "id">>;
|
|
115
|
+
static firstOrCreate(this: object, where: QueryWhere<object>, values?: Record<string, unknown>): Promise<Model<Record<string, unknown>, "id">>;
|
|
116
|
+
static updateOrCreate(this: object, where: QueryWhere<object>, values?: Record<string, unknown>): Promise<Model<Record<string, unknown>, "id">>;
|
|
44
117
|
save(): Promise<this>;
|
|
45
118
|
update(changes: Partial<TEntity>): Promise<this>;
|
|
46
119
|
delete(): Promise<boolean>;
|
|
@@ -50,8 +123,19 @@ declare class Model<TEntity extends object, PrimaryKey extends keyof TEntity & s
|
|
|
50
123
|
loadHasOne<TChild extends object, LocalKey extends keyof TEntity & string, ForeignKey extends keyof TChild & string, Alias extends string>(as: Alias, relation: HasOneRelation<TEntity, TChild, LocalKey, ForeignKey>, childRepository: BaseRepository<TChild, keyof TChild & string>, options?: Omit<QueryOptions<TChild>, "where">): Promise<this & Record<Alias, TChild | undefined>>;
|
|
51
124
|
loadBelongsTo<TParent extends object, ForeignKey extends keyof TEntity & string, OwnerKey extends keyof TParent & string, Alias extends string>(as: Alias, relation: BelongsToRelation<TEntity, TParent, ForeignKey, OwnerKey>, parentRepository: BaseRepository<TParent, OwnerKey>, options?: Omit<QueryOptions<TParent>, "where">): Promise<this & Record<Alias, TParent | undefined>>;
|
|
52
125
|
loadBelongsToMany<TRelated extends object, Pivot extends object, ParentKey extends keyof TEntity & string, RelatedKey extends keyof TRelated & string, ForeignPivotKey extends keyof Pivot & string, RelatedPivotKey extends keyof Pivot & string, Alias extends string>(as: Alias, relation: BelongsToManyRelation<TEntity, TRelated, Pivot, ParentKey, RelatedKey, ForeignPivotKey, RelatedPivotKey>, relatedRepository: BaseRepository<TRelated, RelatedKey>, options?: Omit<QueryOptions<TRelated>, "where">): Promise<this & Record<Alias, TRelated[]>>;
|
|
126
|
+
hasMany<TRelated extends object, RelatedKey extends keyof TRelated & string>(related: RelatedRef<TRelated, RelatedKey>, foreignKey?: keyof TRelated & string, localKey?: PrimaryKey): HasManyRelationQuery<TEntity, PrimaryKey, TRelated, RelatedKey>;
|
|
127
|
+
hasOne<TRelated extends object, RelatedKey extends keyof TRelated & string>(related: RelatedRef<TRelated, RelatedKey>, foreignKey?: keyof TRelated & string, localKey?: PrimaryKey): HasOneRelationQuery<TEntity, PrimaryKey, TRelated, RelatedKey>;
|
|
128
|
+
belongsTo<TRelated extends object, RelatedKey extends keyof TRelated & string>(related: RelatedRef<TRelated, RelatedKey>, foreignKey?: keyof TEntity & string, ownerKey?: RelatedKey): BelongsToRelationQuery<TEntity, PrimaryKey, TRelated, RelatedKey>;
|
|
129
|
+
belongsToMany<TRelated extends object, RelatedKey extends keyof TRelated & string, Pivot extends object = Record<string, unknown>>(related: RelatedRef<TRelated, RelatedKey>, pivotTable?: string, foreignPivotKey?: keyof Pivot & string, relatedPivotKey?: keyof Pivot & string): BelongsToManyRelationQuery<TEntity, PrimaryKey, TRelated, RelatedKey, Pivot>;
|
|
130
|
+
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>;
|
|
131
|
+
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>;
|
|
132
|
+
morphTo(relatedByType: Record<string, RelatedRef<Record<string, unknown>, "id">>, morphName?: string, typeKey?: keyof TEntity & string, idKey?: keyof TEntity & string): MorphToRelationQuery<TEntity, PrimaryKey>;
|
|
133
|
+
load(...names: string[]): Promise<this>;
|
|
134
|
+
loaded<T = unknown>(name: string): T | undefined;
|
|
135
|
+
setLoaded(name: string, value: unknown): this;
|
|
53
136
|
mergeAttributes(patch: Partial<TEntity>): this;
|
|
54
137
|
}
|
|
55
138
|
declare function registerModelRepository<TModelClass>(model: TModelClass, repository: object): TModelClass;
|
|
139
|
+
export { BelongsToManyRelationQuery, BelongsToRelationQuery, HasManyRelationQuery, HasOneRelationQuery, MorphManyRelationQuery, MorphOneRelationQuery, MorphToRelationQuery, } from "./relationQuery.ts";
|
|
56
140
|
export type { CastType, GlobalScopeFn, ModelClassType, ModelConstructor };
|
|
57
|
-
export { applyCasts, dehydrateValue, filterMassAssignable, hydrateValue, Model, registerModelRepository, };
|
|
141
|
+
export { applyCasts, dehydrateValue, filterMassAssignable, hydrateValue, Model, ModelQuery, registerModelClass, registerModelRepository, };
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import type BaseRepository from "./baseRepository.ts";
|
|
2
|
+
import type { BelongsToManyRelation, BelongsToRelation, HasManyRelation, HasOneRelation, MorphManyRelation, MorphOneRelation, MorphToRelation } from "./relationships.ts";
|
|
3
|
+
import type { RepositoryQuery } from "./repositoryQuery.ts";
|
|
4
|
+
import type { QueryOptions, QueryWhere } from "./types.ts";
|
|
5
|
+
type RelatedRecord = {
|
|
6
|
+
id: unknown;
|
|
7
|
+
get<K extends string>(key: K): unknown;
|
|
8
|
+
};
|
|
9
|
+
interface RelatedModelClass<TRelated extends object, RelatedKey extends keyof TRelated & string> {
|
|
10
|
+
name: string;
|
|
11
|
+
repository(): BaseRepository<TRelated, RelatedKey>;
|
|
12
|
+
create(attributes: Record<string, unknown>, forced?: Record<string, unknown>): Promise<RelatedRecord>;
|
|
13
|
+
newFromRecord(record: TRelated, exists?: boolean): RelatedRecord;
|
|
14
|
+
}
|
|
15
|
+
interface RelationHost<TEntity extends object, PrimaryKey extends keyof TEntity & string> {
|
|
16
|
+
readonly id: TEntity[PrimaryKey];
|
|
17
|
+
get<K extends keyof TEntity>(key: K): TEntity[K];
|
|
18
|
+
getRepository(): BaseRepository<TEntity, PrimaryKey>;
|
|
19
|
+
}
|
|
20
|
+
type RelationKind = "hasMany" | "hasOne" | "belongsTo" | "belongsToMany" | "morphMany" | "morphOne" | "morphTo";
|
|
21
|
+
type ExistsClause = {
|
|
22
|
+
sql: string;
|
|
23
|
+
params: unknown[];
|
|
24
|
+
};
|
|
25
|
+
declare class HasManyRelationQuery<TParent extends object, ParentKey extends keyof TParent & string, TChild extends object, ChildKey extends keyof TChild & string> {
|
|
26
|
+
private readonly parent;
|
|
27
|
+
private readonly related;
|
|
28
|
+
readonly relation: HasManyRelation<TParent, TChild, ParentKey, keyof TChild & string>;
|
|
29
|
+
readonly kind: RelationKind;
|
|
30
|
+
private extraWhere;
|
|
31
|
+
private extraOptions;
|
|
32
|
+
constructor(parent: RelationHost<TParent, ParentKey>, related: RelatedModelClass<TChild, ChildKey>, relation: HasManyRelation<TParent, TChild, ParentKey, keyof TChild & string>);
|
|
33
|
+
where(where: QueryWhere<TChild>): this;
|
|
34
|
+
orderBy(orderBy: QueryOptions<TChild>["orderBy"]): this;
|
|
35
|
+
limit(limit: number): this;
|
|
36
|
+
applyEagerLoad(query: RepositoryQuery<TParent, ParentKey>, alias: string): void;
|
|
37
|
+
hydrateEager(row: Record<string, unknown>, alias: string): unknown;
|
|
38
|
+
toExistsClause(parentTable: string): ExistsClause;
|
|
39
|
+
private scopedQuery;
|
|
40
|
+
get(): Promise<RelatedRecord[]>;
|
|
41
|
+
first(): Promise<RelatedRecord | null>;
|
|
42
|
+
count(): Promise<number>;
|
|
43
|
+
then(onfulfilled?: ((value: RelatedRecord[]) => unknown) | null, onrejected?: ((reason: unknown) => unknown) | null): Promise<unknown>;
|
|
44
|
+
create(attributes?: Record<string, unknown>): Promise<RelatedRecord>;
|
|
45
|
+
save(related: RelatedRecord | (Record<string, unknown> & {
|
|
46
|
+
save?: () => Promise<unknown>;
|
|
47
|
+
mergeAttributes?: (patch: Record<string, unknown>) => unknown;
|
|
48
|
+
})): Promise<RelatedRecord>;
|
|
49
|
+
createMany(records: ReadonlyArray<Record<string, unknown>>): Promise<RelatedRecord[]>;
|
|
50
|
+
}
|
|
51
|
+
declare class HasOneRelationQuery<TParent extends object, ParentKey extends keyof TParent & string, TChild extends object, ChildKey extends keyof TChild & string> {
|
|
52
|
+
readonly relation: HasOneRelation<TParent, TChild, ParentKey, keyof TChild & string>;
|
|
53
|
+
readonly kind: RelationKind;
|
|
54
|
+
private readonly inner;
|
|
55
|
+
constructor(parent: RelationHost<TParent, ParentKey>, related: RelatedModelClass<TChild, ChildKey>, relation: HasOneRelation<TParent, TChild, ParentKey, keyof TChild & string>);
|
|
56
|
+
where(where: QueryWhere<TChild>): this;
|
|
57
|
+
orderBy(orderBy: QueryOptions<TChild>["orderBy"]): this;
|
|
58
|
+
applyEagerLoad(query: RepositoryQuery<TParent, ParentKey>, alias: string): void;
|
|
59
|
+
hydrateEager(row: Record<string, unknown>, alias: string): unknown;
|
|
60
|
+
toExistsClause(parentTable: string): ExistsClause;
|
|
61
|
+
get(): Promise<RelatedRecord | null>;
|
|
62
|
+
first(): Promise<RelatedRecord | null>;
|
|
63
|
+
count(): Promise<number>;
|
|
64
|
+
then(onfulfilled?: ((value: RelatedRecord | null) => unknown) | null, onrejected?: ((reason: unknown) => unknown) | null): Promise<unknown>;
|
|
65
|
+
create(attributes?: Record<string, unknown>): Promise<RelatedRecord>;
|
|
66
|
+
save(related: RelatedRecord | Record<string, unknown>): Promise<RelatedRecord>;
|
|
67
|
+
}
|
|
68
|
+
declare class BelongsToRelationQuery<TChild extends object, ChildKey extends keyof TChild & string, TParent extends object, ParentKey extends keyof TParent & string> {
|
|
69
|
+
private readonly parent;
|
|
70
|
+
private readonly related;
|
|
71
|
+
readonly relation: BelongsToRelation<TChild, TParent, keyof TChild & string, ParentKey>;
|
|
72
|
+
readonly kind: RelationKind;
|
|
73
|
+
private extraWhere;
|
|
74
|
+
private extraOptions;
|
|
75
|
+
constructor(parent: RelationHost<TChild, ChildKey>, related: RelatedModelClass<TParent, ParentKey>, relation: BelongsToRelation<TChild, TParent, keyof TChild & string, ParentKey>);
|
|
76
|
+
where(where: QueryWhere<TParent>): this;
|
|
77
|
+
orderBy(orderBy: QueryOptions<TParent>["orderBy"]): this;
|
|
78
|
+
applyEagerLoad(query: RepositoryQuery<TChild, ChildKey>, alias: string): void;
|
|
79
|
+
hydrateEager(row: Record<string, unknown>, alias: string): unknown;
|
|
80
|
+
toExistsClause(parentTable: string): ExistsClause;
|
|
81
|
+
get(): Promise<RelatedRecord | null>;
|
|
82
|
+
first(): Promise<RelatedRecord | null>;
|
|
83
|
+
then(onfulfilled?: ((value: RelatedRecord | null) => unknown) | null, onrejected?: ((reason: unknown) => unknown) | null): Promise<unknown>;
|
|
84
|
+
associate(owner: {
|
|
85
|
+
id?: unknown;
|
|
86
|
+
} | RelatedRecord): Promise<void>;
|
|
87
|
+
dissociate(): Promise<void>;
|
|
88
|
+
}
|
|
89
|
+
declare class BelongsToManyRelationQuery<TParent extends object, ParentKey extends keyof TParent & string, TRelated extends object, RelatedKey extends keyof TRelated & string, Pivot extends object> {
|
|
90
|
+
private readonly parent;
|
|
91
|
+
private readonly related;
|
|
92
|
+
readonly relation: BelongsToManyRelation<TParent, TRelated, Pivot, ParentKey, RelatedKey, keyof Pivot & string, keyof Pivot & string>;
|
|
93
|
+
readonly kind: RelationKind;
|
|
94
|
+
private extraWhere;
|
|
95
|
+
private extraOptions;
|
|
96
|
+
private pivotValues;
|
|
97
|
+
constructor(parent: RelationHost<TParent, ParentKey>, related: RelatedModelClass<TRelated, RelatedKey>, relation: BelongsToManyRelation<TParent, TRelated, Pivot, ParentKey, RelatedKey, keyof Pivot & string, keyof Pivot & string>);
|
|
98
|
+
where(where: QueryWhere<TRelated>): this;
|
|
99
|
+
orderBy(orderBy: QueryOptions<TRelated>["orderBy"]): this;
|
|
100
|
+
applyEagerLoad(query: RepositoryQuery<TParent, ParentKey>, alias: string): void;
|
|
101
|
+
withPivotValues(values: Record<string, unknown>): this;
|
|
102
|
+
hydrateEager(row: Record<string, unknown>, alias: string): unknown;
|
|
103
|
+
toExistsClause(parentTable: string): ExistsClause;
|
|
104
|
+
private connection;
|
|
105
|
+
get(): Promise<RelatedRecord[]>;
|
|
106
|
+
first(): Promise<RelatedRecord | null>;
|
|
107
|
+
count(): Promise<number>;
|
|
108
|
+
then(onfulfilled?: ((value: RelatedRecord[]) => unknown) | null, onrejected?: ((reason: unknown) => unknown) | null): Promise<unknown>;
|
|
109
|
+
attach(ids: unknown | readonly unknown[]): Promise<void>;
|
|
110
|
+
toggle(ids: unknown | readonly unknown[]): Promise<void>;
|
|
111
|
+
detach(ids?: unknown | readonly unknown[]): Promise<void>;
|
|
112
|
+
sync(ids: readonly unknown[]): Promise<void>;
|
|
113
|
+
create(attributes?: Record<string, unknown>): Promise<RelatedRecord>;
|
|
114
|
+
}
|
|
115
|
+
declare class MorphManyRelationQuery<TParent extends object, ParentKey extends keyof TParent & string, TChild extends object, ChildKey extends keyof TChild & string> {
|
|
116
|
+
private readonly parent;
|
|
117
|
+
private readonly related;
|
|
118
|
+
readonly relation: MorphManyRelation<TParent, TChild, ParentKey, keyof TChild & string, keyof TChild & string>;
|
|
119
|
+
readonly kind: RelationKind;
|
|
120
|
+
private extraWhere;
|
|
121
|
+
private extraOptions;
|
|
122
|
+
constructor(parent: RelationHost<TParent, ParentKey>, related: RelatedModelClass<TChild, ChildKey>, relation: MorphManyRelation<TParent, TChild, ParentKey, keyof TChild & string, keyof TChild & string>);
|
|
123
|
+
where(where: QueryWhere<TChild>): this;
|
|
124
|
+
applyEagerLoad(query: RepositoryQuery<TParent, ParentKey>, alias: string): void;
|
|
125
|
+
hydrateEager(row: Record<string, unknown>, alias: string): unknown;
|
|
126
|
+
toExistsClause(parentTable: string): ExistsClause;
|
|
127
|
+
get(): Promise<RelatedRecord[]>;
|
|
128
|
+
first(): Promise<RelatedRecord | null>;
|
|
129
|
+
count(): Promise<number>;
|
|
130
|
+
then(onfulfilled?: ((value: RelatedRecord[]) => unknown) | null, onrejected?: ((reason: unknown) => unknown) | null): Promise<unknown>;
|
|
131
|
+
create(attributes?: Record<string, unknown>): Promise<RelatedRecord>;
|
|
132
|
+
}
|
|
133
|
+
declare class MorphOneRelationQuery<TParent extends object, ParentKey extends keyof TParent & string, TChild extends object, ChildKey extends keyof TChild & string> {
|
|
134
|
+
readonly relation: MorphOneRelation<TParent, TChild, ParentKey, keyof TChild & string, keyof TChild & string>;
|
|
135
|
+
readonly kind: RelationKind;
|
|
136
|
+
private readonly inner;
|
|
137
|
+
constructor(parent: RelationHost<TParent, ParentKey>, related: RelatedModelClass<TChild, ChildKey>, relation: MorphOneRelation<TParent, TChild, ParentKey, keyof TChild & string, keyof TChild & string>);
|
|
138
|
+
where(where: QueryWhere<TChild>): this;
|
|
139
|
+
applyEagerLoad(query: RepositoryQuery<TParent, ParentKey>, alias: string): void;
|
|
140
|
+
hydrateEager(row: Record<string, unknown>, alias: string): unknown;
|
|
141
|
+
toExistsClause(parentTable: string): ExistsClause;
|
|
142
|
+
get(): Promise<RelatedRecord | null>;
|
|
143
|
+
first(): Promise<RelatedRecord | null>;
|
|
144
|
+
count(): Promise<number>;
|
|
145
|
+
then(onfulfilled?: ((value: RelatedRecord | null) => unknown) | null, onrejected?: ((reason: unknown) => unknown) | null): Promise<unknown>;
|
|
146
|
+
create(attributes?: Record<string, unknown>): Promise<RelatedRecord>;
|
|
147
|
+
}
|
|
148
|
+
declare class MorphToRelationQuery<TChild extends object, ChildKey extends keyof TChild & string> {
|
|
149
|
+
private readonly parent;
|
|
150
|
+
private readonly relatedByType;
|
|
151
|
+
readonly relation: MorphToRelation<TChild, keyof TChild & string, keyof TChild & string>;
|
|
152
|
+
readonly kind: RelationKind;
|
|
153
|
+
private extraWhere;
|
|
154
|
+
constructor(parent: RelationHost<TChild, ChildKey>, relatedByType: Record<string, RelatedModelClass<Record<string, unknown>, "id">>, relation: MorphToRelation<TChild, keyof TChild & string, keyof TChild & string>);
|
|
155
|
+
where(where: QueryWhere<Record<string, unknown>>): this;
|
|
156
|
+
applyEagerLoad(query: RepositoryQuery<TChild, ChildKey>, alias: string): void;
|
|
157
|
+
hydrateEager(row: Record<string, unknown>, alias: string): unknown;
|
|
158
|
+
toExistsClause(parentTable: string): ExistsClause;
|
|
159
|
+
get(): Promise<RelatedRecord | null>;
|
|
160
|
+
then(onfulfilled?: ((value: RelatedRecord | null) => unknown) | null, onrejected?: ((reason: unknown) => unknown) | null): Promise<unknown>;
|
|
161
|
+
}
|
|
162
|
+
type AnyRelationQuery = {
|
|
163
|
+
kind: RelationKind;
|
|
164
|
+
applyEagerLoad(query: unknown, alias: string): void;
|
|
165
|
+
hydrateEager(row: Record<string, unknown>, alias: string): unknown;
|
|
166
|
+
get(): Promise<unknown>;
|
|
167
|
+
toExistsClause(parentTable: string): ExistsClause;
|
|
168
|
+
where?(where: Record<string, unknown>): unknown;
|
|
169
|
+
then?: (onfulfilled?: ((value: unknown) => unknown) | null, onrejected?: ((reason: unknown) => unknown) | null) => Promise<unknown>;
|
|
170
|
+
};
|
|
171
|
+
export type { AnyRelationQuery, RelatedModelClass, RelatedRecord, RelationHost };
|
|
172
|
+
export { BelongsToManyRelationQuery, BelongsToRelationQuery, HasManyRelationQuery, HasOneRelationQuery, MorphManyRelationQuery, MorphOneRelationQuery, MorphToRelationQuery, };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { PaginatedResult } from "../pagination/index.ts";
|
|
2
2
|
import type BaseRepository from "./baseRepository.ts";
|
|
3
|
-
import type { BelongsToRelation, HasManyRelation, MorphManyRelation, MorphOneRelation, MorphToRelation } from "./relationships.ts";
|
|
3
|
+
import type { BelongsToManyRelation, BelongsToRelation, HasManyRelation, MorphManyRelation, MorphOneRelation, MorphToRelation } from "./relationships.ts";
|
|
4
4
|
import type { QueryOptions, QueryWhere } from "./types.ts";
|
|
5
5
|
import { WhereBuilder } from "./whereBuilder.ts";
|
|
6
6
|
type LoadedRow = Record<string, unknown>;
|
|
@@ -15,6 +15,11 @@ declare class RepositoryQuery<TEntity extends object, PrimaryKey extends keyof T
|
|
|
15
15
|
orWhere(input: QueryWhere<TEntity> | ((builder: WhereBuilder<TEntity>) => void)): this;
|
|
16
16
|
orderBy(orderBy: QueryOptions<TEntity>["orderBy"]): this;
|
|
17
17
|
limit(limit: number): this;
|
|
18
|
+
whereNull(column: keyof TEntity & string): this;
|
|
19
|
+
whereNotNull(column: keyof TEntity & string): this;
|
|
20
|
+
whereIn(column: keyof TEntity & string, values: readonly unknown[]): this;
|
|
21
|
+
whereExists(sql: string, params?: readonly unknown[]): this;
|
|
22
|
+
whereNotExists(sql: string, params?: readonly unknown[]): this;
|
|
18
23
|
offset(offset: number): this;
|
|
19
24
|
join(left: `${string}.${string}`, right: `${string}.${string}`): this;
|
|
20
25
|
leftJoin(left: `${string}.${string}`, right: `${string}.${string}`): this;
|
|
@@ -25,8 +30,11 @@ declare class RepositoryQuery<TEntity extends object, PrimaryKey extends keyof T
|
|
|
25
30
|
withMorphMany<TChild extends object, LocalKey extends keyof TEntity & string, MorphTypeKey extends keyof TChild & string, MorphIdKey extends keyof TChild & string, Alias extends string>(as: Alias, relation: MorphManyRelation<TEntity, TChild, LocalKey, MorphTypeKey, MorphIdKey>, childRepository: BaseRepository<TChild, keyof TChild & string>, options?: Omit<QueryOptions<TChild>, "where">): this;
|
|
26
31
|
withMorphOne<TChild extends object, LocalKey extends keyof TEntity & string, MorphTypeKey extends keyof TChild & string, MorphIdKey extends keyof TChild & string, Alias extends string>(as: Alias, relation: MorphOneRelation<TEntity, TChild, LocalKey, MorphTypeKey, MorphIdKey>, childRepository: BaseRepository<TChild, keyof TChild & string>, options?: Omit<QueryOptions<TChild>, "where">): this;
|
|
27
32
|
withMorphTo<TParent extends object, MorphTypeKey extends keyof TEntity & string, MorphIdKey extends keyof TEntity & string, Alias extends string>(as: Alias, relation: MorphToRelation<TEntity, MorphTypeKey, MorphIdKey>, repositoriesByType: ReadonlyMap<string, BaseRepository<TParent, keyof TParent & string>>, options?: Omit<QueryOptions<TParent>, "where">): this;
|
|
33
|
+
withBelongsToMany<TRelated extends object, Pivot extends object, ParentKey extends keyof TEntity & string, RelatedKey extends keyof TRelated & string, ForeignPivotKey extends keyof Pivot & string, RelatedPivotKey extends keyof Pivot & string, Alias extends string>(as: Alias, relation: BelongsToManyRelation<TEntity, TRelated, Pivot, ParentKey, RelatedKey, ForeignPivotKey, RelatedPivotKey>, relatedRepository: BaseRepository<TRelated, RelatedKey>, options?: Omit<QueryOptions<TRelated>, "where">): this;
|
|
28
34
|
get(): Promise<Array<TEntity & LoadedRow>>;
|
|
29
35
|
first(): Promise<(TEntity & LoadedRow) | null>;
|
|
36
|
+
count(): Promise<number>;
|
|
37
|
+
attachToRows(rows: readonly TEntity[]): Promise<Array<TEntity & LoadedRow>>;
|
|
30
38
|
paginate(options: {
|
|
31
39
|
page: number;
|
|
32
40
|
perPage: number;
|
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
import type { QueryWhere } from "./types.ts";
|
|
2
|
+
type ExistsClause = {
|
|
3
|
+
sql: string;
|
|
4
|
+
params: readonly unknown[];
|
|
5
|
+
not?: boolean;
|
|
6
|
+
};
|
|
2
7
|
type WhereNode<TEntity extends object> = {
|
|
3
8
|
kind: "and" | "or";
|
|
4
9
|
where: QueryWhere<TEntity>;
|
|
5
10
|
} | {
|
|
6
11
|
kind: "and" | "or";
|
|
7
12
|
group: WhereNode<TEntity>[];
|
|
13
|
+
} | {
|
|
14
|
+
kind: "and" | "or";
|
|
15
|
+
exists: ExistsClause;
|
|
8
16
|
};
|
|
9
17
|
declare class WhereBuilder<TEntity extends object> {
|
|
10
18
|
readonly nodes: WhereNode<TEntity>[];
|
|
@@ -13,5 +21,5 @@ declare class WhereBuilder<TEntity extends object> {
|
|
|
13
21
|
whereGroup(fn: (builder: WhereBuilder<TEntity>) => void): this;
|
|
14
22
|
orWhereGroup(fn: (builder: WhereBuilder<TEntity>) => void): this;
|
|
15
23
|
}
|
|
16
|
-
export type { WhereNode };
|
|
24
|
+
export type { ExistsClause, WhereNode };
|
|
17
25
|
export { WhereBuilder };
|
|
@@ -2,7 +2,9 @@ type EventListener = (payload: unknown) => void | Promise<void>;
|
|
|
2
2
|
declare class EventBus {
|
|
3
3
|
constructor();
|
|
4
4
|
private readonly listeners;
|
|
5
|
+
on(event: string, listener: EventListener): () => void;
|
|
5
6
|
listen(event: string, listener: EventListener): () => void;
|
|
7
|
+
emit(event: string, payload: unknown): Promise<void>;
|
|
6
8
|
dispatch(event: string, payload: unknown): Promise<void>;
|
|
7
9
|
}
|
|
8
10
|
declare function readSharedEventBus(): EventBus;
|
|
@@ -1,7 +1,27 @@
|
|
|
1
1
|
declare function serializeDate(value: Date | string): string;
|
|
2
|
+
declare function whenLoaded<T>(model: {
|
|
3
|
+
loaded: (name: string) => unknown;
|
|
4
|
+
}, relation: string, transform?: (value: unknown) => T): T | undefined;
|
|
5
|
+
declare class JsonResource<T = unknown> {
|
|
6
|
+
protected readonly resource: T;
|
|
7
|
+
static wrap: string | null;
|
|
8
|
+
protected extra: Record<string, unknown>;
|
|
9
|
+
constructor(resource: T);
|
|
10
|
+
static make<TResource>(resource: TResource): JsonResource<TResource>;
|
|
11
|
+
static collection<TResource>(items: readonly TResource[]): ResourceCollection<TResource>;
|
|
12
|
+
additional(data: Record<string, unknown>): this;
|
|
13
|
+
when<TValue>(condition: boolean, value: TValue): TValue | undefined;
|
|
14
|
+
whenLoaded<TValue = unknown>(relation: string, transform?: (value: unknown) => TValue): TValue | undefined;
|
|
15
|
+
toArray(): Record<string, unknown>;
|
|
16
|
+
toResponse(): Record<string, unknown>;
|
|
17
|
+
}
|
|
18
|
+
declare class ResourceCollection<T> extends JsonResource<readonly T[]> {
|
|
19
|
+
toArray(): Record<string, unknown>;
|
|
20
|
+
toResponse(): Record<string, unknown>;
|
|
21
|
+
}
|
|
2
22
|
declare function toResourceCollection<TInput, TOutput>(items: readonly TInput[], transformer: (item: TInput) => TOutput): TOutput[];
|
|
3
23
|
declare function toPaginatedResourceCollection<TInput, TOutput, TMeta extends object>(items: readonly TInput[], meta: TMeta, transformer: (item: TInput) => TOutput): {
|
|
4
24
|
data: TOutput[];
|
|
5
25
|
meta: TMeta;
|
|
6
26
|
};
|
|
7
|
-
export { serializeDate, toPaginatedResourceCollection, toResourceCollection };
|
|
27
|
+
export { JsonResource, ResourceCollection, serializeDate, toPaginatedResourceCollection, toResourceCollection, whenLoaded, };
|
|
@@ -39,6 +39,12 @@ class ServiceContainer {
|
|
|
39
39
|
resolve(key) {
|
|
40
40
|
return this.get(key);
|
|
41
41
|
}
|
|
42
|
+
make(key) {
|
|
43
|
+
return this.resolve(key);
|
|
44
|
+
}
|
|
45
|
+
instance(key, value) {
|
|
46
|
+
return this.set(key, value);
|
|
47
|
+
}
|
|
42
48
|
has(key) {
|
|
43
49
|
return this.services.has(key) || this.singletonFactories.has(key) || this.bindings.has(key);
|
|
44
50
|
}
|