@promakeai/orm 1.0.5 → 1.3.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,293 +0,0 @@
1
- /**
2
- * Field Builder DSL
3
- *
4
- * Type-safe fluent API for field definitions.
5
- *
6
- * @example
7
- * ```typescript
8
- * import { f } from '@promakeai/orm';
9
- *
10
- * const schema = defineSchema({
11
- * tables: {
12
- * products: {
13
- * id: f.id(),
14
- * price: f.decimal().required().min(0),
15
- * name: f.string().translatable().required().trim(),
16
- * email: f.string().lowercase().trim().unique(),
17
- * categoryId: f.int().ref('categories'),
18
- * brandId: f.int().ref({ table: 'brands', onDelete: 'SET_NULL' }),
19
- * },
20
- * },
21
- * });
22
- * ```
23
- */
24
-
25
- import type { FieldDefinition, FieldType, FieldReference } from "../types";
26
-
27
- /**
28
- * Fluent builder for field definitions
29
- */
30
- export class FieldBuilder {
31
- protected definition: FieldDefinition;
32
-
33
- constructor(type: FieldType) {
34
- this.definition = {
35
- type,
36
- nullable: true, // Default nullable
37
- unique: false,
38
- primary: false,
39
- translatable: false,
40
- };
41
- }
42
-
43
- /**
44
- * Mark field as NOT NULL
45
- */
46
- required(): this {
47
- this.definition.nullable = false;
48
- return this;
49
- }
50
-
51
- /**
52
- * Mark field as nullable (default)
53
- */
54
- nullable(): this {
55
- this.definition.nullable = true;
56
- return this;
57
- }
58
-
59
- /**
60
- * Mark field as UNIQUE
61
- */
62
- unique(): this {
63
- this.definition.unique = true;
64
- return this;
65
- }
66
-
67
- /**
68
- * Set default value
69
- */
70
- default(value: unknown): this {
71
- this.definition.default = value;
72
- return this;
73
- }
74
-
75
- /**
76
- * Mark field as translatable (stored in translation table)
77
- * Only valid for string/text fields
78
- */
79
- translatable(): this {
80
- this.definition.translatable = true;
81
- return this;
82
- }
83
-
84
- /**
85
- * Add reference to another table (MongoDB-style)
86
- * @param tableOrOptions - Table name or options object
87
- */
88
- ref(
89
- tableOrOptions:
90
- | string
91
- | {
92
- table: string;
93
- field?: string;
94
- onDelete?: "CASCADE" | "SET_NULL" | "RESTRICT" | "NO_ACTION";
95
- onUpdate?: "CASCADE" | "SET_NULL" | "RESTRICT" | "NO_ACTION";
96
- }
97
- ): this {
98
- this.definition.ref = tableOrOptions;
99
- return this;
100
- }
101
-
102
- /**
103
- * Trim whitespace from string values
104
- */
105
- trim(): this {
106
- this.definition.trim = true;
107
- return this;
108
- }
109
-
110
- /**
111
- * Convert string to lowercase
112
- */
113
- lowercase(): this {
114
- this.definition.lowercase = true;
115
- return this;
116
- }
117
-
118
- /**
119
- * Convert string to uppercase
120
- */
121
- uppercase(): this {
122
- this.definition.uppercase = true;
123
- return this;
124
- }
125
-
126
- /**
127
- * Minimum string length
128
- */
129
- minLength(value: number): this {
130
- this.definition.minLength = value;
131
- return this;
132
- }
133
-
134
- /**
135
- * Maximum string length
136
- */
137
- maxLength(value: number): this {
138
- this.definition.maxLength = value;
139
- return this;
140
- }
141
-
142
- /**
143
- * Minimum numeric value
144
- */
145
- min(value: number): this {
146
- this.definition.min = value;
147
- return this;
148
- }
149
-
150
- /**
151
- * Maximum numeric value
152
- */
153
- max(value: number): this {
154
- this.definition.max = value;
155
- return this;
156
- }
157
-
158
- /**
159
- * Enum values (allowed values)
160
- */
161
- enum(values: string[]): this {
162
- this.definition.enum = values;
163
- return this;
164
- }
165
-
166
- /**
167
- * RegExp pattern to match
168
- */
169
- match(pattern: string): this {
170
- this.definition.match = pattern;
171
- return this;
172
- }
173
-
174
- /**
175
- * Build the final field definition
176
- * @internal
177
- */
178
- build(): FieldDefinition {
179
- return { ...this.definition };
180
- }
181
- }
182
-
183
- /**
184
- * Factory object for creating field builders
185
- *
186
- * @example
187
- * ```typescript
188
- * f.id() // Primary key
189
- * f.string().required().trim() // NOT NULL trimmed string
190
- * f.int().default(0).min(0) // Non-negative integer
191
- * f.string().unique().lowercase() // Unique lowercase string
192
- * f.string().translatable().required() // Translatable NOT NULL
193
- * f.int().ref('users') // Foreign key to users
194
- * f.int().ref({ table: 'users', onDelete: 'CASCADE' }) // With constraint
195
- * f.json().ref('categories') // Array of category IDs
196
- * ```
197
- */
198
- export const f = {
199
- /**
200
- * Primary key field (INTEGER PRIMARY KEY AUTOINCREMENT)
201
- */
202
- id: (): FieldBuilder => {
203
- const builder = new FieldBuilder("id");
204
- builder["definition"].primary = true;
205
- builder["definition"].nullable = false;
206
- return builder;
207
- },
208
-
209
- /**
210
- * String field (VARCHAR/TEXT)
211
- */
212
- string: (): FieldBuilder => new FieldBuilder("string"),
213
-
214
- /**
215
- * Text field (TEXT/LONGTEXT)
216
- */
217
- text: (): FieldBuilder => new FieldBuilder("text"),
218
-
219
- /**
220
- * Integer field (INTEGER)
221
- */
222
- int: (): FieldBuilder => new FieldBuilder("int"),
223
-
224
- /**
225
- * Decimal field (REAL/DECIMAL)
226
- */
227
- decimal: (): FieldBuilder => new FieldBuilder("decimal"),
228
-
229
- /**
230
- * Boolean field (INTEGER 0/1 in SQLite)
231
- */
232
- bool: (): FieldBuilder => new FieldBuilder("bool"),
233
-
234
- /**
235
- * Timestamp field (TEXT in ISO format)
236
- */
237
- timestamp: (): FieldBuilder => new FieldBuilder("timestamp"),
238
-
239
- /**
240
- * JSON field (TEXT with JSON serialization) — untyped, generates Record<string, unknown>
241
- */
242
- json: (): FieldBuilder => new FieldBuilder("json"),
243
-
244
- /**
245
- * String array field — stored as JSON TEXT, generates string[]
246
- */
247
- stringArray: (): FieldBuilder => new FieldBuilder("string[]"),
248
-
249
- /**
250
- * Number array field — stored as JSON TEXT, generates number[]
251
- */
252
- numberArray: (): FieldBuilder => new FieldBuilder("number[]"),
253
-
254
- /**
255
- * Boolean array field — stored as JSON TEXT, generates boolean[]
256
- */
257
- boolArray: (): FieldBuilder => new FieldBuilder("boolean[]"),
258
-
259
- /**
260
- * Typed object field — stored as JSON TEXT, generates typed interface
261
- * @param properties - Property definitions. Keys ending with "?" are optional.
262
- * Values: "string" | "number" | "boolean"
263
- *
264
- * @example
265
- * ```typescript
266
- * f.object({ key: "string", value: "number" })
267
- * // Generates: { key: string; value: number }
268
- * ```
269
- */
270
- object: (properties: Record<string, string>): FieldBuilder => {
271
- const builder = new FieldBuilder("object");
272
- builder["definition"].properties = properties;
273
- return builder;
274
- },
275
-
276
- /**
277
- * Typed object array field — stored as JSON TEXT, generates Array<{...}>
278
- * @param properties - Property definitions. Keys ending with "?" are optional.
279
- * Values: "string" | "number" | "boolean"
280
- *
281
- * @example
282
- * ```typescript
283
- * f.objectArray({ id: "number", slug: "string", "name?": "string" })
284
- * // Generates: Array<{ id: number; slug: string; name?: string }>
285
- * ```
286
- */
287
- objectArray: (properties: Record<string, string>): FieldBuilder => {
288
- const builder = new FieldBuilder("object[]");
289
- builder["definition"].properties = properties;
290
- return builder;
291
- },
292
- };
293
-