@guren/orm 1.0.0-rc.25 → 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 +45 -2
- package/dist/index.js +64 -3
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -579,7 +579,12 @@ declare abstract class Model<TRecord extends PlainObject = PlainObject> {
|
|
|
579
579
|
static casts?: Record<string, CastType>;
|
|
580
580
|
/**
|
|
581
581
|
* Whitelist of fields allowed for mass assignment.
|
|
582
|
-
* 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.
|
|
583
588
|
*
|
|
584
589
|
* @example
|
|
585
590
|
* class User extends Model<UserRecord> {
|
|
@@ -587,6 +592,12 @@ declare abstract class Model<TRecord extends PlainObject = PlainObject> {
|
|
|
587
592
|
* }
|
|
588
593
|
*/
|
|
589
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;
|
|
590
601
|
/**
|
|
591
602
|
* Blacklist of fields excluded from mass assignment.
|
|
592
603
|
* Defaults to `['id']`. Ignored if `fillable` is set.
|
|
@@ -1098,6 +1109,20 @@ declare abstract class Model<TRecord extends PlainObject = PlainObject> {
|
|
|
1098
1109
|
* })
|
|
1099
1110
|
*/
|
|
1100
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>>;
|
|
1101
1126
|
/**
|
|
1102
1127
|
* Update records matching the conditions.
|
|
1103
1128
|
*
|
|
@@ -1109,6 +1134,12 @@ declare abstract class Model<TRecord extends PlainObject = PlainObject> {
|
|
|
1109
1134
|
* await User.update({ id: 1 }, { name: 'Jane Doe' })
|
|
1110
1135
|
*/
|
|
1111
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>>;
|
|
1112
1143
|
/**
|
|
1113
1144
|
* Delete records matching the conditions.
|
|
1114
1145
|
*
|
|
@@ -1328,6 +1359,18 @@ declare class ModelNotFoundException extends Error {
|
|
|
1328
1359
|
constructor(modelName: string, id: unknown, key?: string);
|
|
1329
1360
|
}
|
|
1330
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
|
+
|
|
1331
1374
|
/**
|
|
1332
1375
|
* Interface describing the static methods added by the SoftDeletes mixin.
|
|
1333
1376
|
*/
|
|
@@ -1544,4 +1587,4 @@ declare function serializeRecord(record: PlainObject, options: {
|
|
|
1544
1587
|
/** Serialize an array of records. */
|
|
1545
1588
|
declare function serializeRecords(records: PlainObject[], options: Parameters<typeof serializeRecord>[1]): PlainObject[];
|
|
1546
1589
|
|
|
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 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
|
@@ -921,6 +921,21 @@ function serializeRecords(records, options) {
|
|
|
921
921
|
return records.map((r) => serializeRecord(r, options));
|
|
922
922
|
}
|
|
923
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
|
+
|
|
924
939
|
// src/Model.ts
|
|
925
940
|
var DEFAULT_PAGINATION_SIZE = 15;
|
|
926
941
|
var Model = class _Model {
|
|
@@ -989,7 +1004,12 @@ var Model = class _Model {
|
|
|
989
1004
|
static casts;
|
|
990
1005
|
/**
|
|
991
1006
|
* Whitelist of fields allowed for mass assignment.
|
|
992
|
-
* 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.
|
|
993
1013
|
*
|
|
994
1014
|
* @example
|
|
995
1015
|
* class User extends Model<UserRecord> {
|
|
@@ -997,6 +1017,12 @@ var Model = class _Model {
|
|
|
997
1017
|
* }
|
|
998
1018
|
*/
|
|
999
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;
|
|
1000
1026
|
/**
|
|
1001
1027
|
* Blacklist of fields excluded from mass assignment.
|
|
1002
1028
|
* Defaults to `['id']`. Ignored if `fillable` is set.
|
|
@@ -1299,6 +1325,12 @@ var Model = class _Model {
|
|
|
1299
1325
|
static filterFillable(data) {
|
|
1300
1326
|
const fillableFields = this.fillable;
|
|
1301
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
|
+
}
|
|
1302
1334
|
const filtered2 = {};
|
|
1303
1335
|
for (const key of fillableFields) {
|
|
1304
1336
|
if (key in data) {
|
|
@@ -1891,8 +1923,26 @@ var Model = class _Model {
|
|
|
1891
1923
|
* })
|
|
1892
1924
|
*/
|
|
1893
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) {
|
|
1894
1944
|
const table = this.resolveTable();
|
|
1895
|
-
const filtered = this.filterFillable(data);
|
|
1945
|
+
const filtered = applyFillable ? this.filterFillable(data) : { ...data };
|
|
1896
1946
|
const payload = await this.preparePersistencePayload(filtered);
|
|
1897
1947
|
const hooks = this.hooks;
|
|
1898
1948
|
const observers = this.observers;
|
|
@@ -1939,12 +1989,22 @@ var Model = class _Model {
|
|
|
1939
1989
|
* await User.update({ id: 1 }, { name: 'Jane Doe' })
|
|
1940
1990
|
*/
|
|
1941
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) {
|
|
1942
2002
|
const table = this.resolveTable();
|
|
1943
2003
|
const adapter = this.getAdapter();
|
|
1944
2004
|
if (!adapter.update) {
|
|
1945
2005
|
throw new Error("Configured adapter does not support update operations.");
|
|
1946
2006
|
}
|
|
1947
|
-
const filtered = this.filterFillable(data);
|
|
2007
|
+
const filtered = applyFillable ? this.filterFillable(data) : { ...data };
|
|
1948
2008
|
const payload = await this.preparePersistencePayload(filtered);
|
|
1949
2009
|
const hooks = this.hooks;
|
|
1950
2010
|
const observers = this.observers;
|
|
@@ -3084,6 +3144,7 @@ function createSqliteDatabase(options) {
|
|
|
3084
3144
|
export {
|
|
3085
3145
|
DrizzleAdapter,
|
|
3086
3146
|
GlobalScopeRegistry,
|
|
3147
|
+
MassAssignmentException,
|
|
3087
3148
|
Model,
|
|
3088
3149
|
ModelNotFoundException,
|
|
3089
3150
|
QueryBuilder,
|