@rushdb/javascript-sdk 0.17.1 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -21,7 +21,7 @@ export interface HttpClientResponseInterface {
21
21
  toStream: (streamCompleteCallback: () => void) => unknown;
22
22
  }
23
23
  export declare class HttpClient implements HttpClientInterface {
24
- static CONNECTION_CLOSED_ERROR_CODES: string[];
24
+ static CONNECTION_CLOSED_ERROR_CODES: Array<string>;
25
25
  static TIMEOUT_ERROR_CODE: string;
26
26
  makeRequest(url: string, { headers, method, protocol, requestData, timeout }: MakeRequestConfig): Promise<HttpClientResponseInterface>;
27
27
  static makeTimeoutError(): TimeoutError;
@@ -1,4 +1,5 @@
1
+ import type { MaybeArray } from '../types';
1
2
  export type RequestData<T extends Record<string, any> = Record<string, any>> = T;
2
- export type RequestHeaders = Record<string, number | string | string[]>;
3
- export type ResponseHeaderValue = string | string[];
3
+ export type RequestHeaders = Record<string, number | MaybeArray<string>>;
4
+ export type ResponseHeaderValue = MaybeArray<string>;
4
5
  export type ResponseHeaders = Record<string, ResponseHeaderValue>;
@@ -1,3 +1,4 @@
1
+ import type { SearchQuery } from '../types';
1
2
  export declare class UniquenessError extends Error {
2
3
  constructor(label: string, properties: any);
3
4
  }
@@ -5,5 +6,5 @@ export declare class EmptyTargetError extends Error {
5
6
  constructor(message: string);
6
7
  }
7
8
  export declare class NonUniqueResultError extends Error {
8
- constructor(duplicateCount: number, searchParams: Record<string, any>);
9
+ constructor(duplicateCount: number, searchQuery: SearchQuery);
9
10
  }
@@ -1,13 +1,31 @@
1
1
  import type { SearchQuery, Schema, FlattenTypes, InferSchemaTypesRead, InferSchemaTypesWrite, MaybeArray } from '../types/index.js';
2
- import type { DBRecord, DBRecordInstance, DBRecordsArrayInstance, RelationDetachOptions, RelationOptions, RelationTarget } from './record.js';
2
+ import type { DBRecord, DBRecordInstance, DBRecordsArrayInstance, DBRecordTarget, RelationDetachOptions, RelationOptions, RelationTarget } from './record.js';
3
3
  import type { Transaction } from './transaction.js';
4
4
  import { RestApiProxy } from '../api/rest-api-proxy.js';
5
5
  type RushDBInstance = {
6
6
  registerModel(model: Model): void;
7
+ toInstance<S extends Schema = Schema>(record: DBRecord<S>): DBRecordInstance<S, SearchQuery<S>>;
7
8
  };
9
+ /**
10
+ * Represents a model in the RushDB database.
11
+ * A model defines the structure and behavior of a specific type of record in the database.
12
+ * It provides methods for CRUD operations and relationship management.
13
+ *
14
+ * @typeParam S - The schema type that defines the structure of the model's records
15
+ */
8
16
  export declare class Model<S extends Schema = any> extends RestApiProxy {
9
17
  readonly label: string;
10
18
  readonly schema: S;
19
+ private readonly rushDBInstance?;
20
+ /**
21
+ * Creates a new Model instance.
22
+ *
23
+ * @param modelName - The name/label of the model in the database
24
+ * @param schema - The schema definition that describes the model's structure
25
+ * @param RushDBInstance - Optional RushDB instance for model registration.
26
+ * This is the recommended way to register models as it automatically
27
+ * registers the model with the RushDB instance during creation.
28
+ */
11
29
  constructor(modelName: string, schema: S, RushDBInstance?: RushDBInstance);
12
30
  /** @description
13
31
  * Type helper for a draft version of the schema.
@@ -35,42 +53,164 @@ export declare class Model<S extends Schema = any> extends RestApiProxy {
35
53
  * allowing efficient management of multiple records simultaneously.
36
54
  */
37
55
  readonly recordsArrayInstance: DBRecordsArrayInstance<S>;
56
+ /**
57
+ * Retrieves the model's label.
58
+ *
59
+ * @returns The label/name of the model
60
+ */
38
61
  getLabel(): string;
39
- find<Q extends SearchQuery<S> = SearchQuery<S>>(params?: Q & {
62
+ /**
63
+ * Converts a database record to a record instance with additional methods.
64
+ *
65
+ * @param record - The database record to convert
66
+ * @returns A record instance with additional methods
67
+ * @throws Error if no RushDB instance was provided during initialization
68
+ */
69
+ toInstance(record: DBRecord<S>): DBRecordInstance<S, SearchQuery<S>>;
70
+ /**
71
+ * Finds records that match the given search criteria.
72
+ *
73
+ * @param searchQuery - Optional search criteria
74
+ * @param transaction - Optional transaction or transaction ID
75
+ * @returns Promise resolving to matching records
76
+ */
77
+ find<Q extends SearchQuery<S> = SearchQuery<S>>(searchQuery?: Q & {
40
78
  labels?: never;
41
79
  }, transaction?: Transaction | string): Promise<DBRecordsArrayInstance<S, Q>>;
42
- findOne<Q extends SearchQuery<S> = SearchQuery<S>>(params?: Q & {
80
+ /**
81
+ * Finds a single record that matches the given search criteria.
82
+ *
83
+ * @param searchQuery - Optional search criteria
84
+ * @param transaction - Optional transaction or transaction ID
85
+ * @returns Promise resolving to the matching record or null if not found
86
+ */
87
+ findOne<Q extends SearchQuery<S> = SearchQuery<S>>(searchQuery?: Q & {
43
88
  labels?: never;
44
89
  limit?: never;
45
90
  skip?: never;
46
- }, transaction?: Transaction | string): Promise<DBRecordInstance<S, Q>>;
47
- findById(id: string, transaction?: Transaction | string): Promise<DBRecordInstance<S, {
48
- where: {
49
- $id: string;
50
- };
51
- }>>;
52
- findUniq<Q extends SearchQuery<S> = SearchQuery<S>>(params?: Q & {
91
+ }, transaction?: Transaction | string): Promise<DBRecordInstance<S, SearchQuery<S>>>;
92
+ /**
93
+ * Finds a record by its ID.
94
+ *
95
+ * @param id - The ID of the record to find
96
+ * @param transaction - Optional transaction or transaction ID
97
+ * @returns Promise resolving to the matching record or null if not found
98
+ */
99
+ findById(id: string, transaction?: Transaction | string): Promise<DBRecordInstance<S, SearchQuery<S>>>;
100
+ /**
101
+ * Finds a unique record that matches the given search criteria.
102
+ *
103
+ * @param searchQuery - Optional search criteria
104
+ * @param transaction - Optional transaction or transaction ID
105
+ * @returns Promise resolving to the unique matching record or null if not found
106
+ */
107
+ findUniq<Q extends SearchQuery<S> = SearchQuery<S>>(searchQuery?: Q & {
53
108
  labels?: never;
54
109
  limit?: never;
55
110
  skip?: never;
56
111
  }, transaction?: Transaction | string): Promise<DBRecordInstance<S, Q>>;
112
+ /**
113
+ * Creates a new record in the database.
114
+ *
115
+ * @param record - The record data to create
116
+ * @param transaction - Optional transaction or transaction ID
117
+ * @returns Promise resolving to the created record
118
+ * @throws UniquenessError if the record violates uniqueness constraints
119
+ */
57
120
  create(record: InferSchemaTypesWrite<S>, transaction?: Transaction | string): Promise<DBRecordInstance<S, SearchQuery<S>>>;
58
- attach(sourceId: string, target: RelationTarget, options?: RelationOptions, transaction?: Transaction | string): Promise<import("../index.node.js").ApiResponse<{
121
+ /**
122
+ * Attaches a relationship between records.
123
+ *
124
+ * @param params - Object containing source, target, and relationship options
125
+ * @param transaction - Optional transaction or transaction ID
126
+ * @returns Promise resolving to the result of the attach operation
127
+ */
128
+ attach({ source, target, options }: {
129
+ source: DBRecordTarget;
130
+ target: RelationTarget;
131
+ options?: RelationOptions;
132
+ }, transaction?: Transaction | string): Promise<import("../index.node.js").ApiResponse<{
59
133
  message: string;
60
- }, Record<string, any>>>;
61
- detach(sourceId: string, target: RelationTarget, options?: RelationDetachOptions, transaction?: Transaction | string): Promise<import("../index.node.js").ApiResponse<{
134
+ }>>;
135
+ /**
136
+ * Detaches a relationship between records.
137
+ *
138
+ * @param params - Object containing source, target, and detach options
139
+ * @param transaction - Optional transaction or transaction ID
140
+ * @returns Promise resolving to the result of the detach operation
141
+ */
142
+ detach({ source, target, options }: {
143
+ source: DBRecordTarget;
144
+ target: RelationTarget;
145
+ options?: RelationDetachOptions;
146
+ }, transaction?: Transaction | string): Promise<import("../index.node.js").ApiResponse<{
62
147
  message: string;
63
- }, Record<string, any>>>;
148
+ }>>;
149
+ /**
150
+ * Internal method to handle both set and update operations.
151
+ *
152
+ * @param target - The target record to modify
153
+ * @param record - The data to set or update
154
+ * @param method - The operation type ('set' or 'update')
155
+ * @param transaction - Optional transaction or transaction ID
156
+ * @returns Promise resolving to the modified record
157
+ * @throws UniquenessError if the modification violates uniqueness constraints
158
+ * @private
159
+ */
64
160
  private handleSetOrUpdate;
65
- set(id: string, record: InferSchemaTypesWrite<S>, transaction?: Transaction | string): Promise<DBRecordInstance<S, SearchQuery<S>>>;
66
- update(id: string, record: Partial<InferSchemaTypesWrite<S>>, transaction?: Transaction | string): Promise<DBRecordInstance<S, SearchQuery<S>>>;
67
- createMany(records: InferSchemaTypesWrite<S>[], transaction?: Transaction | string): Promise<DBRecordsArrayInstance<S, SearchQuery<S>>>;
68
- delete(searchParams: Omit<SearchQuery<S>, 'labels'>, transaction?: Transaction | string): Promise<import("../index.node.js").ApiResponse<{
161
+ /**
162
+ * Sets all fields of a record to the provided values.
163
+ *
164
+ * @param target - The target record to modify
165
+ * @param record - The new values to set
166
+ * @param transaction - Optional transaction or transaction ID
167
+ * @returns Promise resolving to the modified record
168
+ */
169
+ set(target: DBRecordTarget, record: InferSchemaTypesWrite<S>, transaction?: Transaction | string): Promise<DBRecordInstance<S, SearchQuery<S>>>;
170
+ /**
171
+ * Updates specified fields of a record.
172
+ *
173
+ * @param target - The target record to modify
174
+ * @param record - The fields to update and their new values
175
+ * @param transaction - Optional transaction or transaction ID
176
+ * @returns Promise resolving to the modified record
177
+ */
178
+ update(target: DBRecordTarget, record: Partial<InferSchemaTypesWrite<S>>, transaction?: Transaction | string): Promise<DBRecordInstance<S, SearchQuery<S>>>;
179
+ /**
180
+ * Creates multiple records in a single operation.
181
+ *
182
+ * @param records - Array of records to create
183
+ * @param transaction - Optional transaction or transaction ID
184
+ * @returns Promise resolving to the created records
185
+ * @throws UniquenessError if any record violates uniqueness constraints
186
+ */
187
+ createMany(records: Array<InferSchemaTypesWrite<S>>, transaction?: Transaction | string): Promise<DBRecordsArrayInstance<S, SearchQuery<S>>>;
188
+ /**
189
+ * Deletes records that match the given search criteria.
190
+ *
191
+ * @param searchQuery - Search criteria for records to delete
192
+ * @param transaction - Optional transaction or transaction ID
193
+ * @returns Promise resolving to the result of the delete operation
194
+ * @throws EmptyTargetError if no search criteria are provided
195
+ */
196
+ delete(searchQuery: Omit<SearchQuery<S>, 'labels'>, transaction?: Transaction | string): Promise<import("../index.node.js").ApiResponse<{
69
197
  message: string;
70
- }, Record<string, any>>>;
198
+ }>>;
199
+ /**
200
+ * Deletes one or more records by their IDs.
201
+ *
202
+ * @param idOrIds - Single ID or array of IDs to delete
203
+ * @param transaction - Optional transaction or transaction ID
204
+ * @returns Promise resolving to the result of the delete operation
205
+ */
71
206
  deleteById(idOrIds: MaybeArray<string>, transaction?: Transaction | string): Promise<import("../index.node.js").ApiResponse<{
72
207
  message: string;
73
- }, Record<string, any>>>;
208
+ }>>;
74
209
  }
210
+ /**
211
+ * Helper type that infers the type structure from a Model instance.
212
+ *
213
+ * @typeParam M - The Model type to infer from
214
+ */
75
215
  export type InferType<M extends Model<any> = Model<any>> = FlattenTypes<InferSchemaTypesRead<M['schema']>>;
76
216
  export {};
@@ -1,4 +1,4 @@
1
- import type { AnyObject, ExtractAggregateFields, FlattenTypes, InferSchemaTypesRead, InferSchemaTypesWrite, MaybeArray, OptionalKeysRead, PropertyType, PropertyValue, PropertyWithValue, RequiredKeysRead, Schema, SearchQuery } from '../types/index.js';
1
+ import type { ExtractAggregateFields, FlattenTypes, InferSchemaTypesRead, InferSchemaTypesWrite, MaybeArray, OptionalKeysRead, PropertyDraft, RequiredKeysRead, Schema, SearchQuery } from '../types/index.js';
2
2
  import type { Transaction } from './transaction.js';
3
3
  import { RestApiProxy } from '../api/rest-api-proxy.js';
4
4
  type DBRecordInternalProps<S extends Schema = Schema> = {
@@ -33,84 +33,40 @@ export type RelationOptions = {
33
33
  };
34
34
  export type RelationDetachOptions = {
35
35
  direction?: RelationDirection;
36
- typeOrTypes?: string | string[];
36
+ typeOrTypes?: MaybeArray<string>;
37
+ };
38
+ export type DBRecordCreationOptions = {
39
+ returnResult?: boolean;
40
+ suggestTypes?: boolean;
41
+ castNumberArraysToVectors?: boolean;
42
+ convertNumericValuesToNumbers?: boolean;
43
+ capitalizeLabels?: boolean;
44
+ relationshipType?: string;
37
45
  };
38
- export declare class DBRecordsBatchDraft {
39
- label: string;
40
- options?: {
41
- returnResult?: boolean;
42
- suggestTypes?: boolean;
43
- };
44
- payload: MaybeArray<AnyObject>;
45
- constructor({ label, options, payload }: {
46
- label: string;
47
- options?: {
48
- returnResult?: boolean;
49
- suggestTypes?: boolean;
50
- };
51
- payload: AnyObject;
52
- });
53
- toJson(): {
54
- label: string;
55
- options: {
56
- returnResult?: boolean;
57
- suggestTypes?: boolean;
58
- } | undefined;
59
- payload: MaybeArray<AnyObject>;
60
- };
61
- }
62
- export declare class DBRecordDraft {
63
- label: string;
64
- properties?: Array<{
65
- metadata?: string;
66
- name: string;
67
- type: PropertyType;
68
- value: PropertyValue;
69
- valueSeparator?: string;
70
- }>;
71
- constructor({ label, properties }: {
72
- label: string;
73
- properties?: Array<PropertyWithValue & {
74
- metadata?: string;
75
- valueSeparator?: string;
76
- }>;
77
- relation?: RelationOptions;
78
- });
79
- toJson(): {
80
- label: string;
81
- properties: {
82
- metadata?: string;
83
- name: string;
84
- type: PropertyType;
85
- value: PropertyValue;
86
- valueSeparator?: string;
87
- }[] | undefined;
88
- };
89
- }
90
46
  export declare class DBRecordInstance<S extends Schema = Schema, Q extends SearchQuery<S> = SearchQuery<S>> extends RestApiProxy {
91
47
  data?: DBRecordInferred<S, Q>;
92
48
  constructor(data?: DBRecordInferred<S, Q>);
93
- get id(): string;
94
- get label(): string;
95
- get proptypes(): FlattenTypes<FlattenTypes<{ [Key in RequiredKeysRead<S>]: S[Key]["type"]; } & { [Key_1 in OptionalKeysRead<S>]?: S[Key_1]["type"] | undefined; }>> | undefined;
96
- get date(): Date;
97
- get timestamp(): number;
49
+ id(): string;
50
+ label(): string;
51
+ proptypes(): FlattenTypes<FlattenTypes<{ [Key in RequiredKeysRead<S>]: S[Key]["type"]; } & { [Key_1 in OptionalKeysRead<S>]?: S[Key_1]["type"] | undefined; }>> | undefined;
52
+ date(): Date;
53
+ timestamp(): number;
98
54
  delete(transaction?: Transaction | string): Promise<import("../index.node.js").ApiResponse<{
99
55
  message: string;
100
- }, Record<string, any>>>;
101
- update<S extends Schema = Schema>(partialData: DBRecordDraft | Partial<InferSchemaTypesWrite<S>>, transaction?: Transaction | string): Promise<DBRecordInstance<S, SearchQuery<S>>>;
102
- set<S extends Schema = Schema>(data: DBRecordDraft | InferSchemaTypesWrite<S>, transaction?: Transaction | string): Promise<DBRecordInstance<S, SearchQuery<S>>>;
56
+ }>>;
57
+ update<S extends Schema = Schema>(data: Partial<InferSchemaTypesWrite<S>> | Array<PropertyDraft>, transaction?: Transaction | string): Promise<DBRecordInstance<S, SearchQuery<S>>>;
58
+ set<S extends Schema = Schema>(data: InferSchemaTypesWrite<S> | Array<PropertyDraft>, transaction?: Transaction | string): Promise<DBRecordInstance<S, SearchQuery<S>>>;
103
59
  attach(target: RelationTarget, options?: RelationOptions, transaction?: Transaction | string): Promise<import("../index.node.js").ApiResponse<{
104
60
  message: string;
105
- }, Record<string, any>>>;
61
+ }>>;
106
62
  detach(target: RelationTarget, options?: RelationDetachOptions, transaction?: Transaction | string): Promise<import("../index.node.js").ApiResponse<{
107
63
  message: string;
108
- }, Record<string, any>>>;
64
+ }>>;
109
65
  }
110
66
  export declare class DBRecordsArrayInstance<S extends Schema = Schema, Q extends SearchQuery<S> = SearchQuery<S>> extends RestApiProxy {
111
- data?: DBRecordInferred<S, Q>[];
67
+ data?: Array<DBRecordInstance<S, Q>>;
112
68
  total: number | undefined;
113
- searchParams?: SearchQuery<S>;
114
- constructor(data?: DBRecordInferred<S, Q>[], total?: number, searchParams?: SearchQuery<S>);
69
+ searchQuery?: SearchQuery<S>;
70
+ constructor(data?: Array<DBRecordInstance<S, Q>>, total?: number, searchQuery?: SearchQuery<S>);
115
71
  }
116
72
  export {};