redis / redis-om-node

Object mapping, and more, for Redis and Node.js. Written in TypeScript.
MIT License
1.18k stars 80 forks source link

how to define schema for nested object with same field name in different layer? #212

Closed vicodinl closed 1 year ago

vicodinl commented 1 year ago

Suppose there exists an object like this: const person = { name: 'alice', age: 28, children: [ { name: 'bob', age: 4, }, { name: 'charlie', age: 5, } ], } how to define the schema?

By the way, utilizing entities within redis-om-spring is not only straightforward but also exudes simplicity and elegance. the requirement of defining a schema in Node.js can be quite challenging, particularly for intricate objects. is it necessory?

aflorj commented 1 year ago

This should work:

const personSchema = new Schema('person', {
    name: { type: 'string' },
    age: { type: 'number' },
    children: { type: 'string[]', path: '$.children[*].name' },
    children: { type: 'number[]', path: '$.children[*].age' },
})

You don't have to define them if you won't be performing indexing or searching on them.

guyroyse commented 1 year ago

Not quite correct @aflorj but you've got the right idea.

const personSchema = new Schema('person', {
    name: { type: 'string' },
    age: { type: 'number' },
    children_names: { type: 'string[]', path: '$.children[*].name' },
    children_ages: { type: 'number[]', path: '$.children[*].age' },
})

And thanks for helping out!