diff --git a/src/background/mysql.js b/src/background/mysql.js index 6bfc910..1e11b90 100644 --- a/src/background/mysql.js +++ b/src/background/mysql.js @@ -118,7 +118,11 @@ const expand = (sphinx) => { }) }) - sphinx.replaceValues = (table, values, particial = true, key = 'id', callback = () => {}) => new Promise((resolve) => { + sphinx.replaceValues = (table, values, options = {}, callback = () => {}) => new Promise((resolve) => { + const {particial, key, merge, mergeCallback} = Object.assign({ + particial: true, + key: 'id' + }, options) values = Object.assign({}, values) // copy let names = ''; @@ -161,6 +165,16 @@ const expand = (sphinx) => { return } + if(merge) + { + for(const m of merge) + { + values[m] = Object.assign(JSON.parse(row[0][m] || '{}'), values[m]) + if(mergeCallback) + mergeCallback(m, values[m]) + } + } + data = `(${parseValues(Object.assign(row[0], values))})` finalQuery() }) diff --git a/src/background/spider.js b/src/background/spider.js index b477cd9..01b8b3d 100644 --- a/src/background/spider.js +++ b/src/background/spider.js @@ -116,7 +116,15 @@ module.exports = function (send, recive, dataDirectory, version, env) return logT('tracker', 'found', name, 'on', tracker) - this.sphinx.replaceValues('torrents', {hash, info: data}, true, 'hash') + this.sphinx.replaceValues('torrents', {hash, info: data}, {particial: true, key: 'hash', merge: ['info'], mergeCallback: (n, obj) => { + if(n != 'info') + return + + if(!obj.trackers) + obj.trackers = [] + obj.trackers.push(name) + obj.trackers = [...new Set(obj.trackers)] + } }) }) } } diff --git a/tests/sphinx.api.test.js b/tests/sphinx.api.test.js index 5af6c7d..ef7edf5 100644 --- a/tests/sphinx.api.test.js +++ b/tests/sphinx.api.test.js @@ -13,18 +13,28 @@ describe("big table for check", () => { it("replace with add", async function() { await sphinx.query(`insert into feed(id, data) values(1, '{a: 1}')`) assert.equal((await sphinx.query(`select data from feed where id = 1`))[0].data, '{"a":1}') - await sphinx.replaceValues('feed', {id: 1, data: {a: 1, b: 2}}, true) + await sphinx.replaceValues('feed', {id: 1, data: {a: 1, b: 2}}, {particial: true}) assert.equal((await sphinx.query(`select data from feed`))[0].data, '{"a":1,"b":2}') }) it("replace without add", async function() { await sphinx.query(`insert into feed(id, data) values(2, '{a: 1}')`) - await sphinx.replaceValues('feed', {id: 1, data: {a: 1, b: 2, c: 3}}, false) + await sphinx.replaceValues('feed', {id: 1, data: {a: 1, b: 2, c: 3}}, {particial: false}) assert.equal((await sphinx.query(`select data from feed where id = 1`))[0].data, '{"a":1,"b":2,"c":3}') assert.equal((await sphinx.query(`select count(*) as c from feed`))[0].c, 2) }) + it("replace with merge", async function() { + await sphinx.replaceValues('feed', {id: 1, data: {d: 4}}, {particial: true, merge: ['data']}) + assert.equal((await sphinx.query(`select data from feed where id = 1`))[0].data, '{"a":1,"b":2,"c":3,"d":4}') + }) + + it("replace with merge and callback modify", async function() { + await sphinx.replaceValues('feed', {id: 1, data: {d: 6}}, {particial: true, merge: ['data'], mergeCallback: (name, obj) => { console.log(obj); obj.e = 5; } }) + assert.equal((await sphinx.query(`select data from feed where id = 1`))[0].data, '{"a":1,"b":2,"c":3,"d":6,"e":5}') + }) + it("close", async function() { await sphinx.end() })