Просмотр исходного кода

fix: tests of hasMany relation, relation typings and README.md

e22m4u 2 лет назад
Родитель
Сommit
e90312d348

+ 2 - 2
README.md

@@ -669,7 +669,7 @@ schema.defineModel({
     order: {
       type: 'hasOne',
       model: 'order',
-      foreignKey: 'customerId', // опционально
+      foreignKey: 'customerId', // поле целевой модели
     },
   },
 });
@@ -721,7 +721,7 @@ schema.defineModel({
     orders: {
       type: 'hasMany',
       model: 'order',
-      foreignKey: 'customerId', // опционально
+      foreignKey: 'customerId', // поле целевой модели
     },
   },
 });

+ 8 - 26
src/definition/model/relations/relation-definition.d.ts

@@ -99,8 +99,8 @@ export declare type PolyBelongsToDefinition = {
 export declare type HasOneDefinition = {
   type: RelationType.HAS_ONE;
   model: string;
+  foreignKey: string;
   polymorphic?: false;
-  foreignKey?: string;
   discriminator?: undefined;
 };
 
@@ -127,16 +127,7 @@ export declare type PolyHasOneDefinitionWithTargetRelationName = {
 /**
  * The polymorphic "hasOne" relation with target relation keys.
  *
- * @example Required options only.
- * ```
- * {
- *   type: RelationType.HAS_ONE,
- *   model: 'model',
- *   polymorphic: true,
- * }
- * ```
- *
- * @example Verbose definition.
+ * @example
  * ```
  * {
  *   type: RelationType.HAS_ONE,
@@ -151,8 +142,8 @@ export declare type PolyHasOneDefinitionWithTargetKeys = {
   type: RelationType.HAS_ONE;
   model: string;
   polymorphic: true;
-  foreignKey?: string;
-  discriminator?: string;
+  foreignKey: string;
+  discriminator: string;
 };
 
 /**
@@ -170,8 +161,8 @@ export declare type PolyHasOneDefinitionWithTargetKeys = {
 export declare type HasManyDefinition = {
   type: RelationType.HAS_MANY;
   model: string;
+  foreignKey: string;
   polymorphic?: false;
-  foreignKey?: string;
   discriminator?: undefined;
 };
 
@@ -198,16 +189,7 @@ export declare type PolyHasManyDefinitionWithTargetRelationName = {
 /**
  * The polymorphic "hasMany" relation with target relation keys.
  *
- * @example Required options only.
- * ```
- * {
- *   type: RelationType.HAS_MANY,
- *   model: 'model',
- *   polymorphic: true,
- * }
- * ```
- *
- * @example Verbose definition.
+ * @example
  * ```
  * {
  *   type: RelationType.HAS_MANY,
@@ -222,8 +204,8 @@ export declare type PolyHasManyDefinitionWithTargetKeys = {
   type: RelationType.HAS_MANY;
   model: string;
   polymorphic: true;
-  foreignKey?: string;
-  discriminator?: string;
+  foreignKey: string;
+  discriminator: string;
 };
 
 /**

+ 18 - 18
src/definition/model/relations/relations-definition-validator.spec.js

@@ -253,15 +253,15 @@ describe('RelationsDefinitionValidator', function () {
           const validate = v => {
             const foo = {
               type: RelationType.HAS_ONE,
-              model: 'model',
-              foreignKey: v,
+              model: v,
+              foreignKey: 'modelId',
             };
             return () => S.validate('model', {foo});
           };
           const error = v =>
             format(
               'The relation "foo" of the model "model" has the type "hasOne", ' +
-                'so it requires the option "foreignKey" to be a non-empty String, ' +
+                'so it requires the option "model" to be a non-empty String, ' +
                 'but %s given.',
               v,
             );
@@ -273,22 +273,22 @@ describe('RelationsDefinitionValidator', function () {
           expect(validate([])).to.throw(error('Array'));
           expect(validate(undefined)).to.throw(error('undefined'));
           expect(validate(null)).to.throw(error('null'));
-          validate('modelId')();
+          validate('model')();
         });
 
-        it('requires the option "foreignKey" to be a string', function () {
+        it('requires the option "foreignKey" to be a non-empty string', function () {
           const validate = v => {
             const foo = {
               type: RelationType.HAS_ONE,
-              model: v,
-              foreignKey: 'modelId',
+              model: 'model',
+              foreignKey: v,
             };
             return () => S.validate('model', {foo});
           };
           const error = v =>
             format(
               'The relation "foo" of the model "model" has the type "hasOne", ' +
-                'so it requires the option "model" to be a non-empty String, ' +
+                'so it requires the option "foreignKey" to be a non-empty String, ' +
                 'but %s given.',
               v,
             );
@@ -300,7 +300,7 @@ describe('RelationsDefinitionValidator', function () {
           expect(validate([])).to.throw(error('Array'));
           expect(validate(undefined)).to.throw(error('undefined'));
           expect(validate(null)).to.throw(error('null'));
-          validate('model')();
+          validate('modelId')();
         });
 
         it('throws an error if the option "discriminator" is provided', function () {
@@ -479,15 +479,15 @@ describe('RelationsDefinitionValidator', function () {
           const validate = v => {
             const foo = {
               type: RelationType.HAS_MANY,
-              model: 'model',
-              foreignKey: v,
+              model: v,
+              foreignKey: 'modelId',
             };
             return () => S.validate('model', {foo});
           };
           const error = v =>
             format(
               'The relation "foo" of the model "model" has the type "hasMany", ' +
-                'so it requires the option "foreignKey" to be a non-empty String, ' +
+                'so it requires the option "model" to be a non-empty String, ' +
                 'but %s given.',
               v,
             );
@@ -499,22 +499,22 @@ describe('RelationsDefinitionValidator', function () {
           expect(validate([])).to.throw(error('Array'));
           expect(validate(undefined)).to.throw(error('undefined'));
           expect(validate(null)).to.throw(error('null'));
-          validate('modelId')();
+          validate('model')();
         });
 
-        it('requires the option "foreignKey" to be a string', function () {
+        it('requires the option "foreignKey" to be a non-empty string', function () {
           const validate = v => {
             const foo = {
               type: RelationType.HAS_MANY,
-              model: v,
-              foreignKey: 'modelId',
+              model: 'model',
+              foreignKey: v,
             };
             return () => S.validate('model', {foo});
           };
           const error = v =>
             format(
               'The relation "foo" of the model "model" has the type "hasMany", ' +
-                'so it requires the option "model" to be a non-empty String, ' +
+                'so it requires the option "foreignKey" to be a non-empty String, ' +
                 'but %s given.',
               v,
             );
@@ -526,7 +526,7 @@ describe('RelationsDefinitionValidator', function () {
           expect(validate([])).to.throw(error('Array'));
           expect(validate(undefined)).to.throw(error('undefined'));
           expect(validate(null)).to.throw(error('null'));
-          validate('model')();
+          validate('modelId')();
         });
 
         it('throws an error if the option "discriminator" is provided', function () {