コード例 #1
0
ファイル: immutable.js プロジェクト: jcarbo/denormalizr
  describe("parsing union schemas", () => {

    const postSchema = new Schema('posts');
    const userSchema = new Schema('users');

    postSchema.define({
      user: userSchema
    });

    const unionItemSchema = unionOf({
      post: postSchema,
      user: userSchema
    }, { schemaAttribute: 'type' });

    const response = {
      unionItems: [
        {
          id: 1,
          title: 'Some Post',
          user: {
            id: 1,
            name: 'Dan'
          },
          type: 'post'
        },
        {
          id: 2,
          name: 'Ashley',
          type: 'user'
        },
        {
          id: 2,
          title: 'Other Post',
          type: 'post'
        }
      ]
    };

    const data = immutableNormalize(response.unionItems, arrayOf(unionItemSchema));

    it("should return the original response", () => {
      const denormalized = data.result.map(item => denormalize(item, data.entities, unionItemSchema));
      expect(fromJS(denormalized)).to.be.eql(fromJS(response.unionItems));
    });
  });
コード例 #2
0
ファイル: modelizr.js プロジェクト: julienvincent/modelizr
		_.forEach(models, (modelData: ModelDataType | UnionDataType, modelName: string) => {
			if (modelData._unionDataType) {

				/* filter out all non-existing models and warn about them */
				const existingModels = _.filter(modelData.models, model => {
					if (models[model]) return true

					// eslint-disable-next-line no-console
					console.warn(`Model "${model}" on union ${modelName} points to an unknown model`)
				})

				/* create a normalizr union */
				modelData.normalizrSchema = unionOf(_.mapValues(_.mapKeys(existingModels, model => model), model =>
						models[model].normalizrSchema
					), {schemaAttribute: modelData.schemaAttribute}
				)
			}
		})