Skip to content

cozy-client / HasMany

Class: HasMany

Related documents are stored in the relationships attribute of the object, following the JSON API spec.

Responsible for

  • Creating relationships
  • Removing relationships

description

const schema = {
  todos: {
     doctype: 'io.cozy.todos',
     relationships: {
       tasks: {
         doctype: 'io.cozy.tasks',
         type: 'has-many'
       }
     }
   }
}

const todo = {
  label: "Protect people's privacy",
  relationships: {
    tasks: {
      data: [
        {_id: 1, _type: 'io.cozy.tasks'},
        {_id: 2, _type: 'io.cozy.tasks'}
      ]
    }
  }
}

Hierarchy

Constructors

constructor

new HasMany(target, name, doctype, options)

Parameters

Name Type Description
target any Original object containing raw data
name string Attribute under which the association is stored
doctype string Doctype of the documents managed by the association
options Object Options passed from the client
options.dispatch Function Store’s dispatch, comes from the client
options.get Function Get a document from the store
options.mutate Function Execute client mutate
options.query Function Execute client query
options.save Function Execute client save

Inherited from

Association.constructor

Defined in

packages/cozy-client/src/associations/Association.js:76

Properties

dispatch

dispatch: Function

Dispatch an action on the store.

Inherited from

Association.dispatch

Defined in

packages/cozy-client/src/associations/Association.js:139


doctype

doctype: string

Doctype of the relationship

example ‘io.cozy.authors’

Inherited from

Association.doctype

Defined in

packages/cozy-client/src/associations/Association.js:102


get

get: Function

Returns the document from the store

Inherited from

Association.get

Defined in

packages/cozy-client/src/associations/Association.js:110


mutate

mutate: Function

Performs a mutation on the relationship.

function

Inherited from

Association.mutate

Defined in

packages/cozy-client/src/associations/Association.js:125


name

name: string

The name of the relationship.

example ‘author’

Inherited from

Association.name

Defined in

packages/cozy-client/src/associations/Association.js:95


query

query: Function

Performs a query to retrieve relationship documents.

param

function

Inherited from

Association.query

Defined in

packages/cozy-client/src/associations/Association.js:117


save

save: Function

Saves the relationship in store.

Inherited from

Association.save

Defined in

packages/cozy-client/src/associations/Association.js:132


target

target: any

The original document declaring the relationship

Inherited from

Association.target

Defined in

packages/cozy-client/src/associations/Association.js:89

Accessors

count

get count(): number

Returns the total number of documents in the relationship. Does not handle documents absent from the store. If you want to do that, you can use .data.length.

Returns

number

  • Total number of documents in the relationships

Defined in

packages/cozy-client/src/associations/HasMany.js:93


data

get data(): any

Returns store documents

Returns

any

Defined in

packages/cozy-client/src/associations/HasMany.js:76


hasMore

get hasMore(): any

Returns

any

Defined in

packages/cozy-client/src/associations/HasMany.js:82


raw

get raw(): any

Returns the raw relationship data as stored in the original document

For a document with relationships stored as JSON API spec:

const book = {
  title: 'Moby Dick',
  relationships: {
    author: {
      data: {
        doctype: 'io.cozy.authors',
        id: 'herman'
      }
    }
  }
 }

Raw value will be

{
  "doctype": "io.cozy.authors",
  "id": "herman"
}

Derived Associations need to implement this method.

Returns

any

Defined in

packages/cozy-client/src/associations/HasMany.js:69

Methods

addById

addById(idsArg): any

Add a referenced document by id. You need to call save() in order to synchronize your document with the store.

todo We shouldn’t create the array of relationship manually since it’ll not be present in the store as well. We certainly should use something like updateRelationship

Parameters

Name Type
idsArg any

Returns

any

Defined in

packages/cozy-client/src/associations/HasMany.js:127


containsById

containsById(id): boolean

Parameters

Name Type
id any

Returns

boolean

Defined in

packages/cozy-client/src/associations/HasMany.js:108


dehydrate

dehydrate(doc): any

Parameters

Name Type
doc any

Returns

any

Defined in

packages/cozy-client/src/associations/HasMany.js:217


exists

exists(document): boolean

Parameters

Name Type
document any

Returns

boolean

Defined in

packages/cozy-client/src/associations/HasMany.js:104


existsById

existsById(id): boolean

Parameters

Name Type
id any

Returns

boolean

Defined in

packages/cozy-client/src/associations/HasMany.js:114


fetchMore

fetchMore(): void

Returns

void

Defined in

packages/cozy-client/src/associations/HasMany.js:100


getRelationship

getRelationship(): any

Returns

any

Defined in

packages/cozy-client/src/associations/HasMany.js:168


removeById

removeById(idsArg): any

Parameters

Name Type
idsArg any

Returns

any

Defined in

packages/cozy-client/src/associations/HasMany.js:148


updateMetaCount

updateMetaCount(): void

Returns

void

Defined in

packages/cozy-client/src/associations/HasMany.js:159


updateRelationship

updateRelationship(target, updateFn): any

Parameters

Name Type
target any
updateFn any

Returns

any

Defined in

packages/cozy-client/src/associations/HasMany.js:189


updateRelationshipData

updateRelationshipData(getUpdatedRelationshipData): (dispatch: any, getState: any) => void

Parameters

Name Type
getUpdatedRelationshipData any

Returns

fn

▸ (dispatch, getState): void

Parameters

Name Type
dispatch any
getState any

Returns

void

Defined in

packages/cozy-client/src/associations/HasMany.js:193


updateTargetRelationship

updateTargetRelationship(store, updateFn): void

Parameters

Name Type
store any
updateFn any

Returns

void

Defined in

packages/cozy-client/src/associations/HasMany.js:182


getHasManyItem

Static getHasManyItem(doc, relName, relItemId): any

Parameters

Name Type
doc any
relName string
relItemId string

Returns

any

Defined in

packages/cozy-client/src/associations/HasMany.js:250


getHasManyItems

Static getHasManyItems(doc, relName): any

Parameters

Name Type
doc any
relName any

Returns

any

Defined in

packages/cozy-client/src/associations/HasMany.js:259


query

Static query(document, client, assoc): QueryDefinition | CozyClientDocument

Parameters

Name Type Description
document CozyClientDocument Document to query
client any The CozyClient instance
assoc Association The query params

Returns

QueryDefinition | CozyClientDocument

Overrides

Association.query

Defined in

packages/cozy-client/src/associations/HasMany.js:236


removeHasManyItem

Static removeHasManyItem(doc, relName, relItemId): any

Parameters

Name Type
doc any
relName string
relItemId string

Returns

any

Defined in

packages/cozy-client/src/associations/HasMany.js:297


setHasManyItem

Static setHasManyItem(doc, relName, relItemId, relItemAttrs): any

Parameters

Name Type
doc any
relName string
relItemId string
relItemAttrs any

Returns

any

Defined in

packages/cozy-client/src/associations/HasMany.js:271


updateHasManyItem

Static updateHasManyItem(doc, relName, relItemId, updater): any

Parameters

Name Type
doc any
relName string
relItemId string
updater Function

Returns

any

Defined in

packages/cozy-client/src/associations/HasMany.js:321


updateRelationship

Static updateRelationship(doc, relName, updateFn): any

Parameters

Name Type
doc any
relName any
updateFn any

Returns

any

Defined in

packages/cozy-client/src/associations/HasMany.js:332