Skip to content

Instantly share code, notes, and snippets.

@robertoandrade
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertoandrade/e2afd308a5a60a360e2b to your computer and use it in GitHub Desktop.
Save robertoandrade/e2afd308a5a60a360e2b to your computer and use it in GitHub Desktop.
Bookmarklet to export an ERD out of a Parse app's schema
if (!window.app || !window.schemas) {
alert('It doesn\'t seem like you\'re logged in to the Parse Core Data Dashboard. Make sure to login first and then click this bookmarklet.');
} else {
var $form =
$(
'<form method="post" action="http://erd-web-server.herokuapp.com/">' +
'<textarea name="erdText"></textarea>' +
'</form>'
);
var erText =
'title {label: "' + app.attributes.name + ' - Parse Core Data ERD - ' + new Date() + '", font: "Helvetica bold", size: "20"}\n' +
'entity {bgcolor: "#ececfc", font: "Helvetica"}\n';
var erTextRelationships = '';
schemas.each(function(entity) {
erText += '\n[' + entity.id + ']\n';
var columns = cols(entity.id);
$(columns).each(function(i, col) {
var isRelationship = col.type.match('(Relation|Pointer).*');
var type = _.unescape(col.type);
var isRequired = col.required;
erText +=
(i == 0 ? '*' : '') +
(isRelationship ? '+' : '') +
col.name + ' ' +
'{label: "' +
type + ', ' +
(isRequired ? 'not ' : '') + 'null' +
'"}\n'
;
if (isRelationship) {
var relationship = type.match("(.*)<(.*)>");
var relationshipType = relationship[1];
var relatedType = relationship[2];
var isPointer = relationshipType == 'Pointer';
erTextRelationships +=
entity.id + ' ' +
(isPointer ? (isRequired ? '+' : '*') : (isRequired ? '1' : '?')) +
'--' +
(isPointer ? '1' : '+') +
' ' + relatedType +
'\n';
}
});
});
erText += '\n' + erTextRelationships;
console.log('ER Code:', erText);
$form.find('textarea').val(erText);
$form.submit();
console.log('Submitted data!');
}
function cols(className) {
var keys = schemas.get(className).get("keys");
return $(
new DataBrowser.Views.DataBrowser({
state: state.set({"className": className}),
configBlob: configJson,
isAdmin: false
})
.makeColumns()
.splice(1)
)
.each(function(i, col) {
col.required = keys[col.name].required || false;
});
}
<style type="text/css">
* { font-family: Helvetica, Arial };
body a.bookmarklet { font-weight: bold; font-size: 14pt; color: gray; }
p.footnote { font-size: 9pt; }
p.footnote a { color: gray; }
</style>
<body>
<p class="footnote">
Add the <a target="_blank" href="https://en.wikipedia.org/wiki/Bookmarklet">bookmarklet</a> below to your favorites,
ie: drag the link to your favorites bar.
</p>
<a class="bookmarklet" href="javascript:(function(){
_my_script=document.createElement('SCRIPT');
/*_my_script.type='text/javascript';*/
_my_script.src='https://rawgit.com/robertoandrade/e2afd308a5a60a360e2b/raw/parse-erd-bookmarklet.js?' + Math.random();
document.getElementsByTagName('head')[0].appendChild(_my_script);
})();">Parse ERD Bookmarklet</a>
<p class="footnote">
Use it to generate
<a target="_blank" href="https://www.parse.com/questions/database-relations-diagram">Parse.com Core Data ERDs</a>
by clicking the bookmarklet while logged in to the
<a target="_blank" href="https://www.parse.com/apps/YOUR-APP-ID/collections">Parse Core Data Dashboard</a>.
</p>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment