WorldBrain / storex

Storex Core - A modular and portable database abstraction ecosystem for JavaScript
MIT License
150 stars 8 forks source link

Feature: Relationship fetching & filtering #4

Open ShishKabab opened 6 years ago

ShishKabab commented 6 years ago

Motivation: When fetching stuff from storage, we often want to fetch related objects. By doing this in one call, we do not only gain convenience, but give some backends the chance to optimize fetching, for example by generating JOINs in SQL backends.

Design considerations:

Example:

storageManager.registry.registerCollections({
    user: {
        version: new Date(2018, 11, 11),
        fields: {
            displayName: { type: 'string' },
            age: { type: 'number' },
        },
        indices: [],
    },
    singleEmail: {
        fields: {
            address: { type: 'string' },
            isActive: { type: 'string' },
        },
        relationships: [
            { singleChildOf: 'user' }
        ]
    },
    multiEmail: {
        fields: {
            address: { type: 'string' },
            isActive: { type: 'string' },
        },
        relationships: [
            { childOf: 'user' }
        ]
    },
})

storageManager.collection('user').findObjects({}, {relationships: ['singleEmail']})
storageManager.collection('user').findObjects({}, {relationships: {'multiEmail.active': true}})
storageManager.collection('user').findObjects({'singleEmail.active': true}, {relationships: ['singleEmail']})
# Do we allow filtering objects by childOf (one-to-many) relationships?
# Do we allow filtering objects by connects (many-to-many) relationships?