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
62 changes: 37 additions & 25 deletions lib/persisted-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ var debug = require('debug')('loopback:persisted-model');
var PassThrough = require('stream').PassThrough;
var utils = require('./utils');

// workaround for low performance of strong-globalize
// see https://github.com/strongloop/strong-globalize/issues/66
var stringCache = Object.create(null);
g.s = function(str) {
assert.equal(1, arguments.length, 'g.s() does not support parameters');
if (str in stringCache)
return stringCache[str];
var result = g.t(str);
stringCache[str] = result;
return result;
};

module.exports = function(registry) {
var Model = registry.getModel('Model');

Expand Down Expand Up @@ -560,7 +572,7 @@ module.exports = function(registry) {
}

setRemoting(PersistedModel, 'create', {
description: g.f('Create a new instance of the model and persist it into the data source.'),
description: g.s('Create a new instance of the model and persist it into the data source.'),
accessType: 'WRITE',
accepts: {arg: 'data', type: 'object', description: 'Model instance data', http: {source: 'body'}},
returns: {arg: 'data', type: typeName, root: true},
Expand All @@ -569,7 +581,7 @@ module.exports = function(registry) {

setRemoting(PersistedModel, 'upsert', {
aliases: ['updateOrCreate'],
description: g.f('Update an existing model instance or insert a new one ' +
description: g.s('Update an existing model instance or insert a new one ' +
'into the data source.'),
accessType: 'WRITE',
accepts: {arg: 'data', type: 'object', description: 'Model instance data', http: {source: 'body'}},
Expand All @@ -578,7 +590,7 @@ module.exports = function(registry) {
});

setRemoting(PersistedModel, 'exists', {
description: g.f('Check whether a model instance exists in the data source.'),
description: g.s('Check whether a model instance exists in the data source.'),
accessType: 'READ',
accepts: {arg: 'id', type: 'any', description: 'Model id', required: true},
returns: {arg: 'exists', type: 'boolean'},
Expand Down Expand Up @@ -609,29 +621,29 @@ module.exports = function(registry) {
});

setRemoting(PersistedModel, 'findById', {
description: g.f('Find a model instance by {{id}} from the data source.'),
description: g.s('Find a model instance by {{id}} from the data source.'),
accessType: 'READ',
accepts: [
{ arg: 'id', type: 'any', description: 'Model id', required: true,
http: {source: 'path'}},
{ arg: 'filter', type: 'object',
description: g.f('Filter defining fields and include') },
description: g.s('Filter defining fields and include') },
],
returns: {arg: 'data', type: typeName, root: true},
http: {verb: 'get', path: '/:id'},
rest: {after: convertNullToNotFoundError}
});

setRemoting(PersistedModel, 'find', {
description: g.f('Find all instances of the model matched by filter from the data source.'),
description: g.s('Find all instances of the model matched by filter from the data source.'),
accessType: 'READ',
accepts: {arg: 'filter', type: 'object', description: 'Filter defining fields, where, include, order, offset, and limit'},
returns: {arg: 'data', type: [typeName], root: true},
http: {verb: 'get', path: '/'}
});

setRemoting(PersistedModel, 'findOne', {
description: g.f('Find first instance of the model matched by filter from the data source.'),
description: g.s('Find first instance of the model matched by filter from the data source.'),
accessType: 'READ',
accepts: {arg: 'filter', type: 'object', description: 'Filter defining fields, where, include, order, offset, and limit'},
returns: {arg: 'data', type: typeName, root: true},
Expand All @@ -640,7 +652,7 @@ module.exports = function(registry) {
});

setRemoting(PersistedModel, 'destroyAll', {
description: g.f('Delete all matching records.'),
description: g.s('Delete all matching records.'),
accessType: 'WRITE',
accepts: {arg: 'where', type: 'object', description: 'filter.where object'},
returns: {
Expand All @@ -655,17 +667,17 @@ module.exports = function(registry) {

setRemoting(PersistedModel, 'updateAll', {
aliases: ['update'],
description: g.f('Update instances of the model matched by {{where}} from the data source.'),
description: g.s('Update instances of the model matched by {{where}} from the data source.'),
accessType: 'WRITE',
accepts: [
{arg: 'where', type: 'object', http: { source: 'query'},
description: g.f('Criteria to match model instances')},
description: g.s('Criteria to match model instances')},
{arg: 'data', type: 'object', http: {source: 'body'},
description: g.f('An object of model property name/value pairs')},
description: g.s('An object of model property name/value pairs')},
],
returns: {
arg: 'count',
description: g.f('The number of instances updated'),
description: g.s('The number of instances updated'),
type: 'object',
root: true
},
Expand All @@ -674,7 +686,7 @@ module.exports = function(registry) {

setRemoting(PersistedModel, 'deleteById', {
aliases: ['destroyById', 'removeById'],
description: g.f('Delete a model instance by {{id}} from the data source.'),
description: g.s('Delete a model instance by {{id}} from the data source.'),
accessType: 'WRITE',
accepts: {arg: 'id', type: 'any', description: 'Model id', required: true,
http: {source: 'path'}},
Expand All @@ -683,15 +695,15 @@ module.exports = function(registry) {
});

setRemoting(PersistedModel, 'count', {
description: g.f('Count instances of the model matched by where from the data source.'),
description: g.s('Count instances of the model matched by where from the data source.'),
accessType: 'READ',
accepts: {arg: 'where', type: 'object', description: 'Criteria to match model instances'},
returns: {arg: 'count', type: 'number'},
http: {verb: 'get', path: '/count'}
});

setRemoting(PersistedModel.prototype, 'updateAttributes', {
description: g.f('Update attributes for a model instance and persist it into ' +
description: g.s('Update attributes for a model instance and persist it into ' +
'the data source.'),
accessType: 'WRITE',
accepts: {arg: 'data', type: 'object', http: {source: 'body'}, description: 'An object of model property name/value pairs'},
Expand All @@ -701,7 +713,7 @@ module.exports = function(registry) {

if (options.trackChanges || options.enableRemoteReplication) {
setRemoting(PersistedModel, 'diff', {
description: g.f('Get a set of deltas and conflicts since the given checkpoint.'),
description: g.s('Get a set of deltas and conflicts since the given checkpoint.'),
accessType: 'READ',
accepts: [
{arg: 'since', type: 'number', description: 'Find deltas since this checkpoint'},
Expand All @@ -713,7 +725,7 @@ module.exports = function(registry) {
});

setRemoting(PersistedModel, 'changes', {
description: g.f('Get the changes to a model since a given checkpoint.' +
description: g.s('Get the changes to a model since a given checkpoint.' +
'Provide a filter object to reduce the number of results returned.'),
accessType: 'READ',
accepts: [
Expand All @@ -725,7 +737,7 @@ module.exports = function(registry) {
});

setRemoting(PersistedModel, 'checkpoint', {
description: g.f('Create a checkpoint.'),
description: g.s('Create a checkpoint.'),
// The replication algorithm needs to create a source checkpoint,
// even though it is otherwise not making any source changes.
// We need to allow this method for users that don't have full
Expand All @@ -736,14 +748,14 @@ module.exports = function(registry) {
});

setRemoting(PersistedModel, 'currentCheckpoint', {
description: g.f('Get the current checkpoint.'),
description: g.s('Get the current checkpoint.'),
accessType: 'READ',
returns: {arg: 'checkpoint', type: 'object', root: true},
http: {verb: 'get', path: '/checkpoint'}
});

setRemoting(PersistedModel, 'createUpdates', {
description: g.f('Create an update list from a delta list.'),
description: g.s('Create an update list from a delta list.'),
// This operation is read-only, it does not change any local data.
// It is called by the replication algorithm to compile a list
// of changes to apply on the target.
Expand All @@ -754,14 +766,14 @@ module.exports = function(registry) {
});

setRemoting(PersistedModel, 'bulkUpdate', {
description: g.f('Run multiple updates at once. Note: this is not atomic.'),
description: g.s('Run multiple updates at once. Note: this is not atomic.'),
accessType: 'WRITE',
accepts: {arg: 'updates', type: 'array'},
http: {verb: 'post', path: '/bulk-update'}
});

setRemoting(PersistedModel, 'findLastChange', {
description: g.f('Get the most recent change record for this instance.'),
description: g.s('Get the most recent change record for this instance.'),
accessType: 'READ',
accepts: {
arg: 'id', type: 'any', required: true, http: { source: 'path' },
Expand All @@ -773,7 +785,7 @@ module.exports = function(registry) {

setRemoting(PersistedModel, 'updateLastChange', {
description: [
g.f('Update the properties of the most recent change record ' +
g.s('Update the properties of the most recent change record ' +
'kept for this instance.'),
],
accessType: 'WRITE',
Expand All @@ -784,7 +796,7 @@ module.exports = function(registry) {
},
{
arg: 'data', type: 'object', http: {source: 'body'},
description: g.f('An object of Change property name/value pairs'),
description: g.s('An object of Change property name/value pairs'),
},
],
returns: { arg: 'result', type: this.Change.modelName, root: true },
Expand All @@ -810,7 +822,7 @@ module.exports = function(registry) {
}

setRemoting(PersistedModel, 'createChangeStream', {
description: g.f('Create a change stream.'),
description: g.s('Create a change stream.'),
accessType: 'READ',
http: [
{verb: 'post', path: '/change-stream'},
Expand Down
4 changes: 2 additions & 2 deletions lib/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ Registry.prototype.configureModel = function(ModelCtor, config) {
// configuration, so that the datasource picks up updated relations
if (config.dataSource) {
assert(config.dataSource instanceof DataSource,
g.f('Cannot configure %s: {{config.dataSource}} must be an instance ' +
'of {{DataSource}}', ModelCtor.modelName));
'Cannot configure ' + ModelCtor.modelName +
': config.dataSource must be an instance of DataSource');
ModelCtor.attachTo(config.dataSource);
debug('Attached model `%s` to dataSource `%s`',
modelName, config.dataSource.name);
Expand Down