Skip to content
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
32 changes: 32 additions & 0 deletions connector.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var app = require('./app');
var loopback = require('loopback');
var connector = app.dataSources.db.connector;
var ComponentDefinition = app.models.ComponentDefinition;
var ConfigFile = app.models.ConfigFile;
Expand Down Expand Up @@ -89,3 +90,34 @@ connector.all = function(model) {
originalAll.apply(connector, args);
});
}

connector.getIdValue = function(model, data) {
var Entity = loopback.getModel(model);
var entity = new Entity(data);
return entity.getUniqueId();
}

connector.create = function create(model, data, callback) {
var Entity = loopback.getModel(model);
var entity = new Entity(data);
var id = entity.getUniqueId();

this.setIdValue(model, data, id);

if(!this.cache[model]) {
this.cache[model] = {};
}

this.cache[model][id] = serialize(data);
this.saveToFile(id, function(err) {
if(err) return callback(err);
callback(null, id);
});
};

function serialize(obj) {
if(obj === null || obj === undefined) {
return obj;
}
return JSON.stringify(obj);
}
88 changes: 69 additions & 19 deletions models.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
},
"PackageDefinition": {
"properties": {
"componentName": {"type": "string", "required": true}
"componentName": {"type": "string", "required": true},
"name": {"id": true, "type": "string"}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move the id property to Definition model? Is there any Definition subclass that does not have the id property?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ComponentDefinition should be keyed by name.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough.

},
"public": false,
"dataSource": "db",
Expand All @@ -30,6 +31,7 @@
},
"ComponentDefinition": {
"properties": {
"name": {"id": true, "type": "string"},
"defaultPermission": {"type": "string"}
},
"public": true,
Expand Down Expand Up @@ -62,7 +64,9 @@
},
"ComponentModel": {
"properties": {
"name": {"type": "string", "id": true}
"id": {"type": "string", "id": true},
"name": {"type": "string"},
"componentName": {"type": "string", "required": true}
},
"dataSource": "db",
"options": {
Expand Down Expand Up @@ -90,6 +94,8 @@
},
"ModelDefinition": {
"properties": {
"id": {"type": "string", "id": true},
"name": "string",
"plural": "string",
"strict": "boolean",
"scopes": "array",
Expand All @@ -109,46 +115,53 @@
],
"configExtensions": ["json"],
"relations": {
"component": {
"type": "belongsTo",
"model": "ComponentDefinition",
"foreignKey": "componentName"
},
"properties": {
"embed": {"as": "object", "key": "name"},
"type": "hasMany",
"model": "ModelProperty",
"foreignKey": "modelName"
"foreignKey": "modelId"
},
"validations": {
"embed": {"as": "array"},
"type": "hasMany",
"model": "PropertyValidation",
"foreignKey": "modelName"
"foreignKey": "modelId"
},
"relations": {
"embed": {"as": "object", "key": "as"},
"type": "hasMany",
"model": "ModelRelation",
"foreignKey": "fromModel"
"foreignKey": "fromModelId"
},
"accessControls": {
"embed": {"name": "acls", "as": "array"},
"type": "hasMany",
"model": "ModelAccessControl",
"foreignKey": "model"
"foreignKey": "modelId"
},
"methods": {
"embed": {"as": "array"},
"type": "hasMany",
"model": "ModelMethod",
"foreignKey": "model"
"foreignKey": "modelId"
},
"views": {
"type": "hasMany",
"model": "ViewDefinition",
"foreignKey": "model"
"foreignKey": "modelId"
}
}
}
},
"ModelMethod": {
"properties": {
"id": {"type": "string", "id": true},
"modelId": {"type": "string", "required": true},
"aliases": {"type": "array"},
"isStatic": {"type": "boolean"},
"accepts": {"type": "array"},
Expand All @@ -165,6 +178,8 @@
},
"ModelRelation": {
"properties": {
"id": {"type": "string", "id": true},
"modelId": {"type": "string", "required": true},
"type": {"type": "string"},
"as": {"type": "string"},
"foreignKey": {"type": "string"},
Expand All @@ -173,11 +188,20 @@
"public": true,
"dataSource": "db",
"options": {
"base": "WorkspaceEntity"
"base": "WorkspaceEntity",
"relations": {
"model": {
"type": "belongsTo",
"model": "ModelDefinition",
"foreignKey": "modelId"
}
}
}
},
"ModelAccessControl": {
"properties": {
"id": {"type": "string", "id": true},
"modelId": {"type": "string", "required": true},
"property": {"type": "string"},
"route": {"type": "string"},
"principalId": {"type": "string"},
Expand All @@ -187,17 +211,25 @@
"public": true,
"dataSource": "db",
"options": {
"base": "WorkspaceEntity"
"base": "WorkspaceEntity",
"relations": {
"model": {
"type": "belongsTo",
"model": "ModelDefinition",
"foreignKey": "modelId"
}
}
}
},
"ModelProperty": {
"properties": {
"id": {"type": "string", "id": true},
"modelId": {"type": "string", "required": true},
"type": {"type": "string"},
"name": {"type": "string", "id": true},
"name": {"type": "string"},
"generated": {"type": "boolean"},
"required": {"type": "boolean"},
"index": {"type": "boolean"},
"modelName": "string",
"desc": {"type": "string"}
},
"public": true,
Expand All @@ -209,16 +241,23 @@
"type": "hasMany",
"model": "PropertyValid",
"foreignKey": "property"
},
"model": {
"type": "belongsTo",
"model": "ModelDefinition",
"foreignKey": "modelId"
}
}
}
},
"DatabaseColumn": {
"connector": {"type": "string"},
"columnName": {"type": "string"},
"dataType": {"type": "string"},
"dataLength": {"type": "string"},
"nullable": {"type": "boolean"},
"properties": {
"connector": {"type": "string"},
"columnName": {"type": "string"},
"dataType": {"type": "string"},
"dataLength": {"type": "string"},
"nullable": {"type": "boolean"}
},
"options": {
"base": "WorkspaceEntity"
},
Expand All @@ -227,6 +266,8 @@
},
"PropertyValidation": {
"properties": {
"id": {"type": "string", "id": true},
"propertyId": {"type": "string", "required": true},
"type": {"type": "string"},
"message": {"type": "string"},
"min": {"type": "number"},
Expand All @@ -238,7 +279,14 @@
"public": true,
"dataSource": "db",
"options": {
"base": "WorkspaceEntity"
"base": "WorkspaceEntity",
"relations": {
"model": {
"type": "belongsTo",
"model": "ModelProperty",
"foreignKey": "propertyId"
}
}
}
},
"ViewDefinition": {
Expand All @@ -265,12 +313,14 @@
},
"DataSourceDefinition": {
"properties": {
"id": {"type": "string", "id": true},
"host": {"type": "string"},
"port": {"type": "number"},
"url": {"type": "string"},
"database": {"type": "string"},
"username": {"type": "string"},
"password": {"type": "string"}
"password": {"type": "string"},
"componentName": {"type": "string", "required": true}
},
"public": true,
"dataSource": "db",
Expand Down
37 changes: 23 additions & 14 deletions models/component-definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ ComponentDefinition.loadIntoCache = function(cache, componentName, components, c
packageFile.load(cb);
}, function(cb) {
packageFile.data.componentName = componentName;
PackageDefinition.addToCache(cache, componentName, packageFile.data || {});
PackageDefinition.addToCache(cache, packageFile.data || {});
cb();
});
}
Expand All @@ -57,7 +57,7 @@ ComponentDefinition.loadIntoCache = function(cache, componentName, components, c
component.data.configFile = component.path;
component.data.name = componentName;
debug('adding to cache component file [%s]', component.path);
ComponentDefinition.addToCache(cache, componentName, component.data);
ComponentDefinition.addToCache(cache, component.data);
cb();
});
} else {
Expand All @@ -67,7 +67,7 @@ ComponentDefinition.loadIntoCache = function(cache, componentName, components, c
configFile: path.join(componentName, 'config.json')
};
debug('adding to cache component entry [%s]', componentData.configFile);
ComponentDefinition.addToCache(cache, componentName, componentData);
ComponentDefinition.addToCache(cache, componentData);
cb();
});
}
Expand All @@ -86,7 +86,7 @@ ComponentDefinition.loadIntoCache = function(cache, componentName, components, c
var componentModel = modelDefs[modelName];
componentModel.componentName = componentName;
componentModel.name = modelName;
ComponentModel.addToCache(cache, modelName, componentModel);
ComponentModel.addToCache(cache, componentModel);
});

