@deessejs/collections 0.0.51 → 0.2.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.
Files changed (123) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +204 -0
  3. package/dist/index.js +868 -22
  4. package/dist/index.js.map +1 -0
  5. package/dist/index.mjs +821 -0
  6. package/dist/index.mjs.map +1 -0
  7. package/dist/next/index.js +421 -51
  8. package/dist/next/index.js.map +1 -0
  9. package/dist/next/index.mjs +377 -0
  10. package/dist/next/index.mjs.map +1 -0
  11. package/package.json +76 -72
  12. package/dist/collections/define.d.ts +0 -43
  13. package/dist/collections/define.js +0 -13
  14. package/dist/collections/extend-fields.d.ts +0 -3
  15. package/dist/collections/extend-fields.js +0 -13
  16. package/dist/collections/index.d.ts +0 -1
  17. package/dist/collections/index.js +0 -17
  18. package/dist/collections/types.d.ts +0 -29
  19. package/dist/collections/types.js +0 -2
  20. package/dist/config/index.d.ts +0 -2
  21. package/dist/config/index.js +0 -45
  22. package/dist/config/orchestrator.d.ts +0 -8
  23. package/dist/config/orchestrator.js +0 -70
  24. package/dist/config/types.d.ts +0 -14
  25. package/dist/config/types.js +0 -2
  26. package/dist/database/types.d.ts +0 -10
  27. package/dist/database/types.js +0 -2
  28. package/dist/drizzle/generate.d.ts +0 -2
  29. package/dist/drizzle/generate.js +0 -49
  30. package/dist/drizzle/index.d.ts +0 -31
  31. package/dist/drizzle/index.js +0 -24
  32. package/dist/drizzle/types.js +0 -1
  33. package/dist/dsl/collections.d.ts +0 -13
  34. package/dist/dsl/collections.js +0 -14
  35. package/dist/dsl/index.d.ts +0 -1
  36. package/dist/dsl/index.js +0 -17
  37. package/dist/dsl/types.d.ts +0 -1
  38. package/dist/dsl/types.js +0 -2
  39. package/dist/esm/collections/define.d.ts +0 -43
  40. package/dist/esm/collections/define.js +0 -9
  41. package/dist/esm/collections/extend-fields.d.ts +0 -3
  42. package/dist/esm/collections/extend-fields.js +0 -9
  43. package/dist/esm/collections/index.d.ts +0 -1
  44. package/dist/esm/collections/index.js +0 -1
  45. package/dist/esm/collections/types.d.ts +0 -29
  46. package/dist/esm/collections/types.js +0 -1
  47. package/dist/esm/config/index.d.ts +0 -5
  48. package/dist/esm/config/index.js +0 -13
  49. package/dist/esm/config/orchestrator.d.ts +0 -8
  50. package/dist/esm/config/orchestrator.js +0 -66
  51. package/dist/esm/config/types.d.ts +0 -14
  52. package/dist/esm/config/types.js +0 -1
  53. package/dist/esm/database/types.d.ts +0 -10
  54. package/dist/esm/database/types.js +0 -1
  55. package/dist/esm/drizzle/generate.d.ts +0 -2
  56. package/dist/esm/drizzle/generate.js +0 -30
  57. package/dist/esm/drizzle/index.d.ts +0 -31
  58. package/dist/esm/drizzle/index.js +0 -20
  59. package/dist/esm/drizzle/types.d.ts +0 -0
  60. package/dist/esm/drizzle/types.js +0 -1
  61. package/dist/esm/dsl/collections.d.ts +0 -13
  62. package/dist/esm/dsl/collections.js +0 -10
  63. package/dist/esm/dsl/index.d.ts +0 -1
  64. package/dist/esm/dsl/index.js +0 -1
  65. package/dist/esm/fields/constraints.d.ts +0 -6
  66. package/dist/esm/fields/constraints.js +0 -59
  67. package/dist/esm/fields/core.d.ts +0 -71
  68. package/dist/esm/fields/core.js +0 -27
  69. package/dist/esm/fields/field.d.ts +0 -24
  70. package/dist/esm/fields/field.js +0 -33
  71. package/dist/esm/fields/index.d.ts +0 -4
  72. package/dist/esm/fields/index.js +0 -4
  73. package/dist/esm/fields/types.d.ts +0 -48
  74. package/dist/esm/fields/types.js +0 -1
  75. package/dist/esm/index.d.ts +0 -8
  76. package/dist/esm/index.js +0 -8
  77. package/dist/esm/next/index.d.ts +0 -5
  78. package/dist/esm/next/index.js +0 -17
  79. package/dist/esm/plugins/index.d.ts +0 -2
  80. package/dist/esm/plugins/index.js +0 -3
  81. package/dist/esm/plugins/types.d.ts +0 -1
  82. package/dist/esm/plugins/types.js +0 -1
  83. package/dist/esm/providers/index.d.ts +0 -2
  84. package/dist/esm/providers/index.js +0 -53
  85. package/dist/esm/providers/types.d.ts +0 -8
  86. package/dist/esm/providers/types.js +0 -1
  87. package/dist/esm/utils/deep-partial.d.ts +0 -3
  88. package/dist/esm/utils/deep-partial.js +0 -1
  89. package/dist/esm/utils/exact.d.ts +0 -1
  90. package/dist/esm/utils/exact.js +0 -1
  91. package/dist/esm/utils/union-intersection.d.ts +0 -1
  92. package/dist/esm/utils/union-intersection.js +0 -1
  93. package/dist/esm/worker/index.d.ts +0 -2
  94. package/dist/esm/worker/index.js +0 -32
  95. package/dist/fields/constraints.d.ts +0 -6
  96. package/dist/fields/constraints.js +0 -67
  97. package/dist/fields/core.d.ts +0 -71
  98. package/dist/fields/core.js +0 -33
  99. package/dist/fields/field.d.ts +0 -24
  100. package/dist/fields/field.js +0 -38
  101. package/dist/fields/index.d.ts +0 -4
  102. package/dist/fields/index.js +0 -20
  103. package/dist/fields/types.d.ts +0 -48
  104. package/dist/fields/types.js +0 -2
  105. package/dist/index.d.ts +0 -8
  106. package/dist/next/index.d.ts +0 -5
  107. package/dist/plugins/index.d.ts +0 -2
  108. package/dist/plugins/index.js +0 -7
  109. package/dist/plugins/types.d.ts +0 -1
  110. package/dist/plugins/types.js +0 -2
  111. package/dist/providers/index.d.ts +0 -2
  112. package/dist/providers/index.js +0 -57
  113. package/dist/providers/types.d.ts +0 -8
  114. package/dist/providers/types.js +0 -2
  115. package/dist/utils/deep-partial.d.ts +0 -3
  116. package/dist/utils/deep-partial.js +0 -2
  117. package/dist/utils/exact.d.ts +0 -1
  118. package/dist/utils/exact.js +0 -1
  119. package/dist/utils/union-intersection.d.ts +0 -1
  120. package/dist/utils/union-intersection.js +0 -2
  121. package/dist/worker/index.d.ts +0 -1
  122. package/dist/worker/index.js +0 -101
  123. /package/{dist/drizzle/types.d.ts → public/.gitkeep} +0 -0
