Coder Social home page Coder Social logo

codemirror-mongodb's Introduction

🚧 codemirror-mongodb travis npm

Use CodeMirror with MongoDB.

Demo

https://mongodb-js.github.io/codemirror-mongodb

Background

I recently took on the project of rewriting the query input for MongoDB Compass. It’s a real pain when writing queries to have to keep the query language and the shape of the data you’re querying against in your working memory. MongoDB users have more important work to do.

Autocompletion for field names is a feature request we hear a lot at MongoDB. We have a sketch in Compass of what the schema probably looks like. Leveraging schema analysis to enable autocompletion is a feature we’ve been wanting to build for a long time.

After weighing my options and researching the existing libraries I could potentially use, I kept coming back to one, CodeMirror.

CodeMirror is the defacto open-source code editor. CodeMirror is used in the devtools for Firefox, Chrome, and Safari, in Light Table, Adobe Brackets, Bitbucket, and over 100 other projects.

Usage

var CodeMirror = require('codemirror');
require('codemirror-mongodb/addon/hint/mongodb-hint');

CodeMirror.fromTextArea(document.getElementById('oneliner'), {
  lineNumbers: false,
  scrollbarStyle: 'null',
  mode: 'javascript',
  autoCloseBrackets: true,
  matchBrackets: true,
  theme: 'mongodb',
  extraKeys: {
    'Ctrl-Space': 'autocomplete',
    'Shift-Enter': 'parse'
  },
  mongodb: {
    fields: {
      _id: 'ObjectId',
      name: 'String',
      age: 'Number',
      number_of_pets: 'Number',
      addresses: 'Array',
      'addresses.street': 'String'
    }
  }
}).on('beforeChange', function formatAsSingleLine(cm, change) {
  if (change.update) {
    var newtext = change.text.join('').replace(/\n/g, '');
    change.update(change.from, change.to, [newtext]);
  }
  return true;
});

React

var React = require('react');
var CodeMirror = require('react-codemirror');
require('codemirror-mongodb/addon/hint/mongodb-hint');

var App = React.createClass({
  getInitialState: function() {
    return {
      code: "{}",
    };
  },
  updateCode: function(newCode) {
    this.setState({
      code: newCode.replace(/\n/g, ''),
    });
  },
  render: function() {
    const options = {
      lineNumbers: false,
      scrollbarStyle: 'null',
      mode: 'javascript',
      autoCloseBrackets: true,
      matchBrackets: true,
      theme: 'mongodb',
      extraKeys: {
        'Ctrl-Space': 'autocomplete'
      },
      mongodb: {
        fields: {
          _id: 'ObjectId',
          name: 'String',
          age: 'Number',
          number_of_pets: 'Number',
          addresses: 'Array',
          'addresses.street': 'String'
        }
      }
    };
    return <CodeMirror value={this.state.code} onChange={this.updateCode} options={options} />
  }
});

React.render(<App />, document.getElementById('app'));

Autocompletion Behavior

β–ˆ below is the user cursor position when autocomplete triggered or the resulting cursor position when a completion is applied.

Current schema of the selected namespace is:

var fields = {
  _id: 'ObjectId',
  name: 'String',
  age: 'Number',
  number_of_pets: 'Number'
};
  • ${fieldPath} A completion for v1
  • ${fieldPath}: β–ˆ Maybe nice to add in the future

Blank Slate

Input {β–ˆ}

Completions

  • _id
  • name
  • age
  • number_of_pets

Extended field based on field type

Input {_idβ–ˆ}

Completions

  • _id
  • _id: ObjectId("β–ˆ")

List Field Names With Prefix

Input {nβ–ˆ}

2 matching fields

Completions

  • name
  • number_of_pets

Single Field Matched By Name Prefix

Input {naβ–ˆ}

1 matching field so show extended

Completions

  • name
  • name: β–ˆ with starter
  • name: "β–ˆ" open exact match
  • name: /^β–ˆ/ open prefix regex

Single Field Exact Match By Name

Input {nameβ–ˆ}

Still 1 matching field so show extended

Completions

  • name
  • name: β–ˆ with starter
  • name: "β–ˆ" open exact match
  • name: /^β–ˆ/ open prefix regex

Specify Expression for Field

Input {name: β–ˆ}

Completions

  • β–ˆ with starter
  • "β–ˆ" open exact match
  • /^β–ˆ/ open prefix regex

List Expression Operators for Field

Input {name: {β–ˆ}}

Completions

  • $gte
  • $gt
  • $lte
  • $lt
  • $eq
  • $ne
  • $type
  • $size
  • $exists
  • $exists: false field not set
  • $exists: true field is set
  • $in
  • $in: ["β–ˆ"] for strings, $in: [β–ˆ] for numbers
  • $nin
  • $nin: ["β–ˆ"] for strings, n$in: [β–ˆ] for numbers
  • $all

Todo

License

Apache 2.0

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.