@rushdb/javascript-sdk 1.3.1 → 1.4.0

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.
@@ -1,11 +1,7 @@
1
1
  import type { SearchQuery, Schema, FlattenTypes, InferSchemaTypesRead, InferSchemaTypesWrite, MaybeArray } from '../types/index.js';
2
2
  import type { DBRecord, DBRecordInstance, DBRecordsArrayInstance, DBRecordTarget, RelationDetachOptions, RelationOptions, RelationTarget } from './record.js';
3
3
  import type { Transaction } from './transaction.js';
4
- import { RestApiProxy } from '../api/rest-api-proxy.js';
5
- type RushDBInstance = {
6
- registerModel(model: Model): void;
7
- toInstance<S extends Schema = Schema>(record: DBRecord<S>): DBRecordInstance<S, SearchQuery<S>>;
8
- };
4
+ import { RushDB } from './sdk';
9
5
  /**
10
6
  * Represents a model in the RushDB database.
11
7
  * A model defines the structure and behavior of a specific type of record in the database.
@@ -13,20 +9,19 @@ type RushDBInstance = {
13
9
  *
14
10
  * @typeParam S - The schema type that defines the structure of the model's records
15
11
  */
16
- export declare class Model<S extends Schema = any> extends RestApiProxy {
12
+ export declare class Model<S extends Schema = any> {
17
13
  readonly label: string;
18
14
  readonly schema: S;
19
- private readonly rushDBInstance?;
20
15
  /**
21
16
  * Creates a new Model instance.
22
17
  *
23
18
  * @param modelName - The name/label of the model in the database
24
19
  * @param schema - The schema definition that describes the model's structure
25
- * @param RushDBInstance - Optional RushDB instance for model registration.
20
+ * @param rushDBInstance - Optional RushDB instance for model registration.
26
21
  * This is the recommended way to register models as it automatically
27
22
  * registers the model with the RushDB instance during creation.
28
23
  */
29
- constructor(modelName: string, schema: S, RushDBInstance?: RushDBInstance);
24
+ constructor(modelName: string, schema: S);
30
25
  /** @description
31
26
  * Type helper for a draft version of the schema.
32
27
  * Represents a flat object containing only the record's own properties
@@ -36,7 +31,14 @@ export declare class Model<S extends Schema = any> extends RestApiProxy {
36
31
  */
37
32
  readonly draft: InferType<Model<S>>;
38
33
  /** @description
39
- * Type helper for a fully-defined record with database representation.
34
+ * Type helper for a SearchQuery of the schema.
35
+ * Represents a structured query input that enables filtering,
36
+ * sorting, pagination, and aggregation of records based on schema-defined fields.
37
+ * Useful for composing reusable, type-safe search expressions.
38
+ */
39
+ readonly searchQuery: SearchQuery<S>;
40
+ /** @description
41
+ * Type helper for a fully defined record with database representation.
40
42
  * Similar to the draft, but includes all fields that come with the record's
41
43
  * database-side representation, such as `__id`, `__label`, and `__proptypes`.
42
44
  */
@@ -60,13 +62,28 @@ export declare class Model<S extends Schema = any> extends RestApiProxy {
60
62
  */
61
63
  getLabel(): string;
62
64
  /**
63
- * Converts a database record to a record instance with additional methods.
65
+ * Retrieves the initialized RushDB instance or waits for initialization to complete.
66
+ *
67
+ * This method is called internally by all data access methods in the Model class.
68
+ * It ensures that the database connection is properly established before executing
69
+ * any operations. For this to work, make sure you've created a RushDB instance
70
+ * somewhere in your code and imported that module in your application's entry point.
71
+ *
72
+ * Note: If you get "No RushDB instance found" errors, it usually means the module
73
+ * where you create your RushDB instance hasn't been imported yet.
74
+ *
75
+ * @returns Promise resolving to the initialized RushDB instance
76
+ * @throws Error if no RushDB instance has been created yet
77
+ */
78
+ getRushDBInstance(): Promise<RushDB>;
79
+ /**
80
+ * Converts a database record to a record instance (DBRecordInstance) with additional methods.
64
81
  *
65
82
  * @param record - The database record to convert
66
83
  * @returns A record instance with additional methods
67
84
  * @throws Error if no RushDB instance was provided during initialization
68
85
  */
69
- toInstance(record: DBRecord<S>): DBRecordInstance<S, SearchQuery<S>>;
86
+ toDBRecordInstance(record: DBRecord<S>): Promise<DBRecordInstance<Schema, SearchQuery<Schema>>>;
70
87
  /**
71
88
  * Finds records that match the given search criteria.
72
89
  *
@@ -213,4 +230,3 @@ export declare class Model<S extends Schema = any> extends RestApiProxy {
213
230
  * @typeParam M - The Model type to infer from
214
231
  */
215
232
  export type InferType<M extends Model<any> = Model<any>> = FlattenTypes<InferSchemaTypesRead<M['schema']>>;
216
- export {};
@@ -1,6 +1,9 @@
1
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
- import { RestApiProxy } from '../api/rest-api-proxy.js';
3
+ /**
4
+ * Internal properties that are present on all database records.
5
+ * These are system fields managed by RushDB.
6
+ */
4
7
  type DBRecordInternalProps<S extends Schema = Schema> = {
5
8
  readonly __id: string;
6
9
  readonly __label: string;
@@ -10,15 +13,36 @@ type DBRecordInternalProps<S extends Schema = Schema> = {
10
13
  [Key in OptionalKeysRead<S>]?: S[Key]['type'];
11
14
  }>;
12
15
  };
16
+ /**
17
+ * Properties defined by the user's schema.
18
+ */
13
19
  export type RecordProps<S extends Schema = Schema> = S extends S ? InferSchemaTypesRead<S> : {
14
20
  [K in keyof S]?: S[K];
15
21
  };
22
+ /**
23
+ * DBRecord with possible aggregation fields based on a search query.
24
+ */
16
25
  export type DBRecordInferred<S extends Schema, Q extends SearchQuery<S>> = Q extends {
17
26
  aggregate: infer A extends Record<string, any>;
18
27
  } ? DBRecord<S> & ExtractAggregateFields<A> : DBRecord<S>;
28
+ /**
29
+ * Represents a database record with both system fields and user-defined fields.
30
+ * This is the raw data structure returned from database queries.
31
+ */
19
32
  export type DBRecord<S extends Schema = Schema> = FlattenTypes<DBRecordInternalProps<S> & FlattenTypes<RecordProps<S>>>;
33
+ /**
34
+ * Target for operations like update, delete, etc.
35
+ * Can be a record object, a record instance, or a record ID.
36
+ */
20
37
  export type DBRecordTarget = DBRecord<any> | DBRecordInstance<any> | string;
38
+ /**
39
+ * Target for relationship operations.
40
+ * Can be an array of records, record instances, or record IDs.
41
+ */
21
42
  export type RelationTarget = DBRecordsArrayInstance<any> | MaybeArray<DBRecord<any>> | MaybeArray<DBRecordInstance<any>> | MaybeArray<string>;
43
+ /**
44
+ * Represents a relationship between two records.
45
+ */
22
46
  export type Relation = {
23
47
  sourceId: string;
24
48
  sourceLabel: string;
@@ -26,15 +50,28 @@ export type Relation = {
26
50
  targetLabel: string;
27
51
  type: string;
28
52
  };
53
+ /**
54
+ * Direction of a relationship.
55
+ * 'in' for incoming relationships, 'out' for outgoing relationships.
56
+ */
29
57
  export type RelationDirection = 'in' | 'out';
58
+ /**
59
+ * Options for creating or modifying relationships.
60
+ */
30
61
  export type RelationOptions = {
31
62
  direction?: RelationDirection;
32
63
  type?: string;
33
64
  };
65
+ /**
66
+ * Options for detaching (removing) relationships.
67
+ */
34
68
  export type RelationDetachOptions = {
35
69
  direction?: RelationDirection;
36
70
  typeOrTypes?: MaybeArray<string>;
37
71
  };
72
+ /**
73
+ * Options for creating database records.
74
+ */
38
75
  export type DBRecordCreationOptions = {
39
76
  returnResult?: boolean;
40
77
  suggestTypes?: boolean;
@@ -43,30 +80,136 @@ export type DBRecordCreationOptions = {
43
80
  capitalizeLabels?: boolean;
44
81
  relationshipType?: string;
45
82
  };
46
- export declare class DBRecordInstance<S extends Schema = Schema, Q extends SearchQuery<S> = SearchQuery<S>> extends RestApiProxy {
47
- data?: DBRecordInferred<S, Q>;
83
+ export declare class DBRecordInstance<S extends Schema = Schema, Q extends SearchQuery<S> = SearchQuery<S>> {
84
+ data: DBRecordInferred<S, Q>;
85
+ /**
86
+ * Creates a new DBRecordInstance.
87
+ * Typically, you don't create these directly but receive them from query results.
88
+ *
89
+ * @param data - The raw record data
90
+ */
48
91
  constructor(data?: DBRecordInferred<S, Q>);
92
+ /**
93
+ *
94
+ * Checks if this record instance exists.
95
+ * A record is considered to exist if it has a valid ID and label.
96
+ *
97
+ * @returns True if the record exists, false otherwise
98
+ */
99
+ exists(): boolean;
100
+ /**
101
+ * Gets the unique ID of this record.
102
+ *
103
+ * @returns The record's ID
104
+ * @throws Error if the ID is missing or invalid
105
+ */
49
106
  id(): string;
107
+ /**
108
+ * Gets the label (type) of this record.
109
+ *
110
+ * @returns The record's label
111
+ * @throws Error if the label is missing or invalid
112
+ */
50
113
  label(): string;
114
+ /**
115
+ * Gets the property types of this record.
116
+ *
117
+ * @returns The record's property types
118
+ * @throws Error if the property types are missing or invalid
119
+ */
51
120
  proptypes(): FlattenTypes<FlattenTypes<{ [Key in RequiredKeysRead<S>]: S[Key]["type"]; } & { [Key_1 in OptionalKeysRead<S>]?: S[Key_1]["type"] | undefined; }>> | undefined;
121
+ /**
122
+ * Gets the creation date of this record.
123
+ * The date is derived from the record's ID.
124
+ *
125
+ * @returns The record's creation date
126
+ * @throws Error if the ID is missing or invalid
127
+ */
52
128
  date(): Date;
129
+ /**
130
+ * Gets the creation timestamp of this record.
131
+ * The timestamp is derived from the record's ID.
132
+ *
133
+ * @returns The record's creation timestamp
134
+ * @throws Error if the ID is missing or invalid
135
+ */
53
136
  timestamp(): number;
137
+ /**
138
+ * Deletes this record from the database.
139
+ *
140
+ * @param transaction - Optional transaction or transaction ID
141
+ * @returns Promise resolving to the result of the delete operation
142
+ * @throws Error if the record data is undefined
143
+ */
54
144
  delete(transaction?: Transaction | string): Promise<import("../index.node.js").ApiResponse<{
55
145
  message: string;
56
146
  }>>;
147
+ /**
148
+ * Updates specific fields of this record.
149
+ *
150
+ * @param data - The fields to update and their new values
151
+ * @param transaction - Optional transaction or transaction ID
152
+ * @returns Promise resolving to the updated record
153
+ * @throws Error if the record data is undefined
154
+ */
57
155
  update<S extends Schema = Schema>(data: Partial<InferSchemaTypesWrite<S>> | Array<PropertyDraft>, transaction?: Transaction | string): Promise<DBRecordInstance<S, SearchQuery<S>>>;
156
+ /**
157
+ * Sets all fields of this record to the provided values.
158
+ * This replaces all existing field values.
159
+ *
160
+ * @param data - The new field values
161
+ * @param transaction - Optional transaction or transaction ID
162
+ * @returns Promise resolving to the updated record
163
+ * @throws Error if the record data is undefined
164
+ */
58
165
  set<S extends Schema = Schema>(data: InferSchemaTypesWrite<S> | Array<PropertyDraft>, transaction?: Transaction | string): Promise<DBRecordInstance<S, SearchQuery<S>>>;
166
+ /**
167
+ * Creates a relationship between this record and another record or records.
168
+ *
169
+ * @param target - The target record(s) to create a relationship with
170
+ * @param options - Optional relationship options
171
+ * @param transaction - Optional transaction or transaction ID
172
+ * @returns Promise resolving to the result of the attach operation
173
+ * @throws Error if the record data is undefined
174
+ */
59
175
  attach(target: RelationTarget, options?: RelationOptions, transaction?: Transaction | string): Promise<import("../index.node.js").ApiResponse<{
60
176
  message: string;
61
177
  }>>;
178
+ /**
179
+ * Removes a relationship between this record and another record or records.
180
+ *
181
+ * @param target - The target record(s) to remove the relationship from
182
+ * @param options - Optional detach options
183
+ * @param transaction - Optional transaction or transaction ID
184
+ * @returns Promise resolving to the result of the detach operation
185
+ * @throws Error if the record data is undefined
186
+ */
62
187
  detach(target: RelationTarget, options?: RelationDetachOptions, transaction?: Transaction | string): Promise<import("../index.node.js").ApiResponse<{
63
188
  message: string;
64
189
  }>>;
65
190
  }
66
- export declare class DBRecordsArrayInstance<S extends Schema = Schema, Q extends SearchQuery<S> = SearchQuery<S>> extends RestApiProxy {
67
- data?: Array<DBRecordInstance<S, Q>>;
68
- total: number | undefined;
191
+ /**
192
+ * Represents a collection of database records with additional methods for batch operations.
193
+ * This class is typically returned from queries that match multiple records.
194
+ *
195
+ * @typeParam S - The schema type that defines the records' structure
196
+ * @typeParam Q - The search query type that was used to retrieve these records
197
+ */
198
+ export declare class DBRecordsArrayInstance<S extends Schema = Schema, Q extends SearchQuery<S> = SearchQuery<S>> {
199
+ /** The array of record instances */
200
+ data: Array<DBRecordInstance<S, Q>>;
201
+ /** The total number of records matching the query (may be more than the number in data) */
202
+ total: number;
203
+ /** The search query that was used to retrieve these records */
69
204
  searchQuery?: SearchQuery<S>;
205
+ /**
206
+ * Creates a new DBRecordsArrayInstance.
207
+ * Typically, you don't create these directly but receive them from query results.
208
+ *
209
+ * @param data - The array of record instances
210
+ * @param total - The total number of records matching the query
211
+ * @param searchQuery - The search query that was used to retrieve these records
212
+ */
70
213
  constructor(data?: Array<DBRecordInstance<S, Q>>, total?: number, searchQuery?: SearchQuery<S>);
71
214
  }
72
215
  export {};