Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
lroal committed Jun 22, 2024
1 parent 9f7cb46 commit d89ad5e
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 36 deletions.
22 changes: 22 additions & 0 deletions src/client/cloneRows.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function cloneRows(obj) {
if (obj === null || typeof obj !== 'object') {
return obj;
}
if (Array.isArray(obj)) {
const arrClone = [];
for (let i = 0; i < obj.length; i++) {
arrClone[i] = cloneRows(obj[i]);
}
return arrClone;
}
const clone = {};
const keys = Object.keys(obj);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
clone[key] = cloneRows(obj[key]);
}
return clone;
}


module.exports = cloneRows;
17 changes: 9 additions & 8 deletions src/client/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const createPatch = require('./createPatch');
const stringify = require('./stringify');
const cloneRows = require('./cloneRows');
const netAdapter = require('./netAdapter');
const toKeyPositionMap = require('./toKeyPositionMap');
const rootMap = new WeakMap();
Expand Down Expand Up @@ -483,7 +484,7 @@ function rdbClient(options = {}) {

};
let innerProxy = new Proxy(array, handler);
rootMap.set(array, { json: stringify(array), strategy, originalArray: [...array] });
rootMap.set(array, { json: cloneRows(array), strategy, originalArray: [...array] });
if (strategy !== undefined) {
const { limit, ...cleanStrategy } = { ...strategy };
fetchingStrategyMap.set(array, cleanStrategy);
Expand Down Expand Up @@ -516,7 +517,7 @@ function rdbClient(options = {}) {

};
let innerProxy = new Proxy(row, handler);
rootMap.set(row, { json: stringify(row), strategy });
rootMap.set(row, { json: cloneRows(row), strategy });
fetchingStrategyMap.set(row, strategy);
return innerProxy;
}
Expand Down Expand Up @@ -606,7 +607,7 @@ function rdbClient(options = {}) {
let insertedPositions = getInsertedRowsPosition(array);
let { changed, strategy: newStrategy } = await p;
copyIntoArray(changed, array, [...insertedPositions, ...updatedPositions]);
rootMap.set(array, { json: stringify(array), strategy: newStrategy, originalArray: [...array] });
rootMap.set(array, { json: cloneRows(array), strategy: newStrategy, originalArray: [...array] });
}

