js-data-rethinkdb icon indicating copy to clipboard operation
js-data-rethinkdb copied to clipboard

A way to support `unique` constraint in RethinkDB

Open menosprezzi opened this issue 7 years ago • 1 comments

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);
    };

menosprezzi avatar Aug 03 '18 20:08 menosprezzi

@menosprezzi contributions are welcome!

crobinson42 avatar Aug 05 '18 23:08 crobinson42