Skip to content
Jiri Spac edited this page Sep 22, 2015 · 4 revisions

Moonridge is a framework aimed at rapidly developing single-page-applications without a need to write REST api. You can write most of your data manipulation on the frontend, so your backend can really be a thin layer between DB and SPA.

Moonridge will create it's own api for your from Mongoose schemas. So you provide schemas and rules on the backend, then use a client library to do your CRUD operations on the Moonridge models.

How does a schema look like? Here are a few examples:

var schema = {
		creation_date: { type: Date, default: Date.now },
		cleared_date: { type: Date},
		cleared_by: { type: Schema.Types.ObjectId, ref: 'user'},
		loc: { type: [Number], index: '2dsphere', required: true},
		notoriety: { type: Number, default: 1},
		photos: { type: [Number], default: []}
	}

Then model is registered:

var Schema = require('mongoose').Schema;

module.exports = function (MR) {

	var poo = MR.model('poo', schema, {
		permissions: {
			C: 0,
			R: 0,
			U: 5,
			D: 5
		},
		schemaInit: function (schema) {
			schema.index({ creation_date: 1, loc: 1 }, { unique: true, dropDups: true });
		}

	});

	return poo;
};

Finally you can get the model on your frontend and use it:

TODO

Why can't we use update method with mongoDB query language?

This is a limitation in Mongoose-model.update doesn't trigger validation or middleware. Typically you don't really want any single client to be able to perform updates directly in the DB on multiple documents. That is why Moonridge has it's own 'update' method, which modifies always just single document in memory and then calls save on it. That way Mongoose validation/middleware works as expected. If you want to perform update queries utilizing Mongo query language on the DB, bypass moonridge with your custom API endpoint.

Most operations(assign/set) are easily done with model.update, in case you are working with sets of values, you might find model.addToSet and model.removeFromSet very useful. By using them, you can minimize the amount of data client has to send in order to add one item to an array of items. For example when editing tags for some entity.