MCPcopy
hub / github.com/strongloop/loopback / createChangeObject

Function createChangeObject

lib/persisted-model.js:1920–1967  ·  view source on GitHub ↗
(ctx, type)

Source from the content-addressed store, hash-verified

1918 }
1919
1920 function createChangeObject(ctx, type) {
1921 const where = ctx.where;
1922 let data = ctx.instance || ctx.data;
1923 const whereId = where && where[idName];
1924
1925 // the data includes the id
1926 // or the where includes the id
1927 let target;
1928
1929 if (data && (data[idName] || data[idName] === 0)) {
1930 target = data[idName];
1931 } else if (where && (where[idName] || where[idName] === 0)) {
1932 target = where[idName];
1933 }
1934
1935 const hasTarget = target === 0 || !!target;
1936
1937 // apply filtering if options is set
1938 if (options) {
1939 const filtered = filterNodes([data], options);
1940 if (filtered.length !== 1) {
1941 return null;
1942 }
1943 data = filtered[0];
1944 }
1945
1946 const change = {
1947 target: target,
1948 where: where,
1949 data: data,
1950 };
1951
1952 switch (type) {
1953 case 'save':
1954 if (ctx.isNewInstance === undefined) {
1955 change.type = hasTarget ? 'update' : 'create';
1956 } else {
1957 change.type = ctx.isNewInstance ? 'create' : 'update';
1958 }
1959
1960 break;
1961 case 'delete':
1962 change.type = 'remove';
1963 break;
1964 }
1965
1966 return change;
1967 }
1968
1969 function removeHandlers() {
1970 Model.removeObserver('after save', changeHandler);

Callers 2

changeHandlerFunction · 0.85
deleteHandlerFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…