How to use the normalizr.unionOf function in normalizr

To help you get started, we’ve selected a few normalizr examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github DefinitelyTyped / DefinitelyTyped / normalizr / normalizr-tests.ts View on Github external
// You can specify the name of the attribute that determines the schema
group12.define({
    owner: unionOf(member12, { schemaAttribute: 'type' })
});

// Or you can specify a function to infer it
function inferSchema(entity: any) { /* ... */ }
group12.define({
    creator: unionOf(member12, { schemaAttribute: inferSchema })
});

const group13 = new Schema('groups');
const user13 = new Schema('users');

const member13 = unionOf({
    users: user13,
    groups: group13
}, { schemaAttribute: 'type' });

group13.define({
    owner: member13,
    members: arrayOf(member13),
    relationships: valuesOf(member13)
});

// normalize(obj, schema, [options])

const article14 = new Schema('articles');
const user14 = new Schema('users');

article14.define({
github DefinitelyTyped / DefinitelyTyped / normalizr / normalizr-tests.ts View on Github external
// a member can be either a user or a group
const member12 = {
    users: user12,
    groups: group12
};

// You can specify the name of the attribute that determines the schema
group12.define({
    owner: unionOf(member12, { schemaAttribute: 'type' })
});

// Or you can specify a function to infer it
function inferSchema(entity: any) { /* ... */ }
group12.define({
    creator: unionOf(member12, { schemaAttribute: inferSchema })
});

const group13 = new Schema('groups');
const user13 = new Schema('users');

const member13 = unionOf({
    users: user13,
    groups: group13
}, { schemaAttribute: 'type' });

group13.define({
    owner: member13,
    members: arrayOf(member13),
    relationships: valuesOf(member13)
});
github DefinitelyTyped / DefinitelyTyped / normalizr / normalizr-tests.ts View on Github external
});

// unionOf(schemaMap, [options])

const group12 = new Schema('groups');
const user12 = new Schema('users');

// a member can be either a user or a group
const member12 = {
    users: user12,
    groups: group12
};

// You can specify the name of the attribute that determines the schema
group12.define({
    owner: unionOf(member12, { schemaAttribute: 'type' })
});

// Or you can specify a function to infer it
function inferSchema(entity: any) { /* ... */ }
group12.define({
    creator: unionOf(member12, { schemaAttribute: inferSchema })
});

const group13 = new Schema('groups');
const user13 = new Schema('users');

const member13 = unionOf({
    users: user13,
    groups: group13
}, { schemaAttribute: 'type' });
github julienvincent / modelizr / src / core / modelizr.js View on Github external
_.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}
				)
			}
		})
	}
github julienvincent / modelizr / src / union.js View on Github external
    union.define = () => unionOf(_.mapValues(models, model => model.schema.model), attribute)
github julienvincent / modelizr / src / normalizer.js View on Github external
return Normalize(response, _.mapValues(_query, (entity, key) => {

        const model = entity._isUnion ?
            _unionOf(_.mapValues(entity.models, model => model.schema.model), {schemaAttribute: entity.schemaAttribute})
            : entity.model()

        switch (entity._modelType) {
            case "valuesOf":
                return _valuesOf(model)
            case "arrayOf":
                return _arrayOf(model)
        }

        if (Array.isArray(response[key])) return _arrayOf(model)
        if (hasValuesOf(response[key], model)) return _valuesOf(model)

        return model
    }))
}