feat(dbs): use constructor for memory db
This commit is contained in:
@@ -30,7 +30,11 @@ export class IndexedDbStorage<TSchema extends Document = Document> extends Stora
|
|||||||
|
|
||||||
#db?: IDBPDatabase;
|
#db?: IDBPDatabase;
|
||||||
|
|
||||||
constructor(name: string, promise: Promise<IDBPDatabase>, readonly log: DBLogger) {
|
constructor(
|
||||||
|
name: string,
|
||||||
|
promise: Promise<IDBPDatabase>,
|
||||||
|
readonly log: DBLogger
|
||||||
|
) {
|
||||||
super(name);
|
super(name);
|
||||||
this.#promise = promise;
|
this.#promise = promise;
|
||||||
}
|
}
|
||||||
@@ -124,7 +128,7 @@ export class IndexedDbStorage<TSchema extends Document = Document> extends Stora
|
|||||||
}
|
}
|
||||||
|
|
||||||
const indexes = this.#resolveIndexes(filter);
|
const indexes = this.#resolveIndexes(filter);
|
||||||
let cursor = new Query(filter).find(await this.#getAll({ ...options, ...indexes }));
|
let cursor = new Query(filter).find<TSchema>(await this.#getAll({ ...options, ...indexes }));
|
||||||
if (options !== undefined) {
|
if (options !== undefined) {
|
||||||
cursor = addOptions(cursor, options);
|
cursor = addOptions(cursor, options);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ export class MemoryStorage<TSchema extends Document = Document> extends Storage<
|
|||||||
}
|
}
|
||||||
|
|
||||||
async find(filter?: Filter<WithId<TSchema>>, options?: Options): Promise<WithId<TSchema>[]> {
|
async find(filter?: Filter<WithId<TSchema>>, options?: Options): Promise<WithId<TSchema>[]> {
|
||||||
let cursor = new Query(filter ?? {}).find(Array.from(this.#documents.values()));
|
let cursor = new Query(filter ?? {}).find<TSchema>(Array.from(this.#documents.values()));
|
||||||
if (options !== undefined) {
|
if (options !== undefined) {
|
||||||
cursor = addOptions(cursor, options);
|
cursor = addOptions(cursor, options);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,11 +3,18 @@ import { Document } from "../Types.js";
|
|||||||
import { MemoryStorage } from "./MemoryDb.Storage.js";
|
import { MemoryStorage } from "./MemoryDb.Storage.js";
|
||||||
import { Registrars } from "./Registrars.js";
|
import { Registrars } from "./Registrars.js";
|
||||||
|
|
||||||
|
type Options = {
|
||||||
|
name: string;
|
||||||
|
registrars: Registrars[];
|
||||||
|
};
|
||||||
|
|
||||||
export class MemoryDatabase<T extends Record<string, Document>> {
|
export class MemoryDatabase<T extends Record<string, Document>> {
|
||||||
|
readonly name: string;
|
||||||
readonly #collections = new Map<keyof T, Collection<T[keyof T]>>();
|
readonly #collections = new Map<keyof T, Collection<T[keyof T]>>();
|
||||||
|
|
||||||
register(registrars: Registrars[]): void {
|
constructor(readonly options: Options) {
|
||||||
for (const { name } of registrars) {
|
this.name = options.name;
|
||||||
|
for (const { name } of options.registrars) {
|
||||||
this.#collections.set(name, new Collection(name, new MemoryStorage(name)));
|
this.#collections.set(name, new Collection(name, new MemoryStorage(name)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ export class ObserverStorage<TSchema extends Document = Document> extends Storag
|
|||||||
}
|
}
|
||||||
|
|
||||||
async find(filter?: Filter<WithId<TSchema>>, options?: Options): Promise<WithId<TSchema>[]> {
|
async find(filter?: Filter<WithId<TSchema>>, options?: Options): Promise<WithId<TSchema>[]> {
|
||||||
let cursor = new Query(filter ?? {}).find(Array.from(this.#documents.values()));
|
let cursor = new Query(filter ?? {}).find<TSchema>(Array.from(this.#documents.values()));
|
||||||
if (options !== undefined) {
|
if (options !== undefined) {
|
||||||
cursor = addOptions(cursor, options);
|
cursor = addOptions(cursor, options);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ function applyQueryOptions<TSchema extends Document = Document>(
|
|||||||
options?: Options
|
options?: Options
|
||||||
): WithId<TSchema>[] {
|
): WithId<TSchema>[] {
|
||||||
if (options !== undefined) {
|
if (options !== undefined) {
|
||||||
return addOptions(new Query({}).find(documents), options).all() as WithId<TSchema>[];
|
return addOptions(new Query({}).find<TSchema>(documents), options).all() as WithId<TSchema>[];
|
||||||
}
|
}
|
||||||
return documents;
|
return documents;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,10 @@ export abstract class Storage<TSchema extends Document = Document> {
|
|||||||
|
|
||||||
readonly #channel: BroadcastChannel;
|
readonly #channel: BroadcastChannel;
|
||||||
|
|
||||||
constructor(readonly name: string, readonly id = nanoid()) {
|
constructor(
|
||||||
|
readonly name: string,
|
||||||
|
readonly id = nanoid()
|
||||||
|
) {
|
||||||
this.#channel = new BroadcastChannel(`valkyr:db:${name}`);
|
this.#channel = new BroadcastChannel(`valkyr:db:${name}`);
|
||||||
this.#channel.onmessage = ({ data }: MessageEvent<StorageBroadcast<TSchema>>) => {
|
this.#channel.onmessage = ({ data }: MessageEvent<StorageBroadcast<TSchema>>) => {
|
||||||
if (data.name !== this.name) {
|
if (data.name !== this.name) {
|
||||||
@@ -124,7 +127,10 @@ export abstract class Storage<TSchema extends Document = Document> {
|
|||||||
|--------------------------------------------------------------------------------
|
|--------------------------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function addOptions(cursor: Cursor, options: Options): Cursor {
|
export function addOptions<TSchema extends Document = Document>(
|
||||||
|
cursor: Cursor<TSchema>,
|
||||||
|
options: Options
|
||||||
|
): Cursor<TSchema> {
|
||||||
if (options.sort) {
|
if (options.sort) {
|
||||||
cursor.sort(options.sort);
|
cursor.sort(options.sort);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user