mongolite-ts 0.2.2 → 0.2.4

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/types.d.ts CHANGED
@@ -3,7 +3,7 @@
3
3
  */
4
4
  export interface DocumentWithId {
5
5
  _id?: string;
6
- [key: string]: any;
6
+ [key: string]: unknown;
7
7
  }
8
8
  /**
9
9
  * Result of insertOne operation.
@@ -52,6 +52,12 @@ export interface LogicalOperators<T> {
52
52
  $or?: Filter<T>[];
53
53
  $nor?: Filter<T>[];
54
54
  }
55
+ /**
56
+ * Text search operator for full-text search.
57
+ */
58
+ export interface TextSearchOperator {
59
+ $search: string;
60
+ }
55
61
  /**
56
62
  * Filter type for querying documents.
57
63
  * T is the document type.
@@ -59,8 +65,10 @@ export interface LogicalOperators<T> {
59
65
  export type Filter<T> = {
60
66
  [P in keyof T]?: T[P] | QueryOperators<T[P]>;
61
67
  } & {
62
- [key: string]: any;
63
- } & Partial<LogicalOperators<T>>;
68
+ [key: string]: unknown;
69
+ } & Partial<LogicalOperators<T>> & {
70
+ $text?: TextSearchOperator;
71
+ };
64
72
  /**
65
73
  * Operators for updating documents.
66
74
  * T is the document type.
@@ -69,7 +77,7 @@ export interface UpdateOperators<T> {
69
77
  $set?: {
70
78
  [P in keyof T]?: T[P];
71
79
  } & {
72
- [key: string]: any;
80
+ [key: string]: unknown;
73
81
  };
74
82
  $unset?: {
75
83
  [P in keyof T]?: '';
@@ -82,18 +90,18 @@ export interface UpdateOperators<T> {
82
90
  [key: string]: number;
83
91
  };
84
92
  $push?: {
85
- [P in keyof T]?: any | {
86
- $each: any[];
93
+ [P in keyof T]?: unknown | {
94
+ $each: unknown[];
87
95
  };
88
96
  } & {
89
- [key: string]: any | {
90
- $each: any[];
97
+ [key: string]: unknown | {
98
+ $each: unknown[];
91
99
  };
92
100
  };
93
101
  $pull?: {
94
- [P in keyof T]?: any;
102
+ [P in keyof T]?: unknown;
95
103
  } & {
96
- [key: string]: any;
104
+ [key: string]: unknown;
97
105
  };
98
106
  }
99
107
  /**
@@ -123,3 +131,78 @@ export interface SQLiteRow {
123
131
  _id: string;
124
132
  data: string;
125
133
  }
134
+ /**
135
+ * Index specification type.
136
+ * Specifies fields to index and their sort order (1 for ascending, -1 for descending).
137
+ */
138
+ export type IndexSpecification = {
139
+ [key: string]: 1 | -1 | string;
140
+ };
141
+ /**
142
+ * Options for createIndex.
143
+ */
144
+ export interface CreateIndexOptions {
145
+ /**
146
+ * Unique index flag. If true, the index will enforce uniqueness of the indexed field.
147
+ */
148
+ unique?: boolean;
149
+ /**
150
+ * Name of the index. If not specified, a name will be generated.
151
+ */
152
+ name?: string;
153
+ /**
154
+ * If true, create the index in the background and don't block other operations.
155
+ * In SQLite this might not make a significant difference.
156
+ */
157
+ background?: boolean;
158
+ /**
159
+ * If true, MongoDB will ignore any duplicate values when creating a unique index.
160
+ * Not directly applicable to SQLite, but retained for API compatibility.
161
+ */
162
+ sparse?: boolean;
163
+ }
164
+ /**
165
+ * Result of createIndex operation.
166
+ */
167
+ export interface CreateIndexResult {
168
+ /**
169
+ * Indicates whether the index creation operation was acknowledged.
170
+ */
171
+ acknowledged: boolean;
172
+ /**
173
+ * The name of the created index.
174
+ */
175
+ name: string;
176
+ }
177
+ /**
178
+ * Result of dropIndex operation.
179
+ */
180
+ export interface DropIndexResult {
181
+ /**
182
+ * Indicates whether the index deletion operation was acknowledged.
183
+ */
184
+ acknowledged: boolean;
185
+ /**
186
+ * The name of the dropped index.
187
+ */
188
+ name: string;
189
+ }
190
+ /**
191
+ * Result of a listIndexes operation.
192
+ */
193
+ export interface IndexInfo {
194
+ /**
195
+ * Name of the index.
196
+ */
197
+ name: string;
198
+ /**
199
+ * The fields this index is defined on.
200
+ */
201
+ key: {
202
+ [key: string]: number;
203
+ };
204
+ /**
205
+ * Whether this index enforces uniqueness.
206
+ */
207
+ unique?: boolean;
208
+ }
@@ -0,0 +1,4 @@
1
+ export declare function extractRawIndexColumns(sql: any): {
2
+ column: string;
3
+ direction: string | null;
4
+ }[];
@@ -0,0 +1,43 @@
1
+ export function extractRawIndexColumns(sql) {
2
+ const match = sql.match(/\(([\s\S]+)\)/); // allow multiline matching
3
+ if (!match)
4
+ return [];
5
+ const colsPart = match[1];
6
+ // Split on commas not inside parentheses
7
+ const columns = [];
8
+ let current = '';
9
+ let parens = 0;
10
+ for (let i = 0; i < colsPart.length; i++) {
11
+ const char = colsPart[i];
12
+ if (char === '(')
13
+ parens++;
14
+ if (char === ')')
15
+ parens--;
16
+ if (char === ',' && parens === 0) {
17
+ columns.push(current.trim());
18
+ current = '';
19
+ }
20
+ else {
21
+ current += char;
22
+ }
23
+ }
24
+ if (current.trim())
25
+ columns.push(current.trim());
26
+ return columns.map((col) => {
27
+ // Extract direction (ASC or DESC)
28
+ const directionMatch = col.match(/\s+(ASC|DESC)\s*$/i);
29
+ const direction = directionMatch ? directionMatch[1].toUpperCase() : null;
30
+ // Remove direction from column string
31
+ const colWithoutDirection = directionMatch
32
+ ? col.slice(0, directionMatch.index).trim()
33
+ : col.trim();
34
+ // Check for json_extract and pull out path
35
+ const jsonMatch = colWithoutDirection.match(/json_extract\s*\(\s*[^,]+,\s*'([^']+)'\s*\)/i);
36
+ let column = jsonMatch
37
+ ? jsonMatch[1] // the JSON path like '$.age'
38
+ : colWithoutDirection; // fallback to raw column like testCol
39
+ column = column.replace('$.', ''); // Remove leading $ if present
40
+ return { column, direction };
41
+ });
42
+ }
43
+ //# sourceMappingURL=indexing.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"indexing.js","sourceRoot":"","sources":["../../src/utils/indexing.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,sBAAsB,CAAC,GAAG;IACxC,MAAM,KAAK,GAAG,GAAG,CAAC,KAAK,CAAC,eAAe,CAAC,CAAC,CAAC,2BAA2B;IACrE,IAAI,CAAC,KAAK;QAAE,OAAO,EAAE,CAAC;IAEtB,MAAM,QAAQ,GAAG,KAAK,CAAC,CAAC,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SAAS,GAAG,mBAAmB,CAAC,KAAK,CAAC,8CAA8C,CAAC,CAAC;QAC5F,IAAI,MAAM,GAAG,SAAS;YACpB,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,6BAA6B;YAC5C,CAAC,CAAC,mBAAmB,CAAC,CAAC,sCAAsC;QAE/D,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,CAAC,8BAA8B;QACjE,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC;IAC/B,CAAC,CAAC,CAAC;AACL,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mongolite-ts",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "A MongoDB-like client using SQLite as a persistent store, written in TypeScript.",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -14,7 +14,8 @@
14
14
  "lint:fix": "eslint src/**/*.ts tests/**/*.ts --fix",
15
15
  "verify-build": "node --import tsx scripts/verify-build.js",
16
16
  "test-third-party": "node scripts/test-third-party-usage.js",
17
- "test:all": "npm run lint && npm run test && npm run verify-build && npm run test-third-party"
17
+ "test:all": "npm run lint && npm run test && npm run verify-build && npm run test-third-party",
18
+ "benchmark": "npx tsx scripts/benchmark.ts"
18
19
  },
19
20
  "repository": {
20
21
  "type": "git",
@@ -37,10 +38,10 @@
37
38
  "homepage": "https://github.com/semics-tech/mongolite-ts#readme",
38
39
  "devDependencies": {
39
40
  "@types/better-sqlite3": "^7.6.13",
40
- "@types/node": "^20.17.57",
41
+ "@types/node": "^24.0.7",
41
42
  "@typescript-eslint/eslint-plugin": "^7.9.0",
42
43
  "@typescript-eslint/parser": "^7.9.0",
43
- "c8": "^9.1.0",
44
+ "c8": "^10.1.3",
44
45
  "eslint": "^8.57.0",
45
46
  "eslint-config-prettier": "^9.1.0",
46
47
  "eslint-plugin-prettier": "^5.1.3",
@@ -53,7 +54,7 @@
53
54
  "@types/uuid": "^9.0.8",
54
55
  "better-sqlite3": "^12.1.1",
55
56
  "bson": "^6.10.4",
56
- "uuid": "^9.0.1"
57
+ "uuid": "^11.1.0"
57
58
  },
58
59
  "files": [
59
60
  "dist/**/*",
@@ -1,34 +0,0 @@
1
- /**
2
- * Utility functions for working with document objects
3
- */
4
- /**
5
- * Sets a nested value in an object using dot notation.
6
- * @param obj The object to modify.
7
- * @param path The dot notation path (e.g., 'user.profile.name').
8
- * @param value The value to set.
9
- */
10
- export declare function setNestedValue(obj: any, path: string, value: any): void;
11
- /**
12
- * Removes a nested property from an object using dot notation.
13
- * @param obj The object to modify.
14
- * @param path The dot notation path (e.g., 'user.profile.name').
15
- */
16
- export declare function unsetNestedValue(obj: any, path: string): void;
17
- /**
18
- * Gets a nested value from an object using dot notation.
19
- * @param obj The object to query.
20
- * @param path The dot notation path (e.g., 'user.profile.name').
21
- * @returns The value at the path, or undefined if the path doesn't exist.
22
- */
23
- export declare function getNestedValue(obj: any, path: string): any;
24
- /**
25
- * Generate a retry function with exponential backoff
26
- * @param operation The async operation to retry
27
- * @param operationDescription Description for logging
28
- * @param maxRetries Maximum number of retries
29
- * @param initialDelayMs Initial delay between retries (ms)
30
- * @param maxDelayMs Maximum delay between retries (ms)
31
- * @returns The result of the operation if successful
32
- * @throws The original error if all retries fail
33
- */
34
- export declare function retryWithBackoff<R>(operation: () => Promise<R>, operationDescription: string, maxRetries?: number, initialDelayMs?: number, maxDelayMs?: number): Promise<R>;
@@ -1,101 +0,0 @@
1
- /**
2
- * Utility functions for working with document objects
3
- */
4
- /**
5
- * Sets a nested value in an object using dot notation.
6
- * @param obj The object to modify.
7
- * @param path The dot notation path (e.g., 'user.profile.name').
8
- * @param value The value to set.
9
- */
10
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
11
- export function setNestedValue(obj, path, value) {
12
- const keys = path.split('.');
13
- let current = obj;
14
- // Navigate to the last parent in the path
15
- for (let i = 0; i < keys.length - 1; i++) {
16
- const key = keys[i];
17
- // Create nested objects if they don't exist
18
- if (current[key] === undefined || current[key] === null) {
19
- current[key] = {};
20
- }
21
- else if (typeof current[key] !== 'object') {
22
- // If the path exists but is not an object, make it an object
23
- current[key] = {};
24
- }
25
- current = current[key];
26
- }
27
- // Set the value at the final key
28
- current[keys[keys.length - 1]] = value;
29
- }
30
- /**
31
- * Removes a nested property from an object using dot notation.
32
- * @param obj The object to modify.
33
- * @param path The dot notation path (e.g., 'user.profile.name').
34
- */
35
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
36
- export function unsetNestedValue(obj, path) {
37
- const keys = path.split('.');
38
- let current = obj;
39
- // Navigate to the last parent in the path
40
- for (let i = 0; i < keys.length - 1; i++) {
41
- const key = keys[i];
42
- if (current[key] === undefined || current[key] === null) {
43
- // If the path doesn't exist, nothing to unset
44
- return;
45
- }
46
- current = current[key];
47
- }
48
- // Delete the property at the final key
49
- delete current[keys[keys.length - 1]];
50
- }
51
- /**
52
- * Gets a nested value from an object using dot notation.
53
- * @param obj The object to query.
54
- * @param path The dot notation path (e.g., 'user.profile.name').
55
- * @returns The value at the path, or undefined if the path doesn't exist.
56
- */
57
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
58
- export function getNestedValue(obj, path) {
59
- const keys = path.split('.');
60
- let current = obj;
61
- // Navigate through the path
62
- for (let i = 0; i < keys.length; i++) {
63
- const key = keys[i];
64
- if (current === undefined || current === null) {
65
- return undefined;
66
- }
67
- current = current[key];
68
- }
69
- return current;
70
- }
71
- /**
72
- * Generate a retry function with exponential backoff
73
- * @param operation The async operation to retry
74
- * @param operationDescription Description for logging
75
- * @param maxRetries Maximum number of retries
76
- * @param initialDelayMs Initial delay between retries (ms)
77
- * @param maxDelayMs Maximum delay between retries (ms)
78
- * @returns The result of the operation if successful
79
- * @throws The original error if all retries fail
80
- */
81
- export async function retryWithBackoff(operation, operationDescription, maxRetries = 5, initialDelayMs = 100, maxDelayMs = 10000) {
82
- let retries = 0;
83
- let delayMs = initialDelayMs;
84
- while (true) {
85
- try {
86
- return await operation();
87
- }
88
- catch (error) {
89
- if (error.code !== 'SQLITE_BUSY' || retries >= maxRetries) {
90
- throw error;
91
- }
92
- console.warn(`SQLITE_BUSY encountered during ${operationDescription}, retrying (${retries + 1}/${maxRetries})...`);
93
- // Wait for the backoff period
94
- await new Promise((resolve) => setTimeout(resolve, delayMs));
95
- // Increase delay for next retry (exponential backoff)
96
- delayMs = Math.min(delayMs * 2, maxDelayMs);
97
- retries++;
98
- }
99
- }
100
- }
101
- //# sourceMappingURL=document-utils.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"document-utils.js","sourceRoot":"","sources":["../src/document-utils.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;;;;GAKG;AACH,8DAA8D;AAC9D,MAAM,UAAU,cAAc,CAAC,GAAQ,EAAE,IAAY,EAAE,KAAU;IAC/D,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAC7B,IAAI,OAAO,GAAG,GAAG,CAAC;IAElB,0CAA0C;IAC1C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QACzC,MAAM,GAAG,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;QAEpB,4CAA4C;QAC5C,IAAI,OAAO,CAAC,GAAG,CAAC,KAAK,SAAS,IAAI,OAAO,CAAC,GAAG,CAAC,KAAK,IAAI,EAAE,CAAC;YACxD,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC;QACpB,CAAC;aAAM,IAAI,OAAO,OAAO,CAAC,GAAG,CAAC,KAAK,QAAQ,EAAE,CAAC;YAC5C,6DAA6D;YAC7D,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC;QACpB,CAAC;QAED,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC;IACzB,CAAC;IAED,iCAAiC;IACjC,OAAO,CAAC,IAAI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OAAO,CAAC,GAAG,CAAC,CAAC;IACzB,CAAC;IAED,OAAO,OAAO,CAAC;AACjB,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,gBAAgB,CACpC,SAA2B,EAC3B,oBAA4B,EAC5B,UAAU,GAAG,CAAC,EACd,cAAc,GAAG,GAAG,EACpB,UAAU,GAAG,KAAK;IAElB,IAAI,OAAO,GAAG,CAAC,CAAC;IAChB,IAAI,OAAO,GAAG,cAAc,CAAC;IAE7B,OAAO,IAAI,EAAE,CAAC;QACZ,IAAI,CAAC;YACH,OAAO,MAAM,SAAS,EAAE,CAAC;QAC3B,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAK,KAA+B,CAAC,IAAI,KAAK,aAAa,IAAI,OAAO,IAAI,UAAU,EAAE,CAAC;gBACrF,MAAM,KAAK,CAAC;YACd,CAAC;YAED,OAAO,CAAC,IAAI,CACV,kCAAkC,oBAAoB,eAAe,OAAO,GAAG,CAAC,IAAI,UAAU,MAAM,CACrG,CAAC;YAEF,8BAA8B;YAC9B,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC;YAE7D,sDAAsD;YACtD,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,OAAO,GAAG,CAAC,EAAE,UAAU,CAAC,CAAC;YAC5C,OAAO,EAAE,CAAC;QACZ,CAAC;IACH,CAAC;AACH,CAAC"}
@@ -1,51 +0,0 @@
1
- import { DocumentWithId, Filter, SortCriteria, Projection } from './types.js';
2
- import { SQLiteDB } from './db.js';
3
- /**
4
- * Represents a cursor for find operations, allowing chaining of limit, skip, and sort.
5
- */
6
- export declare class FindCursor<T extends DocumentWithId> {
7
- private db;
8
- private collectionName;
9
- private readonly options;
10
- private queryParts;
11
- private limitCount;
12
- private skipCount;
13
- private sortCriteria;
14
- private projectionFields;
15
- private queryBuilder;
16
- constructor(db: SQLiteDB, collectionName: string, initialFilter: Filter<T>, options?: {
17
- verbose?: boolean;
18
- });
19
- private buildSelectQuery;
20
- /**
21
- * Specifies the maximum number of documents the cursor will return.
22
- * @param count The number of documents to limit to.
23
- * @returns The `FindCursor` instance for chaining.
24
- */
25
- limit(count: number): this;
26
- /**
27
- * Specifies the number of documents to skip.
28
- * @param count The number of documents to skip.
29
- * @returns The `FindCursor` instance for chaining.
30
- */
31
- skip(count: number): this;
32
- /**
33
- * Specifies the sorting order for the documents.
34
- * @param sortCriteria An object defining sort order (e.g., `{ age: -1, name: 1 }`).
35
- * @returns The `FindCursor` instance for chaining.
36
- */
37
- sort(sortCriteria: SortCriteria<T>): this;
38
- /**
39
- * Specifies the fields to return (projection).
40
- * @param projection An object where keys are field names and values are 1 (include) or 0 (exclude).
41
- * `_id` is included by default unless explicitly excluded.
42
- * @returns The `FindCursor` instance for chaining.
43
- */
44
- project(projection: Projection<T>): this;
45
- private applyProjection;
46
- /**
47
- * Executes the query and returns all matching documents as an array.
48
- * @returns A promise that resolves to an array of documents.
49
- */
50
- toArray(): Promise<Partial<T>[]>;
51
- }
@@ -1,204 +0,0 @@
1
- import { QueryBuilder } from './query-builder.js';
2
- /**
3
- * Represents a cursor for find operations, allowing chaining of limit, skip, and sort.
4
- */
5
- export class FindCursor {
6
- constructor(db, collectionName, initialFilter, options = {}) {
7
- this.db = db;
8
- this.collectionName = collectionName;
9
- this.options = options;
10
- this.limitCount = null;
11
- this.skipCount = null;
12
- this.sortCriteria = null;
13
- this.projectionFields = null;
14
- this.queryBuilder = new QueryBuilder(options);
15
- this.queryParts = this.buildSelectQuery(initialFilter);
16
- }
17
- buildSelectQuery(filter) {
18
- const params = [];
19
- const whereClause = this.queryBuilder.buildWhereClause(filter, params);
20
- const sql = `SELECT _id, data FROM "${this.collectionName}" WHERE ${whereClause}`;
21
- if (this.options.verbose) {
22
- console.log(`SQL Query: ${sql}`);
23
- console.log(`Parameters: ${JSON.stringify(params)}`);
24
- }
25
- return { sql, params };
26
- }
27
- /**
28
- * Specifies the maximum number of documents the cursor will return.
29
- * @param count The number of documents to limit to.
30
- * @returns The `FindCursor` instance for chaining.
31
- */
32
- limit(count) {
33
- if (count < 0)
34
- throw new Error('Limit must be a non-negative number.');
35
- this.limitCount = count;
36
- return this;
37
- }
38
- /**
39
- * Specifies the number of documents to skip.
40
- * @param count The number of documents to skip.
41
- * @returns The `FindCursor` instance for chaining.
42
- */
43
- skip(count) {
44
- if (count < 0)
45
- throw new Error('Skip must be a non-negative number.');
46
- this.skipCount = count;
47
- return this;
48
- }
49
- /**
50
- * Specifies the sorting order for the documents.
51
- * @param sortCriteria An object defining sort order (e.g., `{ age: -1, name: 1 }`).
52
- * @returns The `FindCursor` instance for chaining.
53
- */
54
- sort(sortCriteria) {
55
- this.sortCriteria = sortCriteria;
56
- return this;
57
- }
58
- /**
59
- * Specifies the fields to return (projection).
60
- * @param projection An object where keys are field names and values are 1 (include) or 0 (exclude).
61
- * `_id` is included by default unless explicitly excluded.
62
- * @returns The `FindCursor` instance for chaining.
63
- */
64
- project(projection) {
65
- this.projectionFields = projection;
66
- return this;
67
- }
68
- applyProjection(doc) {
69
- if (!this.projectionFields)
70
- return doc;
71
- const projectedDoc = {};
72
- let includeMode = true; // true if any field is 1, false if any field is 0 (excluding _id)
73
- let hasExplicitInclusion = false;
74
- // Determine if it's an inclusion or exclusion projection
75
- for (const key in this.projectionFields) {
76
- if (key === '_id')
77
- continue;
78
- if (this.projectionFields[key] === 1 ||
79
- this.projectionFields[key] === true) {
80
- hasExplicitInclusion = true;
81
- break;
82
- }
83
- if (this.projectionFields[key] === 0 ||
84
- this.projectionFields[key] === false) {
85
- includeMode = false;
86
- // No break here, need to check all for explicit inclusions if _id is also 0
87
- }
88
- }
89
- if ((this.projectionFields._id === 0 || this.projectionFields._id === false) &&
90
- !hasExplicitInclusion) {
91
- // If _id is excluded and no other fields are explicitly included,
92
- // it's an exclusion projection where other fields are implicitly included.
93
- includeMode = false;
94
- }
95
- else if (hasExplicitInclusion) {
96
- includeMode = true;
97
- }
98
- if (includeMode) {
99
- // Inclusion mode
100
- for (const key in this.projectionFields) {
101
- if (this.projectionFields[key] === 1 ||
102
- this.projectionFields[key] === true) {
103
- if (key.includes('.')) {
104
- // Handle nested paths for inclusion (basic implementation)
105
- const path = key.split('.');
106
- let current = doc;
107
- let target = projectedDoc;
108
- // Navigate to the last parent in the path
109
- for (let i = 0; i < path.length - 1; i++) {
110
- const segment = path[i];
111
- if (current[segment] === undefined)
112
- break;
113
- if (target[segment] === undefined) {
114
- target[segment] = {};
115
- }
116
- current = current[segment];
117
- target = target[segment];
118
- }
119
- // Set the final property if we reached it
120
- const lastSegment = path[path.length - 1];
121
- if (current && current[lastSegment] !== undefined) {
122
- target[lastSegment] = current[lastSegment];
123
- }
124
- }
125
- else if (key in doc) {
126
- projectedDoc[key] = doc[key];
127
- }
128
- }
129
- }
130
- // _id is included by default in inclusion mode, unless explicitly excluded
131
- if (this.projectionFields._id !== 0 && this.projectionFields._id !== false && '_id' in doc) {
132
- projectedDoc._id = doc._id;
133
- }
134
- }
135
- else {
136
- // Exclusion mode
137
- Object.assign(projectedDoc, doc);
138
- for (const key in this.projectionFields) {
139
- if (this.projectionFields[key] === 0 ||
140
- this.projectionFields[key] === false) {
141
- if (key.includes('.')) {
142
- // Handle nested paths for exclusion (basic implementation)
143
- const path = key.split('.');
144
- let current = projectedDoc;
145
- // Navigate to the parent of the property to exclude
146
- for (let i = 0; i < path.length - 1; i++) {
147
- const segment = path[i];
148
- if (current[segment] === undefined)
149
- break;
150
- current = current[segment];
151
- }
152
- // Delete the final property if we reached its parent
153
- const lastSegment = path[path.length - 1];
154
- if (current && current[lastSegment] !== undefined) {
155
- delete current[lastSegment];
156
- }
157
- }
158
- else {
159
- delete projectedDoc[key];
160
- }
161
- }
162
- }
163
- }
164
- return projectedDoc;
165
- }
166
- /**
167
- * Executes the query and returns all matching documents as an array.
168
- * @returns A promise that resolves to an array of documents.
169
- */
170
- async toArray() {
171
- let finalSql = this.queryParts.sql;
172
- const finalParams = [...this.queryParts.params];
173
- if (this.sortCriteria) {
174
- const sortClauses = Object.entries(this.sortCriteria).map(([field, order]) => {
175
- if (field === '_id') {
176
- return `_id ${order === 1 ? 'ASC' : 'DESC'}`;
177
- }
178
- return `json_extract(data, ${this.queryBuilder.parseJsonPath(field)}) ${order === 1 ? 'ASC' : 'DESC'}`;
179
- });
180
- if (sortClauses.length > 0) {
181
- finalSql += ` ORDER BY ${sortClauses.join(', ')}`;
182
- }
183
- }
184
- if (this.limitCount !== null) {
185
- finalSql += ` LIMIT ?`;
186
- finalParams.push(this.limitCount);
187
- }
188
- if (this.skipCount !== null) {
189
- if (this.limitCount === null) {
190
- // SQLite requires a LIMIT if OFFSET is used.
191
- // Use a very large number if no limit is specified.
192
- finalSql += ` LIMIT -1`; // Or a large number like 999999999
193
- }
194
- finalSql += ` OFFSET ?`;
195
- finalParams.push(this.skipCount);
196
- }
197
- const rows = await this.db.all(finalSql, finalParams);
198
- return rows.map((row) => {
199
- const doc = { _id: row._id, ...JSON.parse(row.data) };
200
- return this.applyProjection(doc);
201
- });
202
- }
203
- }
204
- //# sourceMappingURL=find-cursor.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"find-cursor.js","sourceRoot":"","sources":["../src/find-cursor.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAElD;;GAEG;AACH,MAAM,OAAO,UAAU;IAWrB,YACU,EAAY,EACZ,cAAsB,EAC9B,aAAwB,EACP,UAAiC,EAAE;QAH5C,OAAE,GAAF,EAAE,CAAU;QACZ,mBAAc,GAAd,cAAc,CAAQ;QAEb,YAAO,GAAP,OAAO,CAA4B;QAV9C,eAAU,GAAkB,IAAI,CAAC;QACjC,cAAS,GAAkB,IAAI,CAAC;QAChC,iBAAY,GAA2B,IAAI,CAAC;QAC5C,qBAAgB,GAAyB,IAAI,CAAC;QASpD,IAAI,CAAC,YAAY,GAAG,IAA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6C;gBAC7C,oDAAoD;gBACpD,QAAQ,IAAI,WAAW,CAAC,CAAC,mCAAmC;YAC9D,CAAC;YACD,QAAQ,IAAI,WAAW,CAAC;YACxB,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QACnC,CAAC;QAOD,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,EAAE,CAAC,GAAG,CAAY,QAAQ,EAAE,WAAW,CAAC,CAAC;QACjE,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE;YACtB,MAAM,GAAG,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,EAAO,CAAC;YAC3D,OAAO,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,CAAC;QACnC,CAAC,CAAC,CAAC;IACL,CAAC;CACF"}
@@ -1,18 +0,0 @@
1
- import { Filter, DocumentWithId } from './types.js';
2
- export declare class QueryBuilder<T extends DocumentWithId> {
3
- private readonly options;
4
- constructor(options?: {
5
- verbose?: boolean;
6
- });
7
- /**
8
- * Converts a field path to a JSON path for SQLite's json_extract function.
9
- */
10
- parseJsonPath(path: string): string;
11
- /**
12
- * Builds a WHERE clause for a filter object.
13
- * @param filter The filter object.
14
- * @param params An array to collect query parameters.
15
- * @returns A SQL WHERE clause.
16
- */
17
- buildWhereClause(filter: Filter<T>, params: unknown[]): string;
18
- }