diff --git a/dist/db/clickhouse/index.cjs b/dist/db/clickhouse/index.cjs index 9db90ef..7a15ee4 100644 --- a/dist/db/clickhouse/index.cjs +++ b/dist/db/clickhouse/index.cjs @@ -80,7 +80,7 @@ let Index; */ function ClickhouseDB(name, config = {}){ - if(!this){ + if(!this || this.constructor !== ClickhouseDB){ return new ClickhouseDB(name, config); } if(typeof name === "object"){ @@ -106,7 +106,7 @@ function ClickhouseDB(name, config = {}){ } ClickhouseDB.prototype.mount = function(flexsearch){ //if(flexsearch.constructor === Document){ - if(!flexsearch.encoder){ + if(flexsearch.index){ return flexsearch.mount(this); } defaults.resolution = Math.max(flexsearch.resolution, flexsearch.resolution_ctx); diff --git a/dist/db/indexeddb/index.cjs b/dist/db/indexeddb/index.cjs index 9797b72..f1f0e76 100644 --- a/dist/db/indexeddb/index.cjs +++ b/dist/db/indexeddb/index.cjs @@ -55,7 +55,7 @@ const Index = create_object(); */ function IdxDB(name, config = {}){ - if(!this){ + if(!this || this.constructor !== IdxDB){ return new IdxDB(name, config); } if(typeof name === "object"){ @@ -75,7 +75,7 @@ function IdxDB(name, config = {}){ } IdxDB.prototype.mount = function(flexsearch){ //if(flexsearch.constructor === Document){ - if(!flexsearch.encoder){ + if(flexsearch.index){ return flexsearch.mount(this); } flexsearch.db = this; diff --git a/dist/db/mongodb/index.cjs b/dist/db/mongodb/index.cjs index 75d134c..c43acfc 100644 --- a/dist/db/mongodb/index.cjs +++ b/dist/db/mongodb/index.cjs @@ -45,7 +45,7 @@ let Index = Object.create(null); */ function MongoDB(name, config = {}){ - if(!this){ + if(!this || this.constructor !== MongoDB){ return new MongoDB(name, config); } if(typeof name === "object"){ @@ -70,7 +70,7 @@ function MongoDB(name, config = {}){ MongoDB.prototype.mount = function(flexsearch){ //if(flexsearch.constructor === Document){ - if(!flexsearch.encoder){ + if(flexsearch.index){ return flexsearch.mount(this); } flexsearch.db = this; diff --git a/dist/db/postgres/index.cjs b/dist/db/postgres/index.cjs index 86f217e..3250400 100644 --- a/dist/db/postgres/index.cjs +++ b/dist/db/postgres/index.cjs @@ -74,7 +74,7 @@ let DB; */ function PostgresDB(name, config = {}){ - if(!this){ + if(!this || this.constructor !== PostgresDB){ return new PostgresDB(name, config); } if(typeof name === "object"){ @@ -95,7 +95,7 @@ function PostgresDB(name, config = {}){ } PostgresDB.prototype.mount = function(flexsearch){ //if(flexsearch.constructor === Document){ - if(!flexsearch.encoder){ + if(flexsearch.index){ return flexsearch.mount(this); } flexsearch.db = this; diff --git a/dist/db/redis/index.cjs b/dist/db/redis/index.cjs index 6366f57..3aac402 100644 --- a/dist/db/redis/index.cjs +++ b/dist/db/redis/index.cjs @@ -43,7 +43,7 @@ let DB, TRX; */ function RedisDB(name, config = {}){ - if(!this){ + if(!this || this.constructor !== RedisDB){ return new RedisDB(name, config); } if(typeof name === "object"){ @@ -69,7 +69,7 @@ function RedisDB(name, config = {}){ RedisDB.prototype.mount = function(flexsearch){ //if(flexsearch.constructor === Document){ - if(!flexsearch.encoder){ + if(flexsearch.index){ return flexsearch.mount(this); } flexsearch.db = this; @@ -82,20 +82,24 @@ RedisDB.prototype.open = async function(){ if(this.db){ return this.db } + if(DB){ + return this.db = DB; + } let url = defaults.url; if(!url){ url = defaults.user ? `redis://${defaults.user}:${defaults.pass}@${defaults.host}:${defaults.port}` : `redis://${defaults.host}:${defaults.port}`; } - return this.db = + return this.db = DB = await redis.createClient(url) .on("error", err => console.error(err)) .connect(); }; RedisDB.prototype.close = async function(){ - this.db = null; + DB && await this.db.disconnect(); // this.db.client.disconnect(); + this.db = DB = null; return this; }; @@ -116,14 +120,16 @@ RedisDB.prototype.clear = function(){ function create_result(range, type, resolve, enrich){ if(resolve){ - for(let i = 0, tmp, id; i < range.length; i++){ - tmp = range[i]; - id = type === "number" - ? parseInt(tmp.value || tmp, 10) - : tmp.value || tmp; - range[i] = /*enrich - ? { id, doc: tmp.doc } - :*/ id; + if(type === "number"){ + for(let i = 0, tmp, id; i < range.length; i++){ + tmp = range[i]; + id = type === "number" + ? parseInt(tmp.id || tmp, 10) + : tmp.id || tmp; + range[i] = enrich + ? { id, doc: tmp.doc } + : id; + } } return range; } @@ -132,15 +138,11 @@ function create_result(range, type, resolve, enrich){ for(let i = 0, tmp, id, score; i < range.length; i++){ tmp = range[i]; id = type === "number" - ? parseInt(tmp.value, 10) - : tmp.value; + ? parseInt(tmp.id || tmp, 10) + : tmp.id || tmp; score = tmp.score; result[score] || (result[score] = []); - result[score].push( - enrich - ? { id, doc: tmp.doc } - : id - ); + result[score].push(id); } return result; } diff --git a/dist/db/sqlite/index.cjs b/dist/db/sqlite/index.cjs index db48e08..e408ac3 100644 --- a/dist/db/sqlite/index.cjs +++ b/dist/db/sqlite/index.cjs @@ -68,7 +68,7 @@ const Index = Object.create(null); */ function SqliteDB(name, config = {}){ - if(!this){ + if(!this || this.constructor !== SqliteDB){ return new SqliteDB(name, config); } if(typeof name === "object"){ @@ -92,7 +92,7 @@ function SqliteDB(name, config = {}){ } SqliteDB.prototype.mount = function(flexsearch){ //if(flexsearch.constructor === Document){ - if(!flexsearch.encoder){ + if(flexsearch.index){ return flexsearch.mount(this); } flexsearch.db = this; diff --git a/dist/module-debug/db/redis/index.js b/dist/module-debug/db/redis/index.js index 46ac29a..7987505 100644 --- a/dist/module-debug/db/redis/index.js +++ b/dist/module-debug/db/redis/index.js @@ -63,17 +63,19 @@ RedisDB.prototype.open = async function () { if (this.db) { return this.db; } - + if (DB) { + return this.db = DB; + } let url = defaults.url; if (!url) { url = defaults.user ? `redis://${defaults.user}:${defaults.pass}@${defaults.host}:${defaults.port}` : `redis://${defaults.host}:${defaults.port}`; } - return this.db = await createClient(url).on("error", err => console.error(err)).connect(); + return this.db = DB = await createClient(url).on("error", err => console.error(err)).connect(); }; RedisDB.prototype.close = async function () { - // this.db.client.disconnect(); - this.db = null; + DB && (await this.db.disconnect()); // this.db.client.disconnect(); + this.db = DB = null; return this; }; @@ -87,22 +89,22 @@ RedisDB.prototype.clear = function () { function create_result(range, type, resolve, enrich) { if (resolve) { - for (let i = 0, tmp, id; i < range.length; i++) { - tmp = range[i]; - id = "number" === type ? parseInt(tmp.value || tmp, 10) : tmp.value || tmp; - range[i] = /*enrich - ? { id, doc: tmp.doc } - :*/id; + if ("number" === type) { + for (let i = 0, tmp, id; i < range.length; i++) { + tmp = range[i]; + id = "number" === type ? parseInt(tmp.id || tmp, 10) : tmp.id || tmp; + range[i] = enrich ? { id, doc: tmp.doc } : id; + } } return range; } else { let result = []; for (let i = 0, tmp, id, score; i < range.length; i++) { tmp = range[i]; - id = "number" === type ? parseInt(tmp.value, 10) : tmp.value; + id = "number" === type ? parseInt(tmp.id || tmp, 10) : tmp.id || tmp; score = tmp.score; result[score] || (result[score] = []); - result[score].push(enrich ? { id, doc: tmp.doc } : id); + result[score].push(id); } return result; } diff --git a/dist/module/db/redis/index.js b/dist/module/db/redis/index.js index 46ac29a..7987505 100644 --- a/dist/module/db/redis/index.js +++ b/dist/module/db/redis/index.js @@ -63,17 +63,19 @@ RedisDB.prototype.open = async function () { if (this.db) { return this.db; } - + if (DB) { + return this.db = DB; + } let url = defaults.url; if (!url) { url = defaults.user ? `redis://${defaults.user}:${defaults.pass}@${defaults.host}:${defaults.port}` : `redis://${defaults.host}:${defaults.port}`; } - return this.db = await createClient(url).on("error", err => console.error(err)).connect(); + return this.db = DB = await createClient(url).on("error", err => console.error(err)).connect(); }; RedisDB.prototype.close = async function () { - // this.db.client.disconnect(); - this.db = null; + DB && (await this.db.disconnect()); // this.db.client.disconnect(); + this.db = DB = null; return this; }; @@ -87,22 +89,22 @@ RedisDB.prototype.clear = function () { function create_result(range, type, resolve, enrich) { if (resolve) { - for (let i = 0, tmp, id; i < range.length; i++) { - tmp = range[i]; - id = "number" === type ? parseInt(tmp.value || tmp, 10) : tmp.value || tmp; - range[i] = /*enrich - ? { id, doc: tmp.doc } - :*/id; + if ("number" === type) { + for (let i = 0, tmp, id; i < range.length; i++) { + tmp = range[i]; + id = "number" === type ? parseInt(tmp.id || tmp, 10) : tmp.id || tmp; + range[i] = enrich ? { id, doc: tmp.doc } : id; + } } return range; } else { let result = []; for (let i = 0, tmp, id, score; i < range.length; i++) { tmp = range[i]; - id = "number" === type ? parseInt(tmp.value, 10) : tmp.value; + id = "number" === type ? parseInt(tmp.id || tmp, 10) : tmp.id || tmp; score = tmp.score; result[score] || (result[score] = []); - result[score].push(enrich ? { id, doc: tmp.doc } : id); + result[score].push(id); } return result; } diff --git a/src/db/redis/index.js b/src/db/redis/index.js index 7ee0a5e..529d3a2 100644 --- a/src/db/redis/index.js +++ b/src/db/redis/index.js @@ -71,7 +71,7 @@ RedisDB.prototype.open = async function(){ ? `redis://${defaults.user}:${defaults.pass}@${defaults.host}:${defaults.port}` : `redis://${defaults.host}:${defaults.port}`; } - return this.db = + return this.db = DB = await createClient(url) .on("error", err => console.error(err)) .connect(); @@ -79,7 +79,7 @@ RedisDB.prototype.open = async function(){ RedisDB.prototype.close = async function(){ DB && await this.db.disconnect(); // this.db.client.disconnect(); - this.db = null; + this.db = DB = null; return this; }; @@ -100,31 +100,29 @@ RedisDB.prototype.clear = function(){ function create_result(range, type, resolve, enrich){ if(resolve){ - for(let i = 0, tmp, id; i < range.length; i++){ - tmp = range[i]; - id = type === "number" - ? parseInt(tmp.value || tmp, 10) - : tmp.value || tmp; - range[i] = /*enrich - ? { id, doc: tmp.doc } - :*/ id; + if(type === "number"){ + for(let i = 0, tmp, id; i < range.length; i++){ + tmp = range[i]; + id = type === "number" + ? parseInt(tmp.id || tmp, 10) + : tmp.id || tmp; + range[i] = enrich + ? { id, doc: tmp.doc } + : id; + } } return range; } - else{ + else { let result = []; for(let i = 0, tmp, id, score; i < range.length; i++){ tmp = range[i]; id = type === "number" - ? parseInt(tmp.value, 10) - : tmp.value + ? parseInt(tmp.id || tmp, 10) + : tmp.id || tmp; score = tmp.score; result[score] || (result[score] = []); - result[score].push( - enrich - ? { id, doc: tmp.doc } - : id - ); + result[score].push(id); } return result; }