@guren/orm 1.0.0-rc.25 → 1.0.0-rc.27
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 +59 -4
- package/dist/index.js +84 -5
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -210,10 +210,22 @@ declare class QueryBuilder<TRecord extends PlainObject = PlainObject, TResult ex
|
|
|
210
210
|
}): Promise<PaginatedResult<TResult>>;
|
|
211
211
|
/**
|
|
212
212
|
* Bulk update records matching the current conditions.
|
|
213
|
+
*
|
|
214
|
+
* Mass-assignment protection applies exactly as in `Model.update()`:
|
|
215
|
+
* with a `fillable` allowlist, out-of-allowlist keys throw a
|
|
216
|
+
* MassAssignmentException. Note that bulk updates skip model hooks,
|
|
217
|
+
* observers, mutators, and casts by design.
|
|
218
|
+
*
|
|
213
219
|
* @param data - Data to set on matching records
|
|
214
220
|
* @returns The updated record (adapter-dependent)
|
|
215
221
|
*/
|
|
216
222
|
update(data: PlainObject): Promise<TRecord>;
|
|
223
|
+
/**
|
|
224
|
+
* Bulk update bypassing mass-assignment protection. Use for trusted,
|
|
225
|
+
* server-side-assembled data — never for raw request input.
|
|
226
|
+
*/
|
|
227
|
+
forceUpdate(data: PlainObject): Promise<TRecord>;
|
|
228
|
+
private runBulkUpdate;
|
|
217
229
|
/**
|
|
218
230
|
* Bulk delete records matching the current conditions.
|
|
219
231
|
* @returns Number of deleted records (adapter-dependent)
|
|
@@ -433,7 +445,7 @@ interface PaginateOptions<TRecord extends PlainObject = PlainObject> {
|
|
|
433
445
|
orderBy?: OrderByInput<TRecord>;
|
|
434
446
|
}
|
|
435
447
|
/** Pagination metadata returned with paginated results. */
|
|
436
|
-
interface
|
|
448
|
+
interface ModelPaginationMeta {
|
|
437
449
|
/** Total number of records matching the query */
|
|
438
450
|
total: number;
|
|
439
451
|
/** Number of records per page */
|
|
@@ -454,7 +466,7 @@ interface PaginatedResult<TRecord extends PlainObject = PlainObject> {
|
|
|
454
466
|
/** Records for the current page */
|
|
455
467
|
data: TRecord[];
|
|
456
468
|
/** Pagination metadata */
|
|
457
|
-
meta:
|
|
469
|
+
meta: ModelPaginationMeta;
|
|
458
470
|
}
|
|
459
471
|
/**
|
|
460
472
|
* Interface for ORM adapters that power the Model class.
|
|
@@ -579,7 +591,12 @@ declare abstract class Model<TRecord extends PlainObject = PlainObject> {
|
|
|
579
591
|
static casts?: Record<string, CastType>;
|
|
580
592
|
/**
|
|
581
593
|
* Whitelist of fields allowed for mass assignment.
|
|
582
|
-
* If set,
|
|
594
|
+
* If set, `create()` and `update()` accept only these fields — input
|
|
595
|
+
* containing any other key throws a MassAssignmentException so bugs
|
|
596
|
+
* (and injection attempts) surface immediately instead of being
|
|
597
|
+
* silently discarded. Use `forceCreate()` / `forceUpdate()` for
|
|
598
|
+
* trusted server-side data, or set `strictFillable = false` to
|
|
599
|
+
* restore silent discarding.
|
|
583
600
|
*
|
|
584
601
|
* @example
|
|
585
602
|
* class User extends Model<UserRecord> {
|
|
@@ -587,6 +604,12 @@ declare abstract class Model<TRecord extends PlainObject = PlainObject> {
|
|
|
587
604
|
* }
|
|
588
605
|
*/
|
|
589
606
|
static fillable?: string[];
|
|
607
|
+
/**
|
|
608
|
+
* When `fillable` is set, controls what happens to input fields outside
|
|
609
|
+
* the allowlist: `true` (default) throws a MassAssignmentException,
|
|
610
|
+
* `false` silently discards them (pre-1.0 behavior).
|
|
611
|
+
*/
|
|
612
|
+
static strictFillable?: boolean;
|
|
590
613
|
/**
|
|
591
614
|
* Blacklist of fields excluded from mass assignment.
|
|
592
615
|
* Defaults to `['id']`. Ignored if `fillable` is set.
|
|
@@ -1098,6 +1121,20 @@ declare abstract class Model<TRecord extends PlainObject = PlainObject> {
|
|
|
1098
1121
|
* })
|
|
1099
1122
|
*/
|
|
1100
1123
|
static create<T extends typeof Model>(this: T, data: TCreateFor<T>, writeOptions?: ModelWriteOptions): Promise<TRecordFor<T>>;
|
|
1124
|
+
/**
|
|
1125
|
+
* Create a record bypassing mass-assignment protection. Use for trusted,
|
|
1126
|
+
* server-side-assembled data (OAuth account linking, seeders, system
|
|
1127
|
+
* records) — never for raw request input.
|
|
1128
|
+
*
|
|
1129
|
+
* @example
|
|
1130
|
+
* const user = await User.forceCreate({
|
|
1131
|
+
* name: profile.name,
|
|
1132
|
+
* email: profile.email,
|
|
1133
|
+
* passwordHash: `oauth:${provider}:${profile.id}`,
|
|
1134
|
+
* })
|
|
1135
|
+
*/
|
|
1136
|
+
static forceCreate<T extends typeof Model>(this: T, data: TCreateFor<T>, writeOptions?: ModelWriteOptions): Promise<TRecordFor<T>>;
|
|
1137
|
+
protected static runCreate<T extends typeof Model>(this: T, data: TCreateFor<T>, writeOptions: ModelWriteOptions | undefined, applyFillable: boolean): Promise<TRecordFor<T>>;
|
|
1101
1138
|
/**
|
|
1102
1139
|
* Update records matching the conditions.
|
|
1103
1140
|
*
|
|
@@ -1109,6 +1146,12 @@ declare abstract class Model<TRecord extends PlainObject = PlainObject> {
|
|
|
1109
1146
|
* await User.update({ id: 1 }, { name: 'Jane Doe' })
|
|
1110
1147
|
*/
|
|
1111
1148
|
static update<T extends typeof Model>(this: T, where: WhereClauseFor<T>, data: Partial<TCreateFor<T>>, writeOptions?: ModelWriteOptions): Promise<TRecordFor<T>>;
|
|
1149
|
+
/**
|
|
1150
|
+
* Update records bypassing mass-assignment protection. Use for trusted,
|
|
1151
|
+
* server-side-assembled data — never for raw request input.
|
|
1152
|
+
*/
|
|
1153
|
+
static forceUpdate<T extends typeof Model>(this: T, where: WhereClauseFor<T>, data: Partial<TCreateFor<T>>, writeOptions?: ModelWriteOptions): Promise<TRecordFor<T>>;
|
|
1154
|
+
protected static runUpdate<T extends typeof Model>(this: T, where: WhereClauseFor<T>, data: Partial<TCreateFor<T>>, writeOptions: ModelWriteOptions | undefined, applyFillable: boolean): Promise<TRecordFor<T>>;
|
|
1112
1155
|
/**
|
|
1113
1156
|
* Delete records matching the conditions.
|
|
1114
1157
|
*
|
|
@@ -1328,6 +1371,18 @@ declare class ModelNotFoundException extends Error {
|
|
|
1328
1371
|
constructor(modelName: string, id: unknown, key?: string);
|
|
1329
1372
|
}
|
|
1330
1373
|
|
|
1374
|
+
/**
|
|
1375
|
+
* Thrown when mass assignment receives fields outside the model's
|
|
1376
|
+
* `fillable` allowlist. Silently discarding them hides bugs (a typo'd
|
|
1377
|
+
* fillable entry surfaces as a NOT NULL violation much later) and can
|
|
1378
|
+
* mask injection attempts, so strict models fail loudly instead.
|
|
1379
|
+
*/
|
|
1380
|
+
declare class MassAssignmentException extends Error {
|
|
1381
|
+
readonly model: string;
|
|
1382
|
+
readonly fields: string[];
|
|
1383
|
+
constructor(model: string, fields: string[]);
|
|
1384
|
+
}
|
|
1385
|
+
|
|
1331
1386
|
/**
|
|
1332
1387
|
* Interface describing the static methods added by the SoftDeletes mixin.
|
|
1333
1388
|
*/
|
|
@@ -1544,4 +1599,4 @@ declare function serializeRecord(record: PlainObject, options: {
|
|
|
1544
1599
|
/** Serialize an array of records. */
|
|
1545
1600
|
declare function serializeRecords(records: PlainObject[], options: Parameters<typeof serializeRecord>[1]): PlainObject[];
|
|
1546
1601
|
|
|
1547
|
-
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
|
|
1602
|
+
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 ModelPaginationMeta, 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 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
|
@@ -763,20 +763,38 @@ var QueryBuilder = class {
|
|
|
763
763
|
}
|
|
764
764
|
/**
|
|
765
765
|
* Bulk update records matching the current conditions.
|
|
766
|
+
*
|
|
767
|
+
* Mass-assignment protection applies exactly as in `Model.update()`:
|
|
768
|
+
* with a `fillable` allowlist, out-of-allowlist keys throw a
|
|
769
|
+
* MassAssignmentException. Note that bulk updates skip model hooks,
|
|
770
|
+
* observers, mutators, and casts by design.
|
|
771
|
+
*
|
|
766
772
|
* @param data - Data to set on matching records
|
|
767
773
|
* @returns The updated record (adapter-dependent)
|
|
768
774
|
*/
|
|
769
775
|
async update(data) {
|
|
776
|
+
return this.runBulkUpdate(data, true);
|
|
777
|
+
}
|
|
778
|
+
/**
|
|
779
|
+
* Bulk update bypassing mass-assignment protection. Use for trusted,
|
|
780
|
+
* server-side-assembled data — never for raw request input.
|
|
781
|
+
*/
|
|
782
|
+
async forceUpdate(data) {
|
|
783
|
+
return this.runBulkUpdate(data, false);
|
|
784
|
+
}
|
|
785
|
+
async runBulkUpdate(data, applyFillable) {
|
|
770
786
|
if (!this.adapter.update) {
|
|
771
787
|
throw new Error("Configured adapter does not support update operations.");
|
|
772
788
|
}
|
|
789
|
+
const model = this.modelClass;
|
|
790
|
+
const payload = applyFillable ? model.filterFillable(data) : { ...data };
|
|
773
791
|
const advancedAdapter = this.adapter;
|
|
774
792
|
if (typeof advancedAdapter.updateAdvanced === "function") {
|
|
775
|
-
return advancedAdapter.updateAdvanced(this.table, this.conditions,
|
|
793
|
+
return advancedAdapter.updateAdvanced(this.table, this.conditions, payload, { trx: this.options.trx });
|
|
776
794
|
}
|
|
777
795
|
const simpleWhere = this.toSimpleWhereClause();
|
|
778
796
|
if (simpleWhere) {
|
|
779
|
-
return this.adapter.update(this.table, simpleWhere,
|
|
797
|
+
return this.adapter.update(this.table, simpleWhere, payload, { trx: this.options.trx });
|
|
780
798
|
}
|
|
781
799
|
throw new Error("Advanced conditions require an adapter that supports updateAdvanced.");
|
|
782
800
|
}
|
|
@@ -921,6 +939,21 @@ function serializeRecords(records, options) {
|
|
|
921
939
|
return records.map((r) => serializeRecord(r, options));
|
|
922
940
|
}
|
|
923
941
|
|
|
942
|
+
// src/MassAssignmentException.ts
|
|
943
|
+
var MassAssignmentException = class extends Error {
|
|
944
|
+
model;
|
|
945
|
+
fields;
|
|
946
|
+
constructor(model, fields) {
|
|
947
|
+
const list = fields.map((field) => `"${field}"`).join(", ");
|
|
948
|
+
super(
|
|
949
|
+
`${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.`
|
|
950
|
+
);
|
|
951
|
+
this.name = "MassAssignmentException";
|
|
952
|
+
this.model = model;
|
|
953
|
+
this.fields = fields;
|
|
954
|
+
}
|
|
955
|
+
};
|
|
956
|
+
|
|
924
957
|
// src/Model.ts
|
|
925
958
|
var DEFAULT_PAGINATION_SIZE = 15;
|
|
926
959
|
var Model = class _Model {
|
|
@@ -989,7 +1022,12 @@ var Model = class _Model {
|
|
|
989
1022
|
static casts;
|
|
990
1023
|
/**
|
|
991
1024
|
* Whitelist of fields allowed for mass assignment.
|
|
992
|
-
* If set,
|
|
1025
|
+
* If set, `create()` and `update()` accept only these fields — input
|
|
1026
|
+
* containing any other key throws a MassAssignmentException so bugs
|
|
1027
|
+
* (and injection attempts) surface immediately instead of being
|
|
1028
|
+
* silently discarded. Use `forceCreate()` / `forceUpdate()` for
|
|
1029
|
+
* trusted server-side data, or set `strictFillable = false` to
|
|
1030
|
+
* restore silent discarding.
|
|
993
1031
|
*
|
|
994
1032
|
* @example
|
|
995
1033
|
* class User extends Model<UserRecord> {
|
|
@@ -997,6 +1035,12 @@ var Model = class _Model {
|
|
|
997
1035
|
* }
|
|
998
1036
|
*/
|
|
999
1037
|
static fillable;
|
|
1038
|
+
/**
|
|
1039
|
+
* When `fillable` is set, controls what happens to input fields outside
|
|
1040
|
+
* the allowlist: `true` (default) throws a MassAssignmentException,
|
|
1041
|
+
* `false` silently discards them (pre-1.0 behavior).
|
|
1042
|
+
*/
|
|
1043
|
+
static strictFillable;
|
|
1000
1044
|
/**
|
|
1001
1045
|
* Blacklist of fields excluded from mass assignment.
|
|
1002
1046
|
* Defaults to `['id']`. Ignored if `fillable` is set.
|
|
@@ -1299,6 +1343,12 @@ var Model = class _Model {
|
|
|
1299
1343
|
static filterFillable(data) {
|
|
1300
1344
|
const fillableFields = this.fillable;
|
|
1301
1345
|
if (fillableFields) {
|
|
1346
|
+
if (this.strictFillable !== false) {
|
|
1347
|
+
const blocked = Object.keys(data).filter((key) => !fillableFields.includes(key));
|
|
1348
|
+
if (blocked.length > 0) {
|
|
1349
|
+
throw new MassAssignmentException(this.name, blocked);
|
|
1350
|
+
}
|
|
1351
|
+
}
|
|
1302
1352
|
const filtered2 = {};
|
|
1303
1353
|
for (const key of fillableFields) {
|
|
1304
1354
|
if (key in data) {
|
|
@@ -1891,8 +1941,26 @@ var Model = class _Model {
|
|
|
1891
1941
|
* })
|
|
1892
1942
|
*/
|
|
1893
1943
|
static async create(data, writeOptions) {
|
|
1944
|
+
return this.runCreate(data, writeOptions, true);
|
|
1945
|
+
}
|
|
1946
|
+
/**
|
|
1947
|
+
* Create a record bypassing mass-assignment protection. Use for trusted,
|
|
1948
|
+
* server-side-assembled data (OAuth account linking, seeders, system
|
|
1949
|
+
* records) — never for raw request input.
|
|
1950
|
+
*
|
|
1951
|
+
* @example
|
|
1952
|
+
* const user = await User.forceCreate({
|
|
1953
|
+
* name: profile.name,
|
|
1954
|
+
* email: profile.email,
|
|
1955
|
+
* passwordHash: `oauth:${provider}:${profile.id}`,
|
|
1956
|
+
* })
|
|
1957
|
+
*/
|
|
1958
|
+
static async forceCreate(data, writeOptions) {
|
|
1959
|
+
return this.runCreate(data, writeOptions, false);
|
|
1960
|
+
}
|
|
1961
|
+
static async runCreate(data, writeOptions, applyFillable) {
|
|
1894
1962
|
const table = this.resolveTable();
|
|
1895
|
-
const filtered = this.filterFillable(data);
|
|
1963
|
+
const filtered = applyFillable ? this.filterFillable(data) : { ...data };
|
|
1896
1964
|
const payload = await this.preparePersistencePayload(filtered);
|
|
1897
1965
|
const hooks = this.hooks;
|
|
1898
1966
|
const observers = this.observers;
|
|
@@ -1939,12 +2007,22 @@ var Model = class _Model {
|
|
|
1939
2007
|
* await User.update({ id: 1 }, { name: 'Jane Doe' })
|
|
1940
2008
|
*/
|
|
1941
2009
|
static async update(where, data, writeOptions) {
|
|
2010
|
+
return this.runUpdate(where, data, writeOptions, true);
|
|
2011
|
+
}
|
|
2012
|
+
/**
|
|
2013
|
+
* Update records bypassing mass-assignment protection. Use for trusted,
|
|
2014
|
+
* server-side-assembled data — never for raw request input.
|
|
2015
|
+
*/
|
|
2016
|
+
static async forceUpdate(where, data, writeOptions) {
|
|
2017
|
+
return this.runUpdate(where, data, writeOptions, false);
|
|
2018
|
+
}
|
|
2019
|
+
static async runUpdate(where, data, writeOptions, applyFillable) {
|
|
1942
2020
|
const table = this.resolveTable();
|
|
1943
2021
|
const adapter = this.getAdapter();
|
|
1944
2022
|
if (!adapter.update) {
|
|
1945
2023
|
throw new Error("Configured adapter does not support update operations.");
|
|
1946
2024
|
}
|
|
1947
|
-
const filtered = this.filterFillable(data);
|
|
2025
|
+
const filtered = applyFillable ? this.filterFillable(data) : { ...data };
|
|
1948
2026
|
const payload = await this.preparePersistencePayload(filtered);
|
|
1949
2027
|
const hooks = this.hooks;
|
|
1950
2028
|
const observers = this.observers;
|
|
@@ -3084,6 +3162,7 @@ function createSqliteDatabase(options) {
|
|
|
3084
3162
|
export {
|
|
3085
3163
|
DrizzleAdapter,
|
|
3086
3164
|
GlobalScopeRegistry,
|
|
3165
|
+
MassAssignmentException,
|
|
3087
3166
|
Model,
|
|
3088
3167
|
ModelNotFoundException,
|
|
3089
3168
|
QueryBuilder,
|