Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async replaceOne>(
command: ReplaceOneCommand>
): Promise {
const { entityName, entity } = command;
const operation = PhenylStateUpdater.register(
this.entityState,
entityName,
entity
);
// @ts-ignore operation is nonbreaking
this.entityState = update(this.entityState, operation);
}
static patch(
state: LocalStateOf,
versionDiff: VersionDiff
): GeneralUpdateOperation {
const { entityName, id, versionId, prevVersionId } = versionDiff;
const entityInfo = LocalStateFinder.getEntityInfo(state, {
id,
entityName
}); // Not applicable diff.
if (entityInfo.versionId !== prevVersionId) {
return {};
}
const newOrigin = update(entityInfo.origin);
const newHead = update(newOrigin, ...entityInfo.commits);
return {
$set: {
[createDocumentPath("entities", entityName, id, "origin")]: newOrigin,
[createDocumentPath(
"entities",
entityName,
id,
"versionId"
)]: versionId,
[createDocumentPath("entities", entityName, id, "head")]: newHead
}
};
}
/**
find: async (query: ForeignWhereQuery) => {
if (resData.type !== "find" || query.foreign == null) return resData;
const foreignEntitiesById = await this.getForeignEntities(
resData.payload.entities,
query.foreign
);
const { $set, $docPath } = $bind();
return update(
resData,
// @ts-ignore: has no foreign key
$set($docPath("payload", "foreign", "entities"), foreignEntitiesById)
);
},
static synchronize>(
state: LocalStateOf,
pushCommand: PushCommand,
localCommits: Array
): GeneralUpdateOperation {
const { entityName, id, operations, versionId } = pushCommand;
const entityInfo = LocalStateFinder.getEntityInfo(state, {
id,
entityName
});
const newOrigin = update(entityInfo.origin, ...operations, ...localCommits); // assert(localCommits.length === 0 || entityInfo.commits[0] === localCommits[0])
const newCommits = entityInfo.commits.slice(localCommits.length);
const newHead =
newCommits.length > 0 ? update(newOrigin, ...newCommits) : null;
return {
$set: {
[createDocumentPath("entities", entityName, id)]: {
origin: newOrigin,
versionId,
commits: newCommits,
head: newHead
}
}
};
}
/**
Object.keys(operation).forEach((key: any) => {
// @ts-ignore: Partial has no index signature.
const password = getNestedValue(operation[key], passwordPropName);
if (password) {
const { $set, $docPath } = $bind();
operationWithEncryptedPass = update(
operationWithEncryptedPass,
$set($docPath(key, passwordPropName), encrypt(password))
);
}
});
command: SingleInsertCommand>
): Promise {
const { entityName, value } = command;
// @ts-ignore newValue must contain id
const newValue: M[N] = value.id
? value
: update(value, {
id: timeStampWithRandomString()
});
const operation = PhenylStateUpdater.register(
this.entityState,
entityName,
newValue
);
// @ts-ignore operation is nonbreaking
this.entityState = update(this.entityState, operation);
return newValue;
}
/**
async updateAndFetch>(
command: MultiUpdateCommand
): Promise> {
const { entityName, where } = command; // TODO Performance issue: find() runs twice for just getting N
const values = PhenylStateFinder.find(this.entityState, {
entityName,
where
});
const ids = values.map(value => value.id);
const operation = PhenylStateUpdater.updateMulti(this.entityState, command);
// @ts-ignore operation is nonbreaking
this.entityState = update(this.entityState, operation);
const updatedValues = PhenylStateFinder.getByIds(this.entityState, {
ids,
entityName
});
return updatedValues;
}
/**
pushCommand: PushCommand,
localCommits: Array
): GeneralUpdateOperation {
const { entityName, id, operations, versionId } = pushCommand;
const entityInfo = LocalStateFinder.getEntityInfo(state, {
id,
entityName
});
const newOrigin = update(entityInfo.origin, ...operations, ...localCommits); // assert(localCommits.length === 0 || entityInfo.commits[0] === localCommits[0])
const newCommits = entityInfo.commits.slice(localCommits.length);
const newHead =
newCommits.length > 0 ? update(newOrigin, ...newCommits) : null;
return {
$set: {
[createDocumentPath("entities", entityName, id)]: {
origin: newOrigin,
versionId,
commits: newCommits,
head: newHead
}
}
};
}
/**
export function encryptPasswordInRequestData(
reqData: GeneralUserEntityRequestData,
passwordPropName: DocumentPath,
encrypt: EncryptFunction
): GeneralUserEntityRequestData {
switch (reqData.method) {
case "insertOne":
case "insertAndGet": {
const { payload } = reqData;
const { value } = payload;
const password = getNestedValue(value, passwordPropName);
if (password) {
const { $set, $docPath } = $bind();
const valueWithEncryptedPass = update(
value,
// @ts-ignore password always exists
$set($docPath(passwordPropName), encrypt(password))
);
const { $set: $OtherSet, $docPath: $otherDocPath } = $bind<
typeof reqData
>();
return update(
reqData,
$OtherSet($otherDocPath("payload", "value"), valueWithEncryptedPass)
);
} else {
const valuesWithEncryptedPass = values.map(value => {
const password = getNestedValue(value, passwordPropName);
if (password) {
const { $set, $docPath } = $bind();
return update(
value,
// @ts-ignore password always exists
$set($docPath(passwordPropName), encrypt(password))
);
} else {
return value;
}
});
const { $set, $docPath } = $bind();