Skip to content

A way to support unique constraint in RethinkDB #29

Open
@menosprezzi

Description

@menosprezzi

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions