document-dataply 0.0.9-alpha.13 → 0.0.9-alpha.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/cjs/index.js +85 -10
  2. package/package.json +2 -2
package/dist/cjs/index.js CHANGED
@@ -10997,7 +10997,8 @@ var DocumentDataplyAPI = class extends import_dataply3.DataplyAPI {
10997
10997
  const doc = await this.getDocument(k, tx2);
10998
10998
  if (!doc) continue;
10999
10999
  const flatDoc = this.flattenDocument(doc);
11000
- for (const indexName of backfillTargets) {
11000
+ for (let i = 0, len = backfillTargets.length; i < len; i++) {
11001
+ const indexName = backfillTargets[i];
11001
11002
  if (!(indexName in indexTxMap)) continue;
11002
11003
  const config = this.registeredIndices.get(indexName);
11003
11004
  if (!config) continue;
@@ -11009,8 +11010,8 @@ var DocumentDataplyAPI = class extends import_dataply3.DataplyAPI {
11009
11010
  const ftsConfig = this.getFtsConfig(config);
11010
11011
  const tokens = ftsConfig ? tokenize(v, ftsConfig) : [v];
11011
11012
  const batchInsertData = [];
11012
- for (let i = 0, len = tokens.length; i < len; i++) {
11013
- const token = tokens[i];
11013
+ for (let i2 = 0, len2 = tokens.length; i2 < len2; i2++) {
11014
+ const token = tokens[i2];
11014
11015
  const keyToInsert = this.getTokenKey(k, token);
11015
11016
  const entry = { k, v: token };
11016
11017
  batchInsertData.push([keyToInsert, entry]);
@@ -11211,13 +11212,18 @@ var DocumentDataplyAPI = class extends import_dataply3.DataplyAPI {
11211
11212
  if (config.type === "btree") {
11212
11213
  const primaryField = config.fields[0];
11213
11214
  if (!queryFields.has(primaryField)) continue;
11214
- const condition = query[primaryField];
11215
11215
  const treeTx = await tree.createTransaction();
11216
+ const builtCondition = {};
11216
11217
  let score = 0;
11217
11218
  let isConsecutive = true;
11218
11219
  const coveredFields = [];
11219
11220
  const compositeVerifyFields = [];
11220
- for (const field of config.fields) {
11221
+ const startValues = [];
11222
+ const endValues = [];
11223
+ let startOperator = null;
11224
+ let endOperator = null;
11225
+ for (let i = 0, len = config.fields.length; i < len; i++) {
11226
+ const field = config.fields[i];
11221
11227
  if (!queryFields.has(field)) {
11222
11228
  isConsecutive = false;
11223
11229
  continue;
@@ -11232,11 +11238,53 @@ var DocumentDataplyAPI = class extends import_dataply3.DataplyAPI {
11232
11238
  if (cond !== void 0) {
11233
11239
  if (typeof cond !== "object" || cond === null) {
11234
11240
  score += 100;
11241
+ startValues.push(cond);
11242
+ endValues.push(cond);
11243
+ startOperator = "primaryGte";
11244
+ endOperator = "primaryLte";
11235
11245
  } else if ("primaryEqual" in cond || "equal" in cond) {
11246
+ const val = cond.primaryEqual?.v ?? cond.equal?.v ?? cond.primaryEqual ?? cond.equal;
11236
11247
  score += 100;
11237
- } else if ("primaryGte" in cond || "primaryLte" in cond || "primaryGt" in cond || "primaryLt" in cond || "gte" in cond || "lte" in cond || "gt" in cond || "lt" in cond) {
11248
+ startValues.push(val);
11249
+ endValues.push(val);
11250
+ startOperator = "primaryGte";
11251
+ endOperator = "primaryLte";
11252
+ } else if ("primaryGte" in cond || "gte" in cond) {
11253
+ const val = cond.primaryGte?.v ?? cond.gte?.v ?? cond.primaryGte ?? cond.gte;
11238
11254
  score += 50;
11239
11255
  isConsecutive = false;
11256
+ startValues.push(val);
11257
+ startOperator = "primaryGte";
11258
+ if (endValues.length > 0) {
11259
+ endOperator = "primaryLte";
11260
+ }
11261
+ } else if ("primaryGt" in cond || "gt" in cond) {
11262
+ const val = cond.primaryGt?.v ?? cond.gt?.v ?? cond.primaryGt ?? cond.gt;
11263
+ score += 50;
11264
+ isConsecutive = false;
11265
+ startValues.push(val);
11266
+ startOperator = "primaryGt";
11267
+ if (endValues.length > 0) {
11268
+ endOperator = "primaryLte";
11269
+ }
11270
+ } else if ("primaryLte" in cond || "lte" in cond) {
11271
+ const val = cond.primaryLte?.v ?? cond.lte?.v ?? cond.primaryLte ?? cond.lte;
11272
+ score += 50;
11273
+ isConsecutive = false;
11274
+ endValues.push(val);
11275
+ endOperator = "primaryLte";
11276
+ if (startValues.length > 0) {
11277
+ startOperator = "primaryGte";
11278
+ }
11279
+ } else if ("primaryLt" in cond || "lt" in cond) {
11280
+ const val = cond.primaryLt?.v ?? cond.lt?.v ?? cond.primaryLt ?? cond.lt;
11281
+ score += 50;
11282
+ isConsecutive = false;
11283
+ endValues.push(val);
11284
+ endOperator = "primaryLt";
11285
+ if (startValues.length > 0) {
11286
+ startOperator = "primaryGte";
11287
+ }
11240
11288
  } else if ("primaryOr" in cond || "or" in cond) {
11241
11289
  score += 20;
11242
11290
  isConsecutive = false;
@@ -11250,9 +11298,28 @@ var DocumentDataplyAPI = class extends import_dataply3.DataplyAPI {
11250
11298
  }
11251
11299
  }
11252
11300
  }
11301
+ if (coveredFields.length === 1 && config.fields.length === 1) {
11302
+ Object.assign(builtCondition, query[primaryField]);
11303
+ } else {
11304
+ if (startOperator && startValues.length > 0) {
11305
+ builtCondition[startOperator] = { v: startValues.length === 1 ? startValues[0] : startValues };
11306
+ }
11307
+ if (endOperator && endValues.length > 0) {
11308
+ if (startOperator && startValues.length === endValues.length && startValues.every((val, i) => val === endValues[i])) {
11309
+ delete builtCondition[startOperator];
11310
+ builtCondition["primaryEqual"] = { v: startValues.length === 1 ? startValues[0] : startValues };
11311
+ } else {
11312
+ builtCondition[endOperator] = { v: endValues.length === 1 ? endValues[0] : endValues };
11313
+ }
11314
+ }
11315
+ if (Object.keys(builtCondition).length === 0) {
11316
+ Object.assign(builtCondition, query[primaryField] || {});
11317
+ }
11318
+ }
11253
11319
  let isIndexOrderSupported = false;
11254
11320
  if (orderByField) {
11255
- for (const field of config.fields) {
11321
+ for (let i = 0, len = config.fields.length; i < len; i++) {
11322
+ const field = config.fields[i];
11256
11323
  if (field === orderByField) {
11257
11324
  isIndexOrderSupported = true;
11258
11325
  break;
@@ -11271,7 +11338,7 @@ var DocumentDataplyAPI = class extends import_dataply3.DataplyAPI {
11271
11338
  }
11272
11339
  candidates.push({
11273
11340
  tree: treeTx,
11274
- condition,
11341
+ condition: builtCondition,
11275
11342
  field: primaryField,
11276
11343
  indexName,
11277
11344
  isFtsMatch: false,
@@ -11309,11 +11376,19 @@ var DocumentDataplyAPI = class extends import_dataply3.DataplyAPI {
11309
11376
  rollback();
11310
11377
  return null;
11311
11378
  }
11312
- candidates.sort((a, b) => b.score - a.score);
11379
+ candidates.sort((a, b) => {
11380
+ if (b.score !== a.score) return b.score - a.score;
11381
+ const aConfig = this.registeredIndices.get(a.indexName);
11382
+ const bConfig = this.registeredIndices.get(b.indexName);
11383
+ const aFieldCount = aConfig ? Array.isArray(aConfig.fields) ? aConfig.fields.length : 1 : 0;
11384
+ const bFieldCount = bConfig ? Array.isArray(bConfig.fields) ? bConfig.fields.length : 1 : 0;
11385
+ return aFieldCount - bFieldCount;
11386
+ });
11313
11387
  const driver = candidates[0];
11314
11388
  const others = candidates.slice(1).filter((c) => c.field !== driver.field);
11315
11389
  const compositeVerifyConditions = [];
11316
- for (const field of driver.compositeVerifyFields) {
11390
+ for (let i = 0, len = driver.compositeVerifyFields.length; i < len; i++) {
11391
+ const field = driver.compositeVerifyFields[i];
11317
11392
  if (query[field]) {
11318
11393
  compositeVerifyConditions.push({ field, condition: query[field] });
11319
11394
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "document-dataply",
3
- "version": "0.0.9-alpha.13",
3
+ "version": "0.0.9-alpha.14",
4
4
  "description": "Simple and powerful JSON document database supporting complex queries and flexible indexing policies.",
5
5
  "license": "MIT",
6
6
  "author": "izure <admin@izure.org>",
@@ -51,4 +51,4 @@
51
51
  "ts-jest": "^29.4.6",
52
52
  "typescript": "^5.9.3"
53
53
  }
54
- }
54
+ }