cb();
Expand All @@ -103,11 +103,13 @@ ComponentDefinition.loadIntoCache = function(cache, componentName, components, c
var def = configFile.data || {};
def.componentName = componentName;
def.configFile = configFile.path;
var modelDef = new ModelDefinition(def);

debug('loading [%s] model definition into cache', def.name);

ModelDefinition.addToCache(cache, def.name, def);
ModelDefinition.addRelatedToCache(cache, def.name, def);
ModelDefinition.addToCache(cache, def);
ModelDefinition.addRelatedToCache(cache, def, componentName
, modelDef.getUniqueId());
});
cb();
});
Expand All @@ -126,7 +128,7 @@ ComponentDefinition.loadIntoCache = function(cache, componentName, components, c
def.name = dataSourceName;
def.componentName = componentName;
debug('loading [%s] dataSource into cache', dataSourceName);
DataSourceDefinition.addToCache(cache, dataSourceName, def);
DataSourceDefinition.addToCache(cache, def);
});

cb();
Expand Down Expand Up @@ -184,11 +186,11 @@ ComponentDefinition.saveToFs = function(cache, componentDef, cb) {
var cachedDataSources = DataSourceDefinition.allFromCache(cache);

cachedDataSources.forEach(function(dataSourceDef) {
if (dataSourceDef.componentName === componentName) {
if(dataSourceDef.componentName === componentName) {
dataSourcePath = DataSourceDefinition.getPath(componentName, dataSourceDef);
dataSoureConfig[dataSourceDef.name] = dataSourceDef;
// remove extra data that shouldn't be persisted to the fs
delete dataSourceDef.name;
delete dataSourceDef.id;
delete dataSourceDef.componentName;
}
});
Expand All @@ -204,11 +206,12 @@ ComponentDefinition.saveToFs = function(cache, componentDef, cb) {
var componentModelsConfig = componentModelFile.data = {};

cachedComponentModels.forEach(function(componentModel) {
if (componentModel.componentName !== componentName) return;
componentModelsConfig[componentModel.name] = componentModel;
// remove extra data that shouldn't be persisted to the fs
delete componentModel.name;
delete componentModel.componentName;
if(componentModel.componentName === componentName) {
componentModelsConfig[componentModel.name] = componentModel;
delete componentModel.name;
delete componentModel.id;
delete componentModel.componentName;
}
});

filesToSave.push(componentModelFile);
Expand All @@ -222,6 +225,8 @@ ComponentDefinition.saveToFs = function(cache, componentDef, cb) {
delete modelDef.dataSource;
var modelConfigFile = ModelDefinition.getConfigFile(componentName, modelDef);
modelConfigFile.data = ModelDefinition.getConfigData(cache, modelDef);
delete modelDef.componentName;
delete modelDef.id;
filesToSave.push(modelConfigFile);
}
});
Expand All @@ -244,3 +249,7 @@ ComponentDefinition.hasApp = function(componentDef) {
// e.g. package.json > loopback-workspace > app: true|false
return componentDef.name !== '.';
};

ComponentDefinition.getUniqueId = function(data) {
return data.name || null;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this function already provided by WorkspaceEntity? I.e. it should be enough to include "includeIdPart": ["name"] in the model options.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could rework includeIdPart to support that.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you can leave it as it is now if it is only a single place where getUniqueId is overriden.

Loading