refactor: collection setup

This commit is contained in:
2026-01-03 00:44:59 +01:00
parent b9b12f790d
commit 7df5ed685d
30 changed files with 1056 additions and 1234 deletions

View File

@@ -1,5 +1,6 @@
import { describe, it } from "@std/testing/bdd";
import { expect } from "expect";
import z from "zod";
import { Collection } from "../src/collection.ts";
import { MemoryStorage } from "../src/databases/memory/storage.ts";
@@ -13,20 +14,28 @@ describe("Field Update Operators", () => {
* @see https://www.mongodb.com/docs/manual/reference/operator/update/set/#set-top-level-fields
*/
it("should set top level fields", async () => {
const collection = new Collection<{
quantity: number;
instock: boolean;
reorder: boolean;
details: {
model: string;
make: string;
};
tags: string[];
ratings: {
by: string;
rating: number;
}[];
}>("tests", new MemoryStorage("tests"));
const collection = new Collection({
name: "test",
adapter: new MemoryStorage("tests"),
primaryKey: "id" as const,
schema: {
id: z.string(),
quantity: z.number(),
instock: z.boolean(),
reorder: z.boolean(),
details: z.object({
model: z.string(),
make: z.string(),
}),
tags: z.array(z.string()),
ratings: z.array(
z.object({
by: z.string(),
rating: z.number(),
}),
),
},
});
await collection.insertOne({
id: "100",
@@ -73,20 +82,28 @@ describe("Field Update Operators", () => {
* @see https://www.mongodb.com/docs/manual/reference/operator/update/set/#set-fields-in-embedded-documents
*/
it("should set fields in the embedded documents", async () => {
const collection = new Collection<{
quantity: number;
instock: boolean;
reorder: boolean;
details: {
model: string;
make: string;
};
tags: string[];
ratings: {
by: string;
rating: number;
}[];
}>("tests", new MemoryStorage("tests"));
const collection = new Collection({
name: "test",
adapter: new MemoryStorage("tests"),
primaryKey: "id",
schema: {
id: z.string(),
quantity: z.number(),
instock: z.boolean(),
reorder: z.boolean(),
details: z.object({
model: z.string(),
make: z.string(),
}),
tags: z.array(z.string()),
ratings: z.array(
z.object({
by: z.string(),
rating: z.number(),
}),
),
},
});
await collection.insertOne({
id: "100",
@@ -131,20 +148,28 @@ describe("Field Update Operators", () => {
* @see https://www.mongodb.com/docs/manual/reference/operator/update/set/#set-elements-in-arrays
*/
it("should set elements in arrays", async () => {
const collection = new Collection<{
quantity: number;
instock: boolean;
reorder: boolean;
details: {
model: string;
make: string;
};
tags: string[];
ratings: {
by: string;
rating: number;
}[];
}>("tests", new MemoryStorage("tests"));
const collection = new Collection({
name: "test",
adapter: new MemoryStorage("tests"),
primaryKey: "id",
schema: {
id: z.string(),
quantity: z.number(),
instock: z.boolean(),
reorder: z.boolean(),
details: z.object({
model: z.string(),
make: z.string(),
}),
tags: z.array(z.string()),
ratings: z.array(
z.object({
by: z.string(),
rating: z.number(),
}),
),
},
});
await collection.insertOne({
id: "100",
@@ -189,12 +214,18 @@ describe("Field Update Operators", () => {
describe("$unset", () => {
it("should unset keys", async () => {
const collection = new Collection<{
item: string;
sku: string;
quantity: number;
instock: boolean;
}>("tests", new MemoryStorage("tests"));
const collection = new Collection({
name: "test",
adapter: new MemoryStorage("tests"),
primaryKey: "id",
schema: {
id: z.string(),
item: z.string(),
sku: z.string(),
quantity: z.number(),
instock: z.boolean(),
},
});
await collection.insertMany([
{ id: "1", item: "chisel", sku: "C001", quantity: 4, instock: true },
@@ -252,10 +283,20 @@ describe("Field Update Operators", () => {
describe("Array Update Operators", () => {
describe("$(update)", () => {
it("should replace a object in an array", async () => {
const collection = new Collection<{ grades: { id: string; value: number }[] }>(
"students",
new MemoryStorage("students"),
);
const collection = new Collection({
name: "test",
adapter: new MemoryStorage("tests"),
primaryKey: "id",
schema: {
id: z.string(),
grades: z.array(
z.object({
id: z.string(),
value: z.number(),
}),
),
},
});
await collection.insertOne({
id: "1",
@@ -319,7 +360,7 @@ describe("Array Update Operators", () => {
* @see https://www.mongodb.com/docs/manual/reference/operator/update/positional/#update-values-in-an-array
*/
// it("should update values in an array", async () => {
// const collection = new Collection<{ grades: number[] }>("students", new MemoryStorage("students"));
// const collection = new Collection<"id", { grades: number[] }>("students", new MemoryStorage("students", "id"));
// await collection.insertMany([
// { id: "1", grades: [85, 80, 80] },
@@ -358,9 +399,9 @@ describe("Array Update Operators", () => {
* @see https://www.mongodb.com/docs/manual/reference/operator/update/positional/#update-documents-in-an-array
*/
// it("should update documents in an array", async () => {
// const collection = new Collection<{ grades: { grade: number; mean: number; std: number }[] }>(
// const collection = new Collection<"id", { grades: { grade: number; mean: number; std: number }[] }>(
// "students",
// new MemoryStorage("students"),
// new MemoryStorage("students", "id"),
// );
// await collection.insertOne({
@@ -406,9 +447,9 @@ describe("Array Update Operators", () => {
* @see https://www.mongodb.com/docs/manual/reference/operator/update/positional/#update-embedded-documents-using-multiple-field-matches
*/
// it("should update embedded documents using multiple field matches", async () => {
// const collection = new Collection<{ grades: { grade: number; mean: number; std: number }[] }>(
// const collection = new Collection<"id", { grades: { grade: number; mean: number; std: number }[] }>(
// "students",
// new MemoryStorage("students"),
// new MemoryStorage("students", "id"),
// );
// await collection.insertOne({
@@ -464,10 +505,16 @@ describe("Array Update Operators", () => {
* @see https://www.mongodb.com/docs/manual/reference/operator/update/pull/#remove-all-items-that-equal-a-specified-value
*/
it("should remove all items that equal a specified value", async () => {
const collection = new Collection<{ fruits: string[]; vegetables: string[] }>(
"stores",
new MemoryStorage("stores"),
);
const collection = new Collection({
name: "test",
adapter: new MemoryStorage("tests"),
primaryKey: "id",
schema: {
id: z.string(),
fruits: z.array(z.string()),
vegetables: z.array(z.string()),
},
});
await collection.insertMany([
{
@@ -519,7 +566,15 @@ describe("Array Update Operators", () => {
* @see https://www.mongodb.com/docs/manual/reference/operator/update/pull/#remove-all-items-that-match-a-specified--pull-condition
*/
it("should remove all items that match a specific $pull condition", async () => {
const collection = new Collection<{ votes: number[] }>("profiles", new MemoryStorage("profiles"));
const collection = new Collection({
name: "test",
adapter: new MemoryStorage("tests"),
primaryKey: "id",
schema: {
id: z.string(),
votes: z.array(z.number()),
},
});
await collection.insertOne({ id: "1", votes: [3, 5, 6, 7, 7, 8] });
@@ -548,10 +603,20 @@ describe("Array Update Operators", () => {
* @see https://www.mongodb.com/docs/manual/reference/operator/update/pull/#remove-items-from-an-array-of-documents
*/
it("should remove items from an array of documents", async () => {
const collection = new Collection<{ results: { item: string; score: number }[] }>(
"surveys",
new MemoryStorage("surveys"),
);
const collection = new Collection({
name: "test",
adapter: new MemoryStorage("tests"),
primaryKey: "id",
schema: {
id: z.string(),
results: z.array(
z.object({
item: z.string(),
score: z.number(),
}),
),
},
});
await collection.insertMany([
{
@@ -596,9 +661,26 @@ describe("Array Update Operators", () => {
* @see https://www.mongodb.com/docs/manual/reference/operator/update/pull/#remove-documents-from-nested-arrays
*/
it("should remove documents from nested arrays", async () => {
const collection = new Collection<{
results: { item: string; score: number; answers: { q: number; a: number }[] }[];
}>("surveys", new MemoryStorage("surveys"));
const collection = new Collection({
name: "test",
adapter: new MemoryStorage("tests"),
primaryKey: "id",
schema: {
id: z.string(),
results: z.array(
z.object({
item: z.string(),
score: z.number(),
answers: z.array(
z.object({
q: z.number(),
a: z.number(),
}),
),
}),
),
},
});
await collection.insertMany([
{
@@ -699,7 +781,15 @@ describe("Array Update Operators", () => {
* @see https://www.mongodb.com/docs/manual/reference/operator/update/push/#append-a-value-to-an-array
*/
it("should append a value to an array", async () => {
const collection = new Collection<{ scores: number[] }>("students", new MemoryStorage("students"));
const collection = new Collection({
name: "test",
adapter: new MemoryStorage("tests"),
primaryKey: "id",
schema: {
id: z.string(),
scores: z.array(z.number()),
},
});
await collection.insertOne({ id: "1", scores: [44, 78, 38, 80] });
@@ -726,7 +816,15 @@ describe("Array Update Operators", () => {
* @see https://www.mongodb.com/docs/manual/reference/operator/update/push/#append-a-value-to-arrays-in-multiple-documents
*/
it("should append a value to arrays in multiple documents", async () => {
const collection = new Collection<{ scores: number[] }>("students", new MemoryStorage("students"));
const collection = new Collection({
name: "test",
adapter: new MemoryStorage("tests"),
primaryKey: "id",
schema: {
id: z.string(),
scores: z.array(z.number()),
},
});
await collection.insertMany([
{ id: "1", scores: [44, 78, 38, 80, 89] },
@@ -763,7 +861,16 @@ describe("Array Update Operators", () => {
* @see https://www.mongodb.com/docs/manual/reference/operator/update/push/#append-multiple-values-to-an-array
*/
it("should append multiple values to an array", async () => {
const collection = new Collection<{ name: string; scores: number[] }>("students", new MemoryStorage("students"));
const collection = new Collection({
name: "test",
adapter: new MemoryStorage("tests"),
primaryKey: "id",
schema: {
id: z.string(),
name: z.string(),
scores: z.array(z.number()),
},
});
await collection.insertOne({ id: "1", name: "Joe", scores: [44, 78] });
@@ -792,10 +899,20 @@ describe("Array Update Operators", () => {
* @see https://www.mongodb.com/docs/manual/reference/operator/update/push/#use--push-operator-with-multiple-modifiers
*/
it("should use $push operator with multiple modifiers", async () => {
const collection = new Collection<{ quizzes: { wk: number; score: number }[] }>(
"students",
new MemoryStorage("students"),
);
const collection = new Collection({
name: "test",
adapter: new MemoryStorage("tests"),
primaryKey: "id",
schema: {
id: z.string(),
quizzes: z.array(
z.object({
wk: z.number(),
score: z.number(),
}),
),
},
});
await collection.insertOne({
id: "5",
@@ -845,7 +962,15 @@ describe("Array Update Operators", () => {
* @see https://www.mongodb.com/docs/manual/reference/operator/update/slice/#slice-from-the-end-of-the-array
*/
it("should slice from the end of the array", async () => {
const collection = new Collection<{ scores: number[] }>("students", new MemoryStorage("students"));
const collection = new Collection({
name: "test",
adapter: new MemoryStorage("tests"),
primaryKey: "id",
schema: {
id: z.string(),
scores: z.array(z.number()),
},
});
await collection.insertOne({ id: "1", scores: [40, 50, 60] });
@@ -875,7 +1000,15 @@ describe("Array Update Operators", () => {
* @see https://www.mongodb.com/docs/manual/reference/operator/update/slice/#slice-from-the-front-of-the-array
*/
it("should slice from the front of the array", async () => {
const collection = new Collection<{ scores: number[] }>("students", new MemoryStorage("students"));
const collection = new Collection({
name: "test",
adapter: new MemoryStorage("tests"),
primaryKey: "id",
schema: {
id: z.string(),
scores: z.array(z.number()),
},
});
await collection.insertOne({ id: "2", scores: [89, 90] });
@@ -905,7 +1038,15 @@ describe("Array Update Operators", () => {
* @see https://www.mongodb.com/docs/manual/reference/operator/update/slice/#update-array-using-slice-only
*/
it("should update array using slice only", async () => {
const collection = new Collection<{ scores: number[] }>("students", new MemoryStorage("students"));
const collection = new Collection({
name: "test",
adapter: new MemoryStorage("tests"),
primaryKey: "id",
schema: {
id: z.string(),
scores: z.array(z.number()),
},
});
await collection.insertOne({ id: "3", scores: [89, 70, 100, 20] });
@@ -935,7 +1076,15 @@ describe("Array Update Operators", () => {
* @see https://www.mongodb.com/docs/manual/reference/operator/update/position/#add-elements-at-the-start-of-the-array
*/
it("should add elements to the start of the array", async () => {
const collection = new Collection<{ scores: number[] }>("students", new MemoryStorage("students"));
const collection = new Collection({
name: "test",
adapter: new MemoryStorage("tests"),
primaryKey: "id",
schema: {
id: z.string(),
scores: z.array(z.number()),
},
});
await collection.insertOne({ id: "1", scores: [100] });
@@ -965,7 +1114,15 @@ describe("Array Update Operators", () => {
* @see https://www.mongodb.com/docs/manual/reference/operator/update/position/#add-elements-to-the-middle-of-the-array
*/
it("should add elements to the middle of the array", async () => {
const collection = new Collection<{ scores: number[] }>("students", new MemoryStorage("students"));
const collection = new Collection({
name: "test",
adapter: new MemoryStorage("tests"),
primaryKey: "id",
schema: {
id: z.string(),
scores: z.array(z.number()),
},
});
await collection.insertOne({ id: "2", scores: [50, 60, 70, 100] });
@@ -992,7 +1149,15 @@ describe("Array Update Operators", () => {
});
it("should use a negative index to add elements to the array", async () => {
const collection = new Collection<{ scores: number[] }>("students", new MemoryStorage("students"));
const collection = new Collection({
name: "test",
adapter: new MemoryStorage("tests"),
primaryKey: "id",
schema: {
id: z.string(),
scores: z.array(z.number()),
},
});
await collection.insertOne({ id: "3", scores: [50, 60, 20, 30, 70, 100] });
@@ -1022,10 +1187,20 @@ describe("Array Update Operators", () => {
* @see https://www.mongodb.com/docs/manual/reference/operator/update/sort/#sort-array-of-documents-by-a-field-in-the-documents
*/
it("should sort array of documents by a field in the documents", async () => {
const collection = new Collection<{ quizzes: { id: number; score: number }[] }>(
"students",
new MemoryStorage("students"),
);
const collection = new Collection({
name: "test",
adapter: new MemoryStorage("tests"),
primaryKey: "id",
schema: {
id: z.string(),
quizzes: z.array(
z.object({
id: z.number(),
score: z.number(),
}),
),
},
});
await collection.insertOne({
id: "1",
@@ -1074,7 +1249,15 @@ describe("Array Update Operators", () => {
* @see https://www.mongodb.com/docs/manual/reference/operator/update/sort/#sort-array-elements-that-are-not-documents
*/
it("should sort array elements that are not documents", async () => {
const collection = new Collection<{ tests: number[] }>("students", new MemoryStorage("students"));
const collection = new Collection({
name: "test",
adapter: new MemoryStorage("tests"),
primaryKey: "id",
schema: {
id: z.string(),
tests: z.array(z.number()),
},
});
await collection.insertOne({ id: "2", tests: [89, 70, 89, 50] });
@@ -1106,7 +1289,15 @@ describe("Array Update Operators", () => {
* @see https://www.mongodb.com/docs/manual/reference/operator/update/sort/#update-array-using-sort-only
*/
it("should update array using sort only", async () => {
const collection = new Collection<{ tests: number[] }>("students", new MemoryStorage("students"));
const collection = new Collection({
name: "test",
adapter: new MemoryStorage("tests"),
primaryKey: "id",
schema: {
id: z.string(),
tests: z.array(z.number()),
},
});
await collection.insertOne({ id: "3", tests: [89, 70, 100, 20] });
@@ -1140,11 +1331,20 @@ describe("Array Update Operators", () => {
* @see https://www.mongodb.com/docs/manual/reference/operator/update/inc
*/
it("should increment and decrement values", async () => {
const collection = new Collection<{
sku: string;
quantity: number;
metrics: { orders: number; ratings: number };
}>("products", new MemoryStorage("products"));
const collection = new Collection({
name: "test",
adapter: new MemoryStorage("tests"),
primaryKey: "id",
schema: {
id: z.string(),
sku: z.string(),
quantity: z.number(),
metrics: z.object({
orders: z.number(),
ratings: z.number(),
}),
},
});
await collection.insertOne({ id: "1", sku: "abc123", quantity: 10, metrics: { orders: 2, ratings: 3.5 } });
@@ -1176,10 +1376,20 @@ describe("Array Update Operators", () => {
});
it("should increment value of an array element with array index", async () => {
const collection = new Collection<{ details: { id: number; quantity: number }[] }>(
"products",
new MemoryStorage("products"),
);
const collection = new Collection({
name: "test",
adapter: new MemoryStorage("tests"),
primaryKey: "id",
schema: {
id: z.string(),
details: z.array(
z.object({
id: z.number(),
quantity: z.number(),
}),
),
},
});
await collection.insertOne({
id: "3",