@getstrata/core 0.5.41 → 0.5.42
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/core/database/baseRepository.d.ts +1 -0
- package/dist/core/queue/failedJobRepository.d.ts +1 -0
- package/dist/entries/audit/exportAuditLogs.js +18 -0
- package/dist/entries/auth/sessionGuard.js +443 -0
- package/dist/entries/database/baseRepository.js +1388 -0
- package/dist/entries/database/bindConnection.js +22 -0
- package/dist/entries/database/boundConnection.js +19 -0
- package/dist/entries/database/connection.js +12 -0
- package/dist/entries/database/errors.js +128 -0
- package/dist/entries/database/model.js +948 -0
- package/dist/entries/database/query.js +436 -0
- package/dist/entries/database/relationships.js +162 -0
- package/dist/entries/database/table.js +8 -0
- package/dist/entries/database/transaction.js +129 -0
- package/dist/entries/http/authMiddleware.js +47 -0
- package/dist/entries/http/authorizeMiddleware.js +104 -0
- package/dist/entries/http/metricsMiddleware.js +91 -0
- package/dist/entries/http/parseMultipartUpload.js +144 -0
- package/dist/entries/http/securedRouteModelBinding.js +6 -0
- package/dist/entries/http/webErrorResponse.js +443 -0
- package/dist/entries/http/webFormRequest.js +6 -0
- package/dist/entries/jobs/dispatchWebhookJob.js +18 -0
- package/dist/entries/queue/createAppQueue.js +449 -0
- package/dist/entries/queue/failedJobRepository.js +2306 -0
- package/dist/entries/queue/publicQueue.js +449 -0
- package/dist/entries/queue/queueMetrics.js +449 -0
- package/dist/entries/queue/redisQueue.js +232 -0
- package/dist/entries/security/scimTenantTokens.js +51 -0
- package/dist/entries/tenant/tenantDatabaseScope.js +113 -0
- package/dist/entries/view.js +443 -0
- package/package.json +92 -2
package/dist/entries/view.js
CHANGED
|
@@ -483,6 +483,14 @@ function appendWhereParts(tableName, where, params) {
|
|
|
483
483
|
}
|
|
484
484
|
return clauses.join(" AND ");
|
|
485
485
|
}
|
|
486
|
+
function buildWhereClause(tableName, where = {}) {
|
|
487
|
+
const params = [];
|
|
488
|
+
const body = appendWhereParts(tableName, where, params);
|
|
489
|
+
return {
|
|
490
|
+
clause: body.length > 0 ? ` WHERE ${body}` : "",
|
|
491
|
+
params
|
|
492
|
+
};
|
|
493
|
+
}
|
|
486
494
|
function buildWhereNodeClause(tableName, node, params) {
|
|
487
495
|
if ("where" in node) {
|
|
488
496
|
return appendWhereParts(tableName, node.where, params);
|
|
@@ -785,6 +793,30 @@ function buildDeleteByIdQuery(table, id) {
|
|
|
785
793
|
}
|
|
786
794
|
|
|
787
795
|
// ../../src/core/database/relationships.ts
|
|
796
|
+
function hasMany(definition) {
|
|
797
|
+
return {
|
|
798
|
+
type: "hasMany",
|
|
799
|
+
...definition
|
|
800
|
+
};
|
|
801
|
+
}
|
|
802
|
+
function hasOne(definition) {
|
|
803
|
+
return {
|
|
804
|
+
type: "hasOne",
|
|
805
|
+
...definition
|
|
806
|
+
};
|
|
807
|
+
}
|
|
808
|
+
function belongsTo(definition) {
|
|
809
|
+
return {
|
|
810
|
+
type: "belongsTo",
|
|
811
|
+
...definition
|
|
812
|
+
};
|
|
813
|
+
}
|
|
814
|
+
function belongsToMany(definition) {
|
|
815
|
+
return {
|
|
816
|
+
type: "belongsToMany",
|
|
817
|
+
...definition
|
|
818
|
+
};
|
|
819
|
+
}
|
|
788
820
|
function indexHasManyRelation(parents, children, relation) {
|
|
789
821
|
const groups = new Map;
|
|
790
822
|
for (const parent of parents) {
|
|
@@ -800,6 +832,15 @@ function indexHasManyRelation(parents, children, relation) {
|
|
|
800
832
|
}
|
|
801
833
|
return groups;
|
|
802
834
|
}
|
|
835
|
+
function indexHasOneRelation(parents, children, relation) {
|
|
836
|
+
const grouped = indexHasManyRelation(parents, children, relation);
|
|
837
|
+
const result = new Map;
|
|
838
|
+
for (const parent of parents) {
|
|
839
|
+
const matches = grouped.get(parent[relation.localKey]) ?? [];
|
|
840
|
+
result.set(parent[relation.localKey], matches[0]);
|
|
841
|
+
}
|
|
842
|
+
return result;
|
|
843
|
+
}
|
|
803
844
|
function indexBelongsToRelation(children, parents, relation) {
|
|
804
845
|
const parentsById = new Map;
|
|
805
846
|
for (const parent of parents) {
|
|
@@ -815,6 +856,45 @@ function indexBelongsToRelation(children, parents, relation) {
|
|
|
815
856
|
}
|
|
816
857
|
return result;
|
|
817
858
|
}
|
|
859
|
+
function indexBelongsToManyRelation(parents, pivotRows, relatedRows, relation) {
|
|
860
|
+
const relatedById = new Map;
|
|
861
|
+
for (const related of relatedRows) {
|
|
862
|
+
relatedById.set(related[relation.relatedKey], related);
|
|
863
|
+
}
|
|
864
|
+
const groups = new Map;
|
|
865
|
+
for (const parent of parents) {
|
|
866
|
+
groups.set(parent[relation.parentKey], []);
|
|
867
|
+
}
|
|
868
|
+
for (const pivot of pivotRows) {
|
|
869
|
+
const parentId = pivot[relation.foreignPivotKey];
|
|
870
|
+
const relatedId = pivot[relation.relatedPivotKey];
|
|
871
|
+
const group = groups.get(parentId);
|
|
872
|
+
const related = relatedById.get(relatedId);
|
|
873
|
+
if (!group || !related) {
|
|
874
|
+
continue;
|
|
875
|
+
}
|
|
876
|
+
group.push(related);
|
|
877
|
+
}
|
|
878
|
+
return groups;
|
|
879
|
+
}
|
|
880
|
+
function morphMany(definition) {
|
|
881
|
+
return {
|
|
882
|
+
type: "morphMany",
|
|
883
|
+
...definition
|
|
884
|
+
};
|
|
885
|
+
}
|
|
886
|
+
function morphOne(definition) {
|
|
887
|
+
return {
|
|
888
|
+
type: "morphOne",
|
|
889
|
+
...definition
|
|
890
|
+
};
|
|
891
|
+
}
|
|
892
|
+
function morphTo(definition) {
|
|
893
|
+
return {
|
|
894
|
+
type: "morphTo",
|
|
895
|
+
...definition
|
|
896
|
+
};
|
|
897
|
+
}
|
|
818
898
|
function indexMorphManyRelation(parents, children, relation) {
|
|
819
899
|
const groups = new Map;
|
|
820
900
|
for (const parent of parents) {
|
|
@@ -833,6 +913,15 @@ function indexMorphManyRelation(parents, children, relation) {
|
|
|
833
913
|
}
|
|
834
914
|
return groups;
|
|
835
915
|
}
|
|
916
|
+
function indexMorphOneRelation(parents, children, relation) {
|
|
917
|
+
const grouped = indexMorphManyRelation(parents, children, relation);
|
|
918
|
+
const result = new Map;
|
|
919
|
+
for (const parent of parents) {
|
|
920
|
+
const matches = grouped.get(parent[relation.localKey]) ?? [];
|
|
921
|
+
result.set(parent[relation.localKey], matches[0]);
|
|
922
|
+
}
|
|
923
|
+
return result;
|
|
924
|
+
}
|
|
836
925
|
function indexMorphToRelation(children, parentsByType, relation) {
|
|
837
926
|
const result = new Map;
|
|
838
927
|
for (const child of children) {
|
|
@@ -853,9 +942,15 @@ function indexMorphToRelation(children, parentsByType, relation) {
|
|
|
853
942
|
var boundConnectionHolder = {
|
|
854
943
|
connection: null
|
|
855
944
|
};
|
|
945
|
+
function bindDatabaseConnection(connection) {
|
|
946
|
+
boundConnectionHolder.connection = connection;
|
|
947
|
+
}
|
|
856
948
|
function getBoundDatabaseConnection() {
|
|
857
949
|
return boundConnectionHolder.connection;
|
|
858
950
|
}
|
|
951
|
+
function resetBoundDatabaseConnection() {
|
|
952
|
+
boundConnectionHolder.connection = null;
|
|
953
|
+
}
|
|
859
954
|
|
|
860
955
|
// ../../src/core/runtime/asyncContextStore.ts
|
|
861
956
|
import { AsyncLocalStorage } from "async_hooks";
|
|
@@ -873,9 +968,15 @@ function createAsyncContextStore(key) {
|
|
|
873
968
|
|
|
874
969
|
// ../../src/core/database/connectionContext.ts
|
|
875
970
|
var activeConnection = createAsyncContextStore("@getstrata/databaseConnectionContext");
|
|
971
|
+
function runWithDatabaseConnection(connection, callback) {
|
|
972
|
+
return activeConnection.run(connection, callback);
|
|
973
|
+
}
|
|
876
974
|
function getActiveDatabaseConnection(fallback) {
|
|
877
975
|
return activeConnection.getStore() ?? fallback;
|
|
878
976
|
}
|
|
977
|
+
function hasActiveDatabaseConnection() {
|
|
978
|
+
return activeConnection.getStore() !== undefined;
|
|
979
|
+
}
|
|
879
980
|
|
|
880
981
|
// ../../src/core/database/queryProxy.ts
|
|
881
982
|
var POOL_CONNECTION_METHODS = new Set(["begin", "close", "connect"]);
|
|
@@ -912,6 +1013,12 @@ function registerDefaultDatabasePool(connection) {
|
|
|
912
1013
|
defaultPool.connection = connection;
|
|
913
1014
|
defaultQuery.connection = createDatabaseQueryProxy(connection);
|
|
914
1015
|
}
|
|
1016
|
+
function getDefaultDatabasePool() {
|
|
1017
|
+
if (!defaultPool.connection) {
|
|
1018
|
+
throw new Error("Default database pool is not registered. Call registerDefaultDatabasePool() during app bootstrap.");
|
|
1019
|
+
}
|
|
1020
|
+
return defaultPool.connection;
|
|
1021
|
+
}
|
|
915
1022
|
function getDefaultDatabaseQuery() {
|
|
916
1023
|
if (!defaultQuery.connection) {
|
|
917
1024
|
throw new Error("Default database query handle is not registered. Call registerDefaultDatabasePool() during app bootstrap.");
|
|
@@ -1489,10 +1596,327 @@ class BaseRepository {
|
|
|
1489
1596
|
}
|
|
1490
1597
|
}
|
|
1491
1598
|
var baseRepository_default = BaseRepository;
|
|
1599
|
+
// ../../src/core/database/connection.ts
|
|
1600
|
+
function createDatabaseConnection(source) {
|
|
1601
|
+
return {
|
|
1602
|
+
async unsafe(query, params = []) {
|
|
1603
|
+
return await source.unsafe(query, params);
|
|
1604
|
+
}
|
|
1605
|
+
};
|
|
1606
|
+
}
|
|
1492
1607
|
// ../../src/core/database/model.ts
|
|
1493
1608
|
var modelRepositories = new WeakMap;
|
|
1494
1609
|
var modelGlobalScopes = new WeakMap;
|
|
1495
1610
|
var modelBooted = new WeakSet;
|
|
1611
|
+
function resolveModelRepository(model) {
|
|
1612
|
+
const repository = modelRepositories.get(model);
|
|
1613
|
+
if (!repository) {
|
|
1614
|
+
throw new Error(`${model.name}.repository() is not implemented.`);
|
|
1615
|
+
}
|
|
1616
|
+
return repository;
|
|
1617
|
+
}
|
|
1618
|
+
function modelStatics(model) {
|
|
1619
|
+
return model;
|
|
1620
|
+
}
|
|
1621
|
+
function ensureBooted(model) {
|
|
1622
|
+
if (modelBooted.has(model)) {
|
|
1623
|
+
return;
|
|
1624
|
+
}
|
|
1625
|
+
modelBooted.add(model);
|
|
1626
|
+
const boot = model.boot;
|
|
1627
|
+
if (typeof boot === "function") {
|
|
1628
|
+
boot.call(model);
|
|
1629
|
+
}
|
|
1630
|
+
}
|
|
1631
|
+
function getGlobalScopes(model) {
|
|
1632
|
+
return modelGlobalScopes.get(model) ?? [];
|
|
1633
|
+
}
|
|
1634
|
+
function hydrateValue(value, cast) {
|
|
1635
|
+
if (value === null || value === undefined) {
|
|
1636
|
+
return value;
|
|
1637
|
+
}
|
|
1638
|
+
switch (cast) {
|
|
1639
|
+
case "date":
|
|
1640
|
+
case "datetime":
|
|
1641
|
+
return value instanceof Date ? value : new Date(String(value));
|
|
1642
|
+
case "json":
|
|
1643
|
+
return typeof value === "string" ? JSON.parse(value) : value;
|
|
1644
|
+
case "bool":
|
|
1645
|
+
case "boolean":
|
|
1646
|
+
return value === true || value === 1 || value === "1" || value === "true";
|
|
1647
|
+
default:
|
|
1648
|
+
return value;
|
|
1649
|
+
}
|
|
1650
|
+
}
|
|
1651
|
+
function dehydrateValue(value, cast) {
|
|
1652
|
+
if (value === null || value === undefined) {
|
|
1653
|
+
return value;
|
|
1654
|
+
}
|
|
1655
|
+
switch (cast) {
|
|
1656
|
+
case "date":
|
|
1657
|
+
case "datetime":
|
|
1658
|
+
return value instanceof Date ? value : new Date(String(value));
|
|
1659
|
+
case "json":
|
|
1660
|
+
return typeof value === "string" ? value : JSON.stringify(value);
|
|
1661
|
+
case "bool":
|
|
1662
|
+
case "boolean":
|
|
1663
|
+
return Boolean(value);
|
|
1664
|
+
default:
|
|
1665
|
+
return value;
|
|
1666
|
+
}
|
|
1667
|
+
}
|
|
1668
|
+
function filterMassAssignable(fillable, guarded, input) {
|
|
1669
|
+
const resolvedGuarded = guarded ?? true;
|
|
1670
|
+
if (fillable && fillable.length > 0) {
|
|
1671
|
+
const allowed = new Set(fillable);
|
|
1672
|
+
return Object.fromEntries(Object.entries(input).filter(([key]) => allowed.has(key)));
|
|
1673
|
+
}
|
|
1674
|
+
if (resolvedGuarded === true || resolvedGuarded.includes("*")) {
|
|
1675
|
+
return {};
|
|
1676
|
+
}
|
|
1677
|
+
const blocked = new Set(resolvedGuarded);
|
|
1678
|
+
return Object.fromEntries(Object.entries(input).filter(([key]) => !blocked.has(key)));
|
|
1679
|
+
}
|
|
1680
|
+
function applyCasts(values, casts, direction) {
|
|
1681
|
+
if (Object.keys(casts).length === 0) {
|
|
1682
|
+
return values;
|
|
1683
|
+
}
|
|
1684
|
+
const result = { ...values };
|
|
1685
|
+
const castFn = direction === "hydrate" ? hydrateValue : dehydrateValue;
|
|
1686
|
+
for (const [key, cast] of Object.entries(casts)) {
|
|
1687
|
+
if (key in result && cast) {
|
|
1688
|
+
result[key] = castFn(result[key], cast);
|
|
1689
|
+
}
|
|
1690
|
+
}
|
|
1691
|
+
return result;
|
|
1692
|
+
}
|
|
1693
|
+
function applyTimestampsOnCreate(columns, values, enabled) {
|
|
1694
|
+
if (!enabled) {
|
|
1695
|
+
return values;
|
|
1696
|
+
}
|
|
1697
|
+
const now = new Date;
|
|
1698
|
+
const result = { ...values };
|
|
1699
|
+
if (columns.includes("created_at")) {
|
|
1700
|
+
result.created_at = now;
|
|
1701
|
+
}
|
|
1702
|
+
if (columns.includes("updated_at")) {
|
|
1703
|
+
result.updated_at = now;
|
|
1704
|
+
}
|
|
1705
|
+
return result;
|
|
1706
|
+
}
|
|
1707
|
+
function applyTimestampsOnUpdate(columns, values, enabled) {
|
|
1708
|
+
if (!enabled) {
|
|
1709
|
+
return values;
|
|
1710
|
+
}
|
|
1711
|
+
const result = { ...values };
|
|
1712
|
+
if (columns.includes("updated_at")) {
|
|
1713
|
+
result.updated_at = new Date;
|
|
1714
|
+
}
|
|
1715
|
+
return result;
|
|
1716
|
+
}
|
|
1717
|
+
|
|
1718
|
+
class Model {
|
|
1719
|
+
attributes;
|
|
1720
|
+
repository;
|
|
1721
|
+
static $fillable;
|
|
1722
|
+
static $guarded;
|
|
1723
|
+
static $casts = {};
|
|
1724
|
+
static $timestamps = true;
|
|
1725
|
+
_exists;
|
|
1726
|
+
constructor(attributes, repository, exists = true) {
|
|
1727
|
+
this.attributes = attributes;
|
|
1728
|
+
this.repository = repository;
|
|
1729
|
+
this._exists = exists;
|
|
1730
|
+
}
|
|
1731
|
+
get $exists() {
|
|
1732
|
+
return this._exists;
|
|
1733
|
+
}
|
|
1734
|
+
get(key) {
|
|
1735
|
+
return this.attributes[key];
|
|
1736
|
+
}
|
|
1737
|
+
get id() {
|
|
1738
|
+
return this.attributes[this.primaryKey()];
|
|
1739
|
+
}
|
|
1740
|
+
toObject() {
|
|
1741
|
+
return { ...this.attributes };
|
|
1742
|
+
}
|
|
1743
|
+
primaryKey() {
|
|
1744
|
+
throw new Error(`${this.constructor.name}.primaryKey() is not implemented.`);
|
|
1745
|
+
}
|
|
1746
|
+
static primaryKeyField() {
|
|
1747
|
+
return resolveModelRepository(this).getTable().primaryKey;
|
|
1748
|
+
}
|
|
1749
|
+
static hydrateAttributes(attributes) {
|
|
1750
|
+
const casts = modelStatics(this).$casts ?? {};
|
|
1751
|
+
return applyCasts(attributes, casts, "hydrate");
|
|
1752
|
+
}
|
|
1753
|
+
static dehydrateAttributes(attributes) {
|
|
1754
|
+
const casts = modelStatics(this).$casts ?? {};
|
|
1755
|
+
return applyCasts(attributes, casts, "dehydrate");
|
|
1756
|
+
}
|
|
1757
|
+
static fromRecord(record, repository, exists = true) {
|
|
1758
|
+
const statics = modelStatics(this);
|
|
1759
|
+
const hydrated = statics.hydrateAttributes(record);
|
|
1760
|
+
return new statics(hydrated, repository, exists);
|
|
1761
|
+
}
|
|
1762
|
+
static boot() {}
|
|
1763
|
+
static addGlobalScope(_name, scope) {
|
|
1764
|
+
ensureBooted(this);
|
|
1765
|
+
const existing = modelGlobalScopes.get(this) ?? [];
|
|
1766
|
+
modelGlobalScopes.set(this, [
|
|
1767
|
+
...existing,
|
|
1768
|
+
scope
|
|
1769
|
+
]);
|
|
1770
|
+
}
|
|
1771
|
+
static repository() {
|
|
1772
|
+
return resolveModelRepository(this);
|
|
1773
|
+
}
|
|
1774
|
+
static query() {
|
|
1775
|
+
ensureBooted(this);
|
|
1776
|
+
const repository = resolveModelRepository(this);
|
|
1777
|
+
let query = repository.query();
|
|
1778
|
+
for (const scope of getGlobalScopes(this)) {
|
|
1779
|
+
query = scope(query);
|
|
1780
|
+
}
|
|
1781
|
+
return query;
|
|
1782
|
+
}
|
|
1783
|
+
static async create(attributes) {
|
|
1784
|
+
const statics = modelStatics(this);
|
|
1785
|
+
ensureBooted(this);
|
|
1786
|
+
const repository = resolveModelRepository(this);
|
|
1787
|
+
const table = repository.getTable();
|
|
1788
|
+
const timestamps = statics.$timestamps ?? true;
|
|
1789
|
+
const assignable = filterMassAssignable(statics.$fillable, statics.$guarded, attributes);
|
|
1790
|
+
const withTimestamps = applyTimestampsOnCreate(table.columns, assignable, timestamps);
|
|
1791
|
+
const payload = statics.dehydrateAttributes(withTimestamps);
|
|
1792
|
+
const record = await repository.create(payload);
|
|
1793
|
+
return statics.fromRecord(record, repository, true);
|
|
1794
|
+
}
|
|
1795
|
+
static async find(id) {
|
|
1796
|
+
const statics = modelStatics(this);
|
|
1797
|
+
const repository = resolveModelRepository(this);
|
|
1798
|
+
const primaryKey = repository.getTable().primaryKey;
|
|
1799
|
+
const record = await Model.query.call(this).where({ [primaryKey]: id }).first();
|
|
1800
|
+
return record ? statics.fromRecord(record, repository, true) : null;
|
|
1801
|
+
}
|
|
1802
|
+
static async findOrFail(id, errorFactory) {
|
|
1803
|
+
const model = await Model.find.call(this, id);
|
|
1804
|
+
if (model) {
|
|
1805
|
+
return model;
|
|
1806
|
+
}
|
|
1807
|
+
throw errorFactory?.(id) ?? new NotFoundError(`${this.name} ${String(id)} not found.`);
|
|
1808
|
+
}
|
|
1809
|
+
static async all(options = {}) {
|
|
1810
|
+
const statics = modelStatics(this);
|
|
1811
|
+
const repository = resolveModelRepository(this);
|
|
1812
|
+
let query = Model.query.call(this);
|
|
1813
|
+
if (options.orderBy) {
|
|
1814
|
+
query = query.orderBy(options.orderBy);
|
|
1815
|
+
}
|
|
1816
|
+
if (options.limit !== undefined) {
|
|
1817
|
+
query = query.limit(options.limit);
|
|
1818
|
+
}
|
|
1819
|
+
const rows = await query.get();
|
|
1820
|
+
return rows.map((row) => statics.fromRecord(row, repository, true));
|
|
1821
|
+
}
|
|
1822
|
+
static async firstWhere(where, options = {}) {
|
|
1823
|
+
const statics = modelStatics(this);
|
|
1824
|
+
const repository = resolveModelRepository(this);
|
|
1825
|
+
let query = Model.query.call(this).where(where);
|
|
1826
|
+
if (options.orderBy) {
|
|
1827
|
+
query = query.orderBy(options.orderBy);
|
|
1828
|
+
}
|
|
1829
|
+
const record = await query.first();
|
|
1830
|
+
return record ? statics.fromRecord(record, repository, true) : null;
|
|
1831
|
+
}
|
|
1832
|
+
async save() {
|
|
1833
|
+
const ModelClass = modelStatics(this.constructor);
|
|
1834
|
+
const timestamps = ModelClass.$timestamps ?? true;
|
|
1835
|
+
const casts = ModelClass.$casts ?? {};
|
|
1836
|
+
const table = this.repository.getTable();
|
|
1837
|
+
if (this.$exists) {
|
|
1838
|
+
const changes = applyTimestampsOnUpdate(table.columns, applyCasts(this.attributes, casts, "dehydrate"), timestamps);
|
|
1839
|
+
const record2 = await this.repository.updateByIdOrThrow(this.id, changes);
|
|
1840
|
+
this.attributes = ModelClass.hydrateAttributes(record2);
|
|
1841
|
+
return this;
|
|
1842
|
+
}
|
|
1843
|
+
const assignable = filterMassAssignable(ModelClass.$fillable, ModelClass.$guarded, this.attributes);
|
|
1844
|
+
const withTimestamps = applyTimestampsOnCreate(table.columns, assignable, timestamps);
|
|
1845
|
+
const payload = ModelClass.dehydrateAttributes(withTimestamps);
|
|
1846
|
+
const record = await this.repository.create(payload);
|
|
1847
|
+
this.attributes = ModelClass.hydrateAttributes(record);
|
|
1848
|
+
this._exists = true;
|
|
1849
|
+
return this;
|
|
1850
|
+
}
|
|
1851
|
+
async update(changes) {
|
|
1852
|
+
const ModelClass = modelStatics(this.constructor);
|
|
1853
|
+
const assignable = filterMassAssignable(ModelClass.$fillable, ModelClass.$guarded, changes);
|
|
1854
|
+
Object.assign(this.attributes, assignable);
|
|
1855
|
+
return await this.save();
|
|
1856
|
+
}
|
|
1857
|
+
async delete() {
|
|
1858
|
+
if (resolveSoftDeleteColumn(this.repository.getTable())) {
|
|
1859
|
+
return await this.repository.deleteById(this.id);
|
|
1860
|
+
}
|
|
1861
|
+
return await this.repository.forceDeleteById(this.id);
|
|
1862
|
+
}
|
|
1863
|
+
async forceDelete() {
|
|
1864
|
+
return await this.repository.forceDeleteById(this.id);
|
|
1865
|
+
}
|
|
1866
|
+
async restore() {
|
|
1867
|
+
const ModelClass = modelStatics(this.constructor);
|
|
1868
|
+
const record = await this.repository.restoreById(this.id);
|
|
1869
|
+
if (!record) {
|
|
1870
|
+
return null;
|
|
1871
|
+
}
|
|
1872
|
+
this.attributes = ModelClass.hydrateAttributes(record);
|
|
1873
|
+
return this;
|
|
1874
|
+
}
|
|
1875
|
+
async loadHasMany(as, relation, childRepository, options = {}) {
|
|
1876
|
+
const grouped = await childRepository.withConnection(this.repository.getConnection()).loadHasManyForParents([this.attributes], relation, options);
|
|
1877
|
+
const loaded = grouped.get(this.attributes[relation.localKey]) ?? [];
|
|
1878
|
+
return Object.assign(this, { [as]: loaded });
|
|
1879
|
+
}
|
|
1880
|
+
async loadHasOne(as, relation, childRepository, options = {}) {
|
|
1881
|
+
const loaded = await this.loadHasMany(as, relation, childRepository, { ...options, limit: 1 });
|
|
1882
|
+
const value = loaded[as]?.[0];
|
|
1883
|
+
return Object.assign(this, { [as]: value });
|
|
1884
|
+
}
|
|
1885
|
+
async loadBelongsTo(as, relation, parentRepository, options = {}) {
|
|
1886
|
+
const grouped = await this.repository.loadBelongsToForParents([this.attributes], relation, parentRepository, options);
|
|
1887
|
+
const loaded = grouped.get(this.attributes[relation.foreignKey]);
|
|
1888
|
+
return Object.assign(this, { [as]: loaded });
|
|
1889
|
+
}
|
|
1890
|
+
async loadBelongsToMany(as, relation, relatedRepository, options = {}) {
|
|
1891
|
+
const connection = this.repository.getConnection();
|
|
1892
|
+
const parentId = this.attributes[relation.parentKey];
|
|
1893
|
+
const pivotRows = await connection.unsafe(`SELECT * FROM ${relation.pivotTable} WHERE ${String(relation.foreignPivotKey)} = $1`, [parentId]);
|
|
1894
|
+
if (pivotRows.length === 0) {
|
|
1895
|
+
return Object.assign(this, { [as]: [] });
|
|
1896
|
+
}
|
|
1897
|
+
const relatedIds = [
|
|
1898
|
+
...new Set(pivotRows.map((row) => row[relation.relatedPivotKey]))
|
|
1899
|
+
];
|
|
1900
|
+
const relatedRows = await relatedRepository.withConnection(connection).findAll({
|
|
1901
|
+
...options,
|
|
1902
|
+
where: {
|
|
1903
|
+
[relation.relatedKey]: relatedIds
|
|
1904
|
+
}
|
|
1905
|
+
});
|
|
1906
|
+
const grouped = indexBelongsToManyRelation([this.attributes], pivotRows, relatedRows, relation);
|
|
1907
|
+
const loaded = grouped.get(parentId) ?? [];
|
|
1908
|
+
return Object.assign(this, { [as]: loaded });
|
|
1909
|
+
}
|
|
1910
|
+
mergeAttributes(patch) {
|
|
1911
|
+
Object.assign(this.attributes, patch);
|
|
1912
|
+
return this;
|
|
1913
|
+
}
|
|
1914
|
+
}
|
|
1915
|
+
function registerModelRepository(model, repository) {
|
|
1916
|
+
modelRepositories.set(model, repository);
|
|
1917
|
+
ensureBooted(model);
|
|
1918
|
+
return model;
|
|
1919
|
+
}
|
|
1496
1920
|
// ../../src/core/database/schema/columnDefinition.ts
|
|
1497
1921
|
class ColumnDefinition {
|
|
1498
1922
|
name;
|
|
@@ -2062,6 +2486,19 @@ class SchemaBuilder {
|
|
|
2062
2486
|
function defineTable(definition) {
|
|
2063
2487
|
return definition;
|
|
2064
2488
|
}
|
|
2489
|
+
// ../../src/core/database/transaction.ts
|
|
2490
|
+
function supportsTransactions(connection) {
|
|
2491
|
+
return typeof connection.begin === "function";
|
|
2492
|
+
}
|
|
2493
|
+
async function runInTransaction(operation) {
|
|
2494
|
+
const pool = resolveRepositoryConnection();
|
|
2495
|
+
if (!supportsTransactions(pool)) {
|
|
2496
|
+
throw new Error("Active database connection does not support transactions. Bind a client with begin() via bindDatabaseConnection.");
|
|
2497
|
+
}
|
|
2498
|
+
return await pool.begin(async (transaction) => {
|
|
2499
|
+
return await operation(createDatabaseConnection(transaction));
|
|
2500
|
+
});
|
|
2501
|
+
}
|
|
2065
2502
|
// ../../src/config/database.ts
|
|
2066
2503
|
function readInteger(name, fallback) {
|
|
2067
2504
|
const parsed = Number.parseInt(process.env[name] ?? String(fallback), 10);
|
|
@@ -2247,6 +2684,9 @@ function revealMfaSecret(stored) {
|
|
|
2247
2684
|
|
|
2248
2685
|
// ../../src/core/auth/authContext.ts
|
|
2249
2686
|
var authContext = createAsyncContextStore("@getstrata/authContext");
|
|
2687
|
+
function runWithAuthUser(user, callback) {
|
|
2688
|
+
return authContext.run(user, callback);
|
|
2689
|
+
}
|
|
2250
2690
|
function currentAuthUser() {
|
|
2251
2691
|
return authContext.getStore() ?? null;
|
|
2252
2692
|
}
|
|
@@ -2337,6 +2777,9 @@ function verifyTotp(secret, token, window = 1) {
|
|
|
2337
2777
|
|
|
2338
2778
|
// ../../src/core/tenant/tenantContext.ts
|
|
2339
2779
|
var tenantContext = createAsyncContextStore("@getstrata/tenantContext");
|
|
2780
|
+
function runWithTenant(tenant, callback) {
|
|
2781
|
+
return tenantContext.run(tenant, callback);
|
|
2782
|
+
}
|
|
2340
2783
|
function currentTenant() {
|
|
2341
2784
|
return tenantContext.getStore() ?? null;
|
|
2342
2785
|
}
|