async function patch(patch, concurrencyOptions, strategy) {
Expand Down Expand Up @@ -705,7 +706,7 @@ function rdbClient(options = {}) {
let adapter = netAdapter(url, tableName, { axios: axiosInterceptor, tableOptions });
let { strategy } = await adapter.patch(body);
array.length = 0;
rootMap.set(array, { jsonMap: stringify(array), strategy });
rootMap.set(array, { jsonMap: cloneRows(array), strategy });
}

function setMapValue(rowsMap, keys, row, index) {
Expand Down Expand Up @@ -768,7 +769,7 @@ function rdbClient(options = {}) {
array.splice(i + offset, 1);
offset--;
}
rootMap.set(array, { json: stringify(array), strategy, originalArray: [...array] });
rootMap.set(array, { json: cloneRows(array), strategy, originalArray: [...array] });
fetchingStrategyMap.set(array, strategy);
}

Expand Down Expand Up @@ -803,7 +804,7 @@ function rdbClient(options = {}) {
let adapter = netAdapter(url, tableName, { axios: axiosInterceptor, tableOptions });
let { changed, strategy: newStrategy } = await adapter.patch(body);
copyInto(changed, [row]);
rootMap.set(row, { json: stringify(row), strategy: newStrategy });
rootMap.set(row, { json: cloneRows(row), strategy: newStrategy });
}

async function refreshRow(row, strategy) {
Expand All @@ -827,13 +828,13 @@ function rdbClient(options = {}) {
for (let p in rows[0]) {
row[p] = rows[0][p];
}
rootMap.set(row, { json: stringify(row), strategy });
rootMap.set(row, { json: cloneRows(row), strategy });
fetchingStrategyMap.set(row, strategy);
}

function acceptChangesRow(row) {
const { strategy } = rootMap.get(row);
rootMap.set(row, { json: stringify(row), strategy });
rootMap.set(row, { json: cloneRows(row), strategy });
}

function clearChangesRow(row) {
Expand Down
3 changes: 3 additions & 0 deletions src/client/stringify.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
let dateToISOString = require('../dateToISOString');
let rfdc = require('rfdc');
const isNode = (typeof window === 'undefined');
const clone = rfdc({proto: false, circles: false});

function stringify(value) {
return clone(value);
return JSON.stringify(value, replacer);
}

Expand Down
176 changes: 148 additions & 28 deletions src/getManyDto.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,99 @@ async function getManyDto(table, filter, strategy, spanFromParent) {
return decode(strategy, span, await res[0]);
}

function newCreateRow2(span, keys, _aggregateKeys) {
// console.dir('keys');
// console.dir(keys);
let columnsMap = span.columns;
const columns = span.table._columns.filter(column => !columnsMap || columnsMap.get(column));
// const protoRow = createProto(columns, span);
// const manyNames = [];

function Row(row) {
// console.dir('inside row')
// console.dir(row)
this._row = row;
this._manyObject = {};
}

// Object.defineProperty(Row.prototype, "_row", {
// enumerable: false
// });

// Object.defineProperty(Row.prototype, "_manyObject", {
// enumerable: false
// });


for (let i = 0; i < columns.length; i++) {
Object.defineProperty(Row.prototype, columns[i].alias, {
get() {
return columns[i].decode(this._row[keys[i]]);
},
enumerable: true
});
}

let isMany;
const c = {};
c.visitJoin = () => isMany = false;
c.visitOne = () => isMany = false;
c.visitMany = () => isMany = true;
const legs = span.legs.toArray();
for (let i = 0; i < legs.length; i++) {
legs[i].accept(c);
const name = legs[i].name;
if (isMany) {
Object.defineProperty(Row.prototype, name, {
get() {
console.dir('get many name');
console.dir(name);
if (this._manyObject[name] === undefined) {
console.dir('is undefined.....................')
this._manyObject[name] = [];
}
return this._manyObject[name];
},
enumerable: true,
set: function(value) {
this._manyObject[name] = value;
}
});
}
else {
Object.defineProperty(Row.prototype, name, {
get() {
console.dir('get many name');
console.dir(name);
if (this._manyObject[name] === undefined) {
console.dir('is undefined.....................')
this._manyObject[name] = null;
}
return this._manyObject[name];
},
enumerable: true,
set: function(value) {
this._manyObject[name] = value;
}
});
}

}

span.legs.forEach(onEachLeg);
return createRow;

function createRow(row) {
return new Row(row);
}

function onEachLeg(leg) {
leg.accept(c);
}
}



function newCreateRow(span) {
let columnsMap = span.columns;
const columns = span.table._columns.filter(column => !columnsMap || columnsMap.get(column));
Expand Down Expand Up @@ -110,40 +203,52 @@ async function decode(strategy, span, rows, keys = rows.length > 0 ? Object.keys
const shouldCreateMap = hasManyRelations(span);
for (let i = 0; i < rowsLength; i++) {
const row = rows[i];
let outRow = createRow();
let outRow = createRow(row);
let pkWithNullCount = 0;
for (let j = 0; j < columnsLength; j++) {
if (j < primaryColumnsLength) {
if (row[keys[j]] === null)
pkWithNullCount++;
if (pkWithNullCount === primaryColumnsLength) {
outRow = null;
break;
}
for (let j = 0; j < primaryColumnsLength; j++) {
if (row[keys[j]] === null)
pkWithNullCount++;
if (pkWithNullCount === primaryColumnsLength) {
outRow = null;
break;
}
const column = columns[j];
outRow[column.alias] = column.decode(row[keys[j]]);
if (shouldCreateMap)
fkIds[i] = getIds(outRow);
}
if (shouldCreateMap) {
fkIds[i] = getIds(outRow);
addToMap(rowsMap, primaryColumns, outRow);
}
outRows[i] = outRow;
}
span._rowsMap = rowsMap;
span._ids = fkIds;

const manyPromise = decodeManyRelations(strategy, span);

for (let i = 0; i < rowsLength; i++) {
const row = rows[i];
// console.dir('before create')
// console.dir(row)
let outRow = outRows[i];
for (let j = primaryColumnsLength; j < columnsLength; j++) {
const column = columns[j];
outRow[column.alias] = column.decode(row[keys[j]]);
}

for (let j = 0; j < aggregateKeys.length; j++) {
const key = aggregateKeys[j];
const parse = span.aggregates[key].column?.decode || Number.parseFloat;
outRow[key] = parse(row[keys[j+columnsLength]]);
}

outRows[i] = outRow;
if (shouldCreateMap)
addToMap(rowsMap, primaryColumns, outRow);

}
span._rowsMap = rowsMap;
span._ids = fkIds;

for (let i = 0; i < columnsLength + aggregateKeys.length; i++) {
keys.shift();
}
await decodeRelations(strategy, span, rows, outRows, keys);
await manyPromise;
return outRows;


Expand All @@ -166,19 +271,10 @@ async function decode(strategy, span, rows, keys = rows.length > 0 ? Object.keys

}

async function decodeRelations(strategy, span, rawRows, resultRows, keys) {
async function decodeManyRelations(strategy, span) {
const promises = [];
const c = {};
c.visitJoin = function(leg) {
const name = leg.name;
const p = decode(strategy[name], leg.span, rawRows, keys).then((rows) => {
for (let i = 0; i < rows.length; i++) {
resultRows[i][name] = rows[i];
}
});
promises.push(p);
};

c.visitJoin = () => {};
c.visitOne = c.visitJoin;

c.visitMany = function(leg) {
Expand All @@ -205,6 +301,30 @@ async function decodeRelations(strategy, span, rawRows, resultRows, keys) {

await Promise.all(promises);
}
async function decodeRelations(strategy, span, rawRows, resultRows, keys) {
const promises = [];
const c = {};
c.visitJoin = function(leg) {
const name = leg.name;
const p = decode(strategy[name], leg.span, rawRows, keys).then((rows) => {
for (let i = 0; i < rows.length; i++) {
resultRows[i][name] = rows[i];
}
});
promises.push(p);
};

c.visitOne = c.visitJoin;

c.visitMany = () => {};
span.legs.forEach(onEachLeg);

function onEachLeg(leg) {
leg.accept(c);
}

await Promise.all(promises);
}

function createOneFilter(relation, ids) {
const columns = relation.joinRelation.columns;
Expand Down

0 comments on commit d89ad5e

Please sign in to comment.