package/dist/index.mjs ADDED
@@ -0,0 +1,821 @@
1
+ // src/field-type.ts
2
+ var fieldType = (config) => {
3
+ return () => ({
4
+ schema: config.schema,
5
+ database: config.database ?? {}
6
+ });
7
+ };
8
+
9
+ // src/field.ts
10
+ var field = (config) => {
11
+ return {
12
+ fieldType: config.fieldType,
13
+ required: config.required ?? false,
14
+ unique: config.unique ?? false,
15
+ indexed: config.indexed ?? false,
16
+ default: config.default,
17
+ label: config.label,
18
+ description: config.description
19
+ };
20
+ };
21
+
22
+ // src/fields/f.ts
23
+ import { z } from "zod";
24
+ var getItemType = (itemSchema) => {
25
+ if (itemSchema instanceof z.ZodString) return "text";
26
+ if (itemSchema instanceof z.ZodNumber) return "integer";
27
+ if (itemSchema instanceof z.ZodBoolean) return "boolean";
28
+ if (itemSchema instanceof z.ZodDate) return "timestamp";
29
+ if (itemSchema instanceof z.ZodEnum) return "text";
30
+ if (itemSchema instanceof z.ZodArray) return "array";
31
+ if (itemSchema instanceof z.ZodObject) return "jsonb";
32
+ return "text";
33
+ };
34
+ var f = {
35
+ /**
36
+ * Text field type
37
+ */
38
+ text: () => fieldType({
39
+ schema: z.string(),
40
+ database: { type: "text" }
41
+ })(),
42
+ /**
43
+ * Email field type with built-in validation
44
+ */
45
+ email: () => fieldType({
46
+ schema: z.string().email(),
47
+ database: { type: "text" }
48
+ })(),
49
+ /**
50
+ * URL field type with built-in validation
51
+ */
52
+ url: () => fieldType({
53
+ schema: z.string().url(),
54
+ database: { type: "text" }
55
+ })(),
56
+ /**
57
+ * Number field type
58
+ */
59
+ number: () => fieldType({
60
+ schema: z.number(),
61
+ database: { type: "integer" }
62
+ })(),
63
+ /**
64
+ * Boolean field type
65
+ */
66
+ boolean: () => fieldType({
67
+ schema: z.boolean(),
68
+ database: { type: "boolean" }
69
+ })(),
70
+ /**
71
+ * Date field type (date only, no time)
72
+ */
73
+ date: () => fieldType({
74
+ schema: z.date(),
75
+ database: { type: "date" }
76
+ })(),
77
+ /**
78
+ * Timestamp field type (date with time)
79
+ */
80
+ timestamp: () => fieldType({
81
+ schema: z.date(),
82
+ database: { type: "timestamp" }
83
+ })(),
84
+ /**
85
+ * Creates a select field type
86
+ */
87
+ select: (options) => fieldType({
88
+ schema: z.enum(options),
89
+ database: { type: "text" }
90
+ })(),
91
+ /**
92
+ * JSON field type for storing JSON data
93
+ */
94
+ json: (schema) => fieldType({
95
+ schema: schema ?? z.any(),
96
+ database: { type: "jsonb" }
97
+ })(),
98
+ /**
99
+ * Array field type for storing lists
100
+ */
101
+ array: (itemSchema) => fieldType({
102
+ schema: z.array(itemSchema),
103
+ database: { type: "array", itemType: getItemType(itemSchema) }
104
+ })(),
105
+ /**
106
+ * Creates a relation field type for foreign key relationships
107
+ */
108
+ relation: (options) => {
109
+ const isMany = options.many ?? false;
110
+ const isSingular = options.singular ?? false;
111
+ return fieldType({
112
+ schema: isMany ? z.array(z.string()) : z.string(),
113
+ database: {
114
+ type: "integer",
115
+ references: options.collection,
116
+ through: options.through,
117
+ many: isMany,
118
+ singular: isSingular
119
+ }
120
+ })();
121
+ }
122
+ };
123
+ var text = f.text;
124
+ var email = f.email;
125
+ var url = f.url;
126
+ var number = f.number;
127
+ var boolean = f.boolean;
128
+ var date = f.date;
129
+ var timestamp = f.timestamp;
130
+ var select = f.select;
131
+ var json = f.json;
132
+ var array = f.array;
133
+ var relation = f.relation;
134
+
135
+ // src/collection.ts
136
+ var collection = (config) => {
137
+ return {
138
+ slug: config.slug,
139
+ name: config.name,
140
+ fields: config.fields,
141
+ hooks: config.hooks,
142
+ dataType: config.dataType
143
+ };
144
+ };
145
+
146
+ // src/operations/collection-operations.ts
147
+ import { eq, and, like, gt, gte, lt, lte, isNull, inArray, not, desc, asc } from "drizzle-orm";
148
+ var buildWhereClause = (tableColumns, where) => {
149
+ if (!where) return void 0;
150
+ const conditions = [];
151
+ for (const [key, value] of Object.entries(where)) {
152
+ const column = tableColumns[key];
153
+ if (!column) continue;
154
+ if (value === null || typeof value !== "object") {
155
+ conditions.push(eq(column, value));
156
+ } else {
157
+ const operator = value;
158
+ if ("eq" in operator) {
159
+ conditions.push(eq(column, operator.eq));
160
+ } else if ("neq" in operator) {
161
+ conditions.push(not(eq(column, operator.neq)));
162
+ } else if ("gt" in operator) {
163
+ conditions.push(gt(column, operator.gt));
164
+ } else if ("gte" in operator) {
165
+ conditions.push(gte(column, operator.gte));
166
+ } else if ("lt" in operator) {
167
+ conditions.push(lt(column, operator.lt));
168
+ } else if ("lte" in operator) {
169
+ conditions.push(lte(column, operator.lte));
170
+ } else if ("in" in operator) {
171
+ conditions.push(inArray(column, operator.in));
172
+ } else if ("notIn" in operator) {
173
+ conditions.push(not(inArray(column, operator.notIn)));
174
+ } else if ("contains" in operator) {
175
+ conditions.push(like(column, `%${operator.contains}%`));
176
+ } else if ("startsWith" in operator) {
177
+ conditions.push(like(column, `${operator.startsWith}%`));
178
+ } else if ("endsWith" in operator) {
179
+ conditions.push(like(column, `%${operator.endsWith}`));
180
+ } else if ("isNull" in operator) {
181
+ if (operator.isNull) {
182
+ conditions.push(isNull(column));
183
+ }
184
+ } else if ("not" in operator) {
185
+ conditions.push(not(eq(column, operator.not)));
186
+ }
187
+ }
188
+ }
189
+ if (conditions.length === 0) return void 0;
190
+ if (conditions.length === 1) return conditions[0];
191
+ return and(...conditions);
192
+ };
193
+ var buildOrderBy = (tableColumns, orderBy) => {
194
+ if (!orderBy) return [];
195
+ const orders = Array.isArray(orderBy) ? orderBy : [orderBy];
196
+ return orders.map((order) => {
197
+ for (const [key, direction] of Object.entries(order)) {
198
+ const column = tableColumns[key];
199
+ if (!column) continue;
200
+ return direction === "desc" ? desc(column) : asc(column);
201
+ }
202
+ return void 0;
203
+ }).filter(Boolean);
204
+ };
205
+ var executeBeforeOperationHooks = async (hooks, context) => {
206
+ if (!hooks?.beforeOperation) return;
207
+ for (const hook of hooks.beforeOperation) {
208
+ await hook(context);
209
+ }
210
+ };
211
+ var executeAfterOperationHooks = async (hooks, context) => {
212
+ if (!hooks?.afterOperation) return;
213
+ for (const hook of hooks.afterOperation) {
214
+ await hook(context);
215
+ }
216
+ };
217
+ var executeBeforeCreateHooks = async (hooks, context) => {
218
+ if (!hooks?.beforeCreate) return;
219
+ for (const hook of hooks.beforeCreate) {
220
+ await hook(context);
221
+ }
222
+ };
223
+ var executeAfterCreateHooks = async (hooks, context) => {
224
+ if (!hooks?.afterCreate) return;
225
+ for (const hook of hooks.afterCreate) {
226
+ await hook(context);
227
+ }
228
+ };
229
+ var executeBeforeUpdateHooks = async (hooks, context) => {
230
+ if (!hooks?.beforeUpdate) return;
231
+ for (const hook of hooks.beforeUpdate) {
232
+ await hook(context);
233
+ }
234
+ };
235
+ var executeAfterUpdateHooks = async (hooks, context) => {
236
+ if (!hooks?.afterUpdate) return;
237
+ for (const hook of hooks.afterUpdate) {
238
+ await hook(context);
239
+ }
240
+ };
241
+ var executeBeforeDeleteHooks = async (hooks, context) => {
242
+ if (!hooks?.beforeDelete) return;
243
+ for (const hook of hooks.beforeDelete) {
244
+ await hook(context);
245
+ }
246
+ };
247
+ var executeAfterDeleteHooks = async (hooks, context) => {
248
+ if (!hooks?.afterDelete) return;
249
+ for (const hook of hooks.afterDelete) {
250
+ await hook(context);
251
+ }
252
+ };
253
+ var executeBeforeReadHooks = async (hooks, context) => {
254
+ if (!hooks?.beforeRead) return;
255
+ for (const hook of hooks.beforeRead) {
256
+ await hook(context);
257
+ }
258
+ };
259
+ var executeAfterReadHooks = async (hooks, context) => {
260
+ if (!hooks?.afterRead) return;
261
+ for (const hook of hooks.afterRead) {
262
+ await hook(context);
263
+ }
264
+ };
265
+ var createCollectionOperations = (_collection, _slug, _db, _table, _hooks) => {
266
+ const tableColumns = _table;
267
+ const db = _db;
268
+ const hooks = _hooks;
269
+ if (!db) {
270
+ return {
271
+ findMany: async () => [],
272
+ findUnique: async () => void 0,
273
+ findFirst: async () => void 0,
274
+ create: async () => void 0,
275
+ createMany: async () => 0,
276
+ update: async () => void 0,
277
+ updateMany: async () => 0,
278
+ delete: async () => void 0,
279
+ deleteMany: async () => 0,
280
+ count: async () => 0,
281
+ exists: async () => false
282
+ };
283
+ }
284
+ return {
285
+ findMany: async (options) => {
286
+ const whereClause = buildWhereClause(tableColumns, options?.where);
287
+ const orderByClause = buildOrderBy(tableColumns, options?.orderBy);
288
+ await executeBeforeOperationHooks(hooks, {
289
+ collection: _slug,
290
+ operation: "read",
291
+ where: options?.where
292
+ });
293
+ await executeBeforeReadHooks(hooks, {
294
+ collection: _slug,
295
+ operation: "read",
296
+ query: options,
297
+ db
298
+ });
299
+ let query = db.select().from(_table);
300
+ if (whereClause) {
301
+ query = query.where(whereClause);
302
+ }
303
+ if (orderByClause.length > 0) {
304
+ query = query.orderBy(...orderByClause);
305
+ }
306
+ if (options?.offset) {
307
+ query = query.offset(options.offset);
308
+ }
309
+ if (options?.limit) {
310
+ query = query.limit(options.limit);
311
+ }
312
+ const result = await query;
313
+ await executeAfterReadHooks(hooks, {
314
+ collection: _slug,
315
+ operation: "read",
316
+ query: options,
317
+ result,
318
+ db
319
+ });
320
+ await executeAfterOperationHooks(hooks, {
321
+ collection: _slug,
322
+ operation: "read",
323
+ where: options?.where,
324
+ result
325
+ });
326
+ return result;
327
+ },
328
+ findUnique: async (options) => {
329
+ const whereClause = buildWhereClause(tableColumns, options.where);
330
+ if (!whereClause) return void 0;
331
+ await executeBeforeOperationHooks(hooks, {
332
+ collection: _slug,
333
+ operation: "read",
334
+ where: options.where
335
+ });
336
+ await executeBeforeReadHooks(hooks, {
337
+ collection: _slug,
338
+ operation: "read",
339
+ query: options,
340
+ db
341
+ });
342
+ const result = await db.select().from(_table).where(whereClause).limit(1);
343
+ const returnValue = result[0];
344
+ await executeAfterReadHooks(hooks, {
345
+ collection: _slug,
346
+ operation: "read",
347
+ query: options,
348
+ result: returnValue ? [returnValue] : [],
349
+ db
350
+ });
351
+ await executeAfterOperationHooks(hooks, {
352
+ collection: _slug,
353
+ operation: "read",
354
+ where: options.where,
355
+ result: returnValue
356
+ });
357
+ return returnValue;
358
+ },
359
+ findFirst: async (options) => {
360
+ const whereClause = buildWhereClause(tableColumns, options.where);
361
+ const orderByClause = buildOrderBy(tableColumns, options.orderBy);
362
+ await executeBeforeOperationHooks(hooks, {
363
+ collection: _slug,
364
+ operation: "read",
365
+ where: options.where
366
+ });
367
+ await executeBeforeReadHooks(hooks, {
368
+ collection: _slug,
369
+ operation: "read",
370
+ query: options,
371
+ db
372
+ });
373
+ let query = db.select().from(_table);
374
+ if (whereClause) {
375
+ query = query.where(whereClause);
376
+ }
377
+ if (orderByClause.length > 0) {
378
+ query = query.orderBy(...orderByClause);
379
+ }
380
+ const result = await query.limit(1);
381
+ const returnValue = result[0];
382
+ await executeAfterReadHooks(hooks, {
383
+ collection: _slug,
384
+ operation: "read",
385
+ query: options,
386
+ result: returnValue ? [returnValue] : [],
387
+ db
388
+ });
389
+ await executeAfterOperationHooks(hooks, {
390
+ collection: _slug,
391
+ operation: "read",
392
+ where: options.where,
393
+ result: returnValue
394
+ });
395
+ return returnValue;
396
+ },
397
+ create: async (options) => {
398
+ const data = Array.isArray(options.data) ? options.data : [options.data];
399
+ const firstData = data[0];
400
+ await executeBeforeOperationHooks(hooks, {
401
+ collection: _slug,
402
+ operation: "create",
403
+ data: firstData,
404
+ where: void 0
405
+ });
406
+ await executeBeforeCreateHooks(hooks, {
407
+ collection: _slug,
408
+ operation: "create",
409
+ data: firstData,
410
+ db
411
+ });
412
+ const result = await db.insert(_table).values(data).returning();
413
+ const returnValue = options.returning ? result[0] : void 0;
414
+ await executeAfterCreateHooks(hooks, {
415
+ collection: _slug,
416
+ operation: "create",
417
+ data: firstData,
418
+ result: returnValue,
419
+ db
420
+ });
421
+ await executeAfterOperationHooks(hooks, {
422
+ collection: _slug,
423
+ operation: "create",
424
+ data: firstData,
425
+ result: returnValue
426
+ });
427
+ return returnValue;
428
+ },
429
+ createMany: async (options) => {
430
+ const dataArray = Array.isArray(options.data) ? options.data : [options.data];
431
+ for (const data of dataArray) {
432
+ await executeBeforeOperationHooks(hooks, {
433
+ collection: _slug,
434
+ operation: "create",
435
+ data,
436
+ where: void 0
437
+ });
438
+ await executeBeforeCreateHooks(hooks, {
439
+ collection: _slug,
440
+ operation: "create",
441
+ data,
442
+ db
443
+ });
444
+ }
445
+ const result = await db.insert(_table).values(options.data);
446
+ for (let i = 0; i < dataArray.length; i++) {
447
+ await executeAfterCreateHooks(hooks, {
448
+ collection: _slug,
449
+ operation: "create",
450
+ data: dataArray[i],
451
+ result: result[i],
452
+ db
453
+ });
454
+ await executeAfterOperationHooks(hooks, {
455
+ collection: _slug,
456
+ operation: "create",
457
+ data: dataArray[i],
458
+ result: result[i]
459
+ });
460
+ }
461
+ return result.length || 0;
462
+ },
463
+ update: async (options) => {
464
+ const whereClause = buildWhereClause(tableColumns, options.where);
465
+ if (!whereClause) return void 0;
466
+ const previousResult = await db.select().from(_table).where(whereClause).limit(1);
467
+ const previousData = previousResult[0];
468
+ await executeBeforeOperationHooks(hooks, {
469
+ collection: _slug,
470
+ operation: "update",
471
+ data: options.data,
472
+ where: options.where
473
+ });
474
+ await executeBeforeUpdateHooks(hooks, {
475
+ collection: _slug,
476
+ operation: "update",
477
+ data: options.data,
478
+ where: options.where,
479
+ previousData,
480
+ db
481
+ });
482
+ const result = await db.update(_table).set(options.data).where(whereClause).returning();
483
+ const returnValue = options.returning ? result[0] : void 0;
484
+ await executeAfterUpdateHooks(hooks, {
485
+ collection: _slug,
486
+ operation: "update",
487
+ data: options.data,
488
+ where: options.where,
489
+ previousData,
490
+ result: returnValue,
491
+ db
492
+ });
493
+ await executeAfterOperationHooks(hooks, {
494
+ collection: _slug,
495
+ operation: "update",
496
+ data: options.data,
497
+ where: options.where,
498
+ result: returnValue
499
+ });
500
+ return returnValue;
501
+ },
502
+ updateMany: async (options) => {
503
+ const whereClause = buildWhereClause(tableColumns, options.where);
504
+ if (!whereClause) return 0;
505
+ const previousResults = await db.select().from(_table).where(whereClause);
506
+ await executeBeforeOperationHooks(hooks, {
507
+ collection: _slug,
508
+ operation: "update",
509
+ data: options.data,
510
+ where: options.where
511
+ });
512
+ for (const previousData of previousResults) {
513
+ await executeBeforeUpdateHooks(hooks, {
514
+ collection: _slug,
515
+ operation: "update",
516
+ data: options.data,
517
+ where: options.where,
518
+ previousData,
519
+ db
520
+ });
521
+ }
522
+ const result = await db.update(_table).set(options.data).where(whereClause);
523
+ for (const previousData of previousResults) {
524
+ await executeAfterUpdateHooks(hooks, {
525
+ collection: _slug,
526
+ operation: "update",
527
+ data: options.data,
528
+ where: options.where,
529
+ previousData,
530
+ db
531
+ });
532
+ }
533
+ await executeAfterOperationHooks(hooks, {
534
+ collection: _slug,
535
+ operation: "update",
536
+ data: options.data,
537
+ where: options.where
538
+ });
539
+ return result.length || 0;
540
+ },
541
+ delete: async (options) => {
542
+ const whereClause = buildWhereClause(tableColumns, options.where);
543
+ if (!whereClause) return void 0;
544
+ const previousResult = await db.select().from(_table).where(whereClause).limit(1);
545
+ const previousData = previousResult[0];
546
+ await executeBeforeOperationHooks(hooks, {
547
+ collection: _slug,
548
+ operation: "delete",
549
+ where: options.where
550
+ });
551
+ await executeBeforeDeleteHooks(hooks, {
552
+ collection: _slug,
553
+ operation: "delete",
554
+ where: options.where,
555
+ previousData,
556
+ db
557
+ });
558
+ const result = await db.delete(_table).where(whereClause).returning();
559
+ const returnValue = options.returning ? result[0] : void 0;
560
+ await executeAfterDeleteHooks(hooks, {
561
+ collection: _slug,
562
+ operation: "delete",
563
+ where: options.where,
564
+ previousData,
565
+ result: returnValue,
566
+ db
567
+ });
568
+ await executeAfterOperationHooks(hooks, {
569
+ collection: _slug,
570
+ operation: "delete",
571
+ where: options.where,
572
+ result: returnValue
573
+ });
574
+ return returnValue;
575
+ },
576
+ deleteMany: async (options) => {
577
+ const whereClause = buildWhereClause(tableColumns, options.where);
578
+ if (!whereClause) return 0;
579
+ const previousResults = await db.select().from(_table).where(whereClause);
580
+ await executeBeforeOperationHooks(hooks, {
581
+ collection: _slug,
582
+ operation: "delete",
583
+ where: options.where
584
+ });
585
+ for (const previousData of previousResults) {
586
+ await executeBeforeDeleteHooks(hooks, {
587
+ collection: _slug,
588
+ operation: "delete",
589
+ where: options.where,
590
+ previousData,
591
+ db
592
+ });
593
+ }
594
+ const result = await db.delete(_table).where(whereClause);
595
+ for (const previousData of previousResults) {
596
+ await executeAfterDeleteHooks(hooks, {
597
+ collection: _slug,
598
+ operation: "delete",
599
+ where: options.where,
600
+ previousData,
601
+ db
602
+ });
603
+ }
604
+ await executeAfterOperationHooks(hooks, {
605
+ collection: _slug,
606
+ operation: "delete",
607
+ where: options.where
608
+ });
609
+ return result.length || 0;
610
+ },
611
+ count: async (options) => {
612
+ const whereClause = buildWhereClause(tableColumns, options?.where);
613
+ await executeBeforeOperationHooks(hooks, {
614
+ collection: _slug,
615
+ operation: "read",
616
+ where: options?.where
617
+ });
618
+ await executeBeforeReadHooks(hooks, {
619
+ collection: _slug,
620
+ operation: "read",
621
+ query: options,
622
+ db
623
+ });
624
+ const result = whereClause ? await db.select().from(_table).where(whereClause) : await db.select().from(_table);
625
+ await executeAfterReadHooks(hooks, {
626
+ collection: _slug,
627
+ operation: "read",
628
+ query: options,
629
+ result,
630
+ db
631
+ });
632
+ await executeAfterOperationHooks(hooks, {
633
+ collection: _slug,
634
+ operation: "read",
635
+ where: options?.where,
636
+ result
637
+ });
638
+ return result.length;
639
+ },
640
+ exists: async (options) => {
641
+ const whereClause = buildWhereClause(tableColumns, options.where);
642
+ if (!whereClause) return false;
643
+ await executeBeforeOperationHooks(hooks, {
644
+ collection: _slug,
645
+ operation: "read",
646
+ where: options.where
647
+ });
648
+ await executeBeforeReadHooks(hooks, {
649
+ collection: _slug,
650
+ operation: "read",
651
+ query: options,
652
+ db
653
+ });
654
+ const result = await db.select().from(_table).where(whereClause).limit(1);
655
+ const returnValue = result.length > 0;
656
+ await executeAfterReadHooks(hooks, {
657
+ collection: _slug,
658
+ operation: "read",
659
+ query: options,
660
+ result,
661
+ db
662
+ });
663
+ await executeAfterOperationHooks(hooks, {
664
+ collection: _slug,
665
+ operation: "read",
666
+ where: options.where,
667
+ result: returnValue
668
+ });
669
+ return returnValue;
670
+ }
671
+ };
672
+ };
673
+
674
+ // src/adapter.ts
675
+ var pgAdapter = (config) => {
676
+ return {
677
+ type: "postgres",
678
+ config: {
679
+ url: config.url,
680
+ migrationsPath: config.migrationsPath ?? "./migrations"
681
+ }
682
+ };
683
+ };
684
+
685
+ // src/schema.ts
686
+ import { pgTable, serial, text as text2, timestamp as timestamp2, uuid, varchar, boolean as boolean2, integer } from "drizzle-orm/pg-core";
687
+ var buildTable = (collection2) => {
688
+ const columns = {
689
+ // Add default id column
690
+ id: serial("id").primaryKey()
691
+ };
692
+ for (const [fieldName, fieldDef] of Object.entries(collection2.fields)) {
693
+ if (fieldName === "id") continue;
694
+ const fieldType2 = fieldDef.fieldType;
695
+ const fieldTypeName = fieldType2.name || fieldType2.type || "text";
696
+ switch (fieldTypeName) {
697
+ case "text":
698
+ columns[fieldName] = text2(fieldName);
699
+ break;
700
+ case "varchar":
701
+ columns[fieldName] = varchar(fieldName, { length: 255 });
702
+ break;
703
+ case "number":
704
+ case "integer":
705
+ columns[fieldName] = integer(fieldName);
706
+ break;
707
+ case "boolean":
708
+ columns[fieldName] = boolean2(fieldName);
709
+ break;
710
+ case "timestamp":
711
+ columns[fieldName] = timestamp2(fieldName);
712
+ break;
713
+ case "uuid":
714
+ columns[fieldName] = uuid(fieldName);
715
+ break;
716
+ default:
717
+ columns[fieldName] = text2(fieldName);
718
+ }
719
+ }
720
+ return pgTable(collection2.slug, columns);
721
+ };
722
+ var buildSchema = (collections) => {
723
+ const tables = {};
724
+ for (const coll of collections) {
725
+ tables[coll.slug] = buildTable(coll);
726
+ }
727
+ return tables;
728
+ };
729
+
730
+ // src/migrations.ts
731
+ var push = async (_adapter, collections) => {
732
+ const schema = buildSchema(collections);
733
+ console.log("[TODO] Push schema with collections:", Object.keys(schema));
734
+ };
735
+ var generate = async (_adapter, collections) => {
736
+ const schema = buildSchema(collections);
737
+ console.log("[TODO] Generate migration with collections:", Object.keys(schema));
738
+ };
739
+ var migrate = async (adapter) => {
740
+ console.log("[TODO] Apply migrations from:", adapter.config.migrationsPath);
741
+ };
742
+
743
+ // src/config.ts
744
+ import { Pool } from "pg";
745
+ import { drizzle } from "drizzle-orm/node-postgres";
746
+ var defineConfig = (options) => {
747
+ let pool = null;
748
+ let dbInstance = null;
749
+ let schema = {};
750
+ if (options.database.type === "postgres") {
751
+ pool = new Pool({
752
+ connectionString: options.database.config.url
753
+ });
754
+ schema = buildSchema(options.collections);
755
+ dbInstance = drizzle(pool, { schema });
756
+ }
757
+ const collectionsMap = {};
758
+ const collectionNames = [];
759
+ for (const coll of options.collections) {
760
+ collectionsMap[coll.slug] = {
761
+ slug: coll.slug,
762
+ name: coll.name,
763
+ fields: coll.fields,
764
+ hooks: coll.hooks,
765
+ dataType: coll.dataType
766
+ };
767
+ collectionNames.push(coll.slug);
768
+ }
769
+ const pluginNames = [];
770
+ if (options.plugins) {
771
+ for (const plugin of options.plugins) {
772
+ pluginNames.push(plugin.name);
773
+ if (plugin.collections) {
774
+ for (const [name, coll] of Object.entries(plugin.collections)) {
775
+ collectionsMap[name] = {
776
+ slug: coll.slug,
777
+ name: coll.name,
778
+ fields: coll.fields,
779
+ hooks: coll.hooks,
780
+ dataType: coll.dataType
781
+ };
782
+ collectionNames.push(name);
783
+ }
784
+ }
785
+ }
786
+ }
787
+ return {
788
+ collections: collectionsMap,
789
+ db: dbInstance,
790
+ $meta: {
791
+ collections: collectionNames,
792
+ plugins: pluginNames
793
+ }
794
+ };
795
+ };
796
+ export {
797
+ array,
798
+ boolean,
799
+ buildSchema,
800
+ buildTable,
801
+ collection,
802
+ createCollectionOperations,
803
+ date,
804
+ defineConfig,
805
+ email,
806
+ f,
807
+ field,
808
+ fieldType,
809
+ generate,
810
+ json,
811
+ migrate,
812
+ number,
813
+ pgAdapter,
814
+ push,
815
+ relation,
816
+ select,
817
+ text,
818
+ timestamp,
819
+ url
820
+ };
821
+ //# sourceMappingURL=index.mjs.map