orchid-orm 1.72.10 → 1.73.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 +22 -6
- package/dist/index.js +8 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +8 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -5
package/dist/index.d.ts
CHANGED
|
@@ -118,17 +118,18 @@ interface BelongsToOptions<Columns extends Column.Shape.QueryInit = Column.Shape
|
|
|
118
118
|
on?: ColumnsShape.InputPartial<Related['columns']['shape']>;
|
|
119
119
|
}
|
|
120
120
|
type BelongsToParams<T extends RelationConfigSelf, FK extends string> = { [Name in FK]: T['columns']['shape'][Name]['__type'] };
|
|
121
|
+
type BelongsToColumnsRequired<T extends RelationConfigSelf, FK extends string> = { [K in FK]: T['columns']['shape'][K]['data']['isNullable'] extends true ? false : true }[FK] extends true ? true : false;
|
|
121
122
|
type BelongsToQuery<T extends Query, Name extends string> = { [P in keyof T]: P extends '__selectable' ? SelectableFromShape<T['shape'], Name> : P extends '__as' ? Name : P extends CreateMethodsNames | DeleteMethodsNames ? never : T[P] } & QueryHasWhere & HasRelJoin;
|
|
122
|
-
interface
|
|
123
|
+
interface BelongsToDataForCreate<Name extends string, FK extends string, Required, Q extends Query> {
|
|
124
|
+
columns: FK;
|
|
125
|
+
nested: Q extends Query.Pick.IsNotReadOnly ? Required extends true ? { [Key in Name]: RelationToOneDataForCreateSameQuery<Q> } : { [Key in Name]?: RelationToOneDataForCreateSameQuery<Q> } : { [Key in Name]?: never };
|
|
126
|
+
}
|
|
127
|
+
interface BelongsToInfo<T extends RelationConfigSelf, FK extends string, Required, Q extends Query> extends RelationConfigBase {
|
|
123
128
|
returnsOne: true;
|
|
124
129
|
required: Required;
|
|
125
130
|
query: Q;
|
|
126
131
|
params: BelongsToParams<T, FK>;
|
|
127
132
|
omitForeignKeyInCreate: FK;
|
|
128
|
-
dataForCreate: {
|
|
129
|
-
columns: FK;
|
|
130
|
-
nested: Q extends Query.Pick.IsNotReadOnly ? Required extends true ? { [Key in Name]: RelationToOneDataForCreateSameQuery<Q> } : { [Key in Name]?: RelationToOneDataForCreateSameQuery<Q> } : { [Key in Name]?: never };
|
|
131
|
-
};
|
|
132
133
|
dataForUpdate: {
|
|
133
134
|
disconnect: boolean;
|
|
134
135
|
} | {
|
|
@@ -530,8 +531,23 @@ interface TableInfo {
|
|
|
530
531
|
name: string;
|
|
531
532
|
}
|
|
532
533
|
interface Table extends Query, TableInfo {}
|
|
534
|
+
type BelongsToRequired<T extends RelationConfigSelf, Rel extends BelongsTo> = Rel['options'] extends {
|
|
535
|
+
required: infer Required extends boolean;
|
|
536
|
+
} ? Required : Rel['options'] extends {
|
|
537
|
+
on: unknown;
|
|
538
|
+
} ? false : BelongsToColumnsRequired<T, Rel['options']['columns'][number] & string>;
|
|
539
|
+
type BelongsToRelationInfo<T extends RelationConfigSelf, Name extends string, Rel extends BelongsTo> = BelongsToInfo<T, Rel['options']['columns'][number] & string, BelongsToRequired<T, Rel>, BelongsToQuery<RelationTableToQuery<Rel>, Name>>;
|
|
540
|
+
type BelongsToCreateData<T extends RelationConfigSelf, Name extends string, Rel extends BelongsTo> = BelongsToDataForCreate<Name, Rel['options']['columns'][number] & string, BelongsToRequired<T, Rel>, BelongsToQuery<RelationTableToQuery<Rel>, Name>>;
|
|
541
|
+
type BelongsToCreateDataColumns<T extends RelationConfigSelf> = { [K in keyof T['relations']]: T['relations'][K] extends BelongsTo ? T['relations'][K]['options']['columns'][number] & string : never }[keyof T['relations']];
|
|
542
|
+
type BelongsToCreateDataForColumn<T extends RelationConfigSelf, Column extends string> = { [K in keyof T['relations']]: T['relations'][K] extends BelongsTo ? Column extends T['relations'][K]['options']['columns'][number] ? BelongsToCreateData<T, K & string, T['relations'][K]> : never : never }[keyof T['relations']];
|
|
543
|
+
type RelationDataForCreate<T extends RelationConfigSelf, Name extends keyof T['relations'] & string> = T['relations'][Name] extends HasOne ? HasOneInfo<T, Name, T['relations'][Name], HasOneQuery<T, Name, RelationTableToQuery<T['relations'][Name]>>>['dataForCreate'] : T['relations'][Name] extends HasMany ? HasManyInfo<T, Name, T['relations'][Name], HasManyQuery<T, Name, RelationTableToQuery<T['relations'][Name]>>>['dataForCreate'] : T['relations'][Name] extends HasAndBelongsToMany ? HasAndBelongsToManyInfo<T, Name, T['relations'][Name]['options']['columns'][number] & string, HasAndBelongsToManyQuery<Name, RelationTableToQuery<T['relations'][Name]>>>['dataForCreate'] : never;
|
|
544
|
+
type RelationDataForCreateOptionalNames<T extends RelationConfigSelf> = { [K in keyof T['relations'] & string]: T['relations'][K] extends BelongsTo ? never : K }[keyof T['relations'] & string];
|
|
545
|
+
type BelongsToRelationsDataForCreate<T extends RelationConfigSelf> = { [Column in BelongsToCreateDataColumns<T>]: BelongsToCreateDataForColumn<T, Column> };
|
|
546
|
+
type RelationsDataForCreateOptional<T extends RelationConfigSelf> = RelationDataForCreateOptionalNames<T> extends never ? EmptyObject : { [K in RelationDataForCreateOptionalNames<T>]: (u: RelationDataForCreate<T, K>) => void }[RelationDataForCreateOptionalNames<T>] extends ((u: infer Obj) => void) ? Obj : EmptyObject;
|
|
533
547
|
interface TableToDb<T extends ORMTableInput, Name extends string | undefined, ReadOnly extends true | undefined, Materialized extends true | undefined = undefined> extends TableInfo, Db<Name, T['columns']['shape'], keyof ShapeColumnPrimaryKeys<T['columns']['shape']> extends never ? never : ShapeColumnPrimaryKeys<T['columns']['shape']>, ShapeUniqueColumns<T['columns']['shape']> | TableDataItemsUniqueColumns<T['columns']['shape'], T['columns']['data']>, TableDataItemsUniqueColumnTuples<T['columns']['shape'], T['columns']['data']>, UniqueConstraints<T['columns']['shape']> | TableDataItemsUniqueConstraints<T['columns']['data']>, T['types'], T['columns']['shape'] & ComputedColumnsFromOptions<T['computed']>, MapTableScopesOption<T>, ColumnsShape.DefaultSelectKeys<T['columns']['shape']>, ReadOnly, Materialized> {
|
|
534
|
-
relations: T extends RelationConfigSelf ? { [K in keyof T['relations'] & string]: T['relations'][K] extends BelongsTo ?
|
|
548
|
+
relations: T extends RelationConfigSelf ? { [K in keyof T['relations'] & string]: T['relations'][K] extends BelongsTo ? BelongsToRelationInfo<T, K, T['relations'][K]> : T['relations'][K] extends HasOne ? HasOneInfo<T, K, T['relations'][K], HasOneQuery<T, K, RelationTableToQuery<T['relations'][K]>>> : T['relations'][K] extends HasMany ? HasManyInfo<T, K, T['relations'][K], HasManyQuery<T, K, RelationTableToQuery<T['relations'][K]>>> : T['relations'][K] extends HasAndBelongsToMany ? HasAndBelongsToManyInfo<T, K, T['relations'][K]['options']['columns'][number] & string, HasAndBelongsToManyQuery<K, RelationTableToQuery<T['relations'][K]>>> : never } : EmptyObject;
|
|
549
|
+
relationsDataForCreate: T extends RelationConfigSelf ? BelongsToRelationsDataForCreate<T> : EmptyObject;
|
|
550
|
+
relationsDataForCreateOptional: T extends RelationConfigSelf ? RelationsDataForCreateOptional<T> : EmptyObject;
|
|
535
551
|
}
|
|
536
552
|
interface PickORMTableInputColumns {
|
|
537
553
|
columns: {
|
package/dist/index.js
CHANGED
|
@@ -410,6 +410,13 @@ var BelongsToVirtualColumn = class extends pqb_internal.VirtualColumn {
|
|
|
410
410
|
this.nestedUpdate(queryForUpdate, set, data);
|
|
411
411
|
}
|
|
412
412
|
};
|
|
413
|
+
const getBelongsToRequired = (tableConfig, relation) => {
|
|
414
|
+
const { required } = relation.options;
|
|
415
|
+
if (typeof required === "boolean") return required;
|
|
416
|
+
return relation.options.columns.every((key) => {
|
|
417
|
+
return !tableConfig.columns.shape[key].data.isNullable;
|
|
418
|
+
});
|
|
419
|
+
};
|
|
413
420
|
const makeBelongsToMethod = (tableConfig, table, relation, relationName, query) => {
|
|
414
421
|
const primaryKeys = relation.options.references;
|
|
415
422
|
const foreignKeys = relation.options.columns;
|
|
@@ -1408,7 +1415,7 @@ const applyRelation = (table, qb, { relationName, relation, dbTable, otherDbTabl
|
|
|
1408
1415
|
else if (type === "hasAndBelongsToMany") data = makeHasAndBelongsToManyMethod(table, dbTable, qb, relation, relationName, query, schema);
|
|
1409
1416
|
else throw new Error(`Unknown relation type ${type}`);
|
|
1410
1417
|
if (data.returns === "one") {
|
|
1411
|
-
if (relation.options.required) (0, pqb_internal._queryTake)(query);
|
|
1418
|
+
if (type === "belongsTo" ? getBelongsToRequired(table, relation) : relation.options.required) (0, pqb_internal._queryTake)(query);
|
|
1412
1419
|
else (0, pqb_internal._queryTakeOptional)(query);
|
|
1413
1420
|
query.q.returnsOne = true;
|
|
1414
1421
|
}
|