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

Use proxies to allow for property access hook #64

Open
bsouthga opened this issue Aug 30, 2016 · 0 comments
Open

Use proxies to allow for property access hook #64

bsouthga opened this issue Aug 30, 2016 · 0 comments

Comments

@bsouthga
Copy link
Contributor

bsouthga commented Aug 30, 2016

It could be interesting if we modeled all property access in Documents through Proxies, that way we could add additional metadata / hooks on lookup.

For example, we could add a deprecated flag to a field which would then issue a console.warn('user.name is deprecated, use user.displayName instead!').

new Collection({
  ...
  name: 'user',
  fields: {
    ...
    name: { is: 'string', deprecated: true, replacement: 'displayName' },
    displayName: { is: 'string' }
  }
})

An example of the change to tyranid itself would be something like this...

//...

async findOne(...args) {
 // ... normal find logic

  var handler = {
      get: function(target, name){
          if (name in this.def.deprecatedFields) {
            console.warn(`property ${name} is deprecated, use ${this.def.deprecatedFields[name].replacement} instead.`);
          }
          return target[name];
      }
  };

  return new Proxy(doc, handler);
}

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

No branches or pull requests

1 participant