@guren/orm 2.0.0 → 2.1.0
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 +61 -2
- package/dist/index.js +5 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1375,6 +1375,27 @@ type ModelClassWithTable<TTable extends TableShape, TBase extends typeof Model,
|
|
|
1375
1375
|
};
|
|
1376
1376
|
/** A key that can appear in a model's create payload: a table column, or a named field its base contributes. */
|
|
1377
1377
|
type CreateKey<TTable extends TableShape, TBase extends typeof Model> = keyof InferModelInsert<TTable> | keyof TCreateFor<TBase>;
|
|
1378
|
+
/**
|
|
1379
|
+
* Named fields a base class contributes to the create payload, such as
|
|
1380
|
+
* `AuthenticatableModel`'s virtual `password`. A plain `Model` base has no
|
|
1381
|
+
* declared createType, so its keys collapse to `string` — guard against that
|
|
1382
|
+
* collapse, or every misspelling would pass the allowlist checks below.
|
|
1383
|
+
*/
|
|
1384
|
+
type BaseContributedKey<TBase extends typeof Model> = string extends keyof TCreateFor<TBase> & string ? never : keyof TCreateFor<TBase> & string;
|
|
1385
|
+
/** A key valid in a `fillable` allowlist: an insertable column, or a base-contributed field. */
|
|
1386
|
+
type FillableKey<TTable extends TableShape, TBase extends typeof Model> = (keyof InferModelInsert<TTable> & string) | BaseContributedKey<TBase>;
|
|
1387
|
+
/** A key of the record read back from the table. */
|
|
1388
|
+
type RecordKey<TTable extends TableShape> = keyof InferModelRecord<TTable> & string;
|
|
1389
|
+
/**
|
|
1390
|
+
* Accessor map keyed by TKey whose functions receive the table's inferred
|
|
1391
|
+
* record. A homomorphic mapped type so TypeScript infers the key union from
|
|
1392
|
+
* the object literal's keys — a plain `Record<string, fn>` constraint fails
|
|
1393
|
+
* here: the accessor functions are context-sensitive, which defers inference
|
|
1394
|
+
* past the point where the key type parameter falls back to its default.
|
|
1395
|
+
*/
|
|
1396
|
+
type AccessorsShape<TTable extends TableShape, TKey extends string> = {
|
|
1397
|
+
[K in TKey]: (record: InferModelRecord<TTable>) => unknown;
|
|
1398
|
+
};
|
|
1378
1399
|
type CreateShape<TTable extends TableShape, TBase extends typeof Model, TOptional extends keyof InferModelInsert<TTable>, TRequire extends CreateKey<TTable, TBase>> = Partial<Pick<InferModelInsert<TTable>, TOptional>> & Omit<InferModelInsert<TTable>, TOptional> & TCreateFor<TBase> & Required<Pick<InferModelInsert<TTable> & TCreateFor<TBase>, TRequire & keyof (InferModelInsert<TTable> & TCreateFor<TBase>)>>;
|
|
1379
1400
|
/**
|
|
1380
1401
|
* Create a table-backed model base class from a Drizzle table.
|
|
@@ -1386,16 +1407,27 @@ type CreateShape<TTable extends TableShape, TBase extends typeof Model, TOptiona
|
|
|
1386
1407
|
* virtual field required. `optionalOnCreate` and `requireOnCreate` reshape the
|
|
1387
1408
|
* inferred type without any cast.
|
|
1388
1409
|
*
|
|
1410
|
+
* The allowlist statics (`fillable`, `hidden`, `visible`, `accessors`,
|
|
1411
|
+
* `appends`) can be passed here instead of declared on the subclass: the
|
|
1412
|
+
* option form checks every name against the table's columns (plus fields the
|
|
1413
|
+
* `base` contributes), so a typo is a compile error instead of a silently
|
|
1414
|
+
* ineffective entry. A `static` declaration on the subclass still works and
|
|
1415
|
+
* shadows the option, matching normal class semantics.
|
|
1416
|
+
*
|
|
1389
1417
|
* @example
|
|
1390
|
-
* export class Post extends defineModel(posts
|
|
1418
|
+
* export class Post extends defineModel(posts, {
|
|
1419
|
+
* fillable: ['title', 'body'],
|
|
1420
|
+
* }) {}
|
|
1391
1421
|
*
|
|
1392
1422
|
* export class User extends defineModel(users, {
|
|
1393
1423
|
* base: AuthenticatableModel,
|
|
1394
1424
|
* optionalOnCreate: ['passwordHash'],
|
|
1395
1425
|
* requireOnCreate: ['password'],
|
|
1426
|
+
* fillable: ['name', 'email', 'password'],
|
|
1427
|
+
* hidden: ['passwordHash', 'rememberToken'],
|
|
1396
1428
|
* }) {}
|
|
1397
1429
|
*/
|
|
1398
|
-
declare function defineModel<TTable extends TableShape, TBase extends typeof Model = typeof Model, const TOptional extends keyof InferModelInsert<TTable> = never, const TRequire extends CreateKey<TTable, TBase> = never>(table: TTable, options?: {
|
|
1430
|
+
declare function defineModel<TTable extends TableShape, TBase extends typeof Model = typeof Model, const TOptional extends keyof InferModelInsert<TTable> = never, const TRequire extends CreateKey<TTable, TBase> = never, TAccessorKey extends string = never>(table: TTable, options?: {
|
|
1399
1431
|
base?: TBase;
|
|
1400
1432
|
/**
|
|
1401
1433
|
* Columns the model fills in itself, so callers need not pass them.
|
|
@@ -1409,6 +1441,33 @@ declare function defineModel<TTable extends TableShape, TBase extends typeof Mod
|
|
|
1409
1441
|
* Type-level only.
|
|
1410
1442
|
*/
|
|
1411
1443
|
requireOnCreate?: readonly TRequire[];
|
|
1444
|
+
/**
|
|
1445
|
+
* Typed form of `static fillable`: mass-assignment allowlist checked
|
|
1446
|
+
* against insertable columns and base-contributed fields.
|
|
1447
|
+
*/
|
|
1448
|
+
fillable?: readonly FillableKey<TTable, TBase>[];
|
|
1449
|
+
/**
|
|
1450
|
+
* Typed form of `static hidden`: fields excluded from serialization,
|
|
1451
|
+
* checked against record columns and declared accessors.
|
|
1452
|
+
*/
|
|
1453
|
+
hidden?: readonly (RecordKey<TTable> | NoInfer<TAccessorKey>)[];
|
|
1454
|
+
/**
|
|
1455
|
+
* Typed form of `static visible`: serialization allowlist, checked
|
|
1456
|
+
* against record columns and declared accessors.
|
|
1457
|
+
*/
|
|
1458
|
+
visible?: readonly (RecordKey<TTable> | NoInfer<TAccessorKey>)[];
|
|
1459
|
+
/**
|
|
1460
|
+
* Typed form of `static accessors`: each function receives the table's
|
|
1461
|
+
* inferred record. Names declared here are what `appends` may reference.
|
|
1462
|
+
* The Record intersection rejects non-object values — with no keys to
|
|
1463
|
+
* infer, the mapped type alone collapses to `{}`, which admits anything.
|
|
1464
|
+
*/
|
|
1465
|
+
accessors?: AccessorsShape<TTable, TAccessorKey> & Record<string, (record: InferModelRecord<TTable>) => unknown>;
|
|
1466
|
+
/**
|
|
1467
|
+
* Typed form of `static appends`: virtual attributes to serialize,
|
|
1468
|
+
* checked against the names declared in `accessors`.
|
|
1469
|
+
*/
|
|
1470
|
+
appends?: readonly NoInfer<TAccessorKey>[];
|
|
1412
1471
|
}): ModelClassWithTable<TTable, TBase, CreateShape<TTable, TBase, TOptional, TRequire>>;
|
|
1413
1472
|
|
|
1414
1473
|
/**
|
package/dist/index.js
CHANGED
|
@@ -2514,6 +2514,11 @@ function defineModel(table, options = {}) {
|
|
|
2514
2514
|
DefinedModel.table = table;
|
|
2515
2515
|
DefinedModel.recordType = {};
|
|
2516
2516
|
DefinedModel.createType = {};
|
|
2517
|
+
if (options.fillable) DefinedModel.fillable = [...options.fillable];
|
|
2518
|
+
if (options.hidden) DefinedModel.hidden = [...options.hidden];
|
|
2519
|
+
if (options.visible) DefinedModel.visible = [...options.visible];
|
|
2520
|
+
if (options.accessors) DefinedModel.accessors = options.accessors;
|
|
2521
|
+
if (options.appends) DefinedModel.appends = [...options.appends];
|
|
2517
2522
|
return DefinedModel;
|
|
2518
2523
|
}
|
|
2519
2524
|
function normalizeOrderBy(order) {
|