@anteros/core 0.0.23 → 0.0.24
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/package.json +1 -1
- package/tests/api.test.ts +50 -0
- package/utils/func.ts +9 -7
package/package.json
CHANGED
package/tests/api.test.ts
CHANGED
|
@@ -73,6 +73,56 @@ describe("find", () => {
|
|
|
73
73
|
});
|
|
74
74
|
});
|
|
75
75
|
|
|
76
|
+
describe("$limit", () => {
|
|
77
|
+
it("$limit: 0 means no limit (returns all matching docs)", async () => {
|
|
78
|
+
const marker = `limit-zero-${crypto.randomUUID()}`;
|
|
79
|
+
await rest.bulkWrite("items", Array.from({ length: 101 }, (_, i) => ({
|
|
80
|
+
insertOne: { document: { title: marker, count: i } },
|
|
81
|
+
})) as any);
|
|
82
|
+
|
|
83
|
+
const all = await rest.find("items", { $match: { title: marker }, $limit: 0 });
|
|
84
|
+
expect(all.length).toBe(101); // the old 100 cap would have returned 100
|
|
85
|
+
|
|
86
|
+
await rest.db.collection("items").deleteMany({ title: marker });
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("accepts $limit above 1000", async () => {
|
|
90
|
+
const marker = `limit-big-${crypto.randomUUID()}`;
|
|
91
|
+
await rest.insertMany("items", [
|
|
92
|
+
{ title: marker, count: 1 },
|
|
93
|
+
{ title: marker, count: 2 },
|
|
94
|
+
{ title: marker, count: 3 },
|
|
95
|
+
]);
|
|
96
|
+
|
|
97
|
+
const docs = await rest.find("items", { $match: { title: marker }, $limit: 5000 });
|
|
98
|
+
expect(docs.length).toBe(3);
|
|
99
|
+
|
|
100
|
+
await rest.db.collection("items").deleteMany({ title: marker });
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("defaults to $limit 100", async () => {
|
|
104
|
+
const marker = `limit-default-${crypto.randomUUID()}`;
|
|
105
|
+
await rest.bulkWrite("items", Array.from({ length: 120 }, (_, i) => ({
|
|
106
|
+
insertOne: { document: { title: marker, count: i } },
|
|
107
|
+
})) as any);
|
|
108
|
+
|
|
109
|
+
const docs = await rest.find("items", { $match: { title: marker } });
|
|
110
|
+
expect(docs.length).toBe(100);
|
|
111
|
+
|
|
112
|
+
await rest.db.collection("items").deleteMany({ title: marker });
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("rejects negative $limit with INVALID_LIMIT", async () => {
|
|
116
|
+
const err: any = await rest.find("items", { $limit: -1 }).then(() => null, (e) => e);
|
|
117
|
+
expect(err?.code).toBe("INVALID_LIMIT");
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("rejects non-integer $limit with INVALID_LIMIT", async () => {
|
|
121
|
+
const err: any = await rest.find("items", { $limit: 1.5 }).then(() => null, (e) => e);
|
|
122
|
+
expect(err?.code).toBe("INVALID_LIMIT");
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
|
|
76
126
|
describe("findOne", () => {
|
|
77
127
|
it("returns single doc by _id", async () => {
|
|
78
128
|
const doc = await rest.insertOne("items", { title: "single" });
|
package/utils/func.ts
CHANGED
|
@@ -748,18 +748,20 @@ function buildPipeline(p: FindOptions, options?: {
|
|
|
748
748
|
})
|
|
749
749
|
}
|
|
750
750
|
|
|
751
|
-
// $limit —
|
|
752
|
-
|
|
751
|
+
// $limit — 0 means "no limit" (the $limit stage is omitted);
|
|
752
|
+
// any positive safe integer is allowed (no 1000 cap)
|
|
753
753
|
p.$limit = p?.$limit ?? 100
|
|
754
|
-
if (!Number.isSafeInteger(p.$limit) || p.$limit
|
|
755
|
-
throw new AppError(`Invalid $limit (expected
|
|
754
|
+
if (!Number.isSafeInteger(p.$limit) || p.$limit < 0) {
|
|
755
|
+
throw new AppError(`Invalid $limit (expected a non-negative integer; 0 = no limit)`, {
|
|
756
756
|
code: 'INVALID_LIMIT',
|
|
757
757
|
status: 400
|
|
758
758
|
});
|
|
759
759
|
}
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
760
|
+
if (p.$limit > 0) {
|
|
761
|
+
pipeline.push({
|
|
762
|
+
$limit: p.$limit
|
|
763
|
+
})
|
|
764
|
+
}
|
|
763
765
|
|
|
764
766
|
// $include
|
|
765
767
|
if (p?.$include && Array.isArray(p?.$include)) {
|