RFC: enhancement over dgraph schema

Moved from GitHub dgraph-js/117

Posted by shreyansh-zazz:

Users should be able to define DGraph schema in object format. Defining schema in an object format will enable us to provide more validations to the schema in a clearer way like validation for each predicate, e.g.

UserSchema = new DgraphSchema({
    username: {
        type: 'String',
        index: true,
        immutable: true

    password: {
        type: 'Password',
        min: 6,
        max: 26

A big motivation for such a feature comes from the package mongoose which a MongoDB client for javascript.

Action Items

  • Support DgraphSchema in an object format
  • Provide basic validation options such as:
    • immutable: [true | false]
    • min: [int] and max: [int]
    • unique: [true | false]
    • required: [true | false]
    • match: [new RegExp()]
    • default: [any] // default value for the predicate
    • lowercase: [true | false] // change the string to lowercase before saving into the DB
    • uppercase: [true | false] // change the string to uppercase before saving into the DB
  • return formatted error if provided data fails against the schema

shreyansh-zazz commented :

I am happy to contribute but I will be needing some guidance. If anyone here would like to work on this with me please let me know.

jarifibrahim commented :

cc @vardhanapoorv

vardhanapoorv commented :

Hi @shreyansh-zazz
I like the idea. You can raise a draft PR (also include an example) and I can help you with your questions.

shreyansh-zazz commented :

Before proceeding, we have to make a decision on DgraphClient. There are two options as follows:

  1. new Dgraph.createClient(..args) will create a single DB client instance and all further communications with the database will be done using this client. (current approach)
  2. Let Option 1 be there and we can also expose an API such as Dgraph.connect(...args) which will take the same arguments as createClient(...args). The only difference will be that Dgraph.connect(...args) will set the default DB connection, meaning we can directly use Dgraph.Mutation(), Dgraph.Model() etc.

Here I am missing the DgraphClientStub because I don’t really understand why we need this when we can simply use Dgraph.createClient()

shreyansh-zazz commented :

Also, I think the API is pretty complicated. Maybe we can try to simplify that? There are too many new variable creations to perform one simple mutation, to be honest.

There must be some reason that I am not aware of for this decision, @vardhanapoorv can you explain to me little about this, please?