@3lineas/d1-orm 1.0.3 → 1.0.5
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.
- package/README.md +79 -69
- package/dist/cli/index.js +147 -49
- package/dist/cli/index.mjs +150 -53
- package/dist/index.d.mts +384 -5
- package/dist/index.d.ts +384 -5
- package/dist/index.js +395 -13
- package/dist/index.mjs +394 -13
- package/package.json +6 -4
package/dist/index.d.mts
CHANGED
|
@@ -22,49 +22,228 @@ interface D1ExecResult {
|
|
|
22
22
|
duration: number;
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
+
/**
|
|
26
|
+
* Database connection wrapper for D1.
|
|
27
|
+
*/
|
|
25
28
|
declare class Connection {
|
|
29
|
+
/**
|
|
30
|
+
* The underlying D1Database instance.
|
|
31
|
+
*/
|
|
26
32
|
protected db: D1Database;
|
|
33
|
+
/**
|
|
34
|
+
* Create a new Connection instance.
|
|
35
|
+
*
|
|
36
|
+
* @param database - The D1Database instance.
|
|
37
|
+
*/
|
|
27
38
|
constructor(database: D1Database);
|
|
39
|
+
/**
|
|
40
|
+
* Execute a SELECT statement.
|
|
41
|
+
*
|
|
42
|
+
* @param query - The SQL query string.
|
|
43
|
+
* @param bindings - The parameter bindings.
|
|
44
|
+
* @returns A promise that resolves to an array of results.
|
|
45
|
+
*/
|
|
28
46
|
select(query: string, bindings?: any[]): Promise<any[]>;
|
|
47
|
+
/**
|
|
48
|
+
* Execute an INSERT statement.
|
|
49
|
+
*
|
|
50
|
+
* @param query - The SQL query string.
|
|
51
|
+
* @param bindings - The parameter bindings.
|
|
52
|
+
* @returns A promise that resolves to true on success.
|
|
53
|
+
*/
|
|
29
54
|
insert(query: string, bindings?: any[]): Promise<boolean>;
|
|
55
|
+
/**
|
|
56
|
+
* Execute an UPDATE statement.
|
|
57
|
+
*
|
|
58
|
+
* @param query - The SQL query string.
|
|
59
|
+
* @param bindings - The parameter bindings.
|
|
60
|
+
* @returns A promise that resolves to true on success.
|
|
61
|
+
*/
|
|
30
62
|
update(query: string, bindings?: any[]): Promise<boolean>;
|
|
63
|
+
/**
|
|
64
|
+
* Execute a DELETE statement.
|
|
65
|
+
*
|
|
66
|
+
* @param query - The SQL query string.
|
|
67
|
+
* @param bindings - The parameter bindings.
|
|
68
|
+
* @returns A promise that resolves to true on success.
|
|
69
|
+
*/
|
|
31
70
|
delete(query: string, bindings?: any[]): Promise<boolean>;
|
|
71
|
+
/**
|
|
72
|
+
* Execute an arbitrary SQL statement.
|
|
73
|
+
*
|
|
74
|
+
* @param query - The SQL query string.
|
|
75
|
+
* @param bindings - The parameter bindings.
|
|
76
|
+
* @returns A promise that resolves to true on success.
|
|
77
|
+
*/
|
|
32
78
|
statement(query: string, bindings?: any[]): Promise<boolean>;
|
|
33
79
|
}
|
|
34
80
|
|
|
35
81
|
type D1Value = string | number | boolean | null;
|
|
82
|
+
/**
|
|
83
|
+
* Fluent Query Builder for D1.
|
|
84
|
+
*/
|
|
36
85
|
declare class QueryBuilder {
|
|
86
|
+
/**
|
|
87
|
+
* The database connection.
|
|
88
|
+
*/
|
|
37
89
|
protected connection: Connection;
|
|
90
|
+
/**
|
|
91
|
+
* The table to query.
|
|
92
|
+
*/
|
|
38
93
|
protected table: string;
|
|
94
|
+
/**
|
|
95
|
+
* The columns to select.
|
|
96
|
+
*/
|
|
39
97
|
protected columns: string[];
|
|
98
|
+
/**
|
|
99
|
+
* The where constraints for the query.
|
|
100
|
+
*/
|
|
40
101
|
protected wheres: string[];
|
|
102
|
+
/**
|
|
103
|
+
* The bindings for the query.
|
|
104
|
+
*/
|
|
41
105
|
protected bindings: D1Value[];
|
|
106
|
+
/**
|
|
107
|
+
* The orderings for the query.
|
|
108
|
+
*/
|
|
42
109
|
protected orders: string[];
|
|
110
|
+
/**
|
|
111
|
+
* The maximum number of records to return.
|
|
112
|
+
*/
|
|
43
113
|
protected limitValue: number | null;
|
|
114
|
+
/**
|
|
115
|
+
* The number of records to skip.
|
|
116
|
+
*/
|
|
44
117
|
protected offsetValue: number | null;
|
|
118
|
+
/**
|
|
119
|
+
* Create a new QueryBuilder instance.
|
|
120
|
+
*
|
|
121
|
+
* @param connection - The connection instance.
|
|
122
|
+
* @param table - The table name.
|
|
123
|
+
*/
|
|
45
124
|
constructor(connection: Connection, table: string);
|
|
125
|
+
/**
|
|
126
|
+
* Add a SELECT clause to the query.
|
|
127
|
+
*
|
|
128
|
+
* @param columns - The columns to select.
|
|
129
|
+
* @returns The QueryBuilder instance.
|
|
130
|
+
*/
|
|
46
131
|
select(...columns: string[]): this;
|
|
132
|
+
/**
|
|
133
|
+
* Add a basic WHERE clause to the query.
|
|
134
|
+
*
|
|
135
|
+
* @param column - The column name.
|
|
136
|
+
* @param operator - The operator or value.
|
|
137
|
+
* @param value - The value if operator is provided.
|
|
138
|
+
* @returns The QueryBuilder instance.
|
|
139
|
+
*/
|
|
47
140
|
where(column: string, operator: string, value?: D1Value): this;
|
|
141
|
+
/**
|
|
142
|
+
* Add an OR WHERE clause to the query.
|
|
143
|
+
*
|
|
144
|
+
* @param column - The column name.
|
|
145
|
+
* @param operator - The operator or value.
|
|
146
|
+
* @param value - The value if operator is provided.
|
|
147
|
+
* @returns The QueryBuilder instance.
|
|
148
|
+
*/
|
|
48
149
|
orWhere(column: string, operator: string, value?: D1Value): this;
|
|
150
|
+
/**
|
|
151
|
+
* Add an ORDER BY clause to the query.
|
|
152
|
+
*
|
|
153
|
+
* @param column - The column name.
|
|
154
|
+
* @param direction - The direction of the order.
|
|
155
|
+
* @returns The QueryBuilder instance.
|
|
156
|
+
*/
|
|
49
157
|
orderBy(column: string, direction?: "asc" | "desc"): this;
|
|
158
|
+
/**
|
|
159
|
+
* Add a LIMIT clause to the query.
|
|
160
|
+
*
|
|
161
|
+
* @param limit - The limit value.
|
|
162
|
+
* @returns The QueryBuilder instance.
|
|
163
|
+
*/
|
|
50
164
|
limit(limit: number): this;
|
|
165
|
+
/**
|
|
166
|
+
* Add an OFFSET clause to the query.
|
|
167
|
+
*
|
|
168
|
+
* @param offset - The offset value.
|
|
169
|
+
* @returns The QueryBuilder instance.
|
|
170
|
+
*/
|
|
51
171
|
offset(offset: number): this;
|
|
172
|
+
/**
|
|
173
|
+
* Get the SQL representation of the query.
|
|
174
|
+
*
|
|
175
|
+
* @returns An object containing the SQL string and the bindings.
|
|
176
|
+
*/
|
|
52
177
|
toSql(): {
|
|
53
178
|
sql: string;
|
|
54
179
|
bindings: D1Value[];
|
|
55
180
|
};
|
|
181
|
+
/**
|
|
182
|
+
* Execute the query and get the results.
|
|
183
|
+
*
|
|
184
|
+
* @returns A promise that resolves to an array of records.
|
|
185
|
+
*/
|
|
56
186
|
get<T = any>(): Promise<T[]>;
|
|
187
|
+
/**
|
|
188
|
+
* Execute the query and get the first result.
|
|
189
|
+
*
|
|
190
|
+
* @returns A promise that resolves to the first record or null.
|
|
191
|
+
*/
|
|
57
192
|
first<T = any>(): Promise<T | null>;
|
|
193
|
+
/**
|
|
194
|
+
* Insert a new record into the database.
|
|
195
|
+
*
|
|
196
|
+
* @param data - The data to insert.
|
|
197
|
+
* @returns A promise that resolves to true on success.
|
|
198
|
+
*/
|
|
58
199
|
insert(data: Record<string, D1Value>): Promise<boolean>;
|
|
200
|
+
/**
|
|
201
|
+
* Update records in the database.
|
|
202
|
+
*
|
|
203
|
+
* @param data - The data to update.
|
|
204
|
+
* @returns A promise that resolves to true on success.
|
|
205
|
+
*/
|
|
59
206
|
update(data: Record<string, D1Value>): Promise<boolean>;
|
|
207
|
+
/**
|
|
208
|
+
* Delete records from the database.
|
|
209
|
+
*
|
|
210
|
+
* @returns A promise that resolves to true on success.
|
|
211
|
+
*/
|
|
60
212
|
delete(): Promise<boolean>;
|
|
61
213
|
}
|
|
62
214
|
|
|
215
|
+
/**
|
|
216
|
+
* Database manager for D1 ORM.
|
|
217
|
+
*
|
|
218
|
+
* Handles singleton instance and connection setup.
|
|
219
|
+
*/
|
|
63
220
|
declare class Database {
|
|
221
|
+
/**
|
|
222
|
+
* The singleton instance of the Database.
|
|
223
|
+
*/
|
|
64
224
|
private static instance;
|
|
225
|
+
/**
|
|
226
|
+
* The active database connection.
|
|
227
|
+
*/
|
|
65
228
|
connection: Connection;
|
|
229
|
+
/**
|
|
230
|
+
* Private constructor to enforce singleton pattern.
|
|
231
|
+
*
|
|
232
|
+
* @param d1 - The D1Database instance from Cloudflare.
|
|
233
|
+
*/
|
|
66
234
|
private constructor();
|
|
235
|
+
/**
|
|
236
|
+
* Setup the database connection.
|
|
237
|
+
*
|
|
238
|
+
* @param d1 - The D1Database instance from Cloudflare environment (env.DB).
|
|
239
|
+
*/
|
|
67
240
|
static setup(d1: D1Database): void;
|
|
241
|
+
/**
|
|
242
|
+
* Get the singleton Database instance.
|
|
243
|
+
*
|
|
244
|
+
* @throws Error if setup() has not been called.
|
|
245
|
+
* @returns The Database instance.
|
|
246
|
+
*/
|
|
68
247
|
static getInstance(): Database;
|
|
69
248
|
}
|
|
70
249
|
|
|
@@ -99,51 +278,251 @@ declare class BelongsTo<Related extends Model> extends Relationship<Related> {
|
|
|
99
278
|
addConstraints(): void;
|
|
100
279
|
}
|
|
101
280
|
|
|
281
|
+
/**
|
|
282
|
+
* Base Model class for D1 ORM.
|
|
283
|
+
*
|
|
284
|
+
* Provides Eloquent-like features for interacting with Cloudflare D1.
|
|
285
|
+
*/
|
|
102
286
|
declare class Model {
|
|
287
|
+
/**
|
|
288
|
+
* The table associated with the model.
|
|
289
|
+
*/
|
|
103
290
|
protected static table: string;
|
|
291
|
+
/**
|
|
292
|
+
* Resolver function for the database connection.
|
|
293
|
+
*/
|
|
104
294
|
protected static connectionResolver: () => Database;
|
|
295
|
+
/**
|
|
296
|
+
* The model's attributes.
|
|
297
|
+
*/
|
|
105
298
|
attributes: Record<string, D1Value>;
|
|
299
|
+
/**
|
|
300
|
+
* The attributes that are mass assignable.
|
|
301
|
+
*/
|
|
106
302
|
protected fillable: string[];
|
|
303
|
+
/**
|
|
304
|
+
* The attributes that should be hidden for serialization.
|
|
305
|
+
*/
|
|
107
306
|
protected hidden: string[];
|
|
307
|
+
/**
|
|
308
|
+
* Indicates if the model exists in the database.
|
|
309
|
+
*/
|
|
108
310
|
exists: boolean;
|
|
311
|
+
/**
|
|
312
|
+
* The model's original attribute values.
|
|
313
|
+
*/
|
|
109
314
|
protected original: Record<string, D1Value>;
|
|
315
|
+
/**
|
|
316
|
+
* Create a new Model instance.
|
|
317
|
+
*
|
|
318
|
+
* @param attributes - Initial attributes for the model.
|
|
319
|
+
*/
|
|
110
320
|
constructor(attributes?: Record<string, D1Value>);
|
|
321
|
+
/**
|
|
322
|
+
* Begin querying the model.
|
|
323
|
+
*
|
|
324
|
+
* @returns A new ModelQueryBuilder instance.
|
|
325
|
+
*/
|
|
111
326
|
static query<T extends Model>(this: new () => T): ModelQueryBuilder<T>;
|
|
327
|
+
/**
|
|
328
|
+
* Start a query on a specific connection (currently uses default).
|
|
329
|
+
*
|
|
330
|
+
* @param connectionName - The name of the connection.
|
|
331
|
+
* @returns A new ModelQueryBuilder instance.
|
|
332
|
+
*/
|
|
112
333
|
static on(connectionName?: string): ModelQueryBuilder<Model>;
|
|
334
|
+
/**
|
|
335
|
+
* Retrieve all records for the model.
|
|
336
|
+
*
|
|
337
|
+
* @returns A promise that resolves to an array of model instances.
|
|
338
|
+
*/
|
|
113
339
|
static all<T extends Model>(this: new () => T): Promise<T[]>;
|
|
340
|
+
/**
|
|
341
|
+
* Find a model by its primary key.
|
|
342
|
+
*
|
|
343
|
+
* @param id - The ID of the record.
|
|
344
|
+
* @returns A promise that resolves to the model instance or null if not found.
|
|
345
|
+
*/
|
|
114
346
|
static find<T extends Model>(this: new () => T, id: number | string): Promise<T | null>;
|
|
347
|
+
/**
|
|
348
|
+
* Save a new model and return the instance.
|
|
349
|
+
*
|
|
350
|
+
* @param attributes - Attributes for the new record.
|
|
351
|
+
* @returns A promise that resolves to the created model instance.
|
|
352
|
+
*/
|
|
115
353
|
static create<T extends Model>(this: new () => T, attributes: Record<string, D1Value>): Promise<T>;
|
|
354
|
+
/**
|
|
355
|
+
* Fill the model with an array of attributes.
|
|
356
|
+
*
|
|
357
|
+
* @param attributes - Attributes to fill.
|
|
358
|
+
* @returns The model instance.
|
|
359
|
+
*/
|
|
116
360
|
fill(attributes: Record<string, D1Value>): this;
|
|
361
|
+
/**
|
|
362
|
+
* Save the model to the database.
|
|
363
|
+
*
|
|
364
|
+
* @returns A promise that resolves to true on success, false otherwise.
|
|
365
|
+
*/
|
|
117
366
|
save(): Promise<boolean>;
|
|
367
|
+
/**
|
|
368
|
+
* Delete the model from the database.
|
|
369
|
+
*
|
|
370
|
+
* @returns A promise that resolves to true on success, false otherwise.
|
|
371
|
+
*/
|
|
118
372
|
delete(): Promise<boolean>;
|
|
373
|
+
/**
|
|
374
|
+
* Get the table associated with the model.
|
|
375
|
+
*
|
|
376
|
+
* @returns The table name.
|
|
377
|
+
*/
|
|
119
378
|
getTable(): string;
|
|
379
|
+
/**
|
|
380
|
+
* Get the attributes that have been changed since the last sync.
|
|
381
|
+
*
|
|
382
|
+
* @returns A record of dirty attributes.
|
|
383
|
+
*/
|
|
120
384
|
getDirty(): Record<string, D1Value>;
|
|
385
|
+
/**
|
|
386
|
+
* Sync the original attributes with the current attributes.
|
|
387
|
+
*/
|
|
121
388
|
syncOriginal(): void;
|
|
389
|
+
/**
|
|
390
|
+
* Define a one-to-one relationship.
|
|
391
|
+
*
|
|
392
|
+
* @param related - The related model class.
|
|
393
|
+
* @param foreignKey - The foreign key on the related table.
|
|
394
|
+
* @param localKey - The local key on the current table.
|
|
395
|
+
* @returns A HasOne relationship instance.
|
|
396
|
+
*/
|
|
122
397
|
hasOne<Related extends Model>(related: new () => Related, foreignKey?: string, localKey?: string): HasOne<Related>;
|
|
398
|
+
/**
|
|
399
|
+
* Define a one-to-many relationship.
|
|
400
|
+
*
|
|
401
|
+
* @param related - The related model class.
|
|
402
|
+
* @param foreignKey - The foreign key on the related table.
|
|
403
|
+
* @param localKey - The local key on the current table.
|
|
404
|
+
* @returns A HasMany relationship instance.
|
|
405
|
+
*/
|
|
123
406
|
hasMany<Related extends Model>(related: new () => Related, foreignKey?: string, localKey?: string): HasMany<Related>;
|
|
407
|
+
/**
|
|
408
|
+
* Define an inverse one-to-one or many relationship.
|
|
409
|
+
*
|
|
410
|
+
* @param related - The related model class.
|
|
411
|
+
* @param foreignKey - The foreign key on the current table.
|
|
412
|
+
* @param ownerKey - The owner key on the related table.
|
|
413
|
+
* @returns A BelongsTo relationship instance.
|
|
414
|
+
*/
|
|
124
415
|
belongsTo<Related extends Model>(related: new () => Related, foreignKey?: string, ownerKey?: string): BelongsTo<Related>;
|
|
416
|
+
/**
|
|
417
|
+
* Get the default foreign key name for the model.
|
|
418
|
+
*
|
|
419
|
+
* @returns The foreign key name.
|
|
420
|
+
*/
|
|
125
421
|
getForeignKey(): string;
|
|
422
|
+
/**
|
|
423
|
+
* Get a new query builder for the model's table.
|
|
424
|
+
*
|
|
425
|
+
* @returns A ModelQueryBuilder instance.
|
|
426
|
+
*/
|
|
126
427
|
newQuery<T extends Model>(this: T): ModelQueryBuilder<T>;
|
|
127
428
|
}
|
|
128
429
|
|
|
430
|
+
/**
|
|
431
|
+
* Specialized Query Builder for Models.
|
|
432
|
+
*
|
|
433
|
+
* Automatically maps raw results to Model instances.
|
|
434
|
+
*/
|
|
129
435
|
declare class ModelQueryBuilder<T extends Model> extends QueryBuilder {
|
|
436
|
+
/**
|
|
437
|
+
* The class of the model being queried.
|
|
438
|
+
*/
|
|
130
439
|
protected modelClass: typeof Model;
|
|
440
|
+
/**
|
|
441
|
+
* Create a new ModelQueryBuilder instance.
|
|
442
|
+
*
|
|
443
|
+
* @param connection - The database connection.
|
|
444
|
+
* @param table - The table name.
|
|
445
|
+
* @param modelClass - The class constructor for the model.
|
|
446
|
+
*/
|
|
131
447
|
constructor(connection: any, table: string, modelClass: typeof Model);
|
|
448
|
+
/**
|
|
449
|
+
* Execute the query and return model instances.
|
|
450
|
+
*
|
|
451
|
+
* @returns A promise that resolves to an array of model instances.
|
|
452
|
+
*/
|
|
132
453
|
get<M = T>(): Promise<M[]>;
|
|
454
|
+
/**
|
|
455
|
+
* Execute the query and return the first matching model instance.
|
|
456
|
+
*
|
|
457
|
+
* @returns A promise that resolves to the model instance or null.
|
|
458
|
+
*/
|
|
133
459
|
first<M = T>(): Promise<M | null>;
|
|
460
|
+
/**
|
|
461
|
+
* Get the underlying model class.
|
|
462
|
+
*
|
|
463
|
+
* @returns The model class constructor.
|
|
464
|
+
*/
|
|
134
465
|
getModel(): typeof Model;
|
|
135
466
|
}
|
|
136
467
|
|
|
468
|
+
/**
|
|
469
|
+
* Represents a database column definition.
|
|
470
|
+
*/
|
|
471
|
+
declare class Column {
|
|
472
|
+
protected name: string;
|
|
473
|
+
protected type: string;
|
|
474
|
+
protected isNullable: boolean;
|
|
475
|
+
protected isUnique: boolean;
|
|
476
|
+
protected defaultValue: any;
|
|
477
|
+
constructor(name: string, type: string);
|
|
478
|
+
/**
|
|
479
|
+
* Set the column as nullable.
|
|
480
|
+
*/
|
|
481
|
+
nullable(): this;
|
|
482
|
+
/**
|
|
483
|
+
* Set the column as unique.
|
|
484
|
+
*/
|
|
485
|
+
unique(): this;
|
|
486
|
+
/**
|
|
487
|
+
* Set the default value for the column.
|
|
488
|
+
*/
|
|
489
|
+
default(value: any): this;
|
|
490
|
+
/**
|
|
491
|
+
* Convert the column definition to SQL.
|
|
492
|
+
*/
|
|
493
|
+
toSql(): string;
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* Schema Blueprint for defining tables.
|
|
497
|
+
*/
|
|
137
498
|
declare class Blueprint {
|
|
138
499
|
protected table: string;
|
|
139
|
-
protected columns: string[];
|
|
500
|
+
protected columns: (Column | string)[];
|
|
140
501
|
protected commands: string[];
|
|
141
502
|
constructor(table: string);
|
|
503
|
+
/**
|
|
504
|
+
* Add an auto-incrementing ID column.
|
|
505
|
+
*/
|
|
142
506
|
id(): this;
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
507
|
+
/**
|
|
508
|
+
* Add a string (TEXT) column.
|
|
509
|
+
*/
|
|
510
|
+
string(column: string): Column;
|
|
511
|
+
/**
|
|
512
|
+
* Add an integer column.
|
|
513
|
+
*/
|
|
514
|
+
integer(column: string): Column;
|
|
515
|
+
/**
|
|
516
|
+
* Add a boolean column.
|
|
517
|
+
*/
|
|
518
|
+
boolean(column: string): Column;
|
|
519
|
+
/**
|
|
520
|
+
* Add created_at and updated_at timestamp columns.
|
|
521
|
+
*/
|
|
146
522
|
timestamps(): this;
|
|
523
|
+
/**
|
|
524
|
+
* Convert the blueprint to a set of SQL statements.
|
|
525
|
+
*/
|
|
147
526
|
toSql(): string[];
|
|
148
527
|
}
|
|
149
528
|
|
|
@@ -152,4 +531,4 @@ declare class Schema {
|
|
|
152
531
|
static dropIfExists(table: string): string;
|
|
153
532
|
}
|
|
154
533
|
|
|
155
|
-
export { BelongsTo, Blueprint, Connection, type D1Database, type D1ExecResult, type D1PreparedStatement, type D1Result, type D1Value, Database, HasMany, HasOne, Model, ModelQueryBuilder, QueryBuilder, Relationship, Schema };
|
|
534
|
+
export { BelongsTo, Blueprint, Column, Connection, type D1Database, type D1ExecResult, type D1PreparedStatement, type D1Result, type D1Value, Database, HasMany, HasOne, Model, ModelQueryBuilder, QueryBuilder, Relationship, Schema };
|