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

Custom Primary Keys #27

Merged
merged 1 commit into from
Dec 30, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ module.exports = (function () {
//TODO: refactor: move this to own method!!!
// Create OrientDB schema
if (collection.attributes){
log.debug('Creating DB class [' + tableName + '] for collection [' + collection.identity + ']');
var attributeName;
for(attributeName in collection.attributes){
var linkedClass = null,
Expand All @@ -241,8 +242,9 @@ module.exports = (function () {
else if (typeof collection.attributes[attributeName] === 'function')
continue;
else if (collection.attributes[attributeName].model || collection.attributes[attributeName].references){
attributeType = 'Link';
linkedClass = collection.attributes[attributeName].model || collection.attributes[attributeName].references;
var useLink = me.collectionsByIdentity[linkedClass].primaryKey === 'id';
attributeType = useLink ? 'Link' : collection.pkFormat;
}
else if (collection.attributes[attributeName].foreignKey){
attributeType = 'Link';
Expand All @@ -264,12 +266,14 @@ module.exports = (function () {
if(collection.attributes[attributeName].columnName)
columnName = collection.attributes[attributeName].columnName;

//log.debug('attributeType for ' + attributeName + ':', attributeType);

if(attributeType){
klass.property.create({
name: columnName,
type: attributeType
});
if(linkedClass)
if(attributeType.toLowerCase().indexOf('link') === 0 && linkedClass)
linksToBeCreated.push({
attributeName: columnName,
klass: klass,
Expand Down Expand Up @@ -505,7 +509,7 @@ module.exports = (function () {
collectionInstance = this.collections[collection];
attributes = collectionInstance.attributes;

_document = new Document(options, attributes);
_document = new Document(options, attributes, self);

if(!_document.nestedAssociations)
return self.dbCreate(collection, _document.values, cb);
Expand Down Expand Up @@ -576,7 +580,7 @@ module.exports = (function () {
// Catch errors from building query and return to the callback
try {
_query = new Query(options, schema, self.config);
_document = new Document(values, attributes);
_document = new Document(values, attributes, self);
where = _query.getWhereQuery(collection);
} catch(e) {
log.error('Failed to compose update SQL query.', e);
Expand Down
20 changes: 15 additions & 5 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var _ = require('lodash'),
* @api private
*/

var Document = module.exports = function Document(values, schema) {
var Document = module.exports = function Document(values, schema, connection) {

// Keep track of the current document's values
this.values = {};
Expand All @@ -29,6 +29,9 @@ var Document = module.exports = function Document(values, schema) {

// Nested associations that require creating documents
this.nestedAssociations = null;

// Connection
this.connection = connection;

// If values were passed in, use the setter
if(values){
Expand Down Expand Up @@ -139,7 +142,7 @@ Document.prototype.serializeValues = function serializeValues(values) {
return;


// If a foreignKey, check if value matches a orientDB id and if so turn it into an recordId
// If a foreignKey, check if value matches a orientDB id and if so turn it into a recordId
if (foreignKey && utils.matchRecordId(values[key])) {
values[key] = new RID(values[key]);
}
Expand Down Expand Up @@ -173,9 +176,16 @@ Document.prototype.serializeValues = function serializeValues(values) {
delete values[key];
}
}
else if(foreignKey || self.schema[schemaKey].model || self.schema[schemaKey].collection) {
// doesn't match a orientDB id, invalid fKey. Let's suppress it as Waterline does not expect an error
log.warn('Nullifying [' + key + '] as original value is undefined and Waterline does not expect an error.');
else if(self.schema[schemaKey].model || self.schema[schemaKey].collection) {
var targetCollectionName = self.schema[schemaKey].model || self.schema[schemaKey].collection;
var targetCollection = self.connection.collectionsByIdentity[targetCollectionName];
if(targetCollection.primaryKey === 'id'){
log.warn('Nullifying foreign key [' + key + '] (value: [' + values[key] + ']) as value is not a RID and Waterline does not expect an error.');
values[key] = null;
}
}
else if(foreignKey) {
log.warn('Nullifying foreign key [' + key + '] (value: [' + values[key] + ']) as value is not a RID and Waterline does not expect an error.');
values[key] = null;
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "waterline-orientdb",
"version": "0.10.17",
"version": "0.11.0",
"description": "OrientDB adapter for Waterline / Sails.js ORM",
"main": "./lib/adapter.js",
"scripts": {
Expand Down