Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Content of arrays disapearing when using the same schema in two databases #72

Open
PMK89 opened this issue Mar 12, 2017 · 2 comments
Open

Comments

@PMK89
Copy link

PMK89 commented Mar 12, 2017

I am using linvodb3 for a cognitive mapping learning software and have three databases that should use the same schema, a main database where all the data of the user generated objects are stored, one for templates of this objects and another one for history (changed and deleted objects). As soon as I use the same schema file all arrays are shown empty at the moment I query the database but if I open the database file in an editor or with klausgao's GUI the content of the array is shown.
I could fix the problem in my application by just duplicating the schema with another name but I think this isn't supposed to be that way so I want to bring it to your attention.

@Ivshti
Copy link
Owner

Ivshti commented Mar 14, 2017

can you give a code example? usually, models are specifically tied to a database (collection), so I think this may be intended (although confusing)

@PMK89
Copy link
Author

PMK89 commented Mar 31, 2017

Sorry for the late reply. I post the schema, the controllers for the template and main database no function in the history scripts are called at the moment so I don't think it has an influence.

Schema cmelement.js

var CMElement = {
  id: { type: Number, index: true, unique: true },
  x0: { type: Number, index: true },
  y0: { type: Number, index: true },
  x1: { type: Number, index: true },
  y1: { type: Number, index: true },
  prio: { type: Number, index: true },
  title: { type: String, index: true },
  types: [String],
  coor: {
    x: { type: Number, index: false },
    y: { type: Number, index: false }
  },
  cat: [String],
  z_pos: { type: Number, index: false },
  dragging: { type: Boolean, default: false },
  active: { type: Boolean, default: false },
  cmline: { type: String, index: false },
  cmobject:{ type: String, index: false },
  prep: { type: String, index: false }
}

module.exports = CMElement;

cmelementController.js

var CMElement = require('../models/cmelement');
// handles Databases

var LinvoDB = require("linvodb3");
LinvoDB.dbPath = "./data/element_test21";
var modelName = "elements";
var schema = CMElement;
var options = { };
// options.store = { db: require("level-js") }; // Options passed to LevelUP constructor
console.log(LinvoDB);
var cmelement = new LinvoDB(modelName, schema, options); // New model; Doc is the constructor

	// loads data within specified boundaries
exports.load = function(req, res){
	var l = parseInt(req.query.l);
	var t = parseInt(req.query.t);
	var r = parseInt(req.query.r);
	var b = parseInt(req.query.b);
	// gets all elements within a expanded user view. Maybe should be made better
	cmelement.find({
			$or: [
				{$and: [{x0: { $gt: l, $lt: r }}, {y0: { $gt: t, $lt: b }}]},
				{$and: [{x1: { $gt: l, $lt: r }}, {y1: { $gt: t, $lt: b }}]}
			]
		}, function(err, data) {
		if (err) console.log(err);
		res.jsonp(data);
	});
};

	// changes elements
exports.change = function(req, res){
	//console.log(req);
	if(req.body.id) {
		cmelement.findOne({id: req.body.id}, function(err, data) {
			if (err) console.log(err);
			if (data) {
					data.id = req.body.id;
					data.prio = req.body.prio;
					data.x0 = req.body.x0;
					data.y0 = req.body.y0;
					data.x1 = req.body.x1;
					data.y1 = req.body.y1;
					data.types = req.body.types;
					data.coor = req.body.coor;
					data.cmline = JSON.stringify(req.body.cmline);
					data.cmobject = JSON.stringify(req.body.cmobject);
					data.cat = req.body.cat;
					data.z_pos = req.body.z_pos;
	        // saves changes to database
					data.save(function (err) {
						console.log(err);
					});
					res.jsonp(data);
      }
    });
  }
};
	// finds element by id
exports.findid = function(req, res){
	cmelement.findOne({id: parseInt(req.query.id)}, function(err, data) {
		if (err) console.log(err);
		res.jsonp(data);
	});
};

