jdb-lite
Version:
A lightweight schema-based JSON database library with validation
181 lines • 5.62 kB
TypeScript
export interface EventEmitter {
on(event: string, listener: Function): this;
emit(event: string, ...args: any[]): boolean;
off(event: string, listener: Function): this;
}
export interface SchemaDefinition {
[key: string]: SchemaTypeDefinition | SchemaTypeDefinition[];
}
export interface SchemaTypeDefinition {
type: 'String' | 'Number' | 'Boolean' | 'Date' | 'Array' | 'Object' | 'ObjectId';
required?: boolean;
default?: any;
validate?: (value: any) => boolean;
ref?: string;
unique?: boolean;
index?: boolean;
}
export interface SchemaOptions {
timestamps?: boolean;
collection?: string;
}
export interface Document {
_id: string;
createdAt?: Date;
updatedAt?: Date;
[key: string]: any;
}
export interface Query<T> {
find(filter?: Partial<T>): Promise<T[]>;
findOne(filter?: Partial<T>): Promise<T | null>;
findById(id: string): Promise<T | null>;
findByIdAndUpdate(id: string, update: Partial<T>, options?: {
new?: boolean;
}): Promise<T | null>;
findByIdAndDelete(id: string): Promise<T | null>;
findOneAndUpdate(filter: Partial<T>, update: Partial<T>, options?: {
new?: boolean;
}): Promise<T | null>;
findOneAndDelete(filter: Partial<T>): Promise<T | null>;
updateOne(filter: Partial<T>, update: Partial<T>): Promise<{
modifiedCount: number;
}>;
updateMany(filter: Partial<T>, update: Partial<T>): Promise<{
modifiedCount: number;
}>;
deleteOne(filter: Partial<T>): Promise<{
deletedCount: number;
}>;
deleteMany(filter: Partial<T>): Promise<{
deletedCount: number;
}>;
countDocuments(filter?: Partial<T>): Promise<number>;
sort(sortBy: string | {
[key: string]: 1 | -1;
}): Query<T>;
limit(limit: number): Query<T>;
skip(skip: number): Query<T>;
select(fields: string | string[] | {
[key: string]: 0 | 1;
}): Query<T>;
populate(path: string | string[]): Query<T>;
exec(): Promise<T[]>;
}
export interface Model<T extends Document> extends EventEmitter {
new (doc?: Partial<T>): T & ModelInstance<T>;
find(filter?: Partial<T>): Query<T>;
findOne(filter?: Partial<T>): Promise<T | null>;
findById(id: string): Promise<T | null>;
findByIdAndUpdate(id: string, update: Partial<T>, options?: {
new?: boolean;
}): Promise<T | null>;
findByIdAndDelete(id: string): Promise<T | null>;
findOneAndUpdate(filter: Partial<T>, update: Partial<T>, options?: {
new?: boolean;
}): Promise<T | null>;
findOneAndDelete(filter: Partial<T>): Promise<T | null>;
updateOne(filter: Partial<T>, update: Partial<T>): Promise<{
modifiedCount: number;
}>;
updateMany(filter: Partial<T>, update: Partial<T>): Promise<{
modifiedCount: number;
}>;
deleteOne(filter: Partial<T>): Promise<{
deletedCount: number;
}>;
deleteMany(filter: Partial<T>): Promise<{
deletedCount: number;
}>;
countDocuments(filter?: Partial<T>): Promise<number>;
create(doc: Partial<T>): Promise<T>;
insertMany(docs: Partial<T>[]): Promise<T[]>;
watch(): EventEmitter;
collection: {
name: string;
};
}
export interface ModelInstance<T> {
save(): Promise<T>;
remove(): Promise<T>;
toObject(): T;
toJSON(): T;
isNew: boolean;
isModified(path?: string): boolean;
markModified(path: string): void;
populate(path: string | string[]): Promise<T>;
depopulate(path: string): T;
validate(): Promise<void>;
}
export interface Connection extends EventEmitter {
readyState: number;
name: string;
host: string;
port: number;
models: {
[key: string]: Model<any>;
};
collections: {
[key: string]: Collection;
};
close(): Promise<void>;
}
export interface Collection {
name: string;
conn: Connection;
insertOne(doc: any): Promise<any>;
insertMany(docs: any[]): Promise<any[]>;
find(filter: any): Promise<any[]>;
findOne(filter: any): Promise<any>;
updateOne(filter: any, update: any): Promise<{
modifiedCount: number;
}>;
updateMany(filter: any, update: any): Promise<{
modifiedCount: number;
}>;
deleteOne(filter: any): Promise<{
deletedCount: number;
}>;
deleteMany(filter: any): Promise<{
deletedCount: number;
}>;
countDocuments(filter: any): Promise<number>;
drop(): Promise<void>;
}
export interface JsonSchemaDBStatic {
connect(uri: string, options?: any): Promise<Connection>;
connection: Connection;
disconnect(): Promise<void>;
model<T extends Document>(name: string, schema?: Schema<T>): Model<T>;
Schema: typeof Schema;
Types: {
ObjectId: typeof ObjectId;
};
}
export declare class Schema<T = any> {
constructor(definition: SchemaDefinition, options?: SchemaOptions);
add(obj: SchemaDefinition): void;
pre(method: string, fn: Function): void;
post(method: string, fn: Function): void;
methods: {
[key: string]: Function;
};
statics: {
[key: string]: Function;
};
virtual(name: string): VirtualType;
index(fields: {
[key: string]: 1 | -1;
}): void;
plugin(fn: Function, options?: any): void;
}
export interface VirtualType {
get(fn: Function): VirtualType;
set(fn: Function): VirtualType;
}
export declare class ObjectId {
constructor(id?: string);
toString(): string;
toHexString(): string;
static isValid(id: any): boolean;
}
//# sourceMappingURL=types.d.ts.map