@live-change/relations-plugin
Advanced tools
Comparing version 0.1.9 to 0.1.10
const propertyOf = require('./propertyOf.js') | ||
const itemOf = require('./itemOf.js') | ||
@@ -6,4 +7,5 @@ module.exports = function(app, services) { | ||
app.defaultProcessors.push(propertyOf) | ||
app.defaultProcessors.push(itemOf) | ||
} | ||
module.exports.processors = [ propertyOf ] | ||
module.exports.processors = [ propertyOf, itemOf ] |
174
itemOf.js
@@ -8,2 +8,3 @@ const App = require("@live-change/framework") | ||
} = require('./utils.js') | ||
const {generateId} = require("./utils"); | ||
@@ -44,4 +45,163 @@ function defineView(config, context) { | ||
async function defineCreatedEvent(config, context) { | ||
const { | ||
service, modelRuntime, joinedOthersPropertyName, modelName, modelPropertyName | ||
} = context | ||
const eventName = joinedOthersPropertyName + 'Owned' + modelName + 'Created' | ||
service.events[eventName] = new EventDefinition({ | ||
name: eventName, | ||
execute(properties) { | ||
const id = properties[modelPropertyName] | ||
return modelRuntime().create({ ...properties.data, ...properties.identifiers, id }) | ||
} | ||
}) | ||
} | ||
function defineCreateAction(config, context) { | ||
const { | ||
service, app, model, defaults, modelPropertyName, modelRuntime, | ||
otherPropertyNames, joinedOthersPropertyName, modelName, writeableProperties, joinedOthersClassName | ||
} = context | ||
const eventName = joinedOthersPropertyName + 'Owned' + modelName + 'Created' | ||
const actionName = 'set' + joinedOthersClassName + 'Owned' + modelName | ||
service.actions[actionName] = new ActionDefinition({ | ||
name: actionName, | ||
properties: { | ||
...(model.properties) | ||
}, | ||
access: config.createAccess || config.writeAccess, | ||
skipValidation: true, | ||
//queuedBy: otherPropertyNames, | ||
waitForEvents: true, | ||
async execute(properties, { client, service }, emit) { | ||
const id = properties[modelPropertyName] || app.generateUid() | ||
const entity = await modelRuntime().get(id) | ||
if(entity) throw 'exists' | ||
const identifiers = extractIdentifiers(otherPropertyNames, properties) | ||
const data = extractObjectData(writeableProperties, properties, defaults) | ||
await App.validation.validate(data, validators, { source: action, action, service, app, client }) | ||
emit({ | ||
type: eventName, | ||
[modelPropertyName]: id, | ||
identifiers, data | ||
}) | ||
} | ||
}) | ||
const action = service.actions[actionName] | ||
const validators = App.validation.getValidators(action, service, action) | ||
} | ||
async function defineUpdatedEvent(config, context) { | ||
const { | ||
service, modelRuntime, joinedOthersPropertyName, modelName, modelPropertyName | ||
} = context | ||
const eventName = joinedOthersPropertyName + 'Owned' + modelName + 'Updated' | ||
service.events[eventName] = new EventDefinition({ | ||
name: eventName, | ||
execute(properties) { | ||
const id = properties[modelPropertyName] | ||
return modelRuntime().update(id, { ...properties.data, ...properties.identifiers, id }) | ||
} | ||
}) | ||
} | ||
async function defineUpdateAction(config, context) { | ||
const { | ||
service, app, model, modelRuntime, modelPropertyName, | ||
otherPropertyNames, joinedOthersPropertyName, modelName, writeableProperties, joinedOthersClassName | ||
} = context | ||
const eventName = joinedOthersPropertyName + 'Owned' + modelName + 'Updated' | ||
const actionName = 'update' + joinedOthersClassName + 'Owned' + modelName | ||
service.actions[actionName] = new ActionDefinition({ | ||
name: actionName, | ||
properties: { | ||
...(model.properties) | ||
}, | ||
access: config.updateAccess || config.writeAccess, | ||
skipValidation: true, | ||
//queuedBy: otherPropertyNames, | ||
waitForEvents: true, | ||
async execute(properties, {client, service}, emit) { | ||
const id = properties[modelPropertyName] | ||
const entity = await modelRuntime().get(id) | ||
if(!entity) throw 'not_found' | ||
const entityIdParts = extractIdParts(otherPropertyNames, entity) | ||
const idParts = extractIdParts(otherPropertyNames, properties) | ||
if(JSON.stringify(entityIdParts) != JSON.stringify(idParts)) { | ||
throw 'not_authorized' | ||
} | ||
const identifiers = extractIdentifiers(otherPropertyNames, properties) | ||
const data = extractObjectData(writeableProperties, properties, entity) | ||
await App.validation.validate(data, validators, { source: action, action, service, app, client }) | ||
emit({ | ||
type: eventName, | ||
[modelPropertyName]: id, | ||
identifiers, | ||
data | ||
}) | ||
} | ||
}) | ||
const action = service.actions[actionName] | ||
const validators = App.validation.getValidators(action, service, action) | ||
} | ||
async function defineDeletedEvent(config, context) { | ||
const { | ||
service, modelRuntime, joinedOthersPropertyName, modelName, modelPropertyName | ||
} = context | ||
const eventName = joinedOthersPropertyName + 'Owned' + modelName + 'Deleted' | ||
service.events[eventName] = new EventDefinition({ | ||
name: eventName, | ||
execute(properties) { | ||
const id = properties[modelPropertyName] | ||
return modelRuntime().delete(id) | ||
} | ||
}) | ||
} | ||
async function defineDeleteAction(config, context) { | ||
const { | ||
service, app, model, modelRuntime, modelPropertyName, | ||
otherPropertyNames, joinedOthersPropertyName, modelName, writeableProperties, joinedOthersClassName | ||
} = context | ||
const eventName = joinedOthersPropertyName + 'Owned' + modelName + 'Deleted' | ||
const actionName = 'delete' + joinedOthersClassName + 'Owned' + modelName | ||
service.actions[actionName] = new ActionDefinition({ | ||
name: actionName, | ||
properties: { | ||
...(model.properties) | ||
}, | ||
access: config.deleteAccess || config.writeAccess, | ||
skipValidation: true, | ||
//queuedBy: otherPropertyNames, | ||
waitForEvents: true, | ||
async execute(properties, {client, service}, emit) { | ||
const id = properties[modelPropertyName] | ||
const entity = await modelRuntime().get(id) | ||
if(!entity) throw new Error('not_found') | ||
const entityIdParts = extractIdParts(otherPropertyNames, entity) | ||
const idParts = extractIdParts(otherPropertyNames, properties) | ||
if(JSON.stringify(entityIdParts) != JSON.stringify(idParts)) { | ||
throw new Error('not_authorized') | ||
} | ||
emit({ | ||
type: eventName, | ||
[modelPropertyName]: id | ||
}) | ||
} | ||
}) | ||
} | ||
function defineSortIndex(context, sortFields) { | ||
if(!Array.isArray(sortFields)) sortFields = [sortFields] | ||
console.log("DEFINE SORT INDEX", sortFields) | ||
const sortFieldsUc = sortFields.map(fd=>fd.slice(0, 1).toUpperCase() + fd.slice(1)) | ||
const indexName = 'by' + context.joinedOthersClassName + sortFieldsUc.join('') | ||
context.model.indexes[indexName] = new IndexDefinition({ | ||
property: [...context.otherPropertyNames, ...sortFields] | ||
}) | ||
} | ||
module.exports = function(service, app) { | ||
processModelsAnnotation(service, app, 'propertyOf', (config, context) => { | ||
processModelsAnnotation(service, app, 'itemOf', (config, context) => { | ||
@@ -51,2 +211,8 @@ defineProperties(context.model, context.others, context.otherPropertyNames) | ||
if(config.sortBy) { | ||
for(const sortFields of config.sortBy) { | ||
defineSortIndex(context, sortFields) | ||
} | ||
} | ||
if(config.readAccess) { | ||
@@ -57,2 +223,6 @@ defineView(config, context) | ||
defineCreatedEvent(config, context) | ||
defineUpdatedEvent(config, context) | ||
defineDeletedEvent(config, context) | ||
if(config.setAccess || config.writeAccess) { | ||
@@ -67,5 +237,5 @@ defineCreateAction(config, context) | ||
if(config.resetAccess || config.writeAccess) { | ||
defineReleteAction(config, context); | ||
defineDeleteAction(config, context) | ||
} | ||
}) | ||
} |
{ | ||
"name": "@live-change/relations-plugin", | ||
"version": "0.1.9", | ||
"version": "0.1.10", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -117,3 +117,4 @@ const App = require("@live-change/framework") | ||
const identifiers = extractIdentifiers(otherPropertyNames, properties) | ||
const entity = await modelRuntime().get(identifiers.id) | ||
const id = generateId(otherPropertyNames, properties) | ||
const entity = await modelRuntime().get(id) | ||
if (!entity) throw new Error('not_found') | ||
@@ -148,4 +149,4 @@ const data = extractObjectData(writeableProperties, properties, entity) | ||
const { | ||
service, modelRuntime, | ||
otherPropertyNames, joinedOthersPropertyName, modelName, joinedOthersClassName | ||
service, modelRuntime, modelPropertyName, | ||
otherPropertyNames, joinedOthersPropertyName, modelName, joinedOthersClassName, model | ||
} = context | ||
@@ -156,2 +157,8 @@ const eventName = joinedOthersPropertyName + 'Owned' + modelName + 'Reset' | ||
name: actionName, | ||
properties: { | ||
[modelPropertyName]: { | ||
type: model, | ||
validation: ['nonEmpty'] | ||
} | ||
}, | ||
access: config.resetAccess || config.writeAccess, | ||
@@ -162,3 +169,4 @@ queuedBy: otherPropertyNames, | ||
const identifiers = extractIdentifiers(otherPropertyNames, properties) | ||
const entity = await modelRuntime().get(identifiers.id) | ||
const id = generateId(otherPropertyNames, properties) | ||
const entity = await modelRuntime().get(id) | ||
if (!entity) throw new Error('not_found') | ||
@@ -181,3 +189,2 @@ emit({ | ||
if(config.readAccess) { | ||
@@ -184,0 +191,0 @@ defineView(config, context) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25500
685