// finds element by title fights asynchroniosity
exports.findtitle = function(req, res){
cmelement.find({}, function(err, data) {
  if (err) console.log(err);

});
};


	//finds highest id
exports.findmax = function(req, res){
	cmelement.find({}).sort({id: -1}).limit(1).exec(function(err, data) {
		if (err) console.log(err);
		var max = data[0].id;
		res.jsonp(max);
	});
};

// generates new element
exports.newelement = function(req, res){
  if(req.body.id) {

    var cme = new cmelement;
		cme.id = req.body.id;
		cme.prio = req.body.prio;
		cme.x0 = req.body.x0;
		cme.y0 = req.body.y0;
		cme.x1 = req.body.x1;
		cme.y1 = req.body.y1;
		cme.types = req.body.types;
		cme.title = req.body.title;
		cme.coor = req.body.coor;
		cme.cmline = JSON.stringify(req.body.cmline);
		cme.cmobject = JSON.stringify(req.body.cmobject);
		cme.cat = req.body.cat;
		cme.z_pos = req.body.z_pos;
    cme.save(function (err) {
			console.log(err) // #error message
		});

  }
}

templateController.js

var CMElement = require('../models/cmelement1');
// handles Databases

var LinvoDB = require("linvodb3");
LinvoDB.dbPath = "./data/templates";
var modelName = "templates";
// Non-strict always, can be left empty
var schema = CMElement;
var options = { };
// options.filename = "./test.db"; // Path to database - not necessary
// options.store = { db: require("level-js") }; // Options passed to LevelUP constructor
console.log(LinvoDB);
var cmelement = new LinvoDB(modelName, schema, options); // New model; Doc is the constructor

	// loads data within specified boundaries
exports.temp_load = function(req, res){
	// gets all elements within a expanded user view. Maybe should be made better
	cmelement.find({}).sort({prio: 1}).exec(function(err, data) {
		if (err) console.log(err);
		res.jsonp(data);
	});
};

// removes buttons from database
exports.remove = function(req, res){
	cmelement.remove({  }, { multi: true }, function (err, numRemoved) {
		if (err) console.log(err);
		console.log(numRemoved, ' templates removed')
		res.jsonp(numRemoved + ' templates removed')
	})
};

	// changes elements
exports.temp_change = function(req, res){
	//console.log(req);
	if(req.body.id) {
		cmelement.findOne({id: req.body.id}, function(err, data) {
			if (err) console.log(err);
			if (data) {
				data = req.body;
        // saves changes to database
				data.save(function (err) {
					console.log(err);
				});
      }
    });
  }
};
	// finds element by id
exports.temp_findid = function(req, res){
	cmelement.findOne({id: parseInt(req.query.id)}, function(err, data) {
		if (err) console.log(err);
		res.jsonp(data);
	});
};

	//finds highest priority
exports.temp_findmax = function(req, res){
	cmelement.find({}).sort({prio: 1}).limit(2).exec(function(err, data) {
		if (err) console.log(err);
		res.jsonp(data[0]);
	});
};

// generates new element
exports.newtemplate = function(req, res){
  if(req.body.id) {

		var cme = new cmelement;
    cme.id = req.body.id;
		cme.prio = req.body.prio;
		cme.x0 = req.body.x0;
		cme.y0 = req.body.y0;
		cme.x1 = req.body.x1;
		cme.y1 = req.body.y1;
		cme.title = req.body.title;
		cme.types = req.body.types;
		cme.coor = req.body.coor;
		cme.cmline = JSON.stringify(req.body.cmline);
		cme.cmobject = JSON.stringify(req.body.cmobject);
		cme.cat = req.body.cat;
		cme.z_pos = req.body.z_pos;
		cme.prep = req.body.prep;
		// console.log(cme.types);
		res.jsonp(cme);
    cme.save(function (err) {
			console.log(err) //  #error message
		});
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants