How to use the normalizr.schema.Union 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 goodmind / treact / packages / treact / src / app / store / modules / media / entities.ts View on Github external
const photoSize = new schema.Entity('photoCachedSizes', {
  location: fileLocations,
}, {
  idAttribute: v => v.location.local_id,
})
// TODO: there should be processStrategy
const photoCachedSize = new schema.Entity('photoSizes', {}, {
  idAttribute: v => v.location.local_id,
})
const metaPhotoSize = {
  photoSize,
  photoCachedSize,
}

export const document = new schema.Entity('documents', {
  thumb: new schema.Union(metaPhotoSize, '_'),
}, {
  processStrategy: processDoc,
  idAttribute: pipe(prop('id'), e => +e),
})

const photo = new schema.Entity('photos', {
  sizes: new schema.Array(metaPhotoSize, '_'),
}, {
  processStrategy: processPhoto,
  idAttribute: pipe(prop('id'), e => +e),
})

const webpage = new schema.Object({
  document,
  photo,
})
github digirati-labs / hyperion / src / process / schema.ts View on Github external
Manifest: 'manifest',
  Canvas: 'canvas',
  AnnotationCollection: 'annotationCollection',
  AnnotationPage: 'annotationPage',
  Annotation: 'annotation',
  Range: 'range',
  // Content resources.
  Application: 'contentResource',
  Dataset: 'contentResource',
  Image: 'contentResource',
  Sound: 'contentResource',
  Text: 'contentResource',
  Video: 'contentResource',
  TextualBody: 'contentResource',
};
const resource = new schema.Union(
  {
    collection,
    manifest,
    canvas,
    annotationCollection,
    annotationPage,
    annotation,
    range,
    contentResource,
    service,
  },
  (entity: any): DereferencableResourceTypes => {
    if (RESOURCE_TYPE_MAP[entity.type]) {
      return RESOURCE_TYPE_MAP[entity.type];
    }
    if (entity.profile) {
github gpbl / denormalizr / test / index.js View on Github external
describe('when a schema', () => {
      const postSchema = new schema.Entity('posts');
      const userSchema = new schema.Entity('users');

      postSchema.define({
        user: userSchema,
      });

      const unionItemSchema = new schema.Union({
        post: postSchema,
        user: userSchema,
      }, 'type');

      const response = {
        unionItems: [
          {
            id: 1,
            title: 'Some Post',
            user: {
              id: 1,
              name: 'Dan',
            },
            type: 'post',
          },
          {
github gpbl / denormalizr / test / immutable.js View on Github external
describe('when a schema', () => {
      const postSchema = new Schema.Entity('posts');
      const userSchema = new Schema.Entity('users');

      postSchema.define({
        user: userSchema,
      });

      const unionItemSchema = new Schema.Union({
        post: postSchema,
        user: userSchema,
      }, 'type');

      const response = {
        unionItems: [
          {
            id: 1,
            title: 'Some Post',
            user: {
              id: 1,
              name: 'Dan',
            },
            type: 'post',
          },
          {
github gpbl / denormalizr / test / immutable.js View on Github external
describe('when defining a relationship', () => {
      const groupSchema = new Schema.Entity('groups');
      const userSchema = new Schema.Entity('users');

      const member = new Schema.Union({
        user: userSchema,
        group: groupSchema,
      }, 'type');

      groupSchema.define({
        owner: member,
      });

      const response = {
        groups: [
          {
            id: 1,
            owner: {
              id: 1,
              type: 'user',
              name: 'Dan',
github vuex-orm / vuex-orm / src / schema / Schema.ts View on Github external
union (callback: Normalizr.SchemaFunction): Normalizr.Union {
    return new Normalizr.Union(this.schemas, callback)
  }
github frontity / frontity / packages / wp-source / src / libraries / schemas / index.ts View on Github external
import { schema } from "normalizr";
import { postType } from "./postTypes";
import { taxonomy } from "./taxonomies";
import { author } from "./authors";
import { attachment } from "./attachments";

export const entity = new schema.Union(
  {
    postType,
    taxonomy,
    author,
    attachment
  },
  val => {
    if (val.taxonomy) return "taxonomy";
    else if (val.media_type) return "attachment";
    else if (val.name) return "author";
    return "postType";
  }
);

export const list = new schema.Array(entity);
github goodmind / treact / packages / treact / src / app / store / modules / media / entities.ts View on Github external
const textMedia = (
  keys: string[],
  entity = new schema.Entity('media', {}, mediaSettings),
) =>
  zipObj(keys, map(() => entity, keys))

export const mediaIndexation = [
  'media',
  'documents',
  'photos',
  'photoSizes',
  'photoCachedSizes',
]

export const media = new schema.Union({
  ...textMedia([
    'messageMediaUnsupported',
    'messageMediaContact',
    'messageMediaEmpty',
    'messageMediaVenue',
    'messageMediaGeo',
  ]),
  messageMediaDocument,
  messageMediaPhoto,
  messageMediaGame,
  messageMediaInvoice,
  messageMediaWebPage,
}, '_')
github digirati-labs / hyperion / src / process / schema.ts View on Github external
},
  entity => {
    switch (entity.type) {
      case 'Collection':
        return 'collection';
      case 'Manifest':
        return 'manifest';
      case 'AnnotationCollection':
        return 'annotationCollection';
      default:
        return 'contentResource';
    }
  }
);

const canvasOrReference = new schema.Union(
  {
    selector,
    canvasReference,
    canvas,
  },
  entity => {
    if (entity && entity.id && entity.id.indexOf('#') !== -1) {
      return 'selector';
    }
    if (entity.type === 'SpecificResource') {
      return 'canvasReference';
    }
    return 'canvas';
  }
);
github digirati-labs / hyperion / src / process / schema.ts View on Github external
TextualBody: 'contentResource',
};
const annotationBody = new schema.Union(
  {
    contentResource,
    choice,
  },
  (input: any): AnnotationBodyTypes => {
    if (annotationBodyMappings[input.type]) {
      return annotationBodyMappings[input.type];
    }
    return 'contentResource';
  }
);

const rangeItem = new schema.Union(
  {
    canvas,
    selector,
    range,
    canvasReference,
  },
  input => {
    if (input && input.id && input.id.indexOf('#') !== -1) {
      return 'selector';
    }

    switch (input.type) {
      case 'Canvas':
        return 'canvas';
      case 'Range':
        return 'range';