rxdb
Version:
A local-first realtime NoSQL Database for JavaScript applications - https://rxdb.info/
77 lines (76 loc) • 3.07 kB
TypeScript
import type { DeepMutable, DeepReadonly, HashFunction, MaybeReadonly, RxDocumentData, RxJsonSchema, StringKeys } from './types/index.js';
export declare class RxSchema<RxDocType = any> {
readonly jsonSchema: RxJsonSchema<RxDocumentData<RxDocType>>;
readonly hashFunction: HashFunction;
indexes: MaybeReadonly<string[]>[];
readonly primaryPath: StringKeys<RxDocumentData<RxDocType>>;
finalFields: string[];
constructor(jsonSchema: RxJsonSchema<RxDocumentData<RxDocType>>, hashFunction: HashFunction);
get version(): number;
get defaultValues(): {
[P in keyof RxDocType]: RxDocType[P];
};
getJsonSchemaWithoutMeta(): RxJsonSchema<RxDocumentData<RxDocType>>;
/**
* @overrides itself on the first call
*/
get hash(): Promise<string>;
/**
* checks if a given change on a document is allowed
* Ensures that:
* - final fields are not modified
* @throws {Error} if not valid
*/
validateChange(dataBefore: any, dataAfter: any): void;
/**
* creates the schema-based document-prototype,
* see RxCollection.getDocumentPrototype()
*/
getDocumentPrototype(): any;
getPrimaryOfDocumentData(documentData: Partial<RxDocType>): string;
}
export declare function getIndexes<RxDocType = any>(jsonSchema: RxJsonSchema<RxDocType>): MaybeReadonly<string[]>[];
/**
* array with previous version-numbers
*/
export declare function getPreviousVersions(schema: RxJsonSchema<any>): number[];
export declare function createRxSchema<T>(jsonSchema: RxJsonSchema<T>, hashFunction: HashFunction, runPreCreateHooks?: boolean): RxSchema<T>;
export declare function isRxSchema(obj: any): boolean;
/**
* Helper function to generate the document type out of the schema via TypeScript.
* Always use this together with `as const` on the schema literal so that the
* TypeScript compiler can infer the narrowest possible type.
*
* ## AI Agent Guidance — Canonical Schema Definition Pattern
*
* ```ts
* import {
* toTypedRxJsonSchema,
* ExtractDocumentTypeFromTypedRxJsonSchema,
* RxJsonSchema
* } from 'rxdb';
*
* const todoSchemaLiteral = {
* title: 'todo schema',
* version: 0,
* primaryKey: 'id',
* type: 'object',
* properties: {
* id: { type: 'string', maxLength: 100 },
* title: { type: 'string' },
* completed: { type: 'boolean' },
* createdAt: { type: 'string', format: 'date-time' },
* updatedAt: { type: 'string', format: 'date-time' }
* },
* required: ['id', 'title', 'completed', 'createdAt', 'updatedAt'],
* indexes: ['updatedAt', ['completed', 'updatedAt']]
* } as const;
*
* const schemaTyped = toTypedRxJsonSchema(todoSchemaLiteral);
* export type TodoDocType = ExtractDocumentTypeFromTypedRxJsonSchema<typeof schemaTyped>;
* export const todoSchema: RxJsonSchema<TodoDocType> = todoSchemaLiteral;
* ```
*
* @link https://github.com/pubkey/rxdb/discussions/3467
*/
export declare function toTypedRxJsonSchema<T extends DeepReadonly<RxJsonSchema<any>>>(schema: T): DeepMutable<T>;