@medyll/idae-query 0.68.0 → 0.69.0
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/README.md +42 -148
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/resultSet/resultset.js +5 -17
- package/package.json +5 -2
package/README.md
CHANGED
|
@@ -1,184 +1,78 @@
|
|
|
1
|
-
# @medyll/idae-
|
|
1
|
+
# @medyll/idae-query
|
|
2
2
|
|
|
3
|
-
A powerful and flexible
|
|
3
|
+
A powerful and flexible query library for TypeScript and JavaScript applications.
|
|
4
4
|
|
|
5
5
|
## Features
|
|
6
6
|
|
|
7
|
-
-
|
|
8
|
-
-
|
|
9
|
-
-
|
|
10
|
-
-
|
|
11
|
-
- Flexible data modeling with automatic schema creation
|
|
12
|
-
- Built-in indexing and optimization features
|
|
13
|
-
- Easy integration with front-end frameworks, especially Svelte
|
|
14
|
-
- Robust error handling and logging
|
|
15
|
-
- Versioning and database migration support
|
|
16
|
-
- Support for svelte 5 state
|
|
7
|
+
- Chainable and iterable result sets
|
|
8
|
+
- Sorting, grouping, and pagination support
|
|
9
|
+
- Dot path resolution for nested properties
|
|
10
|
+
- Integration with `@medyll/idae-engine` for data operations
|
|
17
11
|
|
|
18
12
|
## Installation
|
|
19
13
|
|
|
20
14
|
```bash
|
|
21
|
-
npm install @medyll/idae-
|
|
15
|
+
npm install @medyll/idae-query
|
|
22
16
|
```
|
|
23
17
|
|
|
24
18
|
## Quick Start
|
|
25
19
|
|
|
26
|
-
|
|
27
|
-
import { createIdbqDb } from '@medyll/idae-idbql';
|
|
28
|
-
|
|
29
|
-
// Define your data model
|
|
30
|
-
const exampleModel = {
|
|
31
|
-
messages: {
|
|
32
|
-
keyPath: "++id, chatId, created_at",
|
|
33
|
-
ts: {} as ChatMessage,
|
|
34
|
-
},
|
|
35
|
-
chat: {
|
|
36
|
-
keyPath: "&chatId, created_at, dateLastMessage",
|
|
37
|
-
ts: {} as Chat,
|
|
38
|
-
template: {},
|
|
39
|
-
},
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
// Create a database instance
|
|
43
|
-
const idbqStore = createIdbqDb(exampleModel, 1);
|
|
44
|
-
const { idbql, idbqlState, idbDatabase, idbqModel } = idbqStore.create("myDatabase");
|
|
45
|
-
|
|
46
|
-
// Perform database operations
|
|
47
|
-
async function fetchMessages() {
|
|
48
|
-
const messages = await idbql.messages.where({ chatId: "123" }).toArray();
|
|
49
|
-
console.log(messages);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
fetchMessages();
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
## API Reference
|
|
56
|
-
|
|
57
|
-
### createIdbqDb(model, version)
|
|
58
|
-
|
|
59
|
-
Creates an IndexedDB database instance with the specified model and version.
|
|
60
|
-
|
|
61
|
-
### idbql
|
|
62
|
-
|
|
63
|
-
The main interface for database operations. Provides methods for each collection defined in your model.
|
|
64
|
-
|
|
65
|
-
### idbqlState
|
|
66
|
-
|
|
67
|
-
A reactive state object that reflects the current state of your database.
|
|
68
|
-
|
|
69
|
-
### idbDatabase
|
|
70
|
-
|
|
71
|
-
Provides low-level access to the IndexedDB instance.
|
|
72
|
-
|
|
73
|
-
### idbqModel
|
|
74
|
-
|
|
75
|
-
Contains the database model definition.
|
|
76
|
-
|
|
77
|
-
## Query Operations
|
|
20
|
+
Here's a quick example to get you started:
|
|
78
21
|
|
|
79
22
|
```typescript
|
|
80
|
-
|
|
81
|
-
await idbql.messages.add({ chatId: "123", content: "Hello" });
|
|
23
|
+
import { getResultset } from '@medyll/idae-query';
|
|
82
24
|
|
|
83
|
-
|
|
84
|
-
|
|
25
|
+
const data = [
|
|
26
|
+
{ id: 1, name: 'John', age: 25, metadata: { order: 1 } },
|
|
27
|
+
{ id: 2, name: 'Jane', age: 30, metadata: { order: 2 } },
|
|
28
|
+
{ id: 3, name: 'Bob', age: 35, metadata: { order: 3 } },
|
|
29
|
+
{ id: 4, name: 'Alice', age: 40, metadata: { order: 4 } },
|
|
30
|
+
];
|
|
85
31
|
|
|
86
|
-
|
|
87
|
-
await idbql.messages.delete(1);
|
|
32
|
+
const resultSet = getResultset(data);
|
|
88
33
|
|
|
89
|
-
//
|
|
90
|
-
const
|
|
91
|
-
.where({ created_at: { gt: new Date(Date.now() - 86400000) } })
|
|
92
|
-
.toArray();
|
|
93
|
-
```
|
|
94
|
-
|
|
95
|
-
## Transactions
|
|
34
|
+
// Sorting
|
|
35
|
+
const sortedData = resultSet.sortBy({ age: 'asc' });
|
|
96
36
|
|
|
97
|
-
|
|
37
|
+
// Grouping
|
|
38
|
+
const groupedData = resultSet.groupBy('age');
|
|
98
39
|
|
|
99
|
-
|
|
100
|
-
const
|
|
101
|
-
["users", "posts"],
|
|
102
|
-
"readwrite",
|
|
103
|
-
async (tx) => {
|
|
104
|
-
const userStore = tx.objectStore("users");
|
|
105
|
-
const postStore = tx.objectStore("posts");
|
|
106
|
-
|
|
107
|
-
const userId = await userStore.add({ name: "Alice", email: "alice@example.com" });
|
|
108
|
-
const postId = await postStore.add({ userId, title: "Alice's First Post", content: "Hello, World!" });
|
|
109
|
-
|
|
110
|
-
return { userId, postId };
|
|
111
|
-
}
|
|
112
|
-
);
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
## Reactive State Management
|
|
116
|
-
|
|
117
|
-
```typescript
|
|
118
|
-
import { derived } from 'svelte/store';
|
|
40
|
+
// Pagination
|
|
41
|
+
const pageData = resultSet.getPage(1, 2);
|
|
119
42
|
|
|
120
|
-
|
|
43
|
+
console.log(sortedData);
|
|
44
|
+
console.log(groupedData);
|
|
45
|
+
console.log(pageData);
|
|
121
46
|
```
|
|
122
47
|
|
|
123
|
-
##
|
|
48
|
+
## API
|
|
124
49
|
|
|
125
|
-
|
|
126
|
-
<script>
|
|
127
|
-
import { derived } from 'svelte/store';
|
|
128
|
-
import { idbqlState } from './store';
|
|
50
|
+
### `getResultset(data: any[]): ResultSet`
|
|
129
51
|
|
|
130
|
-
|
|
131
|
-
</script>
|
|
52
|
+
Creates a new result set from the provided data.
|
|
132
53
|
|
|
133
|
-
|
|
134
|
-
<p>{message.content}</p>
|
|
135
|
-
{/each}
|
|
136
|
-
```
|
|
54
|
+
### `ResultSet`
|
|
137
55
|
|
|
138
|
-
|
|
56
|
+
A chainable and iterable result set of data.
|
|
139
57
|
|
|
140
|
-
|
|
141
|
-
const idbqStore = createIdbqDb(myModel, 2);
|
|
142
|
-
const { idbDatabase } = idbqStore.create("myDb", {
|
|
143
|
-
upgrade(oldVersion, newVersion, transaction) {
|
|
144
|
-
if (oldVersion < 2) {
|
|
145
|
-
const userStore = transaction.objectStore("users");
|
|
146
|
-
userStore.createIndex("emailIndex", "email", { unique: true });
|
|
147
|
-
}
|
|
148
|
-
},
|
|
149
|
-
});
|
|
150
|
-
```
|
|
58
|
+
#### Methods
|
|
151
59
|
|
|
152
|
-
|
|
60
|
+
- `setOptions(options: ResultsetOptions): ResultSet` - Sets options for the result set.
|
|
61
|
+
- `sortBy(sortOptions: Record<DotPath, 'asc' | 'desc'>): ResultSet` - Sorts the result set by the specified options.
|
|
62
|
+
- `groupBy(groupBy: DotPath): Record<string, any[]>` - Groups the result set by the specified property.
|
|
63
|
+
- `getPage(page: number, pageSize: number): any[]` - Gets the specified page of data.
|
|
153
64
|
|
|
154
|
-
|
|
155
|
-
try {
|
|
156
|
-
await idbql.users.add({ username: "existing_user" });
|
|
157
|
-
} catch (error) {
|
|
158
|
-
if (error instanceof UniqueConstraintError) {
|
|
159
|
-
console.error("Username already exists");
|
|
160
|
-
} else {
|
|
161
|
-
console.error("An unexpected error occurred", error);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
```
|
|
165
|
-
|
|
166
|
-
## Performance Tips
|
|
65
|
+
## Testing
|
|
167
66
|
|
|
168
|
-
|
|
169
|
-
- Limit result sets with `.limit(n)`
|
|
170
|
-
- Use `.count()` instead of `.toArray().length`
|
|
171
|
-
- Optimize queries to use indexes effectively
|
|
67
|
+
To run the tests:
|
|
172
68
|
|
|
173
|
-
|
|
69
|
+
1. Clone the repository
|
|
70
|
+
2. Install dependencies: `npm install` or `yarn install`
|
|
71
|
+
3. Run tests: `npm test` or `yarn test`
|
|
174
72
|
|
|
175
|
-
|
|
73
|
+
The tests cover various scenarios for each method, ensuring the reliability and correctness of the `ResultSet` class.
|
|
176
74
|
|
|
177
75
|
## License
|
|
178
76
|
|
|
179
|
-
This project is licensed under the MIT License.
|
|
180
|
-
|
|
181
|
-
## Support
|
|
77
|
+
This project is licensed under the MIT License.
|
|
182
78
|
|
|
183
|
-
If you encounter any issues or have questions, please file an issue on the GitHub repository.
|
|
184
|
-
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -47,23 +47,11 @@ export function getResultset(data) {
|
|
|
47
47
|
},
|
|
48
48
|
groupBy: {
|
|
49
49
|
value: function (fieldName, keepUngroupedData = true) {
|
|
50
|
-
return dataOp.groupBy({
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
let key = "";
|
|
56
|
-
for (let i = 0; i < finalFieldName.length; i++) {
|
|
57
|
-
key += dotPath<typeof curr>(curr, finalFieldName[i]);
|
|
58
|
-
}
|
|
59
|
-
if (!acc[key]) {
|
|
60
|
-
acc[key] = [];
|
|
61
|
-
}
|
|
62
|
-
acc[key].push(curr);
|
|
63
|
-
return acc;
|
|
64
|
-
},
|
|
65
|
-
{},
|
|
66
|
-
); */
|
|
50
|
+
return dataOp.groupBy({
|
|
51
|
+
dataList: this,
|
|
52
|
+
groupBy: fieldName,
|
|
53
|
+
keepUngroupedData,
|
|
54
|
+
});
|
|
67
55
|
},
|
|
68
56
|
enumerable: true,
|
|
69
57
|
configurable: true,
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@medyll/idae-query",
|
|
3
3
|
"scope": "@medyll",
|
|
4
|
-
"version": "0.
|
|
5
|
-
"description": "A powerful and flexible
|
|
4
|
+
"version": "0.69.0",
|
|
5
|
+
"description": "A powerful and flexible query library for TypeScript and JavaScript applications, featuring a MongoDB-like query interface, strong TypeScript support, and easy integration with front-end frameworks.",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"dev": "vite dev",
|
|
8
8
|
"build": "vite build && npm run package",
|
|
@@ -28,6 +28,9 @@
|
|
|
28
28
|
"!dist/**/*.test.*",
|
|
29
29
|
"!dist/**/*.spec.*"
|
|
30
30
|
],
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@medyll/idae-engine": "next"
|
|
33
|
+
},
|
|
31
34
|
"peerDependencies": {
|
|
32
35
|
"svelte": "^5.0.0-next"
|
|
33
36
|
},
|