Skip to content

Commit

Permalink
Added support for MongoDB syntax (#2518)
Browse files Browse the repository at this point in the history
  • Loading branch information
airs0urce authored Aug 13, 2020
1 parent 2da2beb commit 004eaa7
Show file tree
Hide file tree
Showing 15 changed files with 2,495 additions and 3 deletions.
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions components.json
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,11 @@
"title": "Mizar",
"owner": "Golmote"
},
"mongodb": {
"title": "MongoDB",
"owner": "airs0urce",
"require": "javascript"
},
"monkey": {
"title": "Monkey",
"owner": "Golmote"
Expand Down
96 changes: 96 additions & 0 deletions components/prism-mongodb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
(function (Prism) {

var operators = [
// query and projection
'$eq', '$gt', '$gte', '$in', '$lt', '$lte', '$ne', '$nin', '$and', '$not', '$nor', '$or',
'$exists', '$type', '$expr', '$jsonSchema', '$mod', '$regex', '$text', '$where', '$geoIntersects',
'$geoWithin', '$near', '$nearSphere', '$all', '$elemMatch', '$size', '$bitsAllClear', '$bitsAllSet',
'$bitsAnyClear', '$bitsAnySet', '$comment', '$elemMatch', '$meta', '$slice',

// update
'$currentDate', '$inc', '$min', '$max', '$mul', '$rename', '$set', '$setOnInsert', '$unset',
'$addToSet', '$pop', '$pull', '$push', '$pullAll', '$each', '$position', '$slice', '$sort', '$bit',

// aggregation pipeline stages
'$addFields', '$bucket', '$bucketAuto', '$collStats', '$count', '$currentOp', '$facet', '$geoNear',
'$graphLookup', '$group','$indexStats', '$limit', '$listLocalSessions', '$listSessions', '$lookup',
'$match', '$merge', '$out', '$planCacheStats', '$project', '$redact', '$replaceRoot', '$replaceWith',
'$sample', '$set', '$skip', '$sort', '$sortByCount', '$unionWith', '$unset', '$unwind',

// aggregation pipeline operators
'$abs', '$accumulator', '$acos', '$acosh', '$add', '$addToSet', '$allElementsTrue', '$and',
'$anyElementTrue', '$arrayElemAt', '$arrayToObject', '$asin', '$asinh', '$atan', '$atan2',
'$atanh', '$avg', '$binarySize', '$bsonSize', '$ceil', '$cmp', '$concat', '$concatArrays', '$cond',
'$convert', '$cos', '$dateFromParts', '$dateToParts', '$dateFromString', '$dateToString', '$dayOfMonth',
'$dayOfWeek', '$dayOfYear', '$degreesToRadians', '$divide', '$eq', '$exp', '$filter', '$first',
'$floor', '$function', '$gt', '$gte', '$hour', '$ifNull', '$in', '$indexOfArray', '$indexOfBytes',
'$indexOfCP', '$isArray', '$isNumber', '$isoDayOfWeek', '$isoWeek', '$isoWeekYear', '$last',
'$last', '$let', '$literal', '$ln', '$log', '$log10', '$lt', '$lte', '$ltrim', '$map', '$max',
'$mergeObjects', '$meta', '$min', '$millisecond', '$minute', '$mod', '$month', '$multiply', '$ne',
'$not', '$objectToArray', '$or', '$pow', '$push', '$radiansToDegrees', '$range', '$reduce',
'$regexFind', '$regexFindAll', '$regexMatch', '$replaceOne', '$replaceAll', '$reverseArray', '$round',
'$rtrim', '$second', '$setDifference', '$setEquals', '$setIntersection', '$setIsSubset', '$setUnion',
'$size', '$sin', '$slice', '$split', '$sqrt', '$stdDevPop', '$stdDevSamp', '$strcasecmp', '$strLenBytes',
'$strLenCP', '$substr', '$substrBytes', '$substrCP', '$subtract', '$sum', '$switch', '$tan',
'$toBool', '$toDate', '$toDecimal', '$toDouble', '$toInt', '$toLong', '$toObjectId', '$toString',
'$toLower', '$toUpper', '$trim', '$trunc', '$type', '$week', '$year', '$zip',

// aggregation pipeline query modifiers
'$comment', '$explain', '$hint', '$max', '$maxTimeMS', '$min', '$orderby', '$query',
'$returnKey', '$showDiskLoc', '$natural',
];

var builtinFunctions = [
'ObjectId',
'Code',
'BinData',
'DBRef',
'Timestamp',
'NumberLong',
'NumberDecimal',
'MaxKey',
'MinKey',
'RegExp',
'ISODate',
'UUID',
];

operators = operators.map(function(operator) {
return operator.replace('$', '\\$');
});

var operatorsSource = '(?:' + operators.join('|') + ')\\b';

Prism.languages.mongodb = Prism.languages.extend('javascript', {});

Prism.languages.insertBefore('mongodb', 'string', {
'property': {
pattern: /(?:(["'])(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1|[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)(?=\s*:)/,
greedy: true,
inside: {
'keyword': RegExp('^([\'"])?' + operatorsSource + '(?:\\1)?$')
}
}
});

Prism.languages.mongodb.string.inside = {
url: {
// url pattern
pattern: /https?:\/\/[-\w@:%.+~#=]{1,256}\.[a-z0-9()]{1,6}\b[-\w()@:%+.~#?&/=]*/i,
greedy: true
},
entity: {
// ipv4
pattern: /\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/,
greedy: true
}
};

Prism.languages.insertBefore('mongodb', 'constant', {
'builtin': {
pattern: RegExp('\\b(?:' + builtinFunctions.join('|') + ')\\b'),
alias: 'keyword'
}
});

}(Prism));
1 change: 1 addition & 0 deletions components/prism-mongodb.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 59 additions & 0 deletions examples/prism-mongodb.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<h2>Document</h2>
<pre><code>
{
'_id': ObjectId('5ec72ffe00316be87cab3927'),
'code': Code('function () { return 22; }'),
'binary': BinData(1, '232sa3d323sd232a32sda3s2d3a2s1d23s21d3sa'),
'dbref': DBRef('namespace', ObjectId('5ec72f4200316be87cab3926'), 'db'),
'timestamp': Timestamp(0, 0),
'long': NumberLong(9223372036854775807),
'decimal': NumberDecimal('1000.55'),
'integer': 100,
'maxkey': MaxKey(),
'minkey': MinKey(),
'isodate': ISODate('2012-01-01T00:00:00.000Z'),
'regexp': RegExp('prism(js)?', 'i'),
'string': 'Hello World',
'numberArray': [1, 2, 3],
'stringArray': ['1','2','3'],
'randomKey': null,
'object': { 'a': 1, 'b': 2 },
'max_key2': MaxKey(),
'number': 1234,
'invalid-key': 123,
noQuotesKey: 'value',
}
</code></pre>

<h2>Query</h2>
<pre><code>
db.users.find({
_id: { $nin: ObjectId('5ec72ffe00316be87cab3927') },
age: { $gte: 18, $lte: 99 },
field: { $exists: true }
})
</code></pre>


<h2>Update</h2>
<pre><code>
db.users.updateOne(
{
_id: ObjectId('5ec72ffe00316be87cab3927')
},
{
$set: { age: 30 },
$inc: { updateCount: 1 },
$push: { updateDates: new Date() }
}
)
</code></pre>

<h2>Aggregate</h2>
<pre><code>
db.orders.aggregate([
{ $sort : { age : -1 } },
{ $project : { age : 1, status : 1, name : 1 } },
{ $limit: 5 }
])
</code></pre>
1 change: 1 addition & 0 deletions plugins/autoloader/prism-autoloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"lilypond": "scheme",
"markdown": "markup",
"markup-templating": "markup",
"mongodb": "javascript",
"n4js": "javascript",
"nginx": "clike",
"objectivec": "c",
Expand Down
2 changes: 1 addition & 1 deletion plugins/autoloader/prism-autoloader.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions plugins/show-language/prism-show-language.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
"markup-templating": "Markup templating",
"matlab": "MATLAB",
"mel": "MEL",
"mongodb": "MongoDB",
"moon": "MoonScript",
"n1ql": "N1QL",
"n4js": "N4JS",
Expand Down
Loading

0 comments on commit 004eaa7

Please sign in to comment.