@getstrata/core 0.5.1 → 0.5.4

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.
@@ -12,12 +12,12 @@ declare function buildWhereClause<TEntity extends object>(tableName: string, whe
12
12
  clause: string;
13
13
  params: unknown[];
14
14
  };
15
- declare function buildAdvancedWhereClause<TEntity extends object>(tableName: string, where?: QueryWhere<TEntity>, whereNodes?: readonly WhereNode<TEntity>[]): {
15
+ declare function buildAdvancedWhereClause<TEntity extends object>(tableName: string, where?: QueryWhere<TEntity>, whereNodes?: readonly WhereNode<TEntity>[], params?: unknown[]): {
16
16
  clause: string;
17
17
  params: unknown[];
18
18
  };
19
19
  declare function resolveSoftDeleteColumn<TEntity>(table: TableDefinition<TEntity>): string | null;
20
- declare function buildQueryWhereClause<TEntity extends object>(table: TableDefinition<TEntity>, options?: Pick<QueryOptions<TEntity>, "where" | "withTrashed" | "onlyTrashed">, whereNodes?: readonly WhereNode<TEntity>[]): {
20
+ declare function buildQueryWhereClause<TEntity extends object>(table: TableDefinition<TEntity>, options?: Pick<QueryOptions<TEntity>, "where" | "withTrashed" | "onlyTrashed">, whereNodes?: readonly WhereNode<TEntity>[], params?: unknown[]): {
21
21
  clause: string;
22
22
  params: unknown[];
23
23
  };
@@ -508,8 +508,7 @@ function buildWhereGroupClause(tableName, nodes, params) {
508
508
  }
509
509
  return result;
510
510
  }
511
- function buildAdvancedWhereClause(tableName, where = {}, whereNodes = []) {
512
- const params = [];
511
+ function buildAdvancedWhereClause(tableName, where = {}, whereNodes = [], params = []) {
513
512
  const nodes = [];
514
513
  if (Object.keys(where).length > 0) {
515
514
  nodes.push({ kind: "and", where });
@@ -544,18 +543,18 @@ function appendSoftDeleteScope(table, options, clauses) {
544
543
  clauses.push(`${qualifiedColumn} IS NULL`);
545
544
  }
546
545
  }
547
- function buildQueryWhereClause(table, options = {}, whereNodes = []) {
548
- const { clause, params } = buildAdvancedWhereClause(table.name, options.where ?? {}, whereNodes);
546
+ function buildQueryWhereClause(table, options = {}, whereNodes = [], params = []) {
547
+ const { clause, params: whereParams } = buildAdvancedWhereClause(table.name, options.where ?? {}, whereNodes, params);
549
548
  const softDeleteClauses = [];
550
549
  appendSoftDeleteScope(table, options, softDeleteClauses);
551
550
  if (softDeleteClauses.length === 0) {
552
- return { clause, params };
551
+ return { clause, params: whereParams };
553
552
  }
554
553
  const base = clause.replace(/^ WHERE /, "");
555
554
  const scope = softDeleteClauses.join(" AND ");
556
555
  return {
557
556
  clause: base ? ` WHERE (${base}) AND ${scope}` : ` WHERE ${scope}`,
558
- params
557
+ params: whereParams
559
558
  };
560
559
  }
561
560
  function isQueryOrder(value) {
@@ -659,8 +658,7 @@ function getDefinedColumnEntries(table, values, options = {}) {
659
658
  function buildSelectQuery(table, options = {}, whereNodes = []) {
660
659
  const params = [];
661
660
  const columns = buildSelectList(table, options.select, params);
662
- const { clause, params: whereParams } = buildQueryWhereClause(table, options, whereNodes);
663
- params.push(...whereParams);
661
+ const { clause } = buildQueryWhereClause(table, options, whereNodes, params);
664
662
  const joins = buildJoinClause(options.joins);
665
663
  const groupBy = buildGroupByClause(table.name, options.groupBy);
666
664
  const havingClause = buildHavingClause(table.name, options.having, params);
@@ -1557,26 +1555,26 @@ class Model {
1557
1555
  throw new Error(`${this.constructor.name}.primaryKey() is not implemented.`);
1558
1556
  }
1559
1557
  static primaryKeyField() {
1560
- return resolveModelRepository(Model).getTable().primaryKey;
1558
+ return resolveModelRepository(this).getTable().primaryKey;
1561
1559
  }
1562
1560
  static hydrateAttributes(attributes) {
1563
- const casts = modelStatics(Model).$casts ?? {};
1561
+ const casts = modelStatics(this).$casts ?? {};
1564
1562
  return applyCasts(attributes, casts, "hydrate");
1565
1563
  }
1566
1564
  static dehydrateAttributes(attributes) {
1567
- const casts = modelStatics(Model).$casts ?? {};
1565
+ const casts = modelStatics(this).$casts ?? {};
1568
1566
  return applyCasts(attributes, casts, "dehydrate");
1569
1567
  }
1570
1568
  static fromRecord(record, repository, exists = true) {
1571
- const statics = modelStatics(Model);
1569
+ const statics = modelStatics(this);
1572
1570
  const hydrated = statics.hydrateAttributes(record);
1573
1571
  return new statics(hydrated, repository, exists);
1574
1572
  }
1575
1573
  static boot() {}
1576
1574
  static addGlobalScope(_name, scope) {
1577
- ensureBooted(Model);
1578
- const existing = modelGlobalScopes.get(Model) ?? [];
1579
- modelGlobalScopes.set(Model, [
1575
+ ensureBooted(this);
1576
+ const existing = modelGlobalScopes.get(this) ?? [];
1577
+ modelGlobalScopes.set(this, [
1580
1578
  ...existing,
1581
1579
  scope
1582
1580
  ]);
@@ -1586,17 +1584,17 @@ class Model {
1586
1584
  }
1587
1585
  static query() {
1588
1586
  ensureBooted(this);
1589
- const repository = resolveModelRepository(Model);
1587
+ const repository = resolveModelRepository(this);
1590
1588
  let query = repository.query();
1591
- for (const scope of getGlobalScopes(Model)) {
1589
+ for (const scope of getGlobalScopes(this)) {
1592
1590
  query = scope(query);
1593
1591
  }
1594
1592
  return query;
1595
1593
  }
1596
1594
  static async create(attributes) {
1597
1595
  const statics = modelStatics(this);
1598
- ensureBooted(Model);
1599
- const repository = resolveModelRepository(Model);
1596
+ ensureBooted(this);
1597
+ const repository = resolveModelRepository(this);
1600
1598
  const table = repository.getTable();
1601
1599
  const timestamps = statics.$timestamps ?? true;
1602
1600
  const assignable = filterMassAssignable(statics.$fillable, statics.$guarded, attributes);
@@ -1607,22 +1605,22 @@ class Model {
1607
1605
  }
1608
1606
  static async find(id) {
1609
1607
  const statics = modelStatics(this);
1610
- const repository = resolveModelRepository(Model);
1611
- const primaryKey = statics.primaryKeyField();
1612
- const record = await Model.query.call(Model).where({ [primaryKey]: id }).first();
1608
+ const repository = resolveModelRepository(this);
1609
+ const primaryKey = repository.getTable().primaryKey;
1610
+ const record = await Model.query.call(this).where({ [primaryKey]: id }).first();
1613
1611
  return record ? statics.fromRecord(record, repository, true) : null;
1614
1612
  }
1615
1613
  static async findOrFail(id, errorFactory) {
1616
- const model = await Model.find.call(Model, id);
1614
+ const model = await Model.find.call(this, id);
1617
1615
  if (model) {
1618
1616
  return model;
1619
1617
  }
1620
- throw errorFactory?.(id) ?? new NotFoundError(`${Model.name} ${String(id)} not found.`);
1618
+ throw errorFactory?.(id) ?? new NotFoundError(`${this.name} ${String(id)} not found.`);
1621
1619
  }
1622
1620
  static async all(options = {}) {
1623
1621
  const statics = modelStatics(this);
1624
- const repository = resolveModelRepository(Model);
1625
- let query = Model.query.call(Model);
1622
+ const repository = resolveModelRepository(this);
1623
+ let query = Model.query.call(this);
1626
1624
  if (options.orderBy) {
1627
1625
  query = query.orderBy(options.orderBy);
1628
1626
  }
@@ -1634,8 +1632,8 @@ class Model {
1634
1632
  }
1635
1633
  static async firstWhere(where, options = {}) {
1636
1634
  const statics = modelStatics(this);
1637
- const repository = resolveModelRepository(Model);
1638
- let query = Model.query.call(Model).where(where);
1635
+ const repository = resolveModelRepository(this);
1636
+ let query = Model.query.call(this).where(where);
1639
1637
  if (options.orderBy) {
1640
1638
  query = query.orderBy(options.orderBy);
1641
1639
  }
@@ -306,8 +306,7 @@ function buildWhereGroupClause(tableName, nodes, params) {
306
306
  }
307
307
  return result;
308
308
  }
309
- function buildAdvancedWhereClause(tableName, where = {}, whereNodes = []) {
310
- const params = [];
309
+ function buildAdvancedWhereClause(tableName, where = {}, whereNodes = [], params = []) {
311
310
  const nodes = [];
312
311
  if (Object.keys(where).length > 0) {
313
312
  nodes.push({ kind: "and", where });
@@ -342,18 +341,18 @@ function appendSoftDeleteScope(table, options, clauses) {
342
341
  clauses.push(`${qualifiedColumn} IS NULL`);
343
342
  }
344
343
  }
345
- function buildQueryWhereClause(table, options = {}, whereNodes = []) {
346
- const { clause, params } = buildAdvancedWhereClause(table.name, options.where ?? {}, whereNodes);
344
+ function buildQueryWhereClause(table, options = {}, whereNodes = [], params = []) {
345
+ const { clause, params: whereParams } = buildAdvancedWhereClause(table.name, options.where ?? {}, whereNodes, params);
347
346
  const softDeleteClauses = [];
348
347
  appendSoftDeleteScope(table, options, softDeleteClauses);
349
348
  if (softDeleteClauses.length === 0) {
350
- return { clause, params };
349
+ return { clause, params: whereParams };
351
350
  }
352
351
  const base = clause.replace(/^ WHERE /, "");
353
352
  const scope = softDeleteClauses.join(" AND ");
354
353
  return {
355
354
  clause: base ? ` WHERE (${base}) AND ${scope}` : ` WHERE ${scope}`,
356
- params
355
+ params: whereParams
357
356
  };
358
357
  }
359
358
  function isQueryOrder(value) {
@@ -457,8 +456,7 @@ function getDefinedColumnEntries(table, values, options = {}) {
457
456
  function buildSelectQuery(table, options = {}, whereNodes = []) {
458
457
  const params = [];
459
458
  const columns = buildSelectList(table, options.select, params);
460
- const { clause, params: whereParams } = buildQueryWhereClause(table, options, whereNodes);
461
- params.push(...whereParams);
459
+ const { clause } = buildQueryWhereClause(table, options, whereNodes, params);
462
460
  const joins = buildJoinClause(options.joins);
463
461
  const groupBy = buildGroupByClause(table.name, options.groupBy);
464
462
  const havingClause = buildHavingClause(table.name, options.having, params);
@@ -1355,26 +1353,26 @@ class Model {
1355
1353
  throw new Error(`${this.constructor.name}.primaryKey() is not implemented.`);
1356
1354
  }
1357
1355
  static primaryKeyField() {
1358
- return resolveModelRepository(Model).getTable().primaryKey;
1356
+ return resolveModelRepository(this).getTable().primaryKey;
1359
1357
  }
1360
1358
  static hydrateAttributes(attributes) {
1361
- const casts = modelStatics(Model).$casts ?? {};
1359
+ const casts = modelStatics(this).$casts ?? {};
1362
1360
  return applyCasts(attributes, casts, "hydrate");
1363
1361
  }
1364
1362
  static dehydrateAttributes(attributes) {
1365
- const casts = modelStatics(Model).$casts ?? {};
1363
+ const casts = modelStatics(this).$casts ?? {};
1366
1364
  return applyCasts(attributes, casts, "dehydrate");
1367
1365
  }
1368
1366
  static fromRecord(record, repository, exists = true) {
1369
- const statics = modelStatics(Model);
1367
+ const statics = modelStatics(this);
1370
1368
  const hydrated = statics.hydrateAttributes(record);
1371
1369
  return new statics(hydrated, repository, exists);
1372
1370
  }
1373
1371
  static boot() {}
1374
1372
  static addGlobalScope(_name, scope) {
1375
- ensureBooted(Model);
1376
- const existing = modelGlobalScopes.get(Model) ?? [];
1377
- modelGlobalScopes.set(Model, [
1373
+ ensureBooted(this);
1374
+ const existing = modelGlobalScopes.get(this) ?? [];
1375
+ modelGlobalScopes.set(this, [
1378
1376
  ...existing,
1379
1377
  scope
1380
1378
  ]);
@@ -1384,17 +1382,17 @@ class Model {
1384
1382
  }
1385
1383
  static query() {
1386
1384
  ensureBooted(this);
1387
- const repository = resolveModelRepository(Model);
1385
+ const repository = resolveModelRepository(this);
1388
1386
  let query = repository.query();
1389
- for (const scope of getGlobalScopes(Model)) {
1387
+ for (const scope of getGlobalScopes(this)) {
1390
1388
  query = scope(query);
1391
1389
  }
1392
1390
  return query;
1393
1391
  }
1394
1392
  static async create(attributes) {
1395
1393
  const statics = modelStatics(this);
1396
- ensureBooted(Model);
1397
- const repository = resolveModelRepository(Model);
1394
+ ensureBooted(this);
1395
+ const repository = resolveModelRepository(this);
1398
1396
  const table = repository.getTable();
1399
1397
  const timestamps = statics.$timestamps ?? true;
1400
1398
  const assignable = filterMassAssignable(statics.$fillable, statics.$guarded, attributes);
@@ -1405,22 +1403,22 @@ class Model {
1405
1403
  }
1406
1404
  static async find(id) {
1407
1405
  const statics = modelStatics(this);
1408
- const repository = resolveModelRepository(Model);
1409
- const primaryKey = statics.primaryKeyField();
1410
- const record = await Model.query.call(Model).where({ [primaryKey]: id }).first();
1406
+ const repository = resolveModelRepository(this);
1407
+ const primaryKey = repository.getTable().primaryKey;
1408
+ const record = await Model.query.call(this).where({ [primaryKey]: id }).first();
1411
1409
  return record ? statics.fromRecord(record, repository, true) : null;
1412
1410
  }
1413
1411
  static async findOrFail(id, errorFactory) {
1414
- const model = await Model.find.call(Model, id);
1412
+ const model = await Model.find.call(this, id);
1415
1413
  if (model) {
1416
1414
  return model;
1417
1415
  }
1418
- throw errorFactory?.(id) ?? new NotFoundError(`${Model.name} ${String(id)} not found.`);
1416
+ throw errorFactory?.(id) ?? new NotFoundError(`${this.name} ${String(id)} not found.`);
1419
1417
  }
1420
1418
  static async all(options = {}) {
1421
1419
  const statics = modelStatics(this);
1422
- const repository = resolveModelRepository(Model);
1423
- let query = Model.query.call(Model);
1420
+ const repository = resolveModelRepository(this);
1421
+ let query = Model.query.call(this);
1424
1422
  if (options.orderBy) {
1425
1423
  query = query.orderBy(options.orderBy);
1426
1424
  }
@@ -1432,8 +1430,8 @@ class Model {
1432
1430
  }
1433
1431
  static async firstWhere(where, options = {}) {
1434
1432
  const statics = modelStatics(this);
1435
- const repository = resolveModelRepository(Model);
1436
- let query = Model.query.call(Model).where(where);
1433
+ const repository = resolveModelRepository(this);
1434
+ let query = Model.query.call(this).where(where);
1437
1435
  if (options.orderBy) {
1438
1436
  query = query.orderBy(options.orderBy);
1439
1437
  }
@@ -530,8 +530,7 @@ function buildWhereGroupClause(tableName, nodes, params) {
530
530
  }
531
531
  return result;
532
532
  }
533
- function buildAdvancedWhereClause(tableName, where = {}, whereNodes = []) {
534
- const params = [];
533
+ function buildAdvancedWhereClause(tableName, where = {}, whereNodes = [], params = []) {
535
534
  const nodes = [];
536
535
  if (Object.keys(where).length > 0) {
537
536
  nodes.push({ kind: "and", where });
@@ -566,18 +565,18 @@ function appendSoftDeleteScope(table, options, clauses) {
566
565
  clauses.push(`${qualifiedColumn} IS NULL`);
567
566
  }
568
567
  }
569
- function buildQueryWhereClause(table, options = {}, whereNodes = []) {
570
- const { clause, params } = buildAdvancedWhereClause(table.name, options.where ?? {}, whereNodes);
568
+ function buildQueryWhereClause(table, options = {}, whereNodes = [], params = []) {
569
+ const { clause, params: whereParams } = buildAdvancedWhereClause(table.name, options.where ?? {}, whereNodes, params);
571
570
  const softDeleteClauses = [];
572
571
  appendSoftDeleteScope(table, options, softDeleteClauses);
573
572
  if (softDeleteClauses.length === 0) {
574
- return { clause, params };
573
+ return { clause, params: whereParams };
575
574
  }
576
575
  const base = clause.replace(/^ WHERE /, "");
577
576
  const scope = softDeleteClauses.join(" AND ");
578
577
  return {
579
578
  clause: base ? ` WHERE (${base}) AND ${scope}` : ` WHERE ${scope}`,
580
- params
579
+ params: whereParams
581
580
  };
582
581
  }
583
582
  function isQueryOrder(value) {
@@ -681,8 +680,7 @@ function getDefinedColumnEntries(table, values, options = {}) {
681
680
  function buildSelectQuery(table, options = {}, whereNodes = []) {
682
681
  const params = [];
683
682
  const columns = buildSelectList(table, options.select, params);
684
- const { clause, params: whereParams } = buildQueryWhereClause(table, options, whereNodes);
685
- params.push(...whereParams);
683
+ const { clause } = buildQueryWhereClause(table, options, whereNodes, params);
686
684
  const joins = buildJoinClause(options.joins);
687
685
  const groupBy = buildGroupByClause(table.name, options.groupBy);
688
686
  const havingClause = buildHavingClause(table.name, options.having, params);
@@ -1579,26 +1577,26 @@ class Model {
1579
1577
  throw new Error(`${this.constructor.name}.primaryKey() is not implemented.`);
1580
1578
  }
1581
1579
  static primaryKeyField() {
1582
- return resolveModelRepository(Model).getTable().primaryKey;
1580
+ return resolveModelRepository(this).getTable().primaryKey;
1583
1581
  }
1584
1582
  static hydrateAttributes(attributes) {
1585
- const casts = modelStatics(Model).$casts ?? {};
1583
+ const casts = modelStatics(this).$casts ?? {};
1586
1584
  return applyCasts(attributes, casts, "hydrate");
1587
1585
  }
1588
1586
  static dehydrateAttributes(attributes) {
1589
- const casts = modelStatics(Model).$casts ?? {};
1587
+ const casts = modelStatics(this).$casts ?? {};
1590
1588
  return applyCasts(attributes, casts, "dehydrate");
1591
1589
  }
1592
1590
  static fromRecord(record, repository, exists = true) {
1593
- const statics = modelStatics(Model);
1591
+ const statics = modelStatics(this);
1594
1592
  const hydrated = statics.hydrateAttributes(record);
1595
1593
  return new statics(hydrated, repository, exists);
1596
1594
  }
1597
1595
  static boot() {}
1598
1596
  static addGlobalScope(_name, scope) {
1599
- ensureBooted(Model);
1600
- const existing = modelGlobalScopes.get(Model) ?? [];
1601
- modelGlobalScopes.set(Model, [
1597
+ ensureBooted(this);
1598
+ const existing = modelGlobalScopes.get(this) ?? [];
1599
+ modelGlobalScopes.set(this, [
1602
1600
  ...existing,
1603
1601
  scope
1604
1602
  ]);
@@ -1608,17 +1606,17 @@ class Model {
1608
1606
  }
1609
1607
  static query() {
1610
1608
  ensureBooted(this);
1611
- const repository = resolveModelRepository(Model);
1609
+ const repository = resolveModelRepository(this);
1612
1610
  let query = repository.query();
1613
- for (const scope of getGlobalScopes(Model)) {
1611
+ for (const scope of getGlobalScopes(this)) {
1614
1612
  query = scope(query);
1615
1613
  }
1616
1614
  return query;
1617
1615
  }
1618
1616
  static async create(attributes) {
1619
1617
  const statics = modelStatics(this);
1620
- ensureBooted(Model);
1621
- const repository = resolveModelRepository(Model);
1618
+ ensureBooted(this);
1619
+ const repository = resolveModelRepository(this);
1622
1620
  const table = repository.getTable();
1623
1621
  const timestamps = statics.$timestamps ?? true;
1624
1622
  const assignable = filterMassAssignable(statics.$fillable, statics.$guarded, attributes);
@@ -1629,22 +1627,22 @@ class Model {
1629
1627
  }
1630
1628
  static async find(id) {
1631
1629
  const statics = modelStatics(this);
1632
- const repository = resolveModelRepository(Model);
1633
- const primaryKey = statics.primaryKeyField();
1634
- const record = await Model.query.call(Model).where({ [primaryKey]: id }).first();
1630
+ const repository = resolveModelRepository(this);
1631
+ const primaryKey = repository.getTable().primaryKey;
1632
+ const record = await Model.query.call(this).where({ [primaryKey]: id }).first();
1635
1633
  return record ? statics.fromRecord(record, repository, true) : null;
1636
1634
  }
1637
1635
  static async findOrFail(id, errorFactory) {
1638
- const model = await Model.find.call(Model, id);
1636
+ const model = await Model.find.call(this, id);
1639
1637
  if (model) {
1640
1638
  return model;
1641
1639
  }
1642
- throw errorFactory?.(id) ?? new NotFoundError(`${Model.name} ${String(id)} not found.`);
1640
+ throw errorFactory?.(id) ?? new NotFoundError(`${this.name} ${String(id)} not found.`);
1643
1641
  }
1644
1642
  static async all(options = {}) {
1645
1643
  const statics = modelStatics(this);
1646
- const repository = resolveModelRepository(Model);
1647
- let query = Model.query.call(Model);
1644
+ const repository = resolveModelRepository(this);
1645
+ let query = Model.query.call(this);
1648
1646
  if (options.orderBy) {
1649
1647
  query = query.orderBy(options.orderBy);
1650
1648
  }
@@ -1656,8 +1654,8 @@ class Model {
1656
1654
  }
1657
1655
  static async firstWhere(where, options = {}) {
1658
1656
  const statics = modelStatics(this);
1659
- const repository = resolveModelRepository(Model);
1660
- let query = Model.query.call(Model).where(where);
1657
+ const repository = resolveModelRepository(this);
1658
+ let query = Model.query.call(this).where(where);
1661
1659
  if (options.orderBy) {
1662
1660
  query = query.orderBy(options.orderBy);
1663
1661
  }
@@ -530,8 +530,7 @@ function buildWhereGroupClause(tableName, nodes, params) {
530
530
  }
531
531
  return result;
532
532
  }
533
- function buildAdvancedWhereClause(tableName, where = {}, whereNodes = []) {
534
- const params = [];
533
+ function buildAdvancedWhereClause(tableName, where = {}, whereNodes = [], params = []) {
535
534
  const nodes = [];
536
535
  if (Object.keys(where).length > 0) {
537
536
  nodes.push({ kind: "and", where });
@@ -566,18 +565,18 @@ function appendSoftDeleteScope(table, options, clauses) {
566
565
  clauses.push(`${qualifiedColumn} IS NULL`);
567
566
  }
568
567
  }
569
- function buildQueryWhereClause(table, options = {}, whereNodes = []) {
570
- const { clause, params } = buildAdvancedWhereClause(table.name, options.where ?? {}, whereNodes);
568
+ function buildQueryWhereClause(table, options = {}, whereNodes = [], params = []) {
569
+ const { clause, params: whereParams } = buildAdvancedWhereClause(table.name, options.where ?? {}, whereNodes, params);
571
570
  const softDeleteClauses = [];
572
571
  appendSoftDeleteScope(table, options, softDeleteClauses);
573
572
  if (softDeleteClauses.length === 0) {
574
- return { clause, params };
573
+ return { clause, params: whereParams };
575
574
  }
576
575
  const base = clause.replace(/^ WHERE /, "");
577
576
  const scope = softDeleteClauses.join(" AND ");
578
577
  return {
579
578
  clause: base ? ` WHERE (${base}) AND ${scope}` : ` WHERE ${scope}`,
580
- params
579
+ params: whereParams
581
580
  };
582
581
  }
583
582
  function isQueryOrder(value) {
@@ -681,8 +680,7 @@ function getDefinedColumnEntries(table, values, options = {}) {
681
680
  function buildSelectQuery(table, options = {}, whereNodes = []) {
682
681
  const params = [];
683
682
  const columns = buildSelectList(table, options.select, params);
684
- const { clause, params: whereParams } = buildQueryWhereClause(table, options, whereNodes);
685
- params.push(...whereParams);
683
+ const { clause } = buildQueryWhereClause(table, options, whereNodes, params);
686
684
  const joins = buildJoinClause(options.joins);
687
685
  const groupBy = buildGroupByClause(table.name, options.groupBy);
688
686
  const havingClause = buildHavingClause(table.name, options.having, params);
@@ -1579,26 +1577,26 @@ class Model {
1579
1577
  throw new Error(`${this.constructor.name}.primaryKey() is not implemented.`);
1580
1578
  }
1581
1579
  static primaryKeyField() {
1582
- return resolveModelRepository(Model).getTable().primaryKey;
1580
+ return resolveModelRepository(this).getTable().primaryKey;
1583
1581
  }
1584
1582
  static hydrateAttributes(attributes) {
1585
- const casts = modelStatics(Model).$casts ?? {};
1583
+ const casts = modelStatics(this).$casts ?? {};
1586
1584
  return applyCasts(attributes, casts, "hydrate");
1587
1585
  }
1588
1586
  static dehydrateAttributes(attributes) {
1589
- const casts = modelStatics(Model).$casts ?? {};
1587
+ const casts = modelStatics(this).$casts ?? {};
1590
1588
  return applyCasts(attributes, casts, "dehydrate");
1591
1589
  }
1592
1590
  static fromRecord(record, repository, exists = true) {
1593
- const statics = modelStatics(Model);
1591
+ const statics = modelStatics(this);
1594
1592
  const hydrated = statics.hydrateAttributes(record);
1595
1593
  return new statics(hydrated, repository, exists);
1596
1594
  }
1597
1595
  static boot() {}
1598
1596
  static addGlobalScope(_name, scope) {
1599
- ensureBooted(Model);
1600
- const existing = modelGlobalScopes.get(Model) ?? [];
1601
- modelGlobalScopes.set(Model, [
1597
+ ensureBooted(this);
1598
+ const existing = modelGlobalScopes.get(this) ?? [];
1599
+ modelGlobalScopes.set(this, [
1602
1600
  ...existing,
1603
1601
  scope
1604
1602
  ]);
@@ -1608,17 +1606,17 @@ class Model {
1608
1606
  }
1609
1607
  static query() {
1610
1608
  ensureBooted(this);
1611
- const repository = resolveModelRepository(Model);
1609
+ const repository = resolveModelRepository(this);
1612
1610
  let query = repository.query();
1613
- for (const scope of getGlobalScopes(Model)) {
1611
+ for (const scope of getGlobalScopes(this)) {
1614
1612
  query = scope(query);
1615
1613
  }
1616
1614
  return query;
1617
1615
  }
1618
1616
  static async create(attributes) {
1619
1617
  const statics = modelStatics(this);
1620
- ensureBooted(Model);
1621
- const repository = resolveModelRepository(Model);
1618
+ ensureBooted(this);
1619
+ const repository = resolveModelRepository(this);
1622
1620
  const table = repository.getTable();
1623
1621
  const timestamps = statics.$timestamps ?? true;
1624
1622
  const assignable = filterMassAssignable(statics.$fillable, statics.$guarded, attributes);
@@ -1629,22 +1627,22 @@ class Model {
1629
1627
  }
1630
1628
  static async find(id) {
1631
1629
  const statics = modelStatics(this);
1632
- const repository = resolveModelRepository(Model);
1633
- const primaryKey = statics.primaryKeyField();
1634
- const record = await Model.query.call(Model).where({ [primaryKey]: id }).first();
1630
+ const repository = resolveModelRepository(this);
1631
+ const primaryKey = repository.getTable().primaryKey;
1632
+ const record = await Model.query.call(this).where({ [primaryKey]: id }).first();
1635
1633
  return record ? statics.fromRecord(record, repository, true) : null;
1636
1634
  }
1637
1635
  static async findOrFail(id, errorFactory) {
1638
- const model = await Model.find.call(Model, id);
1636
+ const model = await Model.find.call(this, id);
1639
1637
  if (model) {
1640
1638
  return model;
1641
1639
  }
1642
- throw errorFactory?.(id) ?? new NotFoundError(`${Model.name} ${String(id)} not found.`);
1640
+ throw errorFactory?.(id) ?? new NotFoundError(`${this.name} ${String(id)} not found.`);
1643
1641
  }
1644
1642
  static async all(options = {}) {
1645
1643
  const statics = modelStatics(this);
1646
- const repository = resolveModelRepository(Model);
1647
- let query = Model.query.call(Model);
1644
+ const repository = resolveModelRepository(this);
1645
+ let query = Model.query.call(this);
1648
1646
  if (options.orderBy) {
1649
1647
  query = query.orderBy(options.orderBy);
1650
1648
  }
@@ -1656,8 +1654,8 @@ class Model {
1656
1654
  }
1657
1655
  static async firstWhere(where, options = {}) {
1658
1656
  const statics = modelStatics(this);
1659
- const repository = resolveModelRepository(Model);
1660
- let query = Model.query.call(Model).where(where);
1657
+ const repository = resolveModelRepository(this);
1658
+ let query = Model.query.call(this).where(where);
1661
1659
  if (options.orderBy) {
1662
1660
  query = query.orderBy(options.orderBy);
1663
1661
  }
@@ -771,8 +771,7 @@ function buildWhereGroupClause(tableName, nodes, params) {
771
771
  }
772
772
  return result;
773
773
  }
774
- function buildAdvancedWhereClause(tableName, where = {}, whereNodes = []) {
775
- const params = [];
774
+ function buildAdvancedWhereClause(tableName, where = {}, whereNodes = [], params = []) {
776
775
  const nodes = [];
777
776
  if (Object.keys(where).length > 0) {
778
777
  nodes.push({ kind: "and", where });
@@ -807,18 +806,18 @@ function appendSoftDeleteScope(table, options, clauses) {
807
806
  clauses.push(`${qualifiedColumn} IS NULL`);
808
807
  }
809
808
  }
810
- function buildQueryWhereClause(table, options = {}, whereNodes = []) {
811
- const { clause, params } = buildAdvancedWhereClause(table.name, options.where ?? {}, whereNodes);
809
+ function buildQueryWhereClause(table, options = {}, whereNodes = [], params = []) {
810
+ const { clause, params: whereParams } = buildAdvancedWhereClause(table.name, options.where ?? {}, whereNodes, params);
812
811
  const softDeleteClauses = [];
813
812
  appendSoftDeleteScope(table, options, softDeleteClauses);
814
813
  if (softDeleteClauses.length === 0) {
815
- return { clause, params };
814
+ return { clause, params: whereParams };
816
815
  }
817
816
  const base = clause.replace(/^ WHERE /, "");
818
817
  const scope = softDeleteClauses.join(" AND ");
819
818
  return {
820
819
  clause: base ? ` WHERE (${base}) AND ${scope}` : ` WHERE ${scope}`,
821
- params
820
+ params: whereParams
822
821
  };
823
822
  }
824
823
  function isQueryOrder(value) {
@@ -922,8 +921,7 @@ function getDefinedColumnEntries(table, values, options = {}) {
922
921
  function buildSelectQuery(table, options = {}, whereNodes = []) {
923
922
  const params = [];
924
923
  const columns = buildSelectList(table, options.select, params);
925
- const { clause, params: whereParams } = buildQueryWhereClause(table, options, whereNodes);
926
- params.push(...whereParams);
924
+ const { clause } = buildQueryWhereClause(table, options, whereNodes, params);
927
925
  const joins = buildJoinClause(options.joins);
928
926
  const groupBy = buildGroupByClause(table.name, options.groupBy);
929
927
  const havingClause = buildHavingClause(table.name, options.having, params);
@@ -1750,26 +1748,26 @@ class Model {
1750
1748
  throw new Error(`${this.constructor.name}.primaryKey() is not implemented.`);
1751
1749
  }
1752
1750
  static primaryKeyField() {
1753
- return resolveModelRepository(Model).getTable().primaryKey;
1751
+ return resolveModelRepository(this).getTable().primaryKey;
1754
1752
  }
1755
1753
  static hydrateAttributes(attributes) {
1756
- const casts = modelStatics(Model).$casts ?? {};
1754
+ const casts = modelStatics(this).$casts ?? {};
1757
1755
  return applyCasts(attributes, casts, "hydrate");
1758
1756
  }
1759
1757
  static dehydrateAttributes(attributes) {
1760
- const casts = modelStatics(Model).$casts ?? {};
1758
+ const casts = modelStatics(this).$casts ?? {};
1761
1759
  return applyCasts(attributes, casts, "dehydrate");
1762
1760
  }
1763
1761
  static fromRecord(record, repository, exists = true) {
1764
- const statics = modelStatics(Model);
1762
+ const statics = modelStatics(this);
1765
1763
  const hydrated = statics.hydrateAttributes(record);
1766
1764
  return new statics(hydrated, repository, exists);
1767
1765
  }
1768
1766
  static boot() {}
1769
1767
  static addGlobalScope(_name, scope) {
1770
- ensureBooted(Model);
1771
- const existing = modelGlobalScopes.get(Model) ?? [];
1772
- modelGlobalScopes.set(Model, [
1768
+ ensureBooted(this);
1769
+ const existing = modelGlobalScopes.get(this) ?? [];
1770
+ modelGlobalScopes.set(this, [
1773
1771
  ...existing,
1774
1772
  scope
1775
1773
  ]);
@@ -1779,17 +1777,17 @@ class Model {
1779
1777
  }
1780
1778
  static query() {
1781
1779
  ensureBooted(this);
1782
- const repository = resolveModelRepository(Model);
1780
+ const repository = resolveModelRepository(this);
1783
1781
  let query = repository.query();
1784
- for (const scope of getGlobalScopes(Model)) {
1782
+ for (const scope of getGlobalScopes(this)) {
1785
1783
  query = scope(query);
1786
1784
  }
1787
1785
  return query;
1788
1786
  }
1789
1787
  static async create(attributes) {
1790
1788
  const statics = modelStatics(this);
1791
- ensureBooted(Model);
1792
- const repository = resolveModelRepository(Model);
1789
+ ensureBooted(this);
1790
+ const repository = resolveModelRepository(this);
1793
1791
  const table = repository.getTable();
1794
1792
  const timestamps = statics.$timestamps ?? true;
1795
1793
  const assignable = filterMassAssignable(statics.$fillable, statics.$guarded, attributes);
@@ -1800,22 +1798,22 @@ class Model {
1800
1798
  }
1801
1799
  static async find(id) {
1802
1800
  const statics = modelStatics(this);
1803
- const repository = resolveModelRepository(Model);
1804
- const primaryKey = statics.primaryKeyField();
1805
- const record = await Model.query.call(Model).where({ [primaryKey]: id }).first();
1801
+ const repository = resolveModelRepository(this);
1802
+ const primaryKey = repository.getTable().primaryKey;
1803
+ const record = await Model.query.call(this).where({ [primaryKey]: id }).first();
1806
1804
  return record ? statics.fromRecord(record, repository, true) : null;
1807
1805
  }
1808
1806
  static async findOrFail(id, errorFactory) {
1809
- const model = await Model.find.call(Model, id);
1807
+ const model = await Model.find.call(this, id);
1810
1808
  if (model) {
1811
1809
  return model;
1812
1810
  }
1813
- throw errorFactory?.(id) ?? new NotFoundError(`${Model.name} ${String(id)} not found.`);
1811
+ throw errorFactory?.(id) ?? new NotFoundError(`${this.name} ${String(id)} not found.`);
1814
1812
  }
1815
1813
  static async all(options = {}) {
1816
1814
  const statics = modelStatics(this);
1817
- const repository = resolveModelRepository(Model);
1818
- let query = Model.query.call(Model);
1815
+ const repository = resolveModelRepository(this);
1816
+ let query = Model.query.call(this);
1819
1817
  if (options.orderBy) {
1820
1818
  query = query.orderBy(options.orderBy);
1821
1819
  }
@@ -1827,8 +1825,8 @@ class Model {
1827
1825
  }
1828
1826
  static async firstWhere(where, options = {}) {
1829
1827
  const statics = modelStatics(this);
1830
- const repository = resolveModelRepository(Model);
1831
- let query = Model.query.call(Model).where(where);
1828
+ const repository = resolveModelRepository(this);
1829
+ let query = Model.query.call(this).where(where);
1832
1830
  if (options.orderBy) {
1833
1831
  query = query.orderBy(options.orderBy);
1834
1832
  }
@@ -306,8 +306,7 @@ function buildWhereGroupClause(tableName, nodes, params) {
306
306
  }
307
307
  return result;
308
308
  }
309
- function buildAdvancedWhereClause(tableName, where = {}, whereNodes = []) {
310
- const params = [];
309
+ function buildAdvancedWhereClause(tableName, where = {}, whereNodes = [], params = []) {
311
310
  const nodes = [];
312
311
  if (Object.keys(where).length > 0) {
313
312
  nodes.push({ kind: "and", where });
@@ -342,18 +341,18 @@ function appendSoftDeleteScope(table, options, clauses) {
342
341
  clauses.push(`${qualifiedColumn} IS NULL`);
343
342
  }
344
343
  }
345
- function buildQueryWhereClause(table, options = {}, whereNodes = []) {
346
- const { clause, params } = buildAdvancedWhereClause(table.name, options.where ?? {}, whereNodes);
344
+ function buildQueryWhereClause(table, options = {}, whereNodes = [], params = []) {
345
+ const { clause, params: whereParams } = buildAdvancedWhereClause(table.name, options.where ?? {}, whereNodes, params);
347
346
  const softDeleteClauses = [];
348
347
  appendSoftDeleteScope(table, options, softDeleteClauses);
349
348
  if (softDeleteClauses.length === 0) {
350
- return { clause, params };
349
+ return { clause, params: whereParams };
351
350
  }
352
351
  const base = clause.replace(/^ WHERE /, "");
353
352
  const scope = softDeleteClauses.join(" AND ");
354
353
  return {
355
354
  clause: base ? ` WHERE (${base}) AND ${scope}` : ` WHERE ${scope}`,
356
- params
355
+ params: whereParams
357
356
  };
358
357
  }
359
358
  function isQueryOrder(value) {
@@ -457,8 +456,7 @@ function getDefinedColumnEntries(table, values, options = {}) {
457
456
  function buildSelectQuery(table, options = {}, whereNodes = []) {
458
457
  const params = [];
459
458
  const columns = buildSelectList(table, options.select, params);
460
- const { clause, params: whereParams } = buildQueryWhereClause(table, options, whereNodes);
461
- params.push(...whereParams);
459
+ const { clause } = buildQueryWhereClause(table, options, whereNodes, params);
462
460
  const joins = buildJoinClause(options.joins);
463
461
  const groupBy = buildGroupByClause(table.name, options.groupBy);
464
462
  const havingClause = buildHavingClause(table.name, options.having, params);
@@ -1355,26 +1353,26 @@ class Model {
1355
1353
  throw new Error(`${this.constructor.name}.primaryKey() is not implemented.`);
1356
1354
  }
1357
1355
  static primaryKeyField() {
1358
- return resolveModelRepository(Model).getTable().primaryKey;
1356
+ return resolveModelRepository(this).getTable().primaryKey;
1359
1357
  }
1360
1358
  static hydrateAttributes(attributes) {
1361
- const casts = modelStatics(Model).$casts ?? {};
1359
+ const casts = modelStatics(this).$casts ?? {};
1362
1360
  return applyCasts(attributes, casts, "hydrate");
1363
1361
  }
1364
1362
  static dehydrateAttributes(attributes) {
1365
- const casts = modelStatics(Model).$casts ?? {};
1363
+ const casts = modelStatics(this).$casts ?? {};
1366
1364
  return applyCasts(attributes, casts, "dehydrate");
1367
1365
  }
1368
1366
  static fromRecord(record, repository, exists = true) {
1369
- const statics = modelStatics(Model);
1367
+ const statics = modelStatics(this);
1370
1368
  const hydrated = statics.hydrateAttributes(record);
1371
1369
  return new statics(hydrated, repository, exists);
1372
1370
  }
1373
1371
  static boot() {}
1374
1372
  static addGlobalScope(_name, scope) {
1375
- ensureBooted(Model);
1376
- const existing = modelGlobalScopes.get(Model) ?? [];
1377
- modelGlobalScopes.set(Model, [
1373
+ ensureBooted(this);
1374
+ const existing = modelGlobalScopes.get(this) ?? [];
1375
+ modelGlobalScopes.set(this, [
1378
1376
  ...existing,
1379
1377
  scope
1380
1378
  ]);
@@ -1384,17 +1382,17 @@ class Model {
1384
1382
  }
1385
1383
  static query() {
1386
1384
  ensureBooted(this);
1387
- const repository = resolveModelRepository(Model);
1385
+ const repository = resolveModelRepository(this);
1388
1386
  let query = repository.query();
1389
- for (const scope of getGlobalScopes(Model)) {
1387
+ for (const scope of getGlobalScopes(this)) {
1390
1388
  query = scope(query);
1391
1389
  }
1392
1390
  return query;
1393
1391
  }
1394
1392
  static async create(attributes) {
1395
1393
  const statics = modelStatics(this);
1396
- ensureBooted(Model);
1397
- const repository = resolveModelRepository(Model);
1394
+ ensureBooted(this);
1395
+ const repository = resolveModelRepository(this);
1398
1396
  const table = repository.getTable();
1399
1397
  const timestamps = statics.$timestamps ?? true;
1400
1398
  const assignable = filterMassAssignable(statics.$fillable, statics.$guarded, attributes);
@@ -1405,22 +1403,22 @@ class Model {
1405
1403
  }
1406
1404
  static async find(id) {
1407
1405
  const statics = modelStatics(this);
1408
- const repository = resolveModelRepository(Model);
1409
- const primaryKey = statics.primaryKeyField();
1410
- const record = await Model.query.call(Model).where({ [primaryKey]: id }).first();
1406
+ const repository = resolveModelRepository(this);
1407
+ const primaryKey = repository.getTable().primaryKey;
1408
+ const record = await Model.query.call(this).where({ [primaryKey]: id }).first();
1411
1409
  return record ? statics.fromRecord(record, repository, true) : null;
1412
1410
  }
1413
1411
  static async findOrFail(id, errorFactory) {
1414
- const model = await Model.find.call(Model, id);
1412
+ const model = await Model.find.call(this, id);
1415
1413
  if (model) {
1416
1414
  return model;
1417
1415
  }
1418
- throw errorFactory?.(id) ?? new NotFoundError(`${Model.name} ${String(id)} not found.`);
1416
+ throw errorFactory?.(id) ?? new NotFoundError(`${this.name} ${String(id)} not found.`);
1419
1417
  }
1420
1418
  static async all(options = {}) {
1421
1419
  const statics = modelStatics(this);
1422
- const repository = resolveModelRepository(Model);
1423
- let query = Model.query.call(Model);
1420
+ const repository = resolveModelRepository(this);
1421
+ let query = Model.query.call(this);
1424
1422
  if (options.orderBy) {
1425
1423
  query = query.orderBy(options.orderBy);
1426
1424
  }
@@ -1432,8 +1430,8 @@ class Model {
1432
1430
  }
1433
1431
  static async firstWhere(where, options = {}) {
1434
1432
  const statics = modelStatics(this);
1435
- const repository = resolveModelRepository(Model);
1436
- let query = Model.query.call(Model).where(where);
1433
+ const repository = resolveModelRepository(this);
1434
+ let query = Model.query.call(this).where(where);
1437
1435
  if (options.orderBy) {
1438
1436
  query = query.orderBy(options.orderBy);
1439
1437
  }
@@ -781,8 +781,7 @@ function buildWhereGroupClause(tableName, nodes, params) {
781
781
  }
782
782
  return result;
783
783
  }
784
- function buildAdvancedWhereClause(tableName, where = {}, whereNodes = []) {
785
- const params = [];
784
+ function buildAdvancedWhereClause(tableName, where = {}, whereNodes = [], params = []) {
786
785
  const nodes = [];
787
786
  if (Object.keys(where).length > 0) {
788
787
  nodes.push({ kind: "and", where });
@@ -817,18 +816,18 @@ function appendSoftDeleteScope(table, options, clauses) {
817
816
  clauses.push(`${qualifiedColumn} IS NULL`);
818
817
  }
819
818
  }
820
- function buildQueryWhereClause(table, options = {}, whereNodes = []) {
821
- const { clause, params } = buildAdvancedWhereClause(table.name, options.where ?? {}, whereNodes);
819
+ function buildQueryWhereClause(table, options = {}, whereNodes = [], params = []) {
820
+ const { clause, params: whereParams } = buildAdvancedWhereClause(table.name, options.where ?? {}, whereNodes, params);
822
821
  const softDeleteClauses = [];
823
822
  appendSoftDeleteScope(table, options, softDeleteClauses);
824
823
  if (softDeleteClauses.length === 0) {
825
- return { clause, params };
824
+ return { clause, params: whereParams };
826
825
  }
827
826
  const base = clause.replace(/^ WHERE /, "");
828
827
  const scope = softDeleteClauses.join(" AND ");
829
828
  return {
830
829
  clause: base ? ` WHERE (${base}) AND ${scope}` : ` WHERE ${scope}`,
831
- params
830
+ params: whereParams
832
831
  };
833
832
  }
834
833
  function isQueryOrder(value) {
@@ -932,8 +931,7 @@ function getDefinedColumnEntries(table, values, options = {}) {
932
931
  function buildSelectQuery(table, options = {}, whereNodes = []) {
933
932
  const params = [];
934
933
  const columns = buildSelectList(table, options.select, params);
935
- const { clause, params: whereParams } = buildQueryWhereClause(table, options, whereNodes);
936
- params.push(...whereParams);
934
+ const { clause } = buildQueryWhereClause(table, options, whereNodes, params);
937
935
  const joins = buildJoinClause(options.joins);
938
936
  const groupBy = buildGroupByClause(table.name, options.groupBy);
939
937
  const havingClause = buildHavingClause(table.name, options.having, params);
@@ -1760,26 +1758,26 @@ class Model {
1760
1758
  throw new Error(`${this.constructor.name}.primaryKey() is not implemented.`);
1761
1759
  }
1762
1760
  static primaryKeyField() {
1763
- return resolveModelRepository(Model).getTable().primaryKey;
1761
+ return resolveModelRepository(this).getTable().primaryKey;
1764
1762
  }
1765
1763
  static hydrateAttributes(attributes) {
1766
- const casts = modelStatics(Model).$casts ?? {};
1764
+ const casts = modelStatics(this).$casts ?? {};
1767
1765
  return applyCasts(attributes, casts, "hydrate");
1768
1766
  }
1769
1767
  static dehydrateAttributes(attributes) {
1770
- const casts = modelStatics(Model).$casts ?? {};
1768
+ const casts = modelStatics(this).$casts ?? {};
1771
1769
  return applyCasts(attributes, casts, "dehydrate");
1772
1770
  }
1773
1771
  static fromRecord(record, repository, exists = true) {
1774
- const statics = modelStatics(Model);
1772
+ const statics = modelStatics(this);
1775
1773
  const hydrated = statics.hydrateAttributes(record);
1776
1774
  return new statics(hydrated, repository, exists);
1777
1775
  }
1778
1776
  static boot() {}
1779
1777
  static addGlobalScope(_name, scope) {
1780
- ensureBooted(Model);
1781
- const existing = modelGlobalScopes.get(Model) ?? [];
1782
- modelGlobalScopes.set(Model, [
1778
+ ensureBooted(this);
1779
+ const existing = modelGlobalScopes.get(this) ?? [];
1780
+ modelGlobalScopes.set(this, [
1783
1781
  ...existing,
1784
1782
  scope
1785
1783
  ]);
@@ -1789,17 +1787,17 @@ class Model {
1789
1787
  }
1790
1788
  static query() {
1791
1789
  ensureBooted(this);
1792
- const repository = resolveModelRepository(Model);
1790
+ const repository = resolveModelRepository(this);
1793
1791
  let query = repository.query();
1794
- for (const scope of getGlobalScopes(Model)) {
1792
+ for (const scope of getGlobalScopes(this)) {
1795
1793
  query = scope(query);
1796
1794
  }
1797
1795
  return query;
1798
1796
  }
1799
1797
  static async create(attributes) {
1800
1798
  const statics = modelStatics(this);
1801
- ensureBooted(Model);
1802
- const repository = resolveModelRepository(Model);
1799
+ ensureBooted(this);
1800
+ const repository = resolveModelRepository(this);
1803
1801
  const table = repository.getTable();
1804
1802
  const timestamps = statics.$timestamps ?? true;
1805
1803
  const assignable = filterMassAssignable(statics.$fillable, statics.$guarded, attributes);
@@ -1810,22 +1808,22 @@ class Model {
1810
1808
  }
1811
1809
  static async find(id) {
1812
1810
  const statics = modelStatics(this);
1813
- const repository = resolveModelRepository(Model);
1814
- const primaryKey = statics.primaryKeyField();
1815
- const record = await Model.query.call(Model).where({ [primaryKey]: id }).first();
1811
+ const repository = resolveModelRepository(this);
1812
+ const primaryKey = repository.getTable().primaryKey;
1813
+ const record = await Model.query.call(this).where({ [primaryKey]: id }).first();
1816
1814
  return record ? statics.fromRecord(record, repository, true) : null;
1817
1815
  }
1818
1816
  static async findOrFail(id, errorFactory) {
1819
- const model = await Model.find.call(Model, id);
1817
+ const model = await Model.find.call(this, id);
1820
1818
  if (model) {
1821
1819
  return model;
1822
1820
  }
1823
- throw errorFactory?.(id) ?? new NotFoundError(`${Model.name} ${String(id)} not found.`);
1821
+ throw errorFactory?.(id) ?? new NotFoundError(`${this.name} ${String(id)} not found.`);
1824
1822
  }
1825
1823
  static async all(options = {}) {
1826
1824
  const statics = modelStatics(this);
1827
- const repository = resolveModelRepository(Model);
1828
- let query = Model.query.call(Model);
1825
+ const repository = resolveModelRepository(this);
1826
+ let query = Model.query.call(this);
1829
1827
  if (options.orderBy) {
1830
1828
  query = query.orderBy(options.orderBy);
1831
1829
  }
@@ -1837,8 +1835,8 @@ class Model {
1837
1835
  }
1838
1836
  static async firstWhere(where, options = {}) {
1839
1837
  const statics = modelStatics(this);
1840
- const repository = resolveModelRepository(Model);
1841
- let query = Model.query.call(Model).where(where);
1838
+ const repository = resolveModelRepository(this);
1839
+ let query = Model.query.call(this).where(where);
1842
1840
  if (options.orderBy) {
1843
1841
  query = query.orderBy(options.orderBy);
1844
1842
  }
@@ -515,8 +515,7 @@ function buildWhereGroupClause(tableName, nodes, params) {
515
515
  }
516
516
  return result;
517
517
  }
518
- function buildAdvancedWhereClause(tableName, where = {}, whereNodes = []) {
519
- const params = [];
518
+ function buildAdvancedWhereClause(tableName, where = {}, whereNodes = [], params = []) {
520
519
  const nodes = [];
521
520
  if (Object.keys(where).length > 0) {
522
521
  nodes.push({ kind: "and", where });
@@ -551,18 +550,18 @@ function appendSoftDeleteScope(table, options, clauses) {
551
550
  clauses.push(`${qualifiedColumn} IS NULL`);
552
551
  }
553
552
  }
554
- function buildQueryWhereClause(table, options = {}, whereNodes = []) {
555
- const { clause, params } = buildAdvancedWhereClause(table.name, options.where ?? {}, whereNodes);
553
+ function buildQueryWhereClause(table, options = {}, whereNodes = [], params = []) {
554
+ const { clause, params: whereParams } = buildAdvancedWhereClause(table.name, options.where ?? {}, whereNodes, params);
556
555
  const softDeleteClauses = [];
557
556
  appendSoftDeleteScope(table, options, softDeleteClauses);
558
557
  if (softDeleteClauses.length === 0) {
559
- return { clause, params };
558
+ return { clause, params: whereParams };
560
559
  }
561
560
  const base = clause.replace(/^ WHERE /, "");
562
561
  const scope = softDeleteClauses.join(" AND ");
563
562
  return {
564
563
  clause: base ? ` WHERE (${base}) AND ${scope}` : ` WHERE ${scope}`,
565
- params
564
+ params: whereParams
566
565
  };
567
566
  }
568
567
  function isQueryOrder(value) {
@@ -666,8 +665,7 @@ function getDefinedColumnEntries(table, values, options = {}) {
666
665
  function buildSelectQuery(table, options = {}, whereNodes = []) {
667
666
  const params = [];
668
667
  const columns = buildSelectList(table, options.select, params);
669
- const { clause, params: whereParams } = buildQueryWhereClause(table, options, whereNodes);
670
- params.push(...whereParams);
668
+ const { clause } = buildQueryWhereClause(table, options, whereNodes, params);
671
669
  const joins = buildJoinClause(options.joins);
672
670
  const groupBy = buildGroupByClause(table.name, options.groupBy);
673
671
  const havingClause = buildHavingClause(table.name, options.having, params);
@@ -1564,26 +1562,26 @@ class Model {
1564
1562
  throw new Error(`${this.constructor.name}.primaryKey() is not implemented.`);
1565
1563
  }
1566
1564
  static primaryKeyField() {
1567
- return resolveModelRepository(Model).getTable().primaryKey;
1565
+ return resolveModelRepository(this).getTable().primaryKey;
1568
1566
  }
1569
1567
  static hydrateAttributes(attributes) {
1570
- const casts = modelStatics(Model).$casts ?? {};
1568
+ const casts = modelStatics(this).$casts ?? {};
1571
1569
  return applyCasts(attributes, casts, "hydrate");
1572
1570
  }
1573
1571
  static dehydrateAttributes(attributes) {
1574
- const casts = modelStatics(Model).$casts ?? {};
1572
+ const casts = modelStatics(this).$casts ?? {};
1575
1573
  return applyCasts(attributes, casts, "dehydrate");
1576
1574
  }
1577
1575
  static fromRecord(record, repository, exists = true) {
1578
- const statics = modelStatics(Model);
1576
+ const statics = modelStatics(this);
1579
1577
  const hydrated = statics.hydrateAttributes(record);
1580
1578
  return new statics(hydrated, repository, exists);
1581
1579
  }
1582
1580
  static boot() {}
1583
1581
  static addGlobalScope(_name, scope) {
1584
- ensureBooted(Model);
1585
- const existing = modelGlobalScopes.get(Model) ?? [];
1586
- modelGlobalScopes.set(Model, [
1582
+ ensureBooted(this);
1583
+ const existing = modelGlobalScopes.get(this) ?? [];
1584
+ modelGlobalScopes.set(this, [
1587
1585
  ...existing,
1588
1586
  scope
1589
1587
  ]);
@@ -1593,17 +1591,17 @@ class Model {
1593
1591
  }
1594
1592
  static query() {
1595
1593
  ensureBooted(this);
1596
- const repository = resolveModelRepository(Model);
1594
+ const repository = resolveModelRepository(this);
1597
1595
  let query = repository.query();
1598
- for (const scope of getGlobalScopes(Model)) {
1596
+ for (const scope of getGlobalScopes(this)) {
1599
1597
  query = scope(query);
1600
1598
  }
1601
1599
  return query;
1602
1600
  }
1603
1601
  static async create(attributes) {
1604
1602
  const statics = modelStatics(this);
1605
- ensureBooted(Model);
1606
- const repository = resolveModelRepository(Model);
1603
+ ensureBooted(this);
1604
+ const repository = resolveModelRepository(this);
1607
1605
  const table = repository.getTable();
1608
1606
  const timestamps = statics.$timestamps ?? true;
1609
1607
  const assignable = filterMassAssignable(statics.$fillable, statics.$guarded, attributes);
@@ -1614,22 +1612,22 @@ class Model {
1614
1612
  }
1615
1613
  static async find(id) {
1616
1614
  const statics = modelStatics(this);
1617
- const repository = resolveModelRepository(Model);
1618
- const primaryKey = statics.primaryKeyField();
1619
- const record = await Model.query.call(Model).where({ [primaryKey]: id }).first();
1615
+ const repository = resolveModelRepository(this);
1616
+ const primaryKey = repository.getTable().primaryKey;
1617
+ const record = await Model.query.call(this).where({ [primaryKey]: id }).first();
1620
1618
  return record ? statics.fromRecord(record, repository, true) : null;
1621
1619
  }
1622
1620
  static async findOrFail(id, errorFactory) {
1623
- const model = await Model.find.call(Model, id);
1621
+ const model = await Model.find.call(this, id);
1624
1622
  if (model) {
1625
1623
  return model;
1626
1624
  }
1627
- throw errorFactory?.(id) ?? new NotFoundError(`${Model.name} ${String(id)} not found.`);
1625
+ throw errorFactory?.(id) ?? new NotFoundError(`${this.name} ${String(id)} not found.`);
1628
1626
  }
1629
1627
  static async all(options = {}) {
1630
1628
  const statics = modelStatics(this);
1631
- const repository = resolveModelRepository(Model);
1632
- let query = Model.query.call(Model);
1629
+ const repository = resolveModelRepository(this);
1630
+ let query = Model.query.call(this);
1633
1631
  if (options.orderBy) {
1634
1632
  query = query.orderBy(options.orderBy);
1635
1633
  }
@@ -1641,8 +1639,8 @@ class Model {
1641
1639
  }
1642
1640
  static async firstWhere(where, options = {}) {
1643
1641
  const statics = modelStatics(this);
1644
- const repository = resolveModelRepository(Model);
1645
- let query = Model.query.call(Model).where(where);
1642
+ const repository = resolveModelRepository(this);
1643
+ let query = Model.query.call(this).where(where);
1646
1644
  if (options.orderBy) {
1647
1645
  query = query.orderBy(options.orderBy);
1648
1646
  }
package/dist/index.js CHANGED
@@ -508,8 +508,7 @@ function buildWhereGroupClause(tableName, nodes, params) {
508
508
  }
509
509
  return result;
510
510
  }
511
- function buildAdvancedWhereClause(tableName, where = {}, whereNodes = []) {
512
- const params = [];
511
+ function buildAdvancedWhereClause(tableName, where = {}, whereNodes = [], params = []) {
513
512
  const nodes = [];
514
513
  if (Object.keys(where).length > 0) {
515
514
  nodes.push({ kind: "and", where });
@@ -544,18 +543,18 @@ function appendSoftDeleteScope(table, options, clauses) {
544
543
  clauses.push(`${qualifiedColumn} IS NULL`);
545
544
  }
546
545
  }
547
- function buildQueryWhereClause(table, options = {}, whereNodes = []) {
548
- const { clause, params } = buildAdvancedWhereClause(table.name, options.where ?? {}, whereNodes);
546
+ function buildQueryWhereClause(table, options = {}, whereNodes = [], params = []) {
547
+ const { clause, params: whereParams } = buildAdvancedWhereClause(table.name, options.where ?? {}, whereNodes, params);
549
548
  const softDeleteClauses = [];
550
549
  appendSoftDeleteScope(table, options, softDeleteClauses);
551
550
  if (softDeleteClauses.length === 0) {
552
- return { clause, params };
551
+ return { clause, params: whereParams };
553
552
  }
554
553
  const base = clause.replace(/^ WHERE /, "");
555
554
  const scope = softDeleteClauses.join(" AND ");
556
555
  return {
557
556
  clause: base ? ` WHERE (${base}) AND ${scope}` : ` WHERE ${scope}`,
558
- params
557
+ params: whereParams
559
558
  };
560
559
  }
561
560
  function isQueryOrder(value) {
@@ -659,8 +658,7 @@ function getDefinedColumnEntries(table, values, options = {}) {
659
658
  function buildSelectQuery(table, options = {}, whereNodes = []) {
660
659
  const params = [];
661
660
  const columns = buildSelectList(table, options.select, params);
662
- const { clause, params: whereParams } = buildQueryWhereClause(table, options, whereNodes);
663
- params.push(...whereParams);
661
+ const { clause } = buildQueryWhereClause(table, options, whereNodes, params);
664
662
  const joins = buildJoinClause(options.joins);
665
663
  const groupBy = buildGroupByClause(table.name, options.groupBy);
666
664
  const havingClause = buildHavingClause(table.name, options.having, params);
@@ -1665,26 +1663,26 @@ class Model {
1665
1663
  throw new Error(`${this.constructor.name}.primaryKey() is not implemented.`);
1666
1664
  }
1667
1665
  static primaryKeyField() {
1668
- return resolveModelRepository(Model).getTable().primaryKey;
1666
+ return resolveModelRepository(this).getTable().primaryKey;
1669
1667
  }
1670
1668
  static hydrateAttributes(attributes) {
1671
- const casts = modelStatics(Model).$casts ?? {};
1669
+ const casts = modelStatics(this).$casts ?? {};
1672
1670
  return applyCasts(attributes, casts, "hydrate");
1673
1671
  }
1674
1672
  static dehydrateAttributes(attributes) {
1675
- const casts = modelStatics(Model).$casts ?? {};
1673
+ const casts = modelStatics(this).$casts ?? {};
1676
1674
  return applyCasts(attributes, casts, "dehydrate");
1677
1675
  }
1678
1676
  static fromRecord(record, repository, exists = true) {
1679
- const statics = modelStatics(Model);
1677
+ const statics = modelStatics(this);
1680
1678
  const hydrated = statics.hydrateAttributes(record);
1681
1679
  return new statics(hydrated, repository, exists);
1682
1680
  }
1683
1681
  static boot() {}
1684
1682
  static addGlobalScope(_name, scope) {
1685
- ensureBooted(Model);
1686
- const existing = modelGlobalScopes.get(Model) ?? [];
1687
- modelGlobalScopes.set(Model, [
1683
+ ensureBooted(this);
1684
+ const existing = modelGlobalScopes.get(this) ?? [];
1685
+ modelGlobalScopes.set(this, [
1688
1686
  ...existing,
1689
1687
  scope
1690
1688
  ]);
@@ -1694,17 +1692,17 @@ class Model {
1694
1692
  }
1695
1693
  static query() {
1696
1694
  ensureBooted(this);
1697
- const repository = resolveModelRepository(Model);
1695
+ const repository = resolveModelRepository(this);
1698
1696
  let query = repository.query();
1699
- for (const scope of getGlobalScopes(Model)) {
1697
+ for (const scope of getGlobalScopes(this)) {
1700
1698
  query = scope(query);
1701
1699
  }
1702
1700
  return query;
1703
1701
  }
1704
1702
  static async create(attributes) {
1705
1703
  const statics = modelStatics(this);
1706
- ensureBooted(Model);
1707
- const repository = resolveModelRepository(Model);
1704
+ ensureBooted(this);
1705
+ const repository = resolveModelRepository(this);
1708
1706
  const table = repository.getTable();
1709
1707
  const timestamps = statics.$timestamps ?? true;
1710
1708
  const assignable = filterMassAssignable(statics.$fillable, statics.$guarded, attributes);
@@ -1715,22 +1713,22 @@ class Model {
1715
1713
  }
1716
1714
  static async find(id) {
1717
1715
  const statics = modelStatics(this);
1718
- const repository = resolveModelRepository(Model);
1719
- const primaryKey = statics.primaryKeyField();
1720
- const record = await Model.query.call(Model).where({ [primaryKey]: id }).first();
1716
+ const repository = resolveModelRepository(this);
1717
+ const primaryKey = repository.getTable().primaryKey;
1718
+ const record = await Model.query.call(this).where({ [primaryKey]: id }).first();
1721
1719
  return record ? statics.fromRecord(record, repository, true) : null;
1722
1720
  }
1723
1721
  static async findOrFail(id, errorFactory) {
1724
- const model = await Model.find.call(Model, id);
1722
+ const model = await Model.find.call(this, id);
1725
1723
  if (model) {
1726
1724
  return model;
1727
1725
  }
1728
- throw errorFactory?.(id) ?? new NotFoundError(`${Model.name} ${String(id)} not found.`);
1726
+ throw errorFactory?.(id) ?? new NotFoundError(`${this.name} ${String(id)} not found.`);
1729
1727
  }
1730
1728
  static async all(options = {}) {
1731
1729
  const statics = modelStatics(this);
1732
- const repository = resolveModelRepository(Model);
1733
- let query = Model.query.call(Model);
1730
+ const repository = resolveModelRepository(this);
1731
+ let query = Model.query.call(this);
1734
1732
  if (options.orderBy) {
1735
1733
  query = query.orderBy(options.orderBy);
1736
1734
  }
@@ -1742,8 +1740,8 @@ class Model {
1742
1740
  }
1743
1741
  static async firstWhere(where, options = {}) {
1744
1742
  const statics = modelStatics(this);
1745
- const repository = resolveModelRepository(Model);
1746
- let query = Model.query.call(Model).where(where);
1743
+ const repository = resolveModelRepository(this);
1744
+ let query = Model.query.call(this).where(where);
1747
1745
  if (options.orderBy) {
1748
1746
  query = query.orderBy(options.orderBy);
1749
1747
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@getstrata/core",
3
- "version": "0.5.1",
3
+ "version": "0.5.4",
4
4
  "description": "Strata — Laravel-inspired Bun framework public API",
5
5
  "type": "module",
6
6
  "license": "MIT",