type-surrealdb
Version:
Typescript support and class utils for SurrealDB
105 lines (104 loc) • 3.68 kB
TypeScript
/**
* FieldKind
* Represents the types that can be used in SurrealDB.
*/
type GenericType = '$$generic';
type BasicType = GenericType | 'any' | 'null' | 'bool' | 'bytes' | 'datetime' | 'decimal' | 'duration' | 'float' | 'int' | 'number' | 'object' | 'point' | 'string' | 'uuid';
type ObjectType = 'object';
type TypedType = 'geometry' | 'option' | 'set' | 'array' | 'record';
type FieldType = BasicType | ObjectType | TypedType;
type GeometryDataTypes = 'feature' | 'point' | 'LineString' | 'polygon' | 'multipoint' | 'multiline' | 'multipolygon' | 'collection';
type OptionalDataTypes = 'number' | undefined;
type SetDataTypes = BasicType | undefined;
type ArrayDataTypes = BasicType | string | undefined;
type RecordDataTypes = string | undefined;
/**
* FieldSchemaProperty
* Represents a property within a SurrealDB schema.
*/
interface FieldSchemaProperty {
type?: BasicType | ObjectType | TypedType;
default?: string;
value?: string;
assertion?: string;
indexed?: boolean;
unique?: boolean;
primary?: boolean;
optional?: boolean;
}
interface PrimaryFieldConfig extends FieldSchemaProperty {
primary: boolean;
}
interface ObjectFieldConfig extends FieldSchemaProperty {
type: ObjectType;
object: typeof TableSchema;
properties?: PropertiesTypes;
}
interface BasicFieldConfig extends FieldSchemaProperty {
type: BasicType;
}
interface TypedFieldConfig extends FieldSchemaProperty {
type: TypedType;
typed: GeometryDataTypes | OptionalDataTypes | SetDataTypes | ArrayDataTypes | RecordDataTypes;
}
interface GeometryFieldConfig extends TypedFieldConfig {
type: 'geometry';
typed: GeometryDataTypes;
}
interface OptionalFieldConfig extends TypedFieldConfig {
type: 'option';
typed: OptionalDataTypes;
}
interface SetFieldConfig extends TypedFieldConfig {
type: 'set';
typed: SetDataTypes;
length: number | undefined;
}
interface ArrayFieldConfig extends TypedFieldConfig {
type: 'array';
typed: ArrayDataTypes;
length: number | undefined;
}
interface RecordFieldConfig extends TypedFieldConfig {
type: 'record';
typed: RecordDataTypes;
}
type FieldConfig = PrimaryFieldConfig | BasicFieldConfig | ObjectFieldConfig | GeometryFieldConfig | OptionalFieldConfig | SetFieldConfig | ArrayFieldConfig | RecordFieldConfig;
interface PropertiesTypes {
[keys: string]: FieldConfig;
}
/**
* ObjectSchema
* Represents a schema for a SurrealDB object.
*/
interface IndexSchema {
name?: string;
fields: string[];
unique?: boolean;
}
type TableGenericConfig = {
name: string;
generic: string;
};
type TableConfig = {
tables: (string | TableGenericConfig)[];
schemaMode?: 'SCHEMAFULL' | 'SCHEMALESS';
indexes?: IndexSchema[];
};
export interface SchemaObject extends TableConfig {
properties: PropertiesTypes;
}
export declare class TableSchema {
static SurrealdbSchema: SchemaObject;
}
export declare function Field(kind: FieldType): PropertyDecorator;
export declare function Field(config: FieldConfig): PropertyDecorator;
/**
* This will add the "name" field to the static schema object. While the name could be inferred from the class's
* constructor, it is required because obfuscating the JS bundle in production builds changes the class names, and thus
* produces inconsistent or duplicate SurrealDB table names.
* @param name The table name in SurrealDB. This can be different from the class name
*/
export declare function Table(config: string | string[] | TableConfig): ClassDecorator;
export declare function generateSurqlSchema<T extends typeof TableSchema>(entities: T[], comments?: boolean): string;
export {};