Skip to content

Instantly share code, notes, and snippets.

@botic
Created September 7, 2011 21:18
Show Gist options
  • Select an option

  • Save botic/1201764 to your computer and use it in GitHub Desktop.

Select an option

Save botic/1201764 to your computer and use it in GitHub Desktop.
ringo-sqlstore demo mappings
var Store = require('ringo-sqlstore').Store;
var strings = require("ringo/utils/strings");
var auth= require("./userAuth");
var store = new Store({
"url": "jdbc:mysql://127.0.0.1/chromatic",
"driver": "com.mysql.jdbc.Driver",
"username": "chromatic",
"password": "chromatic"
});
export("User", "Usergroup", "Photo", "File", "Collection", "Lightbox");
var User = store.defineEntity("User", {
"table": "t_user",
"properties": {
"username": {
"type": "string",
"length": 500
},
"password": {
"type": "string",
"length": 128
},
"salt": {
"type": "string",
"length": 128
},
"company": {
"type": "string",
"length": 500
},
"title": {
"type": "string",
"length": 100
},
"firstName":{
"type": "string",
"length": 500
},
"lastName": {
"type": "string",
"length": 500
},
"country": {
"type": "string",
"length": 500
},
"zipCode": {
"type": "string",
"length": 100
},
"city": {
"type": "string",
"length": 500
},
"street": {
"type": "string",
"length": 500
},
"phone": {
"type": "string",
"length": 100
},
"blocked": "boolean",
"admin": "boolean",
"usergroups": {
"type": "collection",
"entity": "Usergroup",
"through": "RelUserUsergroup", // the entity to join with
"join": "RelUserUsergroup.user == User.id", // the join predicate
"foreignProperty": "RelUserUsergroup.usergroup" // the column containing the foreign key
},
"lightboxes": {
"type": "collection",
"entity": "Lightbox",
"foreignProperty": "owner"
}
}
});
User.create = function(username, password, firstName, lastName, company, title, country, zipCode, city, street, phone, blocked, admin) {
var salt = strings.random(10) + ((new Date().getTime() % 3571) * 691) + strings.random(5) + (Math.random() * 3209).toFixed(0);
var user = new User();
user.username = username;
user.password = auth.encryptPassword(password, salt);
user.salt = salt;
// Optional data
user.firstName = firstName || "";
user.lastName = lastName || "";
user.company = company || "";
user.title = title || "";
user.country = country || "";
user.zipCode = zipCode || "";
user.city = city || "";
user.street = street || "";
user.phone = phone || "";
user.blocked = blocked || false;
user.admin = admin || false;
user.save();
};
var Usergroup = store.defineEntity("Usergroup", {
"table": "t_usergroup",
"properties": {
"name": {
"type": "string",
"length": 500
},
"users": {
"type": "collection",
"entity": "User",
"through": "RelUserUsergroup", // the entity to join with
"join": "RelUserUsergroup.usergroup == Usergroup.id", // the join predicate
"foreignProperty": "RelUserUsergroup.user" // the column containing the foreign key
}
}
});
store.defineEntity("RelUserUsergroup", {
"table": "t_user_usergroup",
"properties": {
"user": {
"type": "object",
"entity": "User",
"column": "rel_user"
},
"usergroup": {
"type": "object",
"entity": "Usergroup",
"column": "rel_usergroup"
}
}
});
var Photo = store.defineEntity("Photo", {
"table": "t_photo",
"properties": {
"reference": {
"type": "string",
"length": 50
},
"photographer": {
"type": "string",
"length": 500
},
"copyright": {
"type": "string",
"length": 500
},
"headline": {
"type": "string",
"length": 500
},
"caption": {
"type": "text"
},
"keywords": {
"type": "text"
},
"uploadTime": {
"type": "date"
},
"takenOn": {
"type": "date"
},
"sublocation": {
"type": "string",
"length": 500
},
"city": {
"type": "string",
"length": 500
},
"province": {
"type": "string",
"length": 500
},
"countryCode": {
"type": "string",
"length": 3
},
"specialInstructions": {
"type": "text"
},
"files": {
"type": "collection",
"entity": "Files",
"foreignProperty": "photo"
},
"creator": {
"type": "object",
"entity": "User",
"column": "photo_f_user"
}
}
});
var File = store.defineEntity("File", {
"table": "t_file",
"properties": {
"path": {
"type": "string",
"length": 2000
},
"width": {
"type": "integer"
}
,
"height": {
"type": "integer"
},
"photo": {
"type": "object",
"entity": "Photo",
"column": "file_f_photo"
}
}
});
var Collection = store.defineEntity("Collection", {
"table": "t_collection",
"properties": {
"name": {
"type": "string",
"length": 500
},
"photos": {
"type": "collection",
"entity": "Photo",
"through": "RelCollectionPhoto", // the entity to join with
"join": "RelCollectionPhoto.collection == Collection.id", // the join predicate
"foreignProperty": "RelCollectionPhoto.photo" // the column containing the foreign key
},
"usergroups": {
"type": "collection",
"entity": "Usergroup",
"through": "RelCollectionUsergroup", // the entity to join with
"join": "RelCollectionUsergroup.collection == Collection.id", // the join predicate
"foreignProperty": "RelCollectionUsergroup.usergroup" // the column containing the foreign key
}
}
});
store.defineEntity("RelCollectionPhoto", {
"table": "t_collection_photo",
"properties": {
"collection": {
"type": "object",
"entity": "Collection",
"column": "rel_collection"
},
"photo": {
"type": "object",
"entity": "Photo",
"column": "rel_photo"
}
}
});
store.defineEntity("RelCollectionUsergroup", {
"table": "t_collection_usergroup",
"properties": {
"collection": {
"type": "object",
"entity": "Collection",
"column": "rel_collection"
},
"usergroup": {
"type": "object",
"entity": "Usergroup",
"column": "rel_usergroup"
}
}
});
var Lightbox = store.defineEntity("Lightbox", {
"table": "t_lightbox",
"properties": {
"name": {
"type": "string",
"length": 500
},
"secretKey": {
"type": "string",
"length": 500
},
"owner": {
"type": "object",
"entity": "User",
"column": "lightbox_f_user"
},
"photos": {
"type": "collection",
"entity": "Photo",
"through": "RelLightboxPhoto", // the entity to join with
"join": "RelLightboxPhoto.lightbox == Lightbox.id", // the join predicate
"foreignProperty": "RelLightboxPhoto.photo" // the column containing the foreign key
}
}
});
store.defineEntity("RelLightboxPhoto", {
"table": "t_lightbox_photo",
"properties": {
"lightbox": {
"type": "object",
"entity": "Lightbox",
"column": "rel_lightbox"
},
"photo": {
"type": "object",
"entity": "Photo",
"column": "rel_photo"
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment