Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding filters list to sync.server pushUpdates and adding test case f…

…or partial sync
  • Loading branch information...
commit ddb824f8d02b9026033ad379be89465e47411db5 1 parent cc33ade
Jacob Mumm authored
View
9 lib/persistence.sync.server.js
@@ -67,8 +67,13 @@ function entityValToJson(value, type) {
}
-exports.pushUpdates = function(session, tx, Entity, since, callback) {
- Entity.all(session).filter("_lastChange", ">", since).list(tx, function(items) {
+exports.pushUpdates = function(session, tx, Entity, since, filters, callback) {
+ if(!callback) { callback=filters; filters=null; }
+ var queryCollection = Entity.all(session).filter("_lastChange", ">", since);
+ for(i in filters){
+ queryCollection = queryCollection.filter(filters[i].property, filters[i].operator, filters[i].value);
+ }
+ queryCollection.list(tx, function(items) {
var results = [];
var meta = Entity.meta;
var fieldSpec = meta.fields;
View
19 test/browser/test.sync.partial.html
@@ -0,0 +1,19 @@
+<!DOCTYPE html>
+<html>
+<head>
+<title>Partial Sync Test</title>
+<script src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
+<script src="persistence.js"></script>
+<script src="persistence.store.sql.js"></script>
+<script src="persistence.store.websql.js"></script>
+<script src="persistence.sync.js"></script>
+<script src="test.sync.partial.js"></script>
+</head>
+<body>
+<h1>Partial Sync Test</h1>
+<h2>Examine Local Database</h2>
+<h2>View test.partial.sync.js</h2>
+<p>Full server database contains two teachers with one class each and four or five kids per class.</p>
+<p>By default, test.partial.sync.js pulls down only records that apply to teacher #1.</p>
+</body>
+</html>
View
48 test/browser/test.sync.partial.js
@@ -0,0 +1,48 @@
+//In theory, before doing any syncing, log in
+//retrieve client identifier to use with sync requests
+var teacher_id = 1;
+
+$(document).ready(function(){
+ persistence.store.websql.config(persistence, "teachereval", 'database', 5 * 1024 * 1024);
+ Student = persistence.define('Student', {
+ student_id : "INT",
+ first_name : "TEXT",
+ last_name : "TEXT",
+ });
+
+ Teacher = persistence.define('Teacher', {
+ teacher_id : "INT",
+ first_name : "TEXT",
+ last_name : "TEXT",
+ });
+
+ Class = persistence.define('Class', {
+ class_id : "INT",
+ teacher_id : "INT",
+ class_name : "TEXT",
+ });
+
+ StudentClass = persistence.define('student_class', {
+ student_id : "INT",
+ class_id : "INT",
+ });
+
+ Student.enableSync('/studentUpdates/'+teacher_id);
+ Teacher.enableSync('/teacherUpdates/'+teacher_id);
+ Class.enableSync('/classUpdates/'+teacher_id);
+ StudentClass.enableSync('/studentclassUpdates/'+teacher_id);
+
+ persistence.reset(function(){
+ persistence.schemaSync(function(){
+ persistence.sync.getJSON('/reset', function() {
+ Teacher.syncAll(persistence.sync.preferRemoteConflictHandler, function(){
+ Class.syncAll(persistence.sync.preferRemoteConflictHandler, function(){
+ Student.syncAll(persistence.sync.preferRemoteConflictHandler, function(){
+ StudentClass.syncAll(persistence.sync.preferRemoteConflictHandler, function(){});
+ });
+ });
+ });
+ });
+ });
+ });
+});
View
145 test/node/partial.sync.schema.sql
@@ -0,0 +1,145 @@
+--
+-- Table structure for table `_syncremovedobject`
+--
+
+DROP TABLE IF EXISTS `_syncremovedobject`;
+/*!40101 SET @saved_cs_client = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `_syncremovedobject` (
+ `entity` varchar(255) DEFAULT NULL,
+ `objectId` varchar(32) DEFAULT NULL,
+ `date` bigint(20) DEFAULT NULL,
+ `id` varchar(32) NOT NULL,
+ PRIMARY KEY (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `_syncremovedobject`
+--
+
+LOCK TABLES `_syncremovedobject` WRITE;
+/*!40000 ALTER TABLE `_syncremovedobject` DISABLE KEYS */;
+/*!40000 ALTER TABLE `_syncremovedobject` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `class`
+--
+
+DROP TABLE IF EXISTS `class`;
+/*!40101 SET @saved_cs_client = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `class` (
+ `class_id` int(11) NOT NULL AUTO_INCREMENT,
+ `teacher_id` int(11) NOT NULL,
+ `class_name` varchar(120) NOT NULL,
+ `id` varchar(32) DEFAULT NULL,
+ `_lastChange` bigint(20) DEFAULT NULL,
+ PRIMARY KEY (`class_id`),
+ KEY `fk_teacher_id` (`teacher_id`),
+ CONSTRAINT `fk_teacher_id` FOREIGN KEY (`teacher_id`) REFERENCES `teacher` (`teacher_id`)
+) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `class`
+--
+
+LOCK TABLES `class` WRITE;
+/*!40000 ALTER TABLE `class` DISABLE KEYS */;
+INSERT INTO `class` VALUES (1,1,'Computer Systems','d2ffcf2a33c911e08f5eb58579ce9ead',1297199638),(2,2,'Linux/Dark Arts','d2ffd2e533c911e08f5eb58579ce9ead',1297199638);
+/*!40000 ALTER TABLE `class` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `student`
+--
+
+DROP TABLE IF EXISTS `student`;
+/*!40101 SET @saved_cs_client = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `student` (
+ `student_id` int(11) NOT NULL AUTO_INCREMENT,
+ `first_name` varchar(60) NOT NULL,
+ `last_name` varchar(60) NOT NULL,
+ `id` varchar(32) DEFAULT NULL,
+ `_lastChange` bigint(20) DEFAULT NULL,
+ PRIMARY KEY (`student_id`)
+) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `student`
+--
+
+LOCK TABLES `student` WRITE;
+/*!40000 ALTER TABLE `student` DISABLE KEYS */;
+INSERT INTO `student` VALUES (1,'John','Doe','cd6068c833c911e08f5eb58579ce9ead',1297199634),(2,'Jane','Doe','cd606df733c911e08f5eb58579ce9ead',1297199634),(3,'Chris','Farmer','cd6070b733c911e08f5eb58579ce9ead',1297199634),(4,'Bob','Jones','cd60738433c911e08f5eb58579ce9ead',1297199634),(5,'Christine','Alexander','cd60761833c911e08f5eb58579ce9ead',1297199634),(6,'Abe','Lincoln','cd60786e33c911e08f5eb58579ce9ead',1297199634),(7,'Adrian','Doty','cd607af033c911e08f5eb58579ce9ead',1297199634),(8,'Eileen','Nyman','cd607dbe33c911e08f5eb58579ce9ead',1297199634),(9,'Amber','Chase','cd60807033c911e08f5eb58579ce9ead',1297199634);
+/*!40000 ALTER TABLE `student` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `student_class`
+--
+
+DROP TABLE IF EXISTS `student_class`;
+/*!40101 SET @saved_cs_client = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `student_class` (
+ `student_id` int(11) NOT NULL,
+ `class_id` int(11) NOT NULL,
+ `id` varchar(32) DEFAULT NULL,
+ `_lastChange` bigint(20) DEFAULT NULL,
+ KEY `fk_student_id` (`student_id`),
+ KEY `fk_class_id` (`class_id`),
+ CONSTRAINT `fk_class_id` FOREIGN KEY (`class_id`) REFERENCES `class` (`class_id`) ON DELETE CASCADE ON UPDATE CASCADE,
+ CONSTRAINT `fk_student_id` FOREIGN KEY (`student_id`) REFERENCES `student` (`student_id`) ON DELETE CASCADE ON UPDATE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=latin1;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `student_class`
+--
+
+LOCK TABLES `student_class` WRITE;
+/*!40000 ALTER TABLE `student_class` DISABLE KEYS */;
+INSERT INTO `student_class` VALUES (1,1,'d61678e133c911e08f5eb58579ce9ead',1297199644),(2,1,'d6167d0c33c911e08f5eb58579ce9ead',1297199644),(3,1,'d6167f2833c911e08f5eb58579ce9ead',1297199644),(4,1,'d616812c33c911e08f5eb58579ce9ead',1297199644),(5,1,'d616833c33c911e08f5eb58579ce9ead',1297199644),(6,2,'d616854733c911e08f5eb58579ce9ead',1297199644),(7,2,'d616874e33c911e08f5eb58579ce9ead',1297199644),(8,2,'d616895933c911e08f5eb58579ce9ead',1297199644),(9,2,'d6168b6c33c911e08f5eb58579ce9ead',1297199644);
+/*!40000 ALTER TABLE `student_class` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `teacher`
+--
+
+DROP TABLE IF EXISTS `teacher`;
+/*!40101 SET @saved_cs_client = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `teacher` (
+ `teacher_id` int(11) NOT NULL AUTO_INCREMENT,
+ `first_name` varchar(60) NOT NULL,
+ `last_name` varchar(60) NOT NULL,
+ `id` varchar(32) DEFAULT NULL,
+ `_lastChange` bigint(20) DEFAULT NULL,
+ PRIMARY KEY (`teacher_id`)
+) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `teacher`
+--
+
+LOCK TABLES `teacher` WRITE;
+/*!40000 ALTER TABLE `teacher` DISABLE KEYS */;
+INSERT INTO `teacher` VALUES (1,'Mark','Price','d1521f2933c911e08f5eb58579ce9ead',1297199641),(2,'Tony','Basil','d152235d33c911e08f5eb58579ce9ead',1297199641);
+/*!40000 ALTER TABLE `teacher` ENABLE KEYS */;
+UNLOCK TABLES;
+/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
+
+/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
+/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
+/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
+/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
+/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
+/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
+/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
View
173 test/node/test.sync.partial.server.js
@@ -0,0 +1,173 @@
+var sys = require('sys');
+var connect = require('connect');
+var express = require('express');
+
+var persistence = require('../../lib/persistence').persistence;
+var persistenceStore = require('../../lib/persistence.store.mysql');
+var persistenceSync = require('../../lib/persistence.sync.server');
+
+// Database configuration
+persistenceStore.config(persistence, 'localhost', 3306, 'partialsync', 'psyncuser', 'psyncpass');
+
+// Switch off query logging:
+//persistence.db.log = false;
+
+function log(o) {
+ sys.print(sys.inspect(o) + "\n");
+}
+
+persistenceSync.config(persistence);
+
+// Data model
+Student = persistence.define('Student', {
+ student_id : "INT",
+ first_name : "TEXT",
+ last_name : "TEXT",
+});
+
+Student.enableSync();
+
+Teacher = persistence.define('Teacher', {
+ teacher_id : "INT",
+ first_name : "TEXT",
+ last_name : "TEXT",
+});
+
+Teacher.enableSync();
+
+Class = persistence.define('Class', {
+ class_id : "INT",
+ teacher_id : "INT",
+ class_name : "TEXT",
+});
+
+Class.enableSync();
+
+StudentClass = persistence.define('student_class', {
+ student_id : "INT",
+ class_id : "INT",
+});
+
+StudentClass.enableSync();
+
+
+var app = express.createServer(
+ connect.bodyDecoder(),
+ connect.staticProvider('.'),
+ function(req, res, next) {
+ var end = res.end;
+
+ req.conn = persistenceStore.getSession();
+ res.end = function() {
+ req.conn.close();
+ end.apply(res, arguments);
+ };
+ req.conn.transaction(function(tx) {
+ req.tx = tx;
+ next();
+ });
+ }
+);
+
+
+app.get('/reset', function(req, res) {
+ req.conn.schemaSync(req.tx, function() {
+ req.conn.flush(req.tx, function() {
+ res.send({status: "ok"});
+ });
+ });
+});
+
+app.get('/classUpdates/:teacher_id', function(req, res) {
+ var filters = req.params.teacher_id ? {0:{property:'teacher_id',operator:'=',value:req.params.teacher_id}} : null;
+ persistenceSync.pushUpdates(req.conn, req.tx, Class, req.query.since, filters, function(updates) {
+ res.send(updates);
+ });
+});
+
+app.post('/classUpdates/:teacher_id', function(req, res) {
+ persistenceSync.receiveUpdates(req.conn, req.tx, Class, req.body, function(result) {
+ res.send(result);
+ });
+});
+
+app.get('/studentUpdates/:teacher_id', function(req, res) {
+ var session = persistenceStore.getSession();
+ if(req.params.teacher_id){
+ var student_ids = new Array();
+ var class_ids = new Array();
+ Class.all(session).filter('teacher_id', '=', req.params.teacher_id).list(function(classes){
+ for(i in classes){
+ console.log('class_id = ' + classes[i].class_id);
+ StudentClass.all(session).filter('class_id','=',classes[i].class_id).list(function(sc){
+ for(j in sc){
+ student_ids.push(sc[j].student_id);
+ if(i==(classes.length-1) && j==(sc.length-1)){
+ filters = {0:{property:'student_id', operator:'in', value:student_ids}}
+ persistenceSync.pushUpdates(req.conn, req.tx, Student, req.query.since, filters, function(updates) {
+ res.send(updates);
+ });
+ }
+ }
+ });
+ }
+ });
+ }else{
+ persistenceSync.pushUpdates(req.conn, req.tx, Student, req.query.since, null, function(updates) {
+ res.send(updates);
+ });
+ }
+
+});
+
+app.post('/studentUpdates/:teacher_id', function(req, res) {
+ persistenceSync.receiveUpdates(req.conn, req.tx, Student, req.body, function(result) {
+ res.send(result);
+ });
+});
+
+app.get('/teacherUpdates/:id', function(req, res) {
+ var filters = req.params.id ? {0:{property:'teacher_id',operator:'=',value:req.params.id}} : null;
+ persistenceSync.pushUpdates(req.conn, req.tx, Teacher, req.query.since, filters, function(updates) {
+ res.send(updates);
+ });
+});
+
+app.post('/teacherUpdates/:id', function(req, res) {
+ persistenceSync.receiveUpdates(req.conn, req.tx, Teacher, req.body, function(result) {
+ res.send(result);
+ });
+});
+
+app.get('/studentclassUpdates/:teacher_id', function(req, res) {
+ var session = persistenceStore.getSession();
+ if(req.params.teacher_id){
+ var class_ids = new Array();
+ Class.all(session).filter('teacher_id', '=', req.params.teacher_id).list(function(classes){
+ for(i in classes){
+ class_ids.push(classes[i].class_id);
+ if(i==(classes.length-1)){
+ filters = {0:{property:'class_id', operator:'in', value:class_ids}}
+ persistenceSync.pushUpdates(req.conn, req.tx, StudentClass, req.query.since, filters, function(updates) {
+ res.send(updates);
+ });
+ }
+ }
+ });
+ }else{
+ persistenceSync.pushUpdates(req.conn, req.tx, Student, req.query.since, null, function(updates) {
+ res.send(updates);
+ });
+ }
+});
+
+app.post('/studentclassUpdates/:teacher_id', function(req, res) {
+ persistenceSync.receiveUpdates(req.conn, req.tx, StudentClass, req.body, function(result) {
+ res.send(result);
+ });
+});
+
+
+app.listen(80);
+
+console.log('Server running at http://127.0.0.1:80');
Please sign in to comment.
Something went wrong with that request. Please try again.