@guren/orm 1.0.0-rc.24 → 1.0.0-rc.26
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 +48 -3
- package/dist/index.js +68 -57
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -242,7 +242,9 @@ declare class QueryBuilder<TRecord extends PlainObject = PlainObject, TResult ex
|
|
|
242
242
|
private toSimpleWhereClause;
|
|
243
243
|
/**
|
|
244
244
|
* Load eager relations onto fetched results.
|
|
245
|
-
* Supports dot notation for nested relations
|
|
245
|
+
* Supports dot notation for nested relations at any depth
|
|
246
|
+
* (e.g., 'posts.comments.author') — the full path is delegated to
|
|
247
|
+
* Model.loadRelationInto, which recurses through the relation chain.
|
|
246
248
|
*/
|
|
247
249
|
private readonly loadEagerRelations;
|
|
248
250
|
}
|
|
@@ -577,7 +579,12 @@ declare abstract class Model<TRecord extends PlainObject = PlainObject> {
|
|
|
577
579
|
static casts?: Record<string, CastType>;
|
|
578
580
|
/**
|
|
579
581
|
* Whitelist of fields allowed for mass assignment.
|
|
580
|
-
* If set,
|
|
582
|
+
* If set, `create()` and `update()` accept only these fields — input
|
|
583
|
+
* containing any other key throws a MassAssignmentException so bugs
|
|
584
|
+
* (and injection attempts) surface immediately instead of being
|
|
585
|
+
* silently discarded. Use `forceCreate()` / `forceUpdate()` for
|
|
586
|
+
* trusted server-side data, or set `strictFillable = false` to
|
|
587
|
+
* restore silent discarding.
|
|
581
588
|
*
|
|
582
589
|
* @example
|
|
583
590
|
* class User extends Model<UserRecord> {
|
|
@@ -585,6 +592,12 @@ declare abstract class Model<TRecord extends PlainObject = PlainObject> {
|
|
|
585
592
|
* }
|
|
586
593
|
*/
|
|
587
594
|
static fillable?: string[];
|
|
595
|
+
/**
|
|
596
|
+
* When `fillable` is set, controls what happens to input fields outside
|
|
597
|
+
* the allowlist: `true` (default) throws a MassAssignmentException,
|
|
598
|
+
* `false` silently discards them (pre-1.0 behavior).
|
|
599
|
+
*/
|
|
600
|
+
static strictFillable?: boolean;
|
|
588
601
|
/**
|
|
589
602
|
* Blacklist of fields excluded from mass assignment.
|
|
590
603
|
* Defaults to `['id']`. Ignored if `fillable` is set.
|
|
@@ -1096,6 +1109,20 @@ declare abstract class Model<TRecord extends PlainObject = PlainObject> {
|
|
|
1096
1109
|
* })
|
|
1097
1110
|
*/
|
|
1098
1111
|
static create<T extends typeof Model>(this: T, data: TCreateFor<T>, writeOptions?: ModelWriteOptions): Promise<TRecordFor<T>>;
|
|
1112
|
+
/**
|
|
1113
|
+
* Create a record bypassing mass-assignment protection. Use for trusted,
|
|
1114
|
+
* server-side-assembled data (OAuth account linking, seeders, system
|
|
1115
|
+
* records) — never for raw request input.
|
|
1116
|
+
*
|
|
1117
|
+
* @example
|
|
1118
|
+
* const user = await User.forceCreate({
|
|
1119
|
+
* name: profile.name,
|
|
1120
|
+
* email: profile.email,
|
|
1121
|
+
* passwordHash: `oauth:${provider}:${profile.id}`,
|
|
1122
|
+
* })
|
|
1123
|
+
*/
|
|
1124
|
+
static forceCreate<T extends typeof Model>(this: T, data: TCreateFor<T>, writeOptions?: ModelWriteOptions): Promise<TRecordFor<T>>;
|
|
1125
|
+
protected static runCreate<T extends typeof Model>(this: T, data: TCreateFor<T>, writeOptions: ModelWriteOptions | undefined, applyFillable: boolean): Promise<TRecordFor<T>>;
|
|
1099
1126
|
/**
|
|
1100
1127
|
* Update records matching the conditions.
|
|
1101
1128
|
*
|
|
@@ -1107,6 +1134,12 @@ declare abstract class Model<TRecord extends PlainObject = PlainObject> {
|
|
|
1107
1134
|
* await User.update({ id: 1 }, { name: 'Jane Doe' })
|
|
1108
1135
|
*/
|
|
1109
1136
|
static update<T extends typeof Model>(this: T, where: WhereClauseFor<T>, data: Partial<TCreateFor<T>>, writeOptions?: ModelWriteOptions): Promise<TRecordFor<T>>;
|
|
1137
|
+
/**
|
|
1138
|
+
* Update records bypassing mass-assignment protection. Use for trusted,
|
|
1139
|
+
* server-side-assembled data — never for raw request input.
|
|
1140
|
+
*/
|
|
1141
|
+
static forceUpdate<T extends typeof Model>(this: T, where: WhereClauseFor<T>, data: Partial<TCreateFor<T>>, writeOptions?: ModelWriteOptions): Promise<TRecordFor<T>>;
|
|
1142
|
+
protected static runUpdate<T extends typeof Model>(this: T, where: WhereClauseFor<T>, data: Partial<TCreateFor<T>>, writeOptions: ModelWriteOptions | undefined, applyFillable: boolean): Promise<TRecordFor<T>>;
|
|
1110
1143
|
/**
|
|
1111
1144
|
* Delete records matching the conditions.
|
|
1112
1145
|
*
|
|
@@ -1326,6 +1359,18 @@ declare class ModelNotFoundException extends Error {
|
|
|
1326
1359
|
constructor(modelName: string, id: unknown, key?: string);
|
|
1327
1360
|
}
|
|
1328
1361
|
|
|
1362
|
+
/**
|
|
1363
|
+
* Thrown when mass assignment receives fields outside the model's
|
|
1364
|
+
* `fillable` allowlist. Silently discarding them hides bugs (a typo'd
|
|
1365
|
+
* fillable entry surfaces as a NOT NULL violation much later) and can
|
|
1366
|
+
* mask injection attempts, so strict models fail loudly instead.
|
|
1367
|
+
*/
|
|
1368
|
+
declare class MassAssignmentException extends Error {
|
|
1369
|
+
readonly model: string;
|
|
1370
|
+
readonly fields: string[];
|
|
1371
|
+
constructor(model: string, fields: string[]);
|
|
1372
|
+
}
|
|
1373
|
+
|
|
1329
1374
|
/**
|
|
1330
1375
|
* Interface describing the static methods added by the SoftDeletes mixin.
|
|
1331
1376
|
*/
|
|
@@ -1542,4 +1587,4 @@ declare function serializeRecord(record: PlainObject, options: {
|
|
|
1542
1587
|
/** Serialize an array of records. */
|
|
1543
1588
|
declare function serializeRecords(records: PlainObject[], options: Parameters<typeof serializeRecord>[1]): PlainObject[];
|
|
1544
1589
|
|
|
1545
|
-
export { type AccessorDefinitions, type AccessorFn, type BelongsToManyRecord, type BelongsToManyRelationResult, type BelongsToRecord, type BelongsToRelationResult, type CastType, DrizzleAdapter, type FindManyOptions, GlobalScopeRegistry, type GroupCondition, type HasManyRecord, type HasManyRelationResult, type HasManyThroughRecord, type HasManyThroughRelationResult, type HasOneRecord, type HasOneRelationResult, type HookCallback, type HookName, type InferModelInsert, type InferModelRecord, type MigrationStatusEntry, Model, type ModelHooks, ModelNotFoundException, type ModelObserver, type ModelObserverConstructor, type MorphManyRecord, type MorphManyRelationResult, type MorphToRecord, type MorphToRelationResult, type MutatorDefinitions, type MutatorFn, type MySqlDatabase, type MySqlDatabaseOptions, type ORMAdapter, type ORMAdapterAdvanced, type OrderByClause, type OrderByInput, type OrderDefinition, type OrderDirection, type PaginateOptions, type PaginatedResult, type PaginationMeta, type PlainObject, type PostgresDatabase, type PostgresDatabaseOptions, QueryBuilder, type QueryBuilderOptions, type ScopeFunction, type SeederContext, type SeederHandler, type SimpleCondition, SoftDeletes, type SoftDeletesStatic, type SqliteDatabase, type SqliteDatabaseOptions, type TransactionHandle, type TransactionModelScope, type WhereClause, type WhereCondition, type WhereOperator, type WithRelations, applyAccessors, applyMutators, buildDrizzleConditions, createMySqlDatabase, createPostgresDatabase, createSqliteDatabase, defineModel, defineSeeder, executeHook, executeObservers, loadSeeders, runSeeders, serializeRecord, serializeRecords };
|
|
1590
|
+
export { type AccessorDefinitions, type AccessorFn, type BelongsToManyRecord, type BelongsToManyRelationResult, type BelongsToRecord, type BelongsToRelationResult, type CastType, DrizzleAdapter, type FindManyOptions, GlobalScopeRegistry, type GroupCondition, type HasManyRecord, type HasManyRelationResult, type HasManyThroughRecord, type HasManyThroughRelationResult, type HasOneRecord, type HasOneRelationResult, type HookCallback, type HookName, type InferModelInsert, type InferModelRecord, MassAssignmentException, type MigrationStatusEntry, Model, type ModelHooks, ModelNotFoundException, type ModelObserver, type ModelObserverConstructor, type MorphManyRecord, type MorphManyRelationResult, type MorphToRecord, type MorphToRelationResult, type MutatorDefinitions, type MutatorFn, type MySqlDatabase, type MySqlDatabaseOptions, type ORMAdapter, type ORMAdapterAdvanced, type OrderByClause, type OrderByInput, type OrderDefinition, type OrderDirection, type PaginateOptions, type PaginatedResult, type PaginationMeta, type PlainObject, type PostgresDatabase, type PostgresDatabaseOptions, QueryBuilder, type QueryBuilderOptions, type ScopeFunction, type SeederContext, type SeederHandler, type SimpleCondition, SoftDeletes, type SoftDeletesStatic, type SqliteDatabase, type SqliteDatabaseOptions, type TransactionHandle, type TransactionModelScope, type WhereClause, type WhereCondition, type WhereOperator, type WithRelations, applyAccessors, applyMutators, buildDrizzleConditions, createMySqlDatabase, createPostgresDatabase, createSqliteDatabase, defineModel, defineSeeder, executeHook, executeObservers, loadSeeders, runSeeders, serializeRecord, serializeRecords };
|
package/dist/index.js
CHANGED
|
@@ -879,70 +879,20 @@ var QueryBuilder = class {
|
|
|
879
879
|
}
|
|
880
880
|
/**
|
|
881
881
|
* Load eager relations onto fetched results.
|
|
882
|
-
* Supports dot notation for nested relations
|
|
882
|
+
* Supports dot notation for nested relations at any depth
|
|
883
|
+
* (e.g., 'posts.comments.author') — the full path is delegated to
|
|
884
|
+
* Model.loadRelationInto, which recurses through the relation chain.
|
|
883
885
|
*/
|
|
884
886
|
loadEagerRelations = async (results) => {
|
|
885
887
|
if (this.eagerLoad.length === 0 || results.length === 0) return results;
|
|
886
888
|
const copies = results.map((r) => ({ ...r }));
|
|
887
889
|
const model = this.modelClass;
|
|
888
890
|
for (const relation of this.eagerLoad) {
|
|
889
|
-
|
|
890
|
-
const topLevel = parts[0];
|
|
891
|
-
await model.loadRelationInto(copies, topLevel);
|
|
892
|
-
if (parts.length > 1) {
|
|
893
|
-
const nestedName = parts.slice(1).join(".");
|
|
894
|
-
const nestedRecords = [];
|
|
895
|
-
for (const record of copies) {
|
|
896
|
-
const related = record[topLevel];
|
|
897
|
-
if (Array.isArray(related)) {
|
|
898
|
-
nestedRecords.push(...related);
|
|
899
|
-
} else if (related && typeof related === "object") {
|
|
900
|
-
nestedRecords.push(related);
|
|
901
|
-
}
|
|
902
|
-
}
|
|
903
|
-
if (nestedRecords.length > 0) {
|
|
904
|
-
const topDef = model.getRelationDefinition(topLevel);
|
|
905
|
-
if (topDef) {
|
|
906
|
-
const relatedModel = await resolveRelatedModel(topDef.related);
|
|
907
|
-
if (relatedModel) {
|
|
908
|
-
const nestedModel = relatedModel;
|
|
909
|
-
const nestedParts = parts.slice(1);
|
|
910
|
-
await nestedModel.loadRelationInto(nestedRecords, nestedParts[0]);
|
|
911
|
-
if (nestedParts.length > 1) {
|
|
912
|
-
for (let i = 1; i < nestedParts.length; i++) {
|
|
913
|
-
const subRecords = [];
|
|
914
|
-
for (const nr of nestedRecords) {
|
|
915
|
-
const sub = nr[nestedParts[i - 1]];
|
|
916
|
-
if (Array.isArray(sub)) subRecords.push(...sub);
|
|
917
|
-
else if (sub && typeof sub === "object") subRecords.push(sub);
|
|
918
|
-
}
|
|
919
|
-
if (subRecords.length === 0) break;
|
|
920
|
-
}
|
|
921
|
-
}
|
|
922
|
-
}
|
|
923
|
-
}
|
|
924
|
-
}
|
|
925
|
-
}
|
|
891
|
+
await model.loadRelationInto(copies, relation);
|
|
926
892
|
}
|
|
927
893
|
return copies;
|
|
928
894
|
};
|
|
929
895
|
};
|
|
930
|
-
async function resolveRelatedModel(reference) {
|
|
931
|
-
if (typeof reference === "function" && "prototype" in reference) {
|
|
932
|
-
try {
|
|
933
|
-
if (reference.resolveTable) return reference;
|
|
934
|
-
} catch {
|
|
935
|
-
}
|
|
936
|
-
}
|
|
937
|
-
if (typeof reference === "function") {
|
|
938
|
-
try {
|
|
939
|
-
return await reference();
|
|
940
|
-
} catch {
|
|
941
|
-
return null;
|
|
942
|
-
}
|
|
943
|
-
}
|
|
944
|
-
return null;
|
|
945
|
-
}
|
|
946
896
|
|
|
947
897
|
// src/serialization.ts
|
|
948
898
|
function serializeRecord(record, options) {
|
|
@@ -971,6 +921,21 @@ function serializeRecords(records, options) {
|
|
|
971
921
|
return records.map((r) => serializeRecord(r, options));
|
|
972
922
|
}
|
|
973
923
|
|
|
924
|
+
// src/MassAssignmentException.ts
|
|
925
|
+
var MassAssignmentException = class extends Error {
|
|
926
|
+
model;
|
|
927
|
+
fields;
|
|
928
|
+
constructor(model, fields) {
|
|
929
|
+
const list = fields.map((field) => `"${field}"`).join(", ");
|
|
930
|
+
super(
|
|
931
|
+
`${model}: mass assignment blocked for field(s) ${list} \u2014 not listed in ${model}.fillable. Add them to fillable, use ${model}.forceCreate()/forceUpdate() for trusted server-side data, or set \`static strictFillable = false\` to restore silent discarding.`
|
|
932
|
+
);
|
|
933
|
+
this.name = "MassAssignmentException";
|
|
934
|
+
this.model = model;
|
|
935
|
+
this.fields = fields;
|
|
936
|
+
}
|
|
937
|
+
};
|
|
938
|
+
|
|
974
939
|
// src/Model.ts
|
|
975
940
|
var DEFAULT_PAGINATION_SIZE = 15;
|
|
976
941
|
var Model = class _Model {
|
|
@@ -1039,7 +1004,12 @@ var Model = class _Model {
|
|
|
1039
1004
|
static casts;
|
|
1040
1005
|
/**
|
|
1041
1006
|
* Whitelist of fields allowed for mass assignment.
|
|
1042
|
-
* If set,
|
|
1007
|
+
* If set, `create()` and `update()` accept only these fields — input
|
|
1008
|
+
* containing any other key throws a MassAssignmentException so bugs
|
|
1009
|
+
* (and injection attempts) surface immediately instead of being
|
|
1010
|
+
* silently discarded. Use `forceCreate()` / `forceUpdate()` for
|
|
1011
|
+
* trusted server-side data, or set `strictFillable = false` to
|
|
1012
|
+
* restore silent discarding.
|
|
1043
1013
|
*
|
|
1044
1014
|
* @example
|
|
1045
1015
|
* class User extends Model<UserRecord> {
|
|
@@ -1047,6 +1017,12 @@ var Model = class _Model {
|
|
|
1047
1017
|
* }
|
|
1048
1018
|
*/
|
|
1049
1019
|
static fillable;
|
|
1020
|
+
/**
|
|
1021
|
+
* When `fillable` is set, controls what happens to input fields outside
|
|
1022
|
+
* the allowlist: `true` (default) throws a MassAssignmentException,
|
|
1023
|
+
* `false` silently discards them (pre-1.0 behavior).
|
|
1024
|
+
*/
|
|
1025
|
+
static strictFillable;
|
|
1050
1026
|
/**
|
|
1051
1027
|
* Blacklist of fields excluded from mass assignment.
|
|
1052
1028
|
* Defaults to `['id']`. Ignored if `fillable` is set.
|
|
@@ -1349,6 +1325,12 @@ var Model = class _Model {
|
|
|
1349
1325
|
static filterFillable(data) {
|
|
1350
1326
|
const fillableFields = this.fillable;
|
|
1351
1327
|
if (fillableFields) {
|
|
1328
|
+
if (this.strictFillable !== false) {
|
|
1329
|
+
const blocked = Object.keys(data).filter((key) => !fillableFields.includes(key));
|
|
1330
|
+
if (blocked.length > 0) {
|
|
1331
|
+
throw new MassAssignmentException(this.name, blocked);
|
|
1332
|
+
}
|
|
1333
|
+
}
|
|
1352
1334
|
const filtered2 = {};
|
|
1353
1335
|
for (const key of fillableFields) {
|
|
1354
1336
|
if (key in data) {
|
|
@@ -1941,8 +1923,26 @@ var Model = class _Model {
|
|
|
1941
1923
|
* })
|
|
1942
1924
|
*/
|
|
1943
1925
|
static async create(data, writeOptions) {
|
|
1926
|
+
return this.runCreate(data, writeOptions, true);
|
|
1927
|
+
}
|
|
1928
|
+
/**
|
|
1929
|
+
* Create a record bypassing mass-assignment protection. Use for trusted,
|
|
1930
|
+
* server-side-assembled data (OAuth account linking, seeders, system
|
|
1931
|
+
* records) — never for raw request input.
|
|
1932
|
+
*
|
|
1933
|
+
* @example
|
|
1934
|
+
* const user = await User.forceCreate({
|
|
1935
|
+
* name: profile.name,
|
|
1936
|
+
* email: profile.email,
|
|
1937
|
+
* passwordHash: `oauth:${provider}:${profile.id}`,
|
|
1938
|
+
* })
|
|
1939
|
+
*/
|
|
1940
|
+
static async forceCreate(data, writeOptions) {
|
|
1941
|
+
return this.runCreate(data, writeOptions, false);
|
|
1942
|
+
}
|
|
1943
|
+
static async runCreate(data, writeOptions, applyFillable) {
|
|
1944
1944
|
const table = this.resolveTable();
|
|
1945
|
-
const filtered = this.filterFillable(data);
|
|
1945
|
+
const filtered = applyFillable ? this.filterFillable(data) : { ...data };
|
|
1946
1946
|
const payload = await this.preparePersistencePayload(filtered);
|
|
1947
1947
|
const hooks = this.hooks;
|
|
1948
1948
|
const observers = this.observers;
|
|
@@ -1989,12 +1989,22 @@ var Model = class _Model {
|
|
|
1989
1989
|
* await User.update({ id: 1 }, { name: 'Jane Doe' })
|
|
1990
1990
|
*/
|
|
1991
1991
|
static async update(where, data, writeOptions) {
|
|
1992
|
+
return this.runUpdate(where, data, writeOptions, true);
|
|
1993
|
+
}
|
|
1994
|
+
/**
|
|
1995
|
+
* Update records bypassing mass-assignment protection. Use for trusted,
|
|
1996
|
+
* server-side-assembled data — never for raw request input.
|
|
1997
|
+
*/
|
|
1998
|
+
static async forceUpdate(where, data, writeOptions) {
|
|
1999
|
+
return this.runUpdate(where, data, writeOptions, false);
|
|
2000
|
+
}
|
|
2001
|
+
static async runUpdate(where, data, writeOptions, applyFillable) {
|
|
1992
2002
|
const table = this.resolveTable();
|
|
1993
2003
|
const adapter = this.getAdapter();
|
|
1994
2004
|
if (!adapter.update) {
|
|
1995
2005
|
throw new Error("Configured adapter does not support update operations.");
|
|
1996
2006
|
}
|
|
1997
|
-
const filtered = this.filterFillable(data);
|
|
2007
|
+
const filtered = applyFillable ? this.filterFillable(data) : { ...data };
|
|
1998
2008
|
const payload = await this.preparePersistencePayload(filtered);
|
|
1999
2009
|
const hooks = this.hooks;
|
|
2000
2010
|
const observers = this.observers;
|
|
@@ -3134,6 +3144,7 @@ function createSqliteDatabase(options) {
|
|
|
3134
3144
|
export {
|
|
3135
3145
|
DrizzleAdapter,
|
|
3136
3146
|
GlobalScopeRegistry,
|
|
3147
|
+
MassAssignmentException,
|
|
3137
3148
|
Model,
|
|
3138
3149
|
ModelNotFoundException,
|
|
3139
3150
|
QueryBuilder,
|