@orieljs/database 0.1.0-alpha.13 → 0.1.0-alpha.15

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.d.ts CHANGED
@@ -600,6 +600,7 @@ declare class Connection implements DatabaseConnection {
600
600
  protected readonly db: Kysely<any>;
601
601
  private readonly context;
602
602
  private readonly pinned;
603
+ private savepoints;
603
604
  private loggingQueries;
604
605
  private queryLog;
605
606
  constructor(name: string, config: ConnectionConfig);
@@ -834,7 +835,12 @@ declare class Grammar {
834
835
  parameter(value: unknown): Sql;
835
836
  operator(operator: string): string;
836
837
  columnize(columns: ReadonlyArray<string | Expression>): Sql;
837
- compileSelect(query: Builder$1<unknown>): Sql;
838
+ compileSelect(query: Builder$1<unknown>, extraColumn?: Sql): Sql;
839
+ /**
840
+ * Laravel 11's compileGroupLimit(): the query numbers its rows per `column` in its own order, and an outer query keeps
841
+ * those inside the limit and offset, in row-number order. SQLite 3.25+, MySQL 8 and PostgreSQL have window functions.
842
+ */
843
+ compileGroupLimit(query: Builder$1<unknown>): Sql;
838
844
  /** Everything after the select list: from, joins, where, group by, having, order by, limit/offset, lock. */
839
845
  compileComponents(query: Builder$1<unknown>): Sql;
840
846
  compileOrders(query: Builder$1<unknown>): Sql | undefined;
@@ -858,7 +864,10 @@ declare class Grammar {
858
864
  compileHavings(havings: readonly Having[]): Sql | undefined;
859
865
  compileLimitOffset(limit: number | undefined, offset: number | undefined): Sql | undefined;
860
866
  compileLock(mode: 'update' | 'share' | undefined): Sql | undefined;
861
- /** `select count(*) as aggregate …`. Orders, limit and offset are dropped; grouped or distinct queries are wrapped. */
867
+ /**
868
+ * `select count(*) as aggregate …`. Orders, limit and offset are dropped. Grouped or distinct queries, and those with a
869
+ * `having` (which may name a selected alias, withCount()'s `comments_count`), are wrapped, keeping their columns.
870
+ */
862
871
  compileAggregate(query: Builder$1<unknown>, fn: string, column: string | Expression): Sql;
863
872
  compileExists(query: Builder$1<unknown>): Sql;
864
873
  compileInsert(query: Builder$1<unknown>, rows: readonly Row[]): Sql;
@@ -986,6 +995,12 @@ declare class Builder$1<T = Row> implements QueryBuilder<T> {
986
995
  orders: Order[];
987
996
  limitValue: number | undefined;
988
997
  offsetValue: number | undefined;
998
+ /** A limit and offset per value of `column` (each parent of an eager load), compiled as a window (limitPerGroup()). */
999
+ groupLimit: {
1000
+ column: string;
1001
+ limit: number | undefined;
1002
+ offset: number | undefined;
1003
+ } | undefined;
989
1004
  lockMode: 'update' | 'share' | undefined;
990
1005
  constructor(connection: Connection, table?: string | Expression);
991
1006
  from(table: string | Expression): this;
@@ -1096,6 +1111,11 @@ declare class Builder$1<T = Row> implements QueryBuilder<T> {
1096
1111
  protected newCollection(items: T[]): Collection<T>;
1097
1112
  /** The raw rows, without a Collection around them. */
1098
1113
  runSelect(): Promise<T[]>;
1114
+ /**
1115
+ * @internal Eager loading (Laravel 11's groupLimit()): the limit and offset set so far apply to each value of `column`,
1116
+ * the parent's key, instead of to the whole query. Compiled as row_number() over (partition by …).
1117
+ */
1118
+ limitPerGroup(column: string): this;
1099
1119
  first(): Promise<T | undefined>;
1100
1120
  find(id: unknown, keyName?: string): Promise<T | undefined>;
1101
1121
  value<V = unknown>(column: string): Promise<V | undefined>;
@@ -1217,6 +1237,8 @@ declare class DatabaseManagerImpl implements DatabaseManager {
1217
1237
  private readonly config;
1218
1238
  private readonly connections;
1219
1239
  private defaultName;
1240
+ /** usingConnection()'s name, for its own async context only: requests running beside it keep theirs. */
1241
+ private readonly using;
1220
1242
  /** Pass the config, or a function that reads it (the provider reads config('database') lazily). */
1221
1243
  constructor(config: DatabaseConfig | (() => DatabaseConfig));
1222
1244
  private settings;
@@ -1226,8 +1248,8 @@ declare class DatabaseManagerImpl implements DatabaseManager {
1226
1248
  /** Overrides config('database.default'); undefined clears the override. */
1227
1249
  setDefaultConnection(name: string | undefined): void;
1228
1250
  /**
1229
- * Runs `callback` with `name` as the default connection (Laravel's usingConnection()), then restores the previous
1230
- * override, or none, so the default follows config('database.default') again. Without a name, just runs it.
1251
+ * Runs `callback` with `name` as the default connection (Laravel's usingConnection()), for the callback's own async
1252
+ * context only: other requests running meanwhile keep their default. Without a name, just runs it.
1231
1253
  */
1232
1254
  usingConnection<T>(name: string | undefined, callback: () => Promise<T>): Promise<T>;
1233
1255
  /** The connections opened so far. */
@@ -1383,12 +1405,6 @@ declare class EloquentCollection<M extends Model = Model> extends Collection<M>
1383
1405
  toQuery(): Builder<M>;
1384
1406
  }
1385
1407
 
1386
- /**
1387
- * A relation is the related model's query builder, constrained to the parent: post.comments() is a
1388
- * Builder<Comment> with `where comments.post_id = ?`, so every builder method (where, orderBy, paginate, a local
1389
- * scope, …) works on it. Subclasses add eager loading (addEagerConstraints/initRelation/match) and the existence
1390
- * queries behind has()/whereHas()/withCount().
1391
- */
1392
1408
  declare abstract class Relation<R extends Model = Model, P extends Model = Model, Result = unknown> extends Builder<R> {
1393
1409
  readonly parent: P;
1394
1410
  /** The name the relation is loaded under (set when resolved by name, for eager loading and associate()). */
@@ -1399,6 +1415,20 @@ declare abstract class Relation<R extends Model = Model, P extends Model = Model
1399
1415
  protected constructor(related: R, parent: P);
1400
1416
  getParent(): P;
1401
1417
  getRelated(): R;
1418
+ /**
1419
+ * When the related table is the parent's own (a category's children, a user's followers), the existence query reads it
1420
+ * under an alias, `oriel_reserved_<n>`, so its columns aren't the outer row's. The related model takes the alias as its
1421
+ * table, so its qualified columns (keys, soft deletes, a constraint's qualifyColumn()) follow. Returns the alias, or
1422
+ * undefined when the tables differ.
1423
+ */
1424
+ protected aliasSelfTable(query: Builder<R>): string | undefined;
1425
+ /**
1426
+ * @internal After an eager load's constraint: a limit() or offset() set there applies to each parent, as in Laravel 11,
1427
+ * on the relations that know the column holding the parent's key.
1428
+ */
1429
+ limitEagerPerParent(): void;
1430
+ /** The column of the eager query holding each row's parent key; undefined where a per-parent limit makes no sense. */
1431
+ protected eagerPartitionColumn(): string | undefined;
1402
1432
  /** Subclasses call this last in their constructor: adds the per-parent constraints unless eager loading. */
1403
1433
  protected init(): void;
1404
1434
  /** The constraints for one parent: where comments.post_id = post.id. */
@@ -1609,7 +1639,11 @@ declare class Builder<M extends Model = Model> extends Builder$1<M> {
1609
1639
  pluck<V = unknown>(column: Column<M>): Promise<Collection<V>>;
1610
1640
  /** Whether `id` can't be one of the model's unique ids (not a UUID for HasUuids): it matches no row. */
1611
1641
  protected isMalformedKey(id: unknown): boolean;
1612
- /** A malformed unique id (HasUuids, HasUlids) finds nothing without a query, as route model binding does. */
1642
+ /**
1643
+ * A model by its key, or undefined; with an array of keys, the models found, as a collection (Laravel's find()). A
1644
+ * malformed unique id (HasUuids, HasUlids) finds nothing without a query, as route model binding does.
1645
+ */
1646
+ find(ids: readonly unknown[]): Promise<EloquentCollection<M>>;
1613
1647
  find(id: unknown): Promise<M | undefined>;
1614
1648
  findMany(ids: readonly unknown[]): Promise<EloquentCollection<M>>;
1615
1649
  /** find(), or ModelNotFoundError (a 404). With an array, every id must exist. */
@@ -1703,8 +1737,9 @@ interface HasRelationship {
1703
1737
  interface ForRelationship {
1704
1738
  parent: RelatedFactory | Model;
1705
1739
  relationship: string;
1706
- resolved?: Model;
1707
1740
  }
1741
+ /** The for() parents one make()/create()/raw() call has resolved: shared by that call's models, made again by the next. */
1742
+ type ResolvedParents = Map<ForRelationship, Model>;
1708
1743
  /** The builder state newInstance() copies and changes. */
1709
1744
  interface FactoryState<M extends Model> {
1710
1745
  countValue: number | undefined;
@@ -1748,7 +1783,11 @@ declare abstract class Factory<M extends Model = Model> {
1748
1783
  trashed(): this;
1749
1784
  /** Creates related models for each created model: has(Post.factory().count(3)) (relationship default: 'posts'). */
1750
1785
  has(factory: RelatedFactory, relationship?: string, pivot?: Row): this;
1751
- /** Sets a belongsTo parent: for(User.factory()) or for(user) (relationship default: 'user'). Created once. */
1786
+ /**
1787
+ * Sets a belongsTo parent: for(User.factory()) or for(user) (relationship default: 'user'). A factory parent is made
1788
+ * once per make()/create() call, for all of its models, and again by the next call, so a factory kept in a variable
1789
+ * never points at a parent an earlier test rolled back.
1790
+ */
1752
1791
  for(parent: RelatedFactory | Model, relationship?: string): this;
1753
1792
  afterMaking(callback: (model: M) => unknown): this;
1754
1793
  afterCreating(callback: (model: M) => unknown): this;
@@ -1765,7 +1804,7 @@ declare abstract class Factory<M extends Model = Model> {
1765
1804
  protected newInstance(changes: Partial<FactoryState<M>>): this;
1766
1805
  protected makeModels(attributes: Row): Promise<M[]>;
1767
1806
  /** definition() + states + overrides + for() parents, with factory and function values expanded. */
1768
- protected attributesFor(overrides: Row, index: number): Promise<Row>;
1807
+ protected attributesFor(overrides: Row, index: number, parents?: ResolvedParents): Promise<Row>;
1769
1808
  private parentAttributes;
1770
1809
  private createChildren;
1771
1810
  private relationOn;
@@ -1848,6 +1887,7 @@ declare class BelongsToMany<R extends Model, P extends Model = Model> extends Re
1848
1887
  getResults(): Promise<EloquentCollection<R>>;
1849
1888
  initRelation(models: P[], relation: string): P[];
1850
1889
  match(models: P[], results: R[], relation: string): P[];
1890
+ protected eagerPartitionColumn(): string;
1851
1891
  getRelationExistenceQuery(query: Builder<R>, _parentQuery: Builder<P>, columns?: ColumnRef[]): Builder<R>;
1852
1892
  /** Inserts pivot rows for ids (or models, or { id: attributes }), with shared extra attributes. */
1853
1893
  attach(ids: PivotIds, attributes?: Row): Promise<void>;
@@ -1942,6 +1982,7 @@ declare abstract class HasOneOrMany<R extends Model, P extends Model, Result> ex
1942
1982
  protected setForeignAttributes(model: R): R;
1943
1983
  saveMany(models: Iterable<R>): Promise<R[]>;
1944
1984
  createMany(records: Row[]): Promise<R[]>;
1985
+ protected eagerPartitionColumn(): string;
1945
1986
  getRelationExistenceQuery(query: Builder<R>, _parentQuery: Builder<P>, columns?: ColumnRef[]): Builder<R>;
1946
1987
  }
1947
1988
 
@@ -2211,6 +2252,7 @@ declare class Model implements ModelInstance {
2211
2252
  static onlyTrashed<C extends typeof Model>(this: C): ModelBuilder<C>;
2212
2253
  static withoutTrashed<C extends typeof Model>(this: C): ModelBuilder<C>;
2213
2254
  static all<C extends typeof Model>(this: C): Promise<EloquentCollection<Instance<C>>>;
2255
+ static find<C extends typeof Model>(this: C, ids: readonly unknown[]): Promise<EloquentCollection<Instance<C>>>;
2214
2256
  static find<C extends typeof Model>(this: C, id: unknown): Promise<Instance<C> | undefined>;
2215
2257
  static findOrFail<C extends typeof Model>(this: C, ids: readonly unknown[]): Promise<EloquentCollection<Instance<C>>>;
2216
2258
  static findOrFail<C extends typeof Model>(this: C, id: unknown): Promise<Instance<C>>;