Skip to content

Commit

Permalink
Stable prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirlovon committed Aug 3, 2020
1 parent 10f4ad0 commit e7b77ef
Show file tree
Hide file tree
Showing 17 changed files with 386 additions and 486 deletions.
4 changes: 2 additions & 2 deletions lib/cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { SearchQuery, CursorMethod, DatabaseConfig, Acceptable } from './declara
// TODO
export class Cursor<Schema extends Acceptable<Schema>> {
/** Main search query. */
private query: SearchQuery<Schema> = {};
private query: SearchQuery<Schema>;

/** Database configuration. */
private config: DatabaseConfig;
Expand Down Expand Up @@ -68,7 +68,7 @@ export class Cursor<Schema extends Acceptable<Schema>> {

for (let i = 0; i < methods.length; i++) {
const method: CursorMethod = methods[i];
const type = method.type;
const type: string = method.type;

if (type === 'reverse') {
found.reverse();
Expand Down
85 changes: 29 additions & 56 deletions lib/declarations.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
/**
* Database initialization config
*/
/** Database initialization config */
export interface DatabaseConfig {
/**
* Path to the database file.
*/
/** Path to the database file. */
filePath?: string;

/**
* Save data in easy-to-read format.
*/
/** Save data in easy-to-read format. */
pretty: boolean;

/**
Expand All @@ -32,9 +26,7 @@ export interface DatabaseConfig {
schemaValidator?: SchemaValidator;
}

/**
* Database file structure.
*/
/** Database file structure. */
export interface DatabaseFile {
/** Timestamp of the last data writing. */
timestamp: number;
Expand All @@ -43,68 +35,49 @@ export interface DatabaseFile {
documents: Document[];
}

/**
* Any document-like object.
*/
/** Any document-like object. */
export interface Document {
[key: string]: DocumentValue;
}

/**
* Any object without specified structure.
*/
/** Any object without specified structure. */
export interface UnknownObject {
[key: string]: any;
}

/**
*
*/
/** Checking the object for suitability for storage. */
export type Acceptable<T> = { [K in keyof T]: T[K] & DocumentValue };

/**
* Search query.
*/
export type SearchQuery<T> = { [K in keyof T]?: T[K] | SearchFunction | RegExp } & { [key: string]: DocumentValue | SearchFunction | RegExp };
/** Search query. */
export type SearchQuery<T extends Acceptable<T> = Document> = { [K in keyof T]?: SearchQueryValue<T[K]> } | SearchFunction<T> | undefined;

/**
* Update query.
*/
export type UpdateQuery<T> = ({ [K in keyof T]?: T[K] & DocumentValue } & { [key: string]: DocumentValue }) | ((document: T) => void);
/** Update query. */
export type UpdateQuery<T extends Acceptable<T> = Document> = { [K in keyof T]?: UpdateQueryValue<T[K]> } | UpdateFunction<T> | undefined;

/**
* Sorting function.
*/
export type SortFunction = (a: Readonly<DocumentValue>, b: Readonly<DocumentValue>) => number;
/** Supported primitives. */
export type DocumentPrimitive = string | number | boolean | null;

/**
* Search function for search queries.
*/
export type SearchFunction = (DocumentValue: DocumentValue) => boolean;
/** Supported documents values. */
export type DocumentValue = DocumentPrimitive | DocumentPrimitive[] | Document | Document[];

/**
* Manual schema validation.
*/
export type SchemaValidator = (document: Readonly<Document>) => void;
/** Search function for search queries. */
export type SearchFunction<T = Document> = (value: Readonly<T>) => boolean;

/**
* Supported primitives.
*/
export type DocumentPrimitive = string | number | boolean | null;
export type SearchQueryValue<T = DocumentValue> = T | SearchFieldFunction<T> | RegExp | undefined;

export type UpdateQueryValue<T = DocumentValue> = T | UpdateFieldFunction<T> | undefined;

/**
* Supported documents DocumentValues.
*/
export type DocumentValue = DocumentPrimitive | DocumentPrimitive[] | Document | Document[] | undefined;
export type UpdateFunction<T = Document> = (value: T) => T;
export type SearchFieldFunction<T = DocumentValue> = (value: Readonly<T>) => boolean;
export type UpdateFieldFunction<T = DocumentValue> = (value: T) => T;

/**
* Search query value.
*/
export type SearchQueryValue = DocumentValue | SearchFunction | RegExp | undefined;
/** Manual schema validation. */
export type SchemaValidator = (document: Readonly<Document>) => void;

/** Sorting function */
export type SortFunction = (a: Readonly<DocumentValue>, b: Readonly<DocumentValue>) => number;

/**
* Cursor methods.
*/
/** Cursor methods. */
export type CursorMethod =
| { type: 'limit'; number: number }
| { type: 'skip'; number: number }
Expand Down
4 changes: 1 addition & 3 deletions lib/error.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { isError, isString } from './types.ts';

/**
* Custom database error.
*/
/** Custom database error. */
export class DatabaseError extends Error {
/** Error name. */
public name: string = 'DatabaseError';
Expand Down
Loading

0 comments on commit e7b77ef

Please sign in to comment.