document-dataply 0.0.2-alpha.1 → 0.0.2-alpha.2

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.
package/dist/cjs/index.js CHANGED
@@ -7254,7 +7254,7 @@ var DocumentDataply = class {
7254
7254
  for (const field in query) {
7255
7255
  const conditions = query[field];
7256
7256
  let newConditions;
7257
- if (typeof conditions !== "object") {
7257
+ if (typeof conditions !== "object" || conditions === null) {
7258
7258
  newConditions = { primaryEqual: { v: conditions } };
7259
7259
  } else {
7260
7260
  newConditions = {};
@@ -7263,7 +7263,11 @@ var DocumentDataply = class {
7263
7263
  const after = this.operatorConverters[before];
7264
7264
  const v = conditions[before];
7265
7265
  if (!after) continue;
7266
- newConditions[after] = { v };
7266
+ if (before === "or" && Array.isArray(v)) {
7267
+ newConditions[after] = v.map((val) => ({ v: val }));
7268
+ } else {
7269
+ newConditions[after] = { v };
7270
+ }
7267
7271
  }
7268
7272
  }
7269
7273
  result[field] = newConditions;
@@ -7283,7 +7287,10 @@ var DocumentDataply = class {
7283
7287
  const condition = query[field];
7284
7288
  candidates.push({ tree, condition });
7285
7289
  }
7286
- const res = import_dataply3.BPTreeAsync.ChooseDriver(candidates);
7290
+ let res = import_dataply3.BPTreeAsync.ChooseDriver(candidates);
7291
+ if (!res && candidates.length > 0) {
7292
+ res = candidates[0];
7293
+ }
7287
7294
  if (!res) return null;
7288
7295
  return {
7289
7296
  driver: {
@@ -7337,30 +7344,42 @@ var DocumentDataply = class {
7337
7344
  const verbose = this.verboseQuery(query);
7338
7345
  const selectivity = await this.getSelectivityCandidate(verbose);
7339
7346
  if (!selectivity) return [];
7340
- const keys = /* @__PURE__ */ new Set();
7347
+ const documents = [];
7341
7348
  const { driver, others } = selectivity;
7342
- const stream = driver.tree.whereStream(driver.condition);
7349
+ const stream = driver.condition.primaryOr ? (async function* () {
7350
+ const values = driver.condition.primaryOr;
7351
+ for (const val of values) {
7352
+ const nodes = await driver.tree.where({ primaryEqual: val });
7353
+ for (const [pk, node] of nodes) {
7354
+ yield [pk, node];
7355
+ }
7356
+ }
7357
+ })() : driver.condition.primaryNotEqual ? (async function* (api) {
7358
+ const idTree = await api.ensureTree("_id");
7359
+ const nodes = idTree.whereStream({ primaryGte: { v: 0 } });
7360
+ for await (const [pk, node] of nodes) {
7361
+ yield [pk, node];
7362
+ }
7363
+ })(this.api) : driver.tree.whereStream(driver.condition);
7343
7364
  for await (const [pk, val] of stream) {
7365
+ const stringify = await this.api.select(pk, false, tx2);
7366
+ if (!stringify) continue;
7367
+ const doc = JSON.parse(stringify);
7368
+ const flattenedDoc = this.api.flattenDocument(doc);
7344
7369
  let isMatch = true;
7345
- for (const { tree, condition } of others) {
7346
- const targetValue = await tree.get(pk);
7347
- if (targetValue === void 0 || !tree.verify(targetValue, condition)) {
7370
+ const verificationList = driver.condition.primaryNotEqual ? [...others, driver] : others;
7371
+ for (const { tree, condition } of verificationList) {
7372
+ const field = tree.strategy.treeKey;
7373
+ const fieldValue = flattenedDoc[field];
7374
+ if (!tree.verify({ k: pk, v: fieldValue }, condition)) {
7348
7375
  isMatch = false;
7349
7376
  break;
7350
7377
  }
7351
7378
  }
7352
7379
  if (isMatch) {
7353
- keys.add(pk);
7354
- if (keys.size >= limit) break;
7355
- }
7356
- }
7357
- const documents = [];
7358
- for (const key of keys) {
7359
- const stringify = await this.api.select(key, false, tx2);
7360
- if (!stringify) {
7361
- continue;
7380
+ documents.push(doc);
7381
+ if (documents.length >= limit) break;
7362
7382
  }
7363
- documents.push(JSON.parse(stringify));
7364
7383
  }
7365
7384
  return documents;
7366
7385
  }, tx);
@@ -46,7 +46,9 @@ export type DocumentDataplyCondition<V> = {
46
46
  or?: Partial<V>[];
47
47
  };
48
48
  export type DocumentDataplyQuery<T> = {
49
- [key in keyof T]: T[key] | DocumentDataplyCondition<T[key]>;
49
+ [key in keyof T]?: T[key] | DocumentDataplyCondition<T[key]>;
50
+ } & {
51
+ [key: string]: any;
50
52
  };
51
53
  export interface DataplyTreeValue<T> {
52
54
  k: number;
@@ -55,13 +57,21 @@ export interface DataplyTreeValue<T> {
55
57
  /**
56
58
  * T가 객체인지 확인하고, 객체라면 하위 키를 재귀적으로 탐색합니다.
57
59
  */
58
- type DeepFlattenKeys<T, Prefix extends string = ""> = T extends object ? {
59
- [K in keyof T & string]: NonNullable<T[K]> extends object ? DeepFlattenKeys<NonNullable<T[K]>, `${Prefix}${K}.`> : `${Prefix}${K}`;
60
- }[keyof T & string] : Prefix extends `${infer P}.` ? P : "";
60
+ type Prev = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
61
+ /**
62
+ * T 객체인지 확인하고, 객체라면 하위 키를 재귀적으로 탐색합니다.
63
+ * Depth 제한을 두어 "Type instantiation is excessively deep and possibly infinite" 에러를 방지합니다.
64
+ */
65
+ export type DeepFlattenKeys<T, Prefix extends string = "", D extends number = 12> = [
66
+ D
67
+ ] extends [0] ? never : T extends Primitive ? (Prefix extends `${infer P}.` ? P : never) : T extends readonly any[] ? ((Prefix extends `${infer P}.` ? P : never) | DeepFlattenKeys<T[number], `${Prefix}${number}.`, Prev[D]>) : T extends object ? {
68
+ [K in keyof T & string]: NonNullable<T[K]> extends Primitive ? `${Prefix}${K}` : `${Prefix}${K}` | DeepFlattenKeys<NonNullable<T[K]>, `${Prefix}${K}.`, Prev[D]>;
69
+ }[keyof T & string] : never;
61
70
  /**
62
71
  * 경로 문자열(Path)을 기반으로 원본 객체(T)에서 타입을 찾아옵니다.
72
+ * 배열 인덱스 접근 허용 (ex: tags.0)
63
73
  */
64
- type GetTypeByPath<T, Path extends string> = Path extends `${infer Key}.${infer Rest}` ? Key extends keyof T ? GetTypeByPath<NonNullable<T[Key]>, Rest> : never : Path extends keyof T ? T[Path] : never;
74
+ type GetTypeByPath<T, Path extends string> = T extends readonly (infer U)[] ? Path extends `${infer Key}.${infer Rest}` ? Key extends `${number}` ? GetTypeByPath<U, Rest> : Key extends keyof T ? GetTypeByPath<T[Key], Rest> : never : Path extends `${number}` ? U : Path extends keyof T ? T[Path] : never : Path extends `${infer Key}.${infer Rest}` ? Key extends keyof T ? GetTypeByPath<NonNullable<T[Key]>, Rest> : never : Path extends keyof T ? T[Path] : never;
65
75
  export type FinalFlatten<T> = {
66
76
  [P in DeepFlattenKeys<T>]: GetTypeByPath<T, P & string>;
67
77
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "document-dataply",
3
- "version": "0.0.2-alpha.1",
3
+ "version": "0.0.2-alpha.2",
4
4
  "description": "",
5
5
  "license": "MIT",
6
6
  "author": "izure <admin@izure.org>",