Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic key support #90

Open
Benabra opened this issue Jan 7, 2017 · 2 comments
Open

Dynamic key support #90

Benabra opened this issue Jan 7, 2017 · 2 comments

Comments

@Benabra
Copy link

Benabra commented Jan 7, 2017

Hi,

Thank your for this great project.
I have just one functional question, I have a dynamic key like :

{
_id: ObjectId('oNe1D')
certifications: [{
 $userId: {
     'certified': true,
     'level': 5
 }]
}

$userId is a dynamic key, so i cant define it on my model schema.
I dont know if we can do this with camo our we need to implement it manualy ?

Thank's

@perimetral
Copy link

Try to use syntax for computable keys:

let w = 'qq';
let r = {
  x: 1,
  [w + 'ee']: 2
};
console.log(r);  //  result will be "{ x: 1, qqee: 2 }"

I'm not sure this will work correctly in Camo schema definition, but as long as class constructors work synchronously, this must work.

Anyway i don't see schema definitions in your example. It has two syntax errors: no comma after second line and missing of closing brace at seventh one. But this isn't main: you are asking about schema providing instance.

If you have some field defined as Object inside your schema, there will not be any issues with using of computable keys in it, considering there are no documents with fields referenced to computable key until key is computed and persistently saved.

You are free to do this way (will insert initial data but not more, as long as $userId is variable and is computed only once to determine key to use, so any next inserts will fail with "key exists" error):

certifications: [{
  [$userId]: {  //  notice change
    certified: true,
    level: 5
  }
}];

Also you are free to do this way (best way):

certifications: [{
  user: $userId,
  certified: true,
  level: 5
}];

Or this way:

certifications: {  //  notice change again
  [$userId]: {
    certified: true,
    level: 5
  },
  //  [$userId2]: ...
};

@Benabra
Copy link
Author

Benabra commented May 16, 2019

/close

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants