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,11 +1,11 @@
{
"biome.enabled": true,
"deno.enable": true,
"deno.lint": false,
"editor.defaultFormatter": "biomejs.biome",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
},
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
"source.organizeImports.biome": "explicit",
"source.fixAll.biome": "explicit"
}
}

45
biome.json Normal file
View File

@@ -0,0 +1,45 @@
{
"formatter": {
"enabled": true,
"formatWithErrors": false,
"indentStyle": "space",
"indentWidth": 2,
"lineEnding": "lf",
"lineWidth": 120,
"attributePosition": "auto"
},
"linter": {
"enabled": true,
"rules": {
"recommended": true,
"suspicious": {
"noConfusingVoidType": "off",
"noExplicitAny": "off"
},
"complexity": {
"noBannedTypes": "off"
}
}
},
"assist": {
"enabled": true,
"actions": {
"source": {
"organizeImports": {
"level": "on",
"options": {
"groups": [
[":BUN:", ":NODE:"],
":BLANK_LINE:",
":PACKAGE:",
":BLANK_LINE:",
[":ALIAS:"],
":BLANK_LINE:",
":PATH:"
]
}
}
}
}
}
}

View File

@@ -1,6 +1,6 @@
{
"name": "@valkyr/db",
"version": "2.0.0",
"version": "2.1.0",
"exports": {
".": "./src/mod.ts"
},
@@ -8,12 +8,19 @@
"exclude": [".github", ".vscode", ".gitignore", "tests"]
},
"tasks": {
"check": "deno check ./src/mod.ts",
"lint": "npx eslint -c eslint.config.mjs --fix .",
"fmt": "npx prettier --write .",
"test": "deno test --allow-all",
"check": {
"command": "deno run -A npm:@biomejs/biome check --write ./src",
"description": "Format, lint, and organize imports of the entire project."
},
"test": {
"command": "deno test --allow-all",
"description": "Runs all defined tests across the entire project."
},
"test:publish": "deno publish --dry-run",
"ncu": "npx ncu -u -p npm"
"ncu": {
"command": "npx ncu -u -p npm",
"description": "Updates all the dependencies in package.json to their latest versions."
}
},
"compilerOptions": {
"lib": ["deno.window", "dom"]

797
deno.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -6,24 +6,22 @@
"bugs": "https://github.com/valkyrjs/db/issues",
"license": "MIT",
"dependencies": {
"bson": "6",
"dot-prop": "9",
"fast-equals": "5",
"idb": "8",
"mingo": "6",
"rfdc": "1",
"rxjs": "7"
"bson": "7.0.0",
"dot-prop": "10.1.0",
"fast-equals": "6.0.0",
"idb": "8.0.3",
"mingo": "7.1.1",
"rfdc": "1.4.1",
"rxjs": "7.8.2",
"zod": "4.3.4"
},
"devDependencies": {
"@std/async": "npm:@jsr/std__async@1",
"@std/assert": "npm:@jsr/std__assert@1",
"@std/testing": "npm:@jsr/std__testing@1",
"@valkyr/testcontainers": "npm:@jsr/valkyr__testcontainers@2",
"eslint": "9",
"eslint-plugin-simple-import-sort": "12",
"expect": "30",
"fake-indexeddb": "6",
"prettier": "3",
"typescript-eslint": "8"
"@biomejs/biome": "2.2.4",
"expect": "30.2.0",
"fake-indexeddb": "6.2.5"
}
}

View File

@@ -1,4 +1,4 @@
import type { Document, WithId } from "./types.ts";
import type { AnyObject } from "mingo/types";
export const BroadcastChannel =
globalThis.BroadcastChannel ??
@@ -8,16 +8,16 @@ export const BroadcastChannel =
close() {}
};
export type StorageBroadcast<TSchema extends Document = Document> =
export type StorageBroadcast =
| {
name: string;
type: "insertOne" | "updateOne";
data: WithId<TSchema>;
data: AnyObject;
}
| {
name: string;
type: "insertMany" | "updateMany" | "remove";
data: WithId<TSchema>[];
data: AnyObject[];
}
| {
name: string;

View File

@@ -1,18 +1,12 @@
import { UpdateOptions } from "mingo/core";
import { UpdateExpression } from "mingo/updater";
import { Observable, Subject, Subscription } from "rxjs";
import type { AnyObject, Criteria } from "mingo/types";
import type { Modifier } from "mingo/updater";
import { Observable, type Subject, type Subscription } from "rxjs";
import type z from "zod";
import type { ZodObject, ZodRawShape } from "zod";
import { observe, observeOne } from "./observe/mod.ts";
import {
ChangeEvent,
InsertManyResult,
InsertOneResult,
Options,
RemoveResult,
Storage,
UpdateResult,
} from "./storage/mod.ts";
import { Document, Filter, WithId } from "./types.ts";
import type { ChangeEvent, InsertResult, QueryOptions, Storage, UpdateResult } from "./storage/mod.ts";
import type { AnyDocument } from "./types.ts";
/*
|--------------------------------------------------------------------------------
@@ -20,59 +14,100 @@ import { Document, Filter, WithId } from "./types.ts";
|--------------------------------------------------------------------------------
*/
export class Collection<TSchema extends Document = Document> {
constructor(
readonly name: string,
readonly storage: Storage<TSchema>,
) {}
export class Collection<
TOptions extends AnyCollectionOptions = AnyCollectionOptions,
TAdapter extends Storage = TOptions["adapter"],
TPrimaryKey extends string = TOptions["primaryKey"],
TSchema extends AnyDocument = z.output<ZodObject<TOptions["schema"]>>,
> {
declare readonly $schema: TSchema;
constructor(readonly options: TOptions) {}
get observable(): {
change: Subject<ChangeEvent<TSchema>>;
change: Subject<ChangeEvent>;
flush: Subject<void>;
} {
return this.storage.observable;
}
get storage(): TAdapter {
return this.options.adapter;
}
/*
|--------------------------------------------------------------------------------
| Mutators
|--------------------------------------------------------------------------------
*/
async insertOne(document: Partial<WithId<TSchema>>): Promise<InsertOneResult> {
return this.storage.resolve().then((storage) => storage.insertOne(document));
async insertOne(values: TSchema | Omit<TSchema, TPrimaryKey>): Promise<InsertResult> {
return this.storage.resolve().then((storage) =>
storage.insertOne({
collection: this.options.name,
pkey: this.options.primaryKey,
values,
}),
);
}
async insertMany(documents: Partial<WithId<TSchema>>[]): Promise<InsertManyResult> {
return this.storage.resolve().then((storage) => storage.insertMany(documents));
async insertMany(values: (TSchema | Omit<TSchema, TPrimaryKey>)[]): Promise<InsertResult> {
return this.storage.resolve().then((storage) =>
storage.insertMany({
collection: this.options.name,
pkey: this.options.primaryKey,
values,
}),
);
}
async updateOne(
filter: Filter<WithId<TSchema>>,
expr: UpdateExpression,
arrayFilters?: Filter<WithId<TSchema>>[],
condition?: Filter<WithId<TSchema>>,
options?: UpdateOptions,
condition: Criteria<TSchema>,
modifier: Modifier<TSchema>,
arrayFilters?: AnyObject[],
): Promise<UpdateResult> {
return this.storage.resolve().then((storage) => storage.updateOne(filter, expr, arrayFilters, condition, options));
return this.storage.resolve().then((storage) =>
storage.updateOne({
collection: this.options.name,
pkey: this.options.primaryKey,
condition,
modifier,
arrayFilters,
}),
);
}
async updateMany(
filter: Filter<WithId<TSchema>>,
expr: UpdateExpression,
arrayFilters?: Filter<WithId<TSchema>>[],
condition?: Filter<WithId<TSchema>>,
options?: UpdateOptions,
condition: Criteria<TSchema>,
modifier: Modifier<TSchema>,
arrayFilters?: AnyObject[],
): Promise<UpdateResult> {
return this.storage.resolve().then((storage) => storage.updateMany(filter, expr, arrayFilters, condition, options));
return this.storage.resolve().then((storage) =>
storage.updateMany({
collection: this.options.name,
pkey: this.options.primaryKey,
condition,
modifier,
arrayFilters,
}),
);
}
async replaceOne(filter: Filter<WithId<TSchema>>, document: TSchema): Promise<UpdateResult> {
return this.storage.resolve().then((storage) => storage.replace(filter, document));
async replaceOne(condition: Criteria<TSchema>, document: TSchema): Promise<UpdateResult> {
return this.storage.resolve().then((storage) =>
storage.replace({
collection: this.options.name,
pkey: this.options.primaryKey,
condition,
document,
}),
);
}
async remove(filter: Filter<WithId<TSchema>>): Promise<RemoveResult> {
return this.storage.resolve().then((storage) => storage.remove(filter));
async remove(condition: Criteria<TSchema>): Promise<number> {
return this.storage
.resolve()
.then((storage) => storage.remove({ collection: this.options.name, pkey: this.options.primaryKey, condition }));
}
/*
@@ -82,37 +117,37 @@ export class Collection<TSchema extends Document = Document> {
*/
subscribe(
filter?: Filter<WithId<TSchema>>,
condition?: Criteria<TSchema>,
options?: SubscribeToSingle,
next?: (document: WithId<TSchema> | undefined) => void,
next?: (document: TSchema | undefined) => void,
): Subscription;
subscribe(
filter?: Filter<WithId<TSchema>>,
condition?: Criteria<TSchema>,
options?: SubscribeToMany,
next?: (documents: WithId<TSchema>[], changed: WithId<TSchema>[], type: ChangeEvent["type"]) => void,
next?: (documents: TSchema[], changed: TSchema[], type: ChangeEvent["type"]) => void,
): Subscription;
subscribe(filter: Filter<WithId<TSchema>> = {}, options?: Options, next?: (...args: any[]) => void): Subscription {
subscribe(condition: Criteria<TSchema> = {}, options?: QueryOptions, next?: (...args: any[]) => void): Subscription {
if (options?.limit === 1) {
return this.#observeOne(filter).subscribe({ next });
return this.#observeOne(condition).subscribe({ next });
}
return this.#observe(filter, options).subscribe({
next: (value: [WithId<TSchema>[], WithId<TSchema>[], ChangeEvent["type"]]) => next?.(...value),
return this.#observe(condition, options).subscribe({
next: (value: [TSchema[], TSchema[], ChangeEvent["type"]]) => next?.(...value),
});
}
#observe(
filter: Filter<WithId<TSchema>> = {},
options?: Options,
): Observable<[WithId<TSchema>[], WithId<TSchema>[], ChangeEvent["type"]]> {
return new Observable<[WithId<TSchema>[], WithId<TSchema>[], ChangeEvent["type"]]>((subscriber) => {
filter: Criteria<TSchema> = {},
options?: QueryOptions,
): Observable<[TSchema[], TSchema[], ChangeEvent["type"]]> {
return new Observable<[TSchema[], TSchema[], ChangeEvent["type"]]>((subscriber) => {
return observe(this as any, filter, options, (values, changed, type) =>
subscriber.next([values, changed, type] as any),
);
});
}
#observeOne(filter: Filter<WithId<TSchema>> = {}): Observable<WithId<TSchema> | undefined> {
return new Observable<WithId<TSchema> | undefined>((subscriber) => {
#observeOne(filter: Criteria<TSchema> = {}): Observable<TSchema | undefined> {
return new Observable<TSchema | undefined>((subscriber) => {
return observeOne(this as any, filter, (values) => subscriber.next(values as any));
});
}
@@ -126,32 +161,34 @@ export class Collection<TSchema extends Document = Document> {
/**
* Retrieve a record by the document 'id' key.
*/
async findById(id: string): Promise<WithId<TSchema> | undefined> {
return this.storage.resolve().then((storage) => storage.findById(id));
async findById(id: string): Promise<TSchema | undefined> {
return this.storage.resolve().then((storage) => storage.findById({ collection: this.options.name, id }));
}
/**
* Performs a mingo filter search over the collection data and returns
* a single document if one was found matching the filter and options.
*/
async findOne(filter: Filter<WithId<TSchema>> = {}, options?: Options): Promise<WithId<TSchema> | undefined> {
return this.find(filter, options).then(([document]) => document);
async findOne(condition: Criteria<TSchema> = {}, options?: QueryOptions): Promise<TSchema | undefined> {
return this.find(condition, options).then(([document]) => document);
}
/**
* Performs a mingo filter search over the collection data and returns any
* documents matching the provided filter and options.
*/
async find(filter: Filter<WithId<TSchema>> = {}, options?: Options): Promise<WithId<TSchema>[]> {
return this.storage.resolve().then((storage) => storage.find(filter, options));
async find(condition: Criteria<TSchema> = {}, options?: QueryOptions): Promise<TSchema[]> {
return this.storage
.resolve()
.then((storage) => storage.find({ collection: this.options.name, condition, options }));
}
/**
* Performs a mingo filter search over the collection data and returns
* the count of all documents found matching the filter and options.
*/
async count(filter?: Filter<WithId<TSchema>>): Promise<number> {
return this.storage.resolve().then((storage) => storage.count(filter));
async count(condition?: Criteria<TSchema>): Promise<number> {
return this.storage.resolve().then((storage) => storage.count({ collection: this.options.name, condition }));
}
/**
@@ -172,18 +209,32 @@ export class Collection<TSchema extends Document = Document> {
*/
export type SubscriptionOptions = {
sort?: Options["sort"];
skip?: Options["skip"];
range?: Options["range"];
offset?: Options["offset"];
limit?: Options["limit"];
index?: Options["index"];
sort?: QueryOptions["sort"];
skip?: QueryOptions["skip"];
range?: QueryOptions["range"];
offset?: QueryOptions["offset"];
limit?: QueryOptions["limit"];
index?: QueryOptions["index"];
};
export type SubscribeToSingle = Options & {
export type SubscribeToSingle = QueryOptions & {
limit: 1;
};
export type SubscribeToMany = Options & {
export type SubscribeToMany = QueryOptions & {
limit?: number;
};
type AnyCollectionOptions = CollectionOptions<any, any, any, any>;
type CollectionOptions<
TName extends string,
TAdapter extends Storage,
TPrimaryKey extends string | number | symbol,
TSchema extends ZodRawShape,
> = {
name: TName;
adapter: TAdapter;
primaryKey: TPrimaryKey;
schema: TSchema;
};

View File

@@ -1,16 +1,16 @@
import { hashCodeQuery } from "../../hash.ts";
import { Options } from "../../storage/mod.ts";
import type { Document, Filter, WithId } from "../../types.ts";
import type { QueryOptions } from "../../storage/mod.ts";
import type { Document, Filter } from "../../types.ts";
export class IndexedDbCache<TSchema extends Document = Document> {
export class IndexedDBCache<TSchema extends Document = Document> {
readonly #cache = new Map<number, string[]>();
readonly #documents = new Map<string, WithId<TSchema>>();
readonly #documents = new Map<string, TSchema>();
hash(filter: Filter<WithId<TSchema>>, options: Options): number {
hash(filter: Filter<TSchema>, options: QueryOptions): number {
return hashCodeQuery(filter, options);
}
set(hashCode: number, documents: WithId<TSchema>[]) {
set(hashCode: number, documents: TSchema[]) {
this.#cache.set(
hashCode,
documents.map((document) => document.id),
@@ -20,10 +20,10 @@ export class IndexedDbCache<TSchema extends Document = Document> {
}
}
get(hashCode: number): WithId<TSchema>[] | undefined {
get(hashCode: number): TSchema[] | undefined {
const ids = this.#cache.get(hashCode);
if (ids !== undefined) {
return ids.map((id) => this.#documents.get(id) as WithId<TSchema>);
return ids.map((id) => this.#documents.get(id) as TSchema);
}
}

View File

@@ -1,29 +1,32 @@
import { IDBPDatabase, openDB } from "idb";
import { type IDBPDatabase, openDB } from "idb";
import { Collection } from "../../collection.ts";
import { DBLogger } from "../../logger.ts";
import { Document } from "../../types.ts";
import { Registrars } from "../registrars.ts";
import { IndexedDbStorage } from "./storage.ts";
import type { DBLogger } from "../../logger.ts";
import type { Document } from "../../types.ts";
import type { Registrars } from "../registrars.ts";
import { IndexedDBStorage } from "./storage.ts";
export class IndexedDatabase<TCollections extends StringRecord<Document>> {
export class IndexedDB<TCollections extends StringRecord<Document>> {
readonly #collections = new Map<keyof TCollections, Collection<TCollections[keyof TCollections]>>();
readonly #db: Promise<IDBPDatabase<unknown>>;
constructor(readonly options: Options) {
this.#db = openDB(options.name, options.version ?? 1, {
upgrade: (db: IDBPDatabase) => {
for (const { name, indexes = [] } of options.registrars) {
const store = db.createObjectStore(name as string, { keyPath: "id" });
store.createIndex("id", "id", { unique: true });
for (const { name, primaryKey = "id", indexes = [] } of options.registrars) {
const store = db.createObjectStore(name as string, { keyPath: primaryKey });
store.createIndex(primaryKey, primaryKey, { unique: true });
for (const [keyPath, options] of indexes) {
store.createIndex(keyPath, keyPath, options);
}
}
},
});
for (const { name } of options.registrars) {
this.#collections.set(name, new Collection(name, new IndexedDbStorage(name, this.#db, options.log ?? log)));
for (const { name, primaryKey = "id" } of options.registrars) {
this.#collections.set(
name,
new Collection(name, new IndexedDBStorage(name, primaryKey, this.#db, options.log ?? log)),
);
}
}
@@ -33,9 +36,7 @@ export class IndexedDatabase<TCollections extends StringRecord<Document>> {
|--------------------------------------------------------------------------------
*/
collection<TSchema extends TCollections[Name], Name extends keyof TCollections = keyof TCollections>(
name: Name,
): Collection<TSchema> {
collection<Name extends keyof TCollections = keyof TCollections>(name: Name) {
const collection = this.#collections.get(name);
if (collection === undefined) {
throw new Error(`Collection '${name as string}' not found`);

View File

@@ -1,9 +1,10 @@
import { IDBPDatabase } from "idb";
import { createUpdater, Query } from "mingo";
import { UpdateOptions } from "mingo/core";
import { UpdateExpression } from "mingo/updater";
import type { IDBPDatabase } from "idb";
import { Query, update } from "mingo";
import type { Criteria, Options } from "mingo/types";
import type { CloneMode, Modifier } from "mingo/updater";
import { DBLogger, InsertLog, QueryLog, RemoveLog, ReplaceLog, UpdateLog } from "../../logger.ts";
import { type DBLogger, InsertLog, QueryLog, RemoveLog, ReplaceLog, UpdateLog } from "../../logger.ts";
import { getDocumentWithPrimaryKey } from "../../primary-key.ts";
import { DuplicateDocumentError } from "../../storage/errors.ts";
import {
getInsertManyResult,
@@ -13,17 +14,18 @@ import {
} from "../../storage/operators/insert.ts";
import { RemoveResult } from "../../storage/operators/remove.ts";
import { UpdateResult } from "../../storage/operators/update.ts";
import { addOptions, Index, Options, Storage } from "../../storage/storage.ts";
import type { Document, Filter, WithId } from "../../types.ts";
import { IndexedDbCache } from "./cache.ts";
import { addOptions, type Index, type QueryOptions, Storage } from "../../storage/storage.ts";
import type { Document, Filter } from "../../types.ts";
import { IndexedDBCache } from "./cache.ts";
const OBJECT_PROTOTYPE = Object.getPrototypeOf({});
const OBJECT_TAG = "[object Object]";
const update = createUpdater({ cloneMode: "deep" });
export class IndexedDbStorage<TSchema extends Document = Document> extends Storage<TSchema> {
readonly #cache = new IndexedDbCache<TSchema>();
export class IndexedDBStorage<TPrimaryKey extends string, TSchema extends Document = Document> extends Storage<
TPrimaryKey,
TSchema
> {
readonly #cache = new IndexedDBCache<TSchema>();
readonly #promise: Promise<IDBPDatabase>;
@@ -31,10 +33,11 @@ export class IndexedDbStorage<TSchema extends Document = Document> extends Stora
constructor(
name: string,
primaryKey: TPrimaryKey,
promise: Promise<IDBPDatabase>,
readonly log: DBLogger,
) {
super(name);
super(name, primaryKey);
this.#promise = promise;
}
@@ -66,11 +69,12 @@ export class IndexedDbStorage<TSchema extends Document = Document> extends Stora
|--------------------------------------------------------------------------------
*/
async insertOne(data: Partial<TSchema>): Promise<InsertOneResult> {
async insertOne(values: TSchema | Omit<TSchema, TPrimaryKey>): Promise<InsertOneResult> {
const logger = new InsertLog(this.name);
const document = { ...data, id: data.id ?? crypto.randomUUID() } as any;
if (await this.has(document.id)) {
const document = getDocumentWithPrimaryKey(this.primaryKey, values);
if (await this.has(document[this.primaryKey])) {
throw new DuplicateDocumentError(document, this as any);
}
await this.db.transaction(this.name, "readwrite", { durability: "relaxed" }).store.add(document);
@@ -83,15 +87,15 @@ export class IndexedDbStorage<TSchema extends Document = Document> extends Stora
return getInsertOneResult(document);
}
async insertMany(data: Partial<TSchema>[]): Promise<InsertManyResult> {
async insertMany(values: (TSchema | Omit<TSchema, TPrimaryKey>)[]): Promise<InsertManyResult> {
const logger = new InsertLog(this.name);
const documents: WithId<TSchema>[] = [];
const documents: TSchema[] = [];
const tx = this.db.transaction(this.name, "readwrite", { durability: "relaxed" });
await Promise.all(
data.map((data) => {
const document = { ...data, id: data.id ?? crypto.randomUUID() } as WithId<TSchema>;
values.map((values) => {
const document = getDocumentWithPrimaryKey(this.primaryKey, values);
documents.push(document);
return tx.store.add(document);
}),
@@ -112,11 +116,11 @@ export class IndexedDbStorage<TSchema extends Document = Document> extends Stora
|--------------------------------------------------------------------------------
*/
async findById(id: string): Promise<WithId<TSchema> | undefined> {
async findById(id: string): Promise<TSchema | undefined> {
return this.db.getFromIndex(this.name, "id", id);
}
async find(filter: Filter<WithId<TSchema>>, options: Options = {}): Promise<WithId<TSchema>[]> {
async find(filter: Filter<TSchema>, options: QueryOptions = {}): Promise<TSchema[]> {
const logger = new QueryLog(this.name, { filter, options });
const hashCode = this.#cache.hash(filter, options);
@@ -132,7 +136,7 @@ export class IndexedDbStorage<TSchema extends Document = Document> extends Stora
cursor = addOptions(cursor, options);
}
const documents = cursor.all() as WithId<TSchema>[];
const documents = cursor.all() as TSchema[];
this.#cache.set(this.#cache.hash(filter, options), documents);
this.log(logger.result());
@@ -151,8 +155,8 @@ export class IndexedDbStorage<TSchema extends Document = Document> extends Stora
if (indexNames.contains(key) === true) {
let val: any;
if (isObject(filter[key]) === true) {
if ((filter as any)[key]["$in"] !== undefined) {
val = (filter as any)[key]["$in"];
if ((filter as any)[key].$in !== undefined) {
val = (filter as any)[key].$in;
}
} else {
val = filter[key];
@@ -168,7 +172,7 @@ export class IndexedDbStorage<TSchema extends Document = Document> extends Stora
return {};
}
async #getAll({ index, offset, range, limit }: Options) {
async #getAll({ index, offset, range, limit }: QueryOptions) {
if (index !== undefined) {
return this.#getAllByIndex(index);
}
@@ -230,34 +234,34 @@ export class IndexedDbStorage<TSchema extends Document = Document> extends Stora
*/
async updateOne(
filter: Filter<WithId<TSchema>>,
expr: UpdateExpression,
arrayFilters?: Filter<WithId<TSchema>>[],
condition?: Filter<WithId<TSchema>>,
options?: UpdateOptions,
filter: Filter<TSchema>,
modifier: Modifier<TSchema>,
arrayFilters?: Filter<TSchema>[],
condition?: Criteria<TSchema>,
options: { cloneMode?: CloneMode; queryOptions?: Partial<Options> } = { cloneMode: "deep" },
): Promise<UpdateResult> {
if (typeof filter.id === "string") {
return this.#update(filter.id, expr, arrayFilters, condition, options);
return this.#update(filter.id, modifier, arrayFilters, condition, options);
}
const documents = await this.find(filter);
if (documents.length > 0) {
return this.#update(documents[0].id, expr, arrayFilters, condition, options);
return this.#update(documents[0].id, modifier, arrayFilters, condition, options);
}
return new UpdateResult(0, 0);
}
async updateMany(
filter: Filter<WithId<TSchema>>,
expr: UpdateExpression,
arrayFilters?: Filter<WithId<TSchema>>[],
condition?: Filter<WithId<TSchema>>,
options?: UpdateOptions,
filter: Filter<TSchema>,
modifier: Modifier<TSchema>,
arrayFilters?: Filter<TSchema>[],
condition?: Criteria<TSchema>,
options: { cloneMode?: CloneMode; queryOptions?: Partial<Options> } = { cloneMode: "deep" },
): Promise<UpdateResult> {
const logger = new UpdateLog(this.name, { filter, expr, arrayFilters, condition, options });
const logger = new UpdateLog(this.name, { filter, modifier, arrayFilters, condition, options });
const ids = await this.find(filter).then((data) => data.map((d) => d.id));
const documents: WithId<TSchema>[] = [];
const documents: TSchema[] = [];
let modifiedCount = 0;
const tx = this.db.transaction(this.name, "readwrite", { durability: "relaxed" });
@@ -267,7 +271,7 @@ export class IndexedDbStorage<TSchema extends Document = Document> extends Stora
if (current === undefined) {
return;
}
const modified = update(current, expr, arrayFilters, condition, options);
const modified = update(current, modifier, arrayFilters, condition, options);
if (modified.length > 0) {
modifiedCount += 1;
documents.push(current);
@@ -287,12 +291,12 @@ export class IndexedDbStorage<TSchema extends Document = Document> extends Stora
return new UpdateResult(ids.length, modifiedCount);
}
async replace(filter: Filter<WithId<TSchema>>, document: WithId<TSchema>): Promise<UpdateResult> {
async replace(filter: Filter<TSchema>, document: TSchema): Promise<UpdateResult> {
const logger = new ReplaceLog(this.name, document);
const ids = await this.find(filter).then((data) => data.map((d) => d.id));
const documents: WithId<TSchema>[] = [];
const documents: TSchema[] = [];
const count = ids.length;
const tx = this.db.transaction(this.name, "readwrite", { durability: "relaxed" });
@@ -315,12 +319,12 @@ export class IndexedDbStorage<TSchema extends Document = Document> extends Stora
async #update(
id: string | number,
expr: UpdateExpression,
arrayFilters?: Filter<WithId<TSchema>>[],
condition?: Filter<WithId<TSchema>>,
options?: UpdateOptions,
modifier: Modifier<TSchema>,
arrayFilters?: Filter<TSchema>[],
condition?: Criteria<TSchema>,
options: { cloneMode?: CloneMode; queryOptions?: Partial<Options> } = { cloneMode: "deep" },
): Promise<UpdateResult> {
const logger = new UpdateLog(this.name, { id, expr });
const logger = new UpdateLog(this.name, { id, modifier });
const tx = this.db.transaction(this.name, "readwrite", { durability: "relaxed" });
@@ -330,7 +334,7 @@ export class IndexedDbStorage<TSchema extends Document = Document> extends Stora
return new UpdateResult(0, 0);
}
const modified = await update(current, expr, arrayFilters, condition, options);
const modified = await update(current, modifier, arrayFilters, condition, options);
if (modified.length > 0) {
await tx.store.put(current);
}
@@ -352,7 +356,7 @@ export class IndexedDbStorage<TSchema extends Document = Document> extends Stora
|--------------------------------------------------------------------------------
*/
async remove(filter: Filter<WithId<TSchema>>): Promise<RemoveResult> {
async remove(filter: Filter<TSchema>): Promise<RemoveResult> {
const logger = new RemoveLog(this.name, { filter });
const documents = await this.find(filter);
@@ -375,7 +379,7 @@ export class IndexedDbStorage<TSchema extends Document = Document> extends Stora
|--------------------------------------------------------------------------------
*/
async count(filter?: Filter<WithId<TSchema>>): Promise<number> {
async count(filter?: Filter<TSchema>): Promise<number> {
if (filter !== undefined) {
return (await this.find(filter)).length;
}

View File

@@ -1,6 +1,6 @@
import { Collection } from "../../collection.ts";
import { Document } from "../../types.ts";
import { Registrars } from "../registrars.ts";
import type { Document } from "../../types.ts";
import type { Registrars } from "../registrars.ts";
import { MemoryStorage } from "./storage.ts";
type Options = {

View File

@@ -1,145 +1,155 @@
import { createUpdater, Query } from "mingo";
import { UpdateOptions } from "mingo/core";
import { UpdateExpression } from "mingo/updater";
import { Query, update } from "mingo";
import type { AnyObject } from "mingo/types";
import { DuplicateDocumentError } from "../../storage/errors.ts";
import { getDocumentWithPrimaryKey } from "../../primary-key.ts";
import { Collections } from "../../storage/collections.ts";
import type { UpdatePayload } from "../../storage/mod.ts";
import type { InsertResult } from "../../storage/operators/insert.ts";
import type { UpdateResult } from "../../storage/operators/update.ts";
import {
getInsertManyResult,
getInsertOneResult,
type InsertManyResult,
type InsertOneResult,
} from "../../storage/operators/insert.ts";
import { RemoveResult } from "../../storage/operators/remove.ts";
import { UpdateResult } from "../../storage/operators/update.ts";
import { addOptions, Options, Storage } from "../../storage/storage.ts";
import type { Document, Filter, WithId } from "../../types.ts";
addOptions,
type CountPayload,
type FindByIdPayload,
type FindPayload,
type InsertManyPayload,
type InsertOnePayload,
type RemovePayload,
type ReplacePayload,
Storage,
} from "../../storage/storage.ts";
import type { AnyDocument } from "../../types.ts";
const update = createUpdater({ cloneMode: "deep" });
export class MemoryStorage<TSchema extends Document = Document> extends Storage<TSchema> {
readonly #documents = new Map<string, WithId<TSchema>>();
export class MemoryStorage extends Storage {
readonly #collections = new Collections();
async resolve() {
return this;
}
async has(id: string): Promise<boolean> {
return this.#documents.has(id);
}
async insertOne({ pkey, values, ...payload }: InsertOnePayload): Promise<InsertResult> {
const collection = this.#collections.get(payload.collection);
async insertOne(data: Partial<TSchema>): Promise<InsertOneResult> {
const document = { ...data, id: data.id ?? crypto.randomUUID() } as WithId<TSchema>;
if (await this.has(document.id)) {
throw new DuplicateDocumentError(document, this as any);
const document = getDocumentWithPrimaryKey(pkey, values);
if (collection.has(document[pkey])) {
return { insertCount: 0, insertIds: [] };
}
this.#documents.set(document.id, document);
collection.set(document[pkey], document);
this.broadcast("insertOne", document);
return getInsertOneResult(document);
return { insertCount: 1, insertIds: [document[pkey]] };
}
async insertMany(documents: Partial<TSchema>[]): Promise<InsertManyResult> {
const result: TSchema[] = [];
for (const data of documents) {
const document = { ...data, id: data.id ?? crypto.randomUUID() } as WithId<TSchema>;
result.push(document);
this.#documents.set(document.id, document);
async insertMany({ pkey, values, ...payload }: InsertManyPayload): Promise<InsertResult> {
const collection = this.#collections.get(payload.collection);
const documents: AnyDocument[] = [];
for (const insert of values) {
const document = getDocumentWithPrimaryKey(pkey, insert);
if (collection.has(document[pkey])) {
continue;
}
collection.set(document[pkey], document);
documents.push(document);
}
this.broadcast("insertMany", result);
if (documents.length > 0) {
this.broadcast("insertMany", documents);
}
return getInsertManyResult(result);
return { insertCount: documents.length, insertIds: documents.map((document) => document[pkey]) };
}
async findById(id: string): Promise<WithId<TSchema> | undefined> {
return this.#documents.get(id);
async findById({ collection, id }: FindByIdPayload): Promise<AnyObject | undefined> {
return this.#collections.get(collection).get(id);
}
async find(filter?: Filter<WithId<TSchema>>, options?: Options): Promise<WithId<TSchema>[]> {
let cursor = new Query(filter ?? {}).find<TSchema>(Array.from(this.#documents.values()));
async find({ condition = {}, options, ...payload }: FindPayload): Promise<AnyDocument[]> {
let cursor = new Query(condition).find<AnyDocument>(this.#collections.documents(payload.collection));
if (options !== undefined) {
cursor = addOptions(cursor, options);
}
return cursor.all() as WithId<TSchema>[];
return cursor.all();
}
async updateOne(
filter: Filter<WithId<TSchema>>,
expr: UpdateExpression,
arrayFilters?: Filter<WithId<TSchema>>[],
condition?: Filter<WithId<TSchema>>,
options?: UpdateOptions,
): Promise<UpdateResult> {
for (const document of await this.find(filter)) {
const modified = update(document, expr, arrayFilters, condition, options);
if (modified.length > 0) {
this.#documents.set(document.id, document);
this.broadcast("updateOne", document);
return new UpdateResult(1, 1);
}
return new UpdateResult(1, 0);
}
return new UpdateResult(0, 0);
}
async updateMany(
filter: Filter<WithId<TSchema>>,
expr: UpdateExpression,
arrayFilters?: Filter<WithId<TSchema>>[],
condition?: Filter<WithId<TSchema>>,
options?: UpdateOptions,
): Promise<UpdateResult> {
const documents: WithId<TSchema>[] = [];
async updateOne({ pkey, condition, modifier, arrayFilters, ...payload }: UpdatePayload): Promise<UpdateResult> {
const collection = this.#collections.get(payload.collection);
let matchedCount = 0;
let modifiedCount = 0;
for (const document of await this.find(filter)) {
for (const document of await this.find({ collection: payload.collection, condition, options: { limit: 1 } })) {
const modified = update(document, modifier, arrayFilters, undefined, { cloneMode: "deep" });
if (modified.length > 0) {
collection.set(document[pkey], document);
this.broadcast("updateOne", document);
modifiedCount += 1;
}
matchedCount += 1;
const modified = update(document, expr, arrayFilters, condition, options);
}
return { matchedCount, modifiedCount };
}
async updateMany({ pkey, condition, modifier, arrayFilters, ...payload }: UpdatePayload): Promise<UpdateResult> {
const collection = this.#collections.get(payload.collection);
const documents: AnyDocument[] = [];
let matchedCount = 0;
let modifiedCount = 0;
for (const document of await this.find({ collection: payload.collection, condition })) {
matchedCount += 1;
const modified = update(document, modifier, arrayFilters, undefined, { cloneMode: "deep" });
if (modified.length > 0) {
modifiedCount += 1;
documents.push(document);
this.#documents.set(document.id, document);
collection.set(document[pkey], document);
}
}
this.broadcast("updateMany", documents);
return new UpdateResult(matchedCount, modifiedCount);
return { matchedCount, modifiedCount };
}
async replace(filter: Filter<WithId<TSchema>>, document: WithId<TSchema>): Promise<UpdateResult> {
const documents: WithId<TSchema>[] = [];
async replace({ pkey, condition, document, ...payload }: ReplacePayload): Promise<UpdateResult> {
const collection = this.#collections.get(payload.collection);
let matchedCount = 0;
let modifiedCount = 0;
for (const current of await this.find(filter)) {
const documents: AnyDocument[] = [];
for (const current of await this.find({ collection: payload.collection, condition })) {
matchedCount += 1;
modifiedCount += 1;
documents.push(document);
this.#documents.set(current.id, document);
collection.set(current[pkey], document);
}
this.broadcast("updateMany", documents);
return new UpdateResult(matchedCount, modifiedCount);
return { matchedCount, modifiedCount };
}
async remove(filter: Filter<WithId<TSchema>>): Promise<RemoveResult> {
const documents = await this.find(filter);
async remove({ pkey, condition, ...payload }: RemovePayload): Promise<number> {
const collection = this.#collections.get(payload.collection);
const documents = await this.find({ collection: payload.collection, condition });
for (const document of documents) {
this.#documents.delete(document.id);
collection.delete(document[pkey]);
}
this.broadcast("remove", documents);
return new RemoveResult(documents.length);
return documents.length;
}
async count(filter?: Filter<WithId<TSchema>>): Promise<number> {
return new Query(filter ?? {}).find(Array.from(this.#documents.values())).count();
async count({ collection, condition = {} }: CountPayload): Promise<number> {
return new Query(condition).find(this.#collections.documents(collection)).all().length;
}
async flush(): Promise<void> {
this.#documents.clear();
this.#collections.flush();
}
}

View File

@@ -1,23 +1,16 @@
import { createUpdater, Query } from "mingo";
import { UpdateOptions } from "mingo/core";
import { UpdateExpression } from "mingo/updater";
import { Query, update } from "mingo";
import type { Criteria, Options } from "mingo/types";
import type { CloneMode, Modifier } from "mingo/updater";
import { getDocumentWithPrimaryKey } from "../../primary-key.ts";
import { DuplicateDocumentError } from "../../storage/errors.ts";
import {
getInsertManyResult,
getInsertOneResult,
InsertManyResult,
InsertOneResult,
} from "../../storage/operators/insert.ts";
import { RemoveResult } from "../../storage/operators/remove.ts";
import type { InsertResult } from "../../storage/operators/insert.ts";
import { UpdateResult } from "../../storage/operators/update.ts";
import { addOptions, Options, Storage } from "../../storage/storage.ts";
import { Document, Filter, WithId } from "../../types.ts";
import { addOptions, type QueryOptions, Storage } from "../../storage/storage.ts";
import type { AnyDocument } from "../../types.ts";
const update = createUpdater({ cloneMode: "deep" });
export class ObserverStorage<TSchema extends Document = Document> extends Storage<TSchema> {
readonly #documents = new Map<string, WithId<TSchema>>();
export class ObserverStorage extends Storage {
readonly #documents = new Map<string, AnyDocument>();
async resolve() {
return this;
@@ -27,48 +20,48 @@ export class ObserverStorage<TSchema extends Document = Document> extends Storag
return this.#documents.has(id);
}
async insertOne(data: Partial<TSchema>): Promise<InsertOneResult> {
const document = { ...data, id: data.id ?? crypto.randomUUID() } as WithId<TSchema>;
if (await this.has(document.id)) {
async insertOne(values: AnyDocument): Promise<InsertResult> {
const document = getDocumentWithPrimaryKey(this.primaryKey, values);
if (await this.has(document[this.primaryKey])) {
throw new DuplicateDocumentError(document, this as any);
}
this.#documents.set(document.id, document);
this.#documents.set(document[this.primaryKey], document);
return getInsertOneResult(document);
}
async insertMany(documents: Partial<TSchema>[]): Promise<InsertManyResult> {
async insertMany(list: TSchema[]): Promise<InsertResult> {
const result: TSchema[] = [];
for (const data of documents) {
const document = { ...data, id: data.id ?? crypto.randomUUID() } as WithId<TSchema>;
for (const values of list) {
const document = getDocumentWithPrimaryKey(this.primaryKey, values);
result.push(document);
this.#documents.set(document.id, document);
}
return getInsertManyResult(result);
}
async findById(id: string): Promise<WithId<TSchema> | undefined> {
async findById(id: string): Promise<TSchema | undefined> {
return this.#documents.get(id);
}
async find(filter?: Filter<WithId<TSchema>>, options?: Options): Promise<WithId<TSchema>[]> {
async find(filter?: Filter<TSchema>, options?: QueryOptions): Promise<TSchema[]> {
let cursor = new Query(filter ?? {}).find<TSchema>(Array.from(this.#documents.values()));
if (options !== undefined) {
cursor = addOptions(cursor, options);
}
return cursor.all() as WithId<TSchema>[];
return cursor.all();
}
async updateOne(
filter: Filter<WithId<TSchema>>,
expr: UpdateExpression,
arrayFilters?: Filter<WithId<TSchema>>[],
condition?: Filter<WithId<TSchema>>,
options?: UpdateOptions,
filter: Filter<TSchema>,
modifier: Modifier<TSchema>,
arrayFilters?: Filter<TSchema>[],
condition?: Criteria<TSchema>,
options: { cloneMode?: CloneMode; queryOptions?: Partial<Options> } = { cloneMode: "deep" },
): Promise<UpdateResult> {
const query = new Query(filter);
for (const document of Array.from(this.#documents.values())) {
if (query.test(document) === true) {
const modified = update(document, expr, arrayFilters, condition, options);
const modified = update(document, modifier, arrayFilters, condition, options);
if (modified.length > 0) {
this.#documents.set(document.id, document);
this.broadcast("updateOne", document);
@@ -81,15 +74,15 @@ export class ObserverStorage<TSchema extends Document = Document> extends Storag
}
async updateMany(
filter: Filter<WithId<TSchema>>,
expr: UpdateExpression,
arrayFilters?: Filter<WithId<TSchema>>[],
condition?: Filter<WithId<TSchema>>,
options?: UpdateOptions,
filter: Filter<TSchema>,
modifier: Modifier<TSchema>,
arrayFilters?: Filter<TSchema>[],
condition?: Criteria<TSchema>,
options: { cloneMode?: CloneMode; queryOptions?: Partial<Options> } = { cloneMode: "deep" },
): Promise<UpdateResult> {
const query = new Query(filter);
const documents: WithId<TSchema>[] = [];
const documents: TSchema[] = [];
let matchedCount = 0;
let modifiedCount = 0;
@@ -97,7 +90,7 @@ export class ObserverStorage<TSchema extends Document = Document> extends Storag
for (const document of Array.from(this.#documents.values())) {
if (query.test(document) === true) {
matchedCount += 1;
const modified = update(filter, expr, arrayFilters, condition, options);
const modified = update(document, modifier, arrayFilters, condition, options);
if (modified.length > 0) {
modifiedCount += 1;
documents.push(document);
@@ -111,10 +104,10 @@ export class ObserverStorage<TSchema extends Document = Document> extends Storag
return new UpdateResult(matchedCount, modifiedCount);
}
async replace(filter: Filter<WithId<TSchema>>, document: WithId<TSchema>): Promise<UpdateResult> {
async replace(filter: Filter<TSchema>, document: TSchema): Promise<UpdateResult> {
const query = new Query(filter);
const documents: WithId<TSchema>[] = [];
const documents: TSchema[] = [];
let matchedCount = 0;
let modifiedCount = 0;
@@ -131,7 +124,7 @@ export class ObserverStorage<TSchema extends Document = Document> extends Storag
return new UpdateResult(matchedCount, modifiedCount);
}
async remove(filter: Filter<WithId<TSchema>>): Promise<RemoveResult> {
async remove(filter: Filter<TSchema>): Promise<RemoveResult> {
const documents = Array.from(this.#documents.values());
const query = new Query(filter);
let count = 0;
@@ -144,8 +137,8 @@ export class ObserverStorage<TSchema extends Document = Document> extends Storag
return new RemoveResult(count);
}
async count(filter?: Filter<WithId<TSchema>>): Promise<number> {
return new Query(filter ?? {}).find(Array.from(this.#documents.values())).count();
async count(filter?: Filter<TSchema>): Promise<number> {
return new Query(filter ?? {}).find(Array.from(this.#documents.values())).all().length;
}
async flush(): Promise<void> {

View File

@@ -1,6 +1,23 @@
export type Registrars = {
/**
* Name of the collection.
*/
name: string;
/**
* Set the primary key of the collection.
* Default: "id"
*/
primaryKey?: string;
/**
* List of custom indexes for the collection.
*/
indexes?: Index[];
};
type Index = [string, any?];
type Index = [IndexKey, IndexOptions?];
type IndexKey = string;
type IndexOptions = { unique: boolean };

View File

@@ -1,5 +1,5 @@
import { Collection } from "../collection.ts";
import { Document, Filter, WithId } from "../types.ts";
import type { Collection } from "../collection.ts";
import type { Document, Filter, WithId } from "../types.ts";
import { isMatch } from "./is-match.ts";
export function observeOne<TSchema extends Document = Document>(

View File

@@ -1,23 +1,24 @@
import { Query } from "mingo";
import type { AnyObject, Criteria } from "mingo/types";
import { Collection } from "../collection.ts";
import { addOptions, ChangeEvent, Options } from "../storage/mod.ts";
import { Document, Filter, WithId } from "../types.ts";
import type { Collection } from "../collection.ts";
import { addOptions, type ChangeEvent, type QueryOptions } from "../storage/mod.ts";
import type { AnyDocument } from "../types.ts";
import { Store } from "./store.ts";
export function observe<TSchema extends Document = Document>(
collection: Collection<TSchema>,
filter: Filter<WithId<TSchema>>,
options: Options | undefined,
onChange: (documents: WithId<TSchema>[], changed: WithId<TSchema>[], type: ChangeEvent<TSchema>["type"]) => void,
export function observe<TCollection extends Collection, TSchema extends AnyObject = TCollection["$schema"]>(
collection: TCollection,
condition: Criteria<TSchema>,
options: QueryOptions | undefined,
onChange: (documents: TSchema[], changed: TSchema[], type: ChangeEvent["type"]) => void,
): {
unsubscribe: () => void;
} {
const store = Store.create<TSchema>();
const store = Store.create();
let debounce: any;
collection.find(filter, options).then(async (documents) => {
collection.find(condition, options).then(async (documents) => {
const resolved = await store.resolve(documents);
onChange(resolved, resolved, "insertMany");
});
@@ -29,17 +30,17 @@ export function observe<TSchema extends Document = Document>(
onChange([], [], "remove");
}),
collection.observable.change.subscribe(async ({ type, data }) => {
let changed: WithId<TSchema>[] = [];
let changed: AnyObject[] = [];
switch (type) {
case "insertOne":
case "updateOne": {
changed = await store[type](data, filter);
changed = await store[type](data, condition);
break;
}
case "insertMany":
case "updateMany":
case "remove": {
changed = await store[type](data, filter);
changed = await store[type](data, condition);
break;
}
}
@@ -64,12 +65,9 @@ export function observe<TSchema extends Document = Document>(
};
}
function applyQueryOptions<TSchema extends Document = Document>(
documents: WithId<TSchema>[],
options?: Options,
): WithId<TSchema>[] {
function applyQueryOptions(documents: AnyDocument[], options?: QueryOptions): AnyDocument[] {
if (options !== undefined) {
return addOptions(new Query({}).find<TSchema>(documents), options).all() as WithId<TSchema>[];
return addOptions(new Query({}).find<AnyDocument>(documents), options).all();
}
return documents;
}

View File

@@ -1,29 +1,29 @@
import { ObserverStorage } from "../databases/observer/storage.ts";
import { Storage } from "../storage/mod.ts";
import { Document, Filter, WithId } from "../types.ts";
import type { Storage } from "../storage/mod.ts";
import type { AnyDocument } from "../types.ts";
import { isMatch } from "./is-match.ts";
export class Store<TSchema extends Document = Document> {
private constructor(private storage: Storage<TSchema>) {}
export class Store {
private constructor(private storage: Storage) {}
static create<TSchema extends Document = Document>() {
return new Store<TSchema>(new ObserverStorage<TSchema>(`observer[${crypto.randomUUID()}]`));
static create() {
return new Store(new ObserverStorage(`observer[${crypto.randomUUID()}]`));
}
get destroy() {
return this.storage.destroy.bind(this.storage);
}
async resolve(documents: WithId<TSchema>[]): Promise<WithId<TSchema>[]> {
async resolve(documents: AnyDocument[]): Promise<AnyDocument[]> {
await this.storage.insertMany(documents);
return this.getDocuments();
}
async getDocuments(): Promise<WithId<TSchema>[]> {
async getDocuments(): Promise<AnyDocument[]> {
return this.storage.find();
}
async insertMany(documents: WithId<TSchema>[], filter: Filter<WithId<TSchema>>): Promise<WithId<TSchema>[]> {
async insertMany(documents: AnyDocument[], filter: Filter<AnyDocument>): Promise<AnyDocument[]> {
const matched = [];
for (const document of documents) {
matched.push(...(await this.insertOne(document, filter)));
@@ -31,15 +31,15 @@ export class Store<TSchema extends Document = Document> {
return matched;
}
async insertOne(document: WithId<TSchema>, filter: Filter<WithId<TSchema>>): Promise<WithId<TSchema>[]> {
if (isMatch<TSchema>(document, filter)) {
async insertOne(document: AnyDocument, filter: Filter<AnyDocument>): Promise<AnyDocument[]> {
if (isMatch<AnyDocument>(document, filter)) {
await this.storage.insertOne(document);
return [document];
}
return [];
}
async updateMany(documents: WithId<TSchema>[], filter: Filter<WithId<TSchema>>): Promise<WithId<TSchema>[]> {
async updateMany(documents: AnyDocument[], filter: Filter<AnyDocument>): Promise<AnyDocument[]> {
const matched = [];
for (const document of documents) {
matched.push(...(await this.updateOne(document, filter)));
@@ -47,33 +47,33 @@ export class Store<TSchema extends Document = Document> {
return matched;
}
async updateOne(document: WithId<TSchema>, filter: Filter<WithId<TSchema>>): Promise<WithId<TSchema>[]> {
async updateOne(document: AnyDocument, filter: Filter<AnyDocument>): Promise<AnyDocument[]> {
if (await this.storage.has(document.id)) {
await this.#updateOrRemove(document, filter);
return [document];
} else if (isMatch<TSchema>(document, filter)) {
} else if (isMatch<AnyDocument>(document, filter)) {
await this.storage.insertOne(document);
return [document];
}
return [];
}
async remove(documents: WithId<TSchema>[]): Promise<WithId<TSchema>[]> {
async remove(documents: AnyDocument[]): Promise<AnyDocument[]> {
const matched = [];
for (const document of documents) {
if (isMatch<TSchema>(document, { id: document.id } as WithId<TSchema>)) {
await this.storage.remove({ id: document.id } as WithId<TSchema>);
if (isMatch<AnyDocument>(document, { id: document.id } as AnyDocument)) {
await this.storage.remove({ id: document.id } as AnyDocument);
matched.push(document);
}
}
return matched;
}
async #updateOrRemove(document: WithId<TSchema>, filter: Filter<WithId<TSchema>>): Promise<void> {
if (isMatch<TSchema>(document, filter)) {
await this.storage.replace({ id: document.id } as WithId<TSchema>, document);
async #updateOrRemove(document: AnyDocument, filter: Filter<AnyDocument>): Promise<void> {
if (isMatch<AnyDocument>(document, filter)) {
await this.storage.replace({ id: document.id } as AnyDocument, document);
} else {
await this.storage.remove({ id: document.id } as WithId<TSchema>);
await this.storage.remove({ id: document.id } as AnyDocument);
}
}

8
src/primary-key.ts Normal file
View File

@@ -0,0 +1,8 @@
import type { AnyDocument } from "./types.ts";
export function getDocumentWithPrimaryKey<TPKey extends string>(pkey: TPKey, document: AnyDocument): AnyDocument {
if (Object.hasOwn(document, pkey) === true) {
return document;
}
return { [pkey]: crypto.randomUUID(), ...document };
}

View File

@@ -0,0 +1,33 @@
import type { AnyObject } from "mingo/types";
import { CollectionNotFoundError } from "./errors.ts";
export class Collections {
#collections = new Map<string, Documents>();
has(name: string): boolean {
return this.#collections.has(name);
}
documents(name: string): AnyObject[] {
return Array.from(this.get(name).values());
}
get(name: string): Documents {
const collection = this.#collections.get(name);
if (collection === undefined) {
throw new CollectionNotFoundError(name);
}
return collection;
}
delete(name: string): boolean {
return this.#collections.delete(name);
}
flush() {
this.#collections.clear();
}
}
type Documents = Map<string, AnyObject>;

View File

@@ -1,25 +1,28 @@
import { RawObject } from "mingo/types";
import { Document } from "../types.ts";
import type { Storage } from "./storage.ts";
import type { AnyObject } from "mingo/types";
export class DuplicateDocumentError extends Error {
readonly type = "DuplicateDocumentError";
constructor(
readonly document: Document,
storage: Storage,
readonly collection: string,
readonly document: AnyObject,
) {
super(
`Collection Insert Violation: Document '${document.id}' already exists in ${storage.name} collection ${storage.id}`,
);
super(`Collection Insert Violation: Document '${document.id}' already exists in '${collection}' collection`);
}
}
export class CollectionNotFoundError extends Error {
readonly type = "CollectionNotFoundError";
constructor(readonly collection: string) {
super(`Collection Retrieve Violation: Collection '${collection}' does not exist`);
}
}
export class DocumentNotFoundError extends Error {
readonly type = "DocumentNotFoundError";
constructor(readonly criteria: RawObject) {
constructor(readonly criteria: AnyObject) {
super(`Collection Update Violation: Document matching criteria does not exists`);
}
}

View File

@@ -1,40 +1,4 @@
import type { Document } from "../../types.ts";
export function getInsertManyResult(documents: Document[]): InsertManyResult {
return {
acknowledged: true,
insertedCount: documents.length,
insertedIds: documents.reduce<{ [key: number]: string | number }>((map, document, index) => {
map[index] = document.id;
return map;
}, {}),
};
}
export function getInsertOneResult(document: Document): InsertOneResult {
return {
acknowledged: true,
insertedId: document.id,
};
}
export type InsertManyResult =
| {
acknowledged: false;
}
| {
acknowledged: true;
insertedCount: number;
insertedIds: {
[key: number]: string | number;
};
};
export type InsertOneResult =
| {
acknowledged: false;
}
| {
acknowledged: true;
insertedId: string | number;
};
export type InsertResult = {
insertCount: number;
insertIds: (string | number | symbol)[];
};

View File

@@ -1,3 +0,0 @@
export class RemoveResult {
constructor(readonly matched = 0) {}
}

View File

@@ -1,6 +1,4 @@
export class UpdateResult {
constructor(
readonly matched = 0,
readonly modified = 0,
) {}
}
export type UpdateResult = {
matchedCount: number;
modifiedCount: number;
};

View File

@@ -1,20 +1,19 @@
import { UpdateOptions } from "mingo/core";
import { Cursor } from "mingo/cursor";
import { UpdateExpression } from "mingo/updater";
import type { Cursor } from "mingo/cursor";
import type { AnyObject, Criteria } from "mingo/types";
import type { Modifier } from "mingo/updater";
import { Subject } from "rxjs";
import { BroadcastChannel, StorageBroadcast } from "../broadcast.ts";
import { Document, Filter, WithId } from "../types.ts";
import { InsertManyResult, InsertOneResult } from "./operators/insert.ts";
import { RemoveResult } from "./operators/remove.ts";
import { UpdateResult } from "./operators/update.ts";
import { BroadcastChannel, type StorageBroadcast } from "../broadcast.ts";
import type { Prettify } from "../types.ts";
import type { InsertResult } from "./operators/insert.ts";
import type { UpdateResult } from "./operators/update.ts";
export abstract class Storage<TSchema extends Document = Document> {
export abstract class Storage {
readonly observable: {
change: Subject<ChangeEvent<TSchema>>;
change: Subject<ChangeEvent>;
flush: Subject<void>;
} = {
change: new Subject<ChangeEvent<TSchema>>(),
change: new Subject<ChangeEvent>(),
flush: new Subject<void>(),
};
@@ -22,12 +21,9 @@ export abstract class Storage<TSchema extends Document = Document> {
readonly #channel: BroadcastChannel;
constructor(
readonly name: string,
readonly id: string = crypto.randomUUID(),
) {
constructor(readonly name: string) {
this.#channel = new BroadcastChannel(`valkyr:db:${name}`);
this.#channel.onmessage = ({ data }: MessageEvent<StorageBroadcast<TSchema>>) => {
this.#channel.onmessage = ({ data }: MessageEvent<StorageBroadcast>) => {
if (data.name !== this.name) {
return;
}
@@ -72,7 +68,7 @@ export abstract class Storage<TSchema extends Document = Document> {
|
*/
broadcast(type: StorageBroadcast<TSchema>["type"], data?: TSchema | TSchema[]): void {
broadcast(type: StorageBroadcast["type"], data?: AnyObject | AnyObject[]): void {
switch (type) {
case "flush": {
this.observable.flush.next();
@@ -92,37 +88,23 @@ export abstract class Storage<TSchema extends Document = Document> {
|--------------------------------------------------------------------------------
*/
abstract has(id: string): Promise<boolean>;
abstract insertOne(payload: InsertOnePayload): Promise<InsertResult>;
abstract insertOne(document: Partial<WithId<TSchema>>): Promise<InsertOneResult>;
abstract insertMany(payload: InsertManyPayload): Promise<InsertResult>;
abstract insertMany(documents: Partial<WithId<TSchema>>[]): Promise<InsertManyResult>;
abstract findById(payload: FindByIdPayload): Promise<AnyObject | undefined>;
abstract findById(id: string): Promise<WithId<TSchema> | undefined>;
abstract find(payload: FindPayload): Promise<AnyObject[]>;
abstract find(filter?: Filter<WithId<TSchema>>, options?: Options): Promise<WithId<TSchema>[]>;
abstract updateOne(payload: UpdatePayload): Promise<UpdateResult>;
abstract updateOne(
filter: Filter<WithId<TSchema>>,
expr: UpdateExpression,
arrayFilters?: Filter<WithId<TSchema>>[],
condition?: Filter<WithId<TSchema>>,
options?: UpdateOptions,
): Promise<UpdateResult>;
abstract updateMany(payload: UpdatePayload): Promise<UpdateResult>;
abstract updateMany(
filter: Filter<WithId<TSchema>>,
expr: UpdateExpression,
arrayFilters?: Filter<WithId<TSchema>>[],
condition?: Filter<WithId<TSchema>>,
options?: UpdateOptions,
): Promise<UpdateResult>;
abstract replace(payload: ReplacePayload): Promise<UpdateResult>;
abstract replace(filter: Filter<WithId<TSchema>>, document: TSchema): Promise<UpdateResult>;
abstract remove(payload: RemovePayload): Promise<number>;
abstract remove(filter: Filter<WithId<TSchema>>): Promise<RemoveResult>;
abstract count(filter?: Filter<WithId<TSchema>>): Promise<number>;
abstract count(payload: CountPayload): Promise<number>;
abstract flush(): Promise<void>;
@@ -143,9 +125,9 @@ export abstract class Storage<TSchema extends Document = Document> {
|--------------------------------------------------------------------------------
*/
export function addOptions<TSchema extends Document = Document>(
export function addOptions<TSchema extends AnyObject = AnyObject>(
cursor: Cursor<TSchema>,
options: Options,
options: QueryOptions,
): Cursor<TSchema> {
if (options.sort) {
cursor.sort(options.sort);
@@ -167,17 +149,17 @@ export function addOptions<TSchema extends Document = Document>(
type Status = "loading" | "ready";
export type ChangeEvent<TSchema extends Document = Document> =
export type ChangeEvent =
| {
type: "insertOne" | "updateOne";
data: WithId<TSchema>;
data: AnyObject;
}
| {
type: "insertMany" | "updateMany" | "remove";
data: WithId<TSchema>[];
data: AnyObject[];
};
export type Options = {
export type QueryOptions = {
sort?: {
[key: string]: 1 | -1;
};
@@ -197,3 +179,68 @@ export type Options = {
export type Index = {
[index: string]: any;
};
export type InsertOnePayload = Prettify<
CollectionPayload &
PrimaryKeyPayload & {
values: AnyObject;
}
>;
export type InsertManyPayload = Prettify<
CollectionPayload &
PrimaryKeyPayload & {
values: AnyObject[];
}
>;
export type FindByIdPayload = Prettify<
CollectionPayload & {
id: string;
}
>;
export type FindPayload = Prettify<
CollectionPayload & {
condition?: Criteria<AnyObject>;
options?: QueryOptions;
}
>;
export type UpdatePayload = Prettify<
CollectionPayload &
PrimaryKeyPayload & {
condition: Criteria<AnyObject>;
modifier: Modifier<AnyObject>;
arrayFilters?: AnyObject[];
}
>;
export type ReplacePayload = Prettify<
CollectionPayload &
PrimaryKeyPayload & {
condition: Criteria<AnyObject>;
document: AnyObject;
}
>;
export type RemovePayload = Prettify<
CollectionPayload &
PrimaryKeyPayload & {
condition: Criteria<AnyObject>;
}
>;
export type CountPayload = Prettify<
CollectionPayload & {
condition?: Criteria<AnyObject>;
}
>;
type CollectionPayload = {
collection: string;
};
type PrimaryKeyPayload = {
pkey: string;
};

View File

@@ -1,12 +1,8 @@
import type { BSONRegExp, BSONType } from "bson";
export type Document = {
[key: string]: any;
};
export type Prettify<T> = { [K in keyof T]: T[K] } & {};
export type WithId<TSchema> = {
id: string;
} & TSchema;
export type AnyDocument = Record<string, any>;
export type Filter<TSchema> = {
[P in keyof TSchema]?: Condition<TSchema[P]>;
@@ -129,12 +125,11 @@ type Flatten<Type> = Type extends ReadonlyArray<infer Item> ? Item : Type;
type IsAny<Type, ResultIfAny, ResultIfNotAny> = true extends false & Type ? ResultIfAny : ResultIfNotAny;
type FilterOperations<T> =
T extends Record<string, any>
? {
[key in keyof T]?: FilterOperators<T[key]>;
}
: FilterOperators<T>;
type FilterOperations<T> = T extends Record<string, any>
? {
[key in keyof T]?: FilterOperators<T[key]>;
}
: FilterOperators<T>;
type ArrayOperator<Type> = {
$each?: Array<Flatten<Type>>;

View File

@@ -2,7 +2,7 @@ import { afterEach, beforeEach, describe, it } from "@std/testing/bdd";
import { expect } from "expect";
import { IndexedDbCache } from "../src/databases/indexeddb/cache.ts";
import { Options } from "../src/storage/storage.ts";
import type { Options } from "../src/storage/storage.ts";
import type { WithId } from "../src/types.ts";
describe("IndexedDbCache", () => {

View File

@@ -3,7 +3,7 @@ import { expect } from "expect";
import { Collection } from "../src/collection.ts";
import { MemoryStorage } from "../src/databases/memory/storage.ts";
import { getUsers, UserDocument } from "./users.mock.ts";
import { getUsers, type UserDocument } from "./users.mock.ts";
/*
|--------------------------------------------------------------------------------

View File

@@ -2,7 +2,7 @@ import { describe, it } from "@std/testing/bdd";
import { expect } from "expect";
import { hashCodeQuery } from "../src/hash.ts";
import { Options } from "../src/mod.ts";
import type { Options } from "../src/mod.ts";
describe("hashCodeQuery", () => {
const filter = { name: { $eq: "Document 1" } };

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",

View File

@@ -1,5 +1,5 @@
import { clone } from "../src/clone.ts";
import { WithId } from "../src/types.ts";
import type { WithId } from "../src/types.ts";
const users: WithId<UserDocument>[] = [
{