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

A way to support unique constraint in RethinkDB #29

Open
menosprezzi opened this issue Aug 3, 2018 · 1 comment
Open

A way to support unique constraint in RethinkDB #29

menosprezzi opened this issue Aug 3, 2018 · 1 comment

Comments

@menosprezzi
Copy link

I was using RethinkDB in my last project, with JSData because it offers a great and simple API.
Like another DB's, rethinkdb doenst support unique constraints, and the developers comunity are adding a workaround to support this. My suggestion here is add a wrapper the create function with some like this:

    const adapter = new RethinkDBAdapter();
    (adapter as any).createFn = adapter.create;
    adapter.create = async function(mapper, props, opts) {
      for (const field of (mapper as any).schema.unique) {
        if (!(await (adapter as any).r.table(mapper.name).getAll(props[field], {index: field}).isEmpty()))
          throw new Error(`Unique constraint violated on field { ${field}: ${props[field]} }`);
      }
      return await (adapter as any).createFn(mapper, props, opts);
    };
@crobinson42
Copy link
Member

@menosprezzi contributions are welcome!

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

2 participants