turbine-orm 0.4.0 → 0.7.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 (57) hide show
  1. package/README.md +243 -26
  2. package/dist/cjs/cli/config.js +151 -0
  3. package/dist/cjs/cli/index.js +1176 -0
  4. package/dist/cjs/cli/migrate.js +446 -0
  5. package/dist/cjs/cli/ui.js +233 -0
  6. package/dist/cjs/client.js +512 -0
  7. package/dist/cjs/errors.js +293 -0
  8. package/dist/cjs/generate.js +321 -0
  9. package/dist/cjs/index.js +94 -0
  10. package/dist/cjs/introspect.js +287 -0
  11. package/dist/cjs/package.json +1 -0
  12. package/dist/cjs/pipeline.js +78 -0
  13. package/dist/cjs/query.js +1891 -0
  14. package/dist/cjs/schema-builder.js +238 -0
  15. package/dist/cjs/schema-sql.js +509 -0
  16. package/dist/cjs/schema.js +140 -0
  17. package/dist/cjs/serverless.js +110 -0
  18. package/dist/cli/config.js +6 -16
  19. package/dist/cli/index.js +256 -49
  20. package/dist/cli/migrate.d.ts +35 -6
  21. package/dist/cli/migrate.js +124 -76
  22. package/dist/cli/ui.js +5 -9
  23. package/dist/client.d.ts +87 -3
  24. package/dist/client.js +122 -46
  25. package/dist/errors.d.ts +138 -0
  26. package/dist/errors.js +278 -0
  27. package/dist/generate.js +37 -11
  28. package/dist/index.d.ts +10 -8
  29. package/dist/index.js +15 -11
  30. package/dist/introspect.js +3 -5
  31. package/dist/pipeline.js +8 -1
  32. package/dist/query.d.ts +310 -45
  33. package/dist/query.js +565 -237
  34. package/dist/schema-builder.js +91 -23
  35. package/dist/schema-sql.d.ts +6 -2
  36. package/dist/schema-sql.js +180 -26
  37. package/dist/schema.js +4 -1
  38. package/dist/serverless.d.ts +91 -139
  39. package/dist/serverless.js +86 -173
  40. package/package.json +44 -21
  41. package/dist/cli/config.d.ts.map +0 -1
  42. package/dist/cli/index.d.ts.map +0 -1
  43. package/dist/cli/migrate.d.ts.map +0 -1
  44. package/dist/cli/ui.d.ts.map +0 -1
  45. package/dist/client.d.ts.map +0 -1
  46. package/dist/generate.d.ts.map +0 -1
  47. package/dist/index.d.ts.map +0 -1
  48. package/dist/introspect.d.ts.map +0 -1
  49. package/dist/pipeline.d.ts.map +0 -1
  50. package/dist/query.d.ts.map +0 -1
  51. package/dist/schema-builder.d.ts.map +0 -1
  52. package/dist/schema-sql.d.ts.map +0 -1
  53. package/dist/schema.d.ts.map +0 -1
  54. package/dist/serverless.d.ts.map +0 -1
  55. package/dist/types.d.ts +0 -93
  56. package/dist/types.d.ts.map +0 -1
  57. package/dist/types.js +0 -126
@@ -0,0 +1,238 @@
1
+ "use strict";
2
+ /**
3
+ * turbine-orm — Schema Builder
4
+ *
5
+ * TypeScript-first schema definition API. Define your database schema
6
+ * as plain objects — no method chaining, no DSL. Fully type-checked,
7
+ * JSON-serializable, and easy to read.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * import { defineSchema } from 'turbine-orm';
12
+ *
13
+ * export default defineSchema({
14
+ * users: {
15
+ * id: { type: 'serial', primaryKey: true },
16
+ * email: { type: 'text', unique: true, notNull: true },
17
+ * name: { type: 'text', notNull: true },
18
+ * bio: { type: 'text' },
19
+ * role: { type: 'varchar', maxLength: 50, default: "'user'" },
20
+ * orgId: { type: 'bigint', notNull: true, references: 'organizations.id' },
21
+ * createdAt: { type: 'timestamp', default: 'now()' },
22
+ * },
23
+ * });
24
+ * ```
25
+ */
26
+ Object.defineProperty(exports, "__esModule", { value: true });
27
+ exports.camelToSnake = exports.column = exports.ColumnBuilder = void 0;
28
+ exports.defineSchema = defineSchema;
29
+ exports.table = table;
30
+ /** Maps shorthand names to actual Postgres type strings */
31
+ const TYPE_MAP = {
32
+ serial: 'BIGSERIAL',
33
+ bigint: 'BIGINT',
34
+ integer: 'INTEGER',
35
+ smallint: 'SMALLINT',
36
+ text: 'TEXT',
37
+ varchar: 'VARCHAR',
38
+ boolean: 'BOOLEAN',
39
+ timestamp: 'TIMESTAMPTZ',
40
+ date: 'DATE',
41
+ json: 'JSONB',
42
+ uuid: 'UUID',
43
+ real: 'REAL',
44
+ double: 'DOUBLE PRECISION',
45
+ numeric: 'NUMERIC',
46
+ bytea: 'BYTEA',
47
+ };
48
+ /** Convert a user-facing ColumnDef to the internal ColumnConfig */
49
+ function resolveColumn(def) {
50
+ if (!(def.type in TYPE_MAP)) {
51
+ throw new Error(`Invalid column type "${def.type}". Valid types: ${Object.keys(TYPE_MAP).join(', ')}`);
52
+ }
53
+ return {
54
+ type: TYPE_MAP[def.type],
55
+ isPrimaryKey: def.primaryKey ?? false,
56
+ isNotNull: def.notNull ?? false,
57
+ isNullable: def.nullable ?? false,
58
+ isUnique: def.unique ?? false,
59
+ defaultValue: def.default ?? null,
60
+ referencesTarget: def.references ?? null,
61
+ maxLength: def.maxLength ?? null,
62
+ };
63
+ }
64
+ /** Check if a value is a TableDef (from legacy table() builder) */
65
+ function isTableDef(v) {
66
+ return typeof v === 'object' && v !== null && 'columns' in v && 'name' in v;
67
+ }
68
+ /**
69
+ * Define the full database schema using plain objects.
70
+ *
71
+ * @example
72
+ * ```ts
73
+ * export default defineSchema({
74
+ * users: {
75
+ * id: { type: 'serial', primaryKey: true },
76
+ * email: { type: 'text', unique: true, notNull: true },
77
+ * name: { type: 'text', notNull: true },
78
+ * },
79
+ * posts: {
80
+ * id: { type: 'serial', primaryKey: true },
81
+ * userId: { type: 'bigint', notNull: true, references: 'users.id' },
82
+ * title: { type: 'text', notNull: true },
83
+ * },
84
+ * });
85
+ * ```
86
+ */
87
+ function defineSchema(input) {
88
+ const tables = {};
89
+ for (const [tableName, value] of Object.entries(input)) {
90
+ if (isTableDef(value)) {
91
+ // Legacy format: defineSchema({ users: table({ ... }) })
92
+ value.name = tableName;
93
+ tables[tableName] = value;
94
+ }
95
+ else {
96
+ // Object format: defineSchema({ users: { id: { type: 'serial' }, ... } })
97
+ const columns = {};
98
+ for (const [fieldName, def] of Object.entries(value)) {
99
+ columns[fieldName] = resolveColumn(def);
100
+ }
101
+ tables[tableName] = { name: tableName, columns };
102
+ }
103
+ }
104
+ return { tables };
105
+ }
106
+ // ---------------------------------------------------------------------------
107
+ // Legacy compat — ColumnBuilder still works for existing code
108
+ // ---------------------------------------------------------------------------
109
+ class ColumnBuilder {
110
+ _config;
111
+ constructor() {
112
+ this._config = {
113
+ type: 'TEXT',
114
+ isPrimaryKey: false,
115
+ isNotNull: false,
116
+ isNullable: false,
117
+ isUnique: false,
118
+ defaultValue: null,
119
+ referencesTarget: null,
120
+ maxLength: null,
121
+ };
122
+ }
123
+ serial() {
124
+ this._config.type = 'BIGSERIAL';
125
+ return this;
126
+ }
127
+ bigint() {
128
+ this._config.type = 'BIGINT';
129
+ return this;
130
+ }
131
+ integer() {
132
+ this._config.type = 'INTEGER';
133
+ return this;
134
+ }
135
+ smallint() {
136
+ this._config.type = 'SMALLINT';
137
+ return this;
138
+ }
139
+ text() {
140
+ this._config.type = 'TEXT';
141
+ return this;
142
+ }
143
+ varchar(length) {
144
+ this._config.type = 'VARCHAR';
145
+ this._config.maxLength = length;
146
+ return this;
147
+ }
148
+ boolean() {
149
+ this._config.type = 'BOOLEAN';
150
+ return this;
151
+ }
152
+ timestamp() {
153
+ this._config.type = 'TIMESTAMPTZ';
154
+ return this;
155
+ }
156
+ date() {
157
+ this._config.type = 'DATE';
158
+ return this;
159
+ }
160
+ json() {
161
+ this._config.type = 'JSONB';
162
+ return this;
163
+ }
164
+ uuid() {
165
+ this._config.type = 'UUID';
166
+ return this;
167
+ }
168
+ real() {
169
+ this._config.type = 'REAL';
170
+ return this;
171
+ }
172
+ doublePrecision() {
173
+ this._config.type = 'DOUBLE PRECISION';
174
+ return this;
175
+ }
176
+ numeric() {
177
+ this._config.type = 'NUMERIC';
178
+ return this;
179
+ }
180
+ bytea() {
181
+ this._config.type = 'BYTEA';
182
+ return this;
183
+ }
184
+ primaryKey() {
185
+ this._config.isPrimaryKey = true;
186
+ return this;
187
+ }
188
+ notNull() {
189
+ this._config.isNotNull = true;
190
+ return this;
191
+ }
192
+ nullable() {
193
+ this._config.isNullable = true;
194
+ return this;
195
+ }
196
+ unique() {
197
+ this._config.isUnique = true;
198
+ return this;
199
+ }
200
+ default(val) {
201
+ this._config.defaultValue = val;
202
+ return this;
203
+ }
204
+ references(target) {
205
+ this._config.referencesTarget = target;
206
+ return this;
207
+ }
208
+ build() {
209
+ return { ...this._config };
210
+ }
211
+ }
212
+ exports.ColumnBuilder = ColumnBuilder;
213
+ /** @deprecated Use defineSchema() with plain objects instead */
214
+ exports.column = new Proxy({}, {
215
+ get(_target, prop) {
216
+ if (prop === 'varchar')
217
+ return (length) => new ColumnBuilder().varchar(length);
218
+ return () => {
219
+ const builder = new ColumnBuilder();
220
+ if (typeof builder[prop] === 'function')
221
+ return builder[prop].call(builder);
222
+ throw new Error(`Unknown column type: ${prop}`);
223
+ };
224
+ },
225
+ });
226
+ /** @deprecated Use defineSchema() with plain objects instead */
227
+ function table(columns) {
228
+ const built = {};
229
+ for (const [fieldName, builder] of Object.entries(columns)) {
230
+ built[fieldName] = builder.build();
231
+ }
232
+ return { name: '', columns: built };
233
+ }
234
+ // ---------------------------------------------------------------------------
235
+ // Helpers
236
+ // ---------------------------------------------------------------------------
237
+ var schema_js_1 = require("./schema.js");
238
+ Object.defineProperty(exports, "camelToSnake", { enumerable: true, get: function () { return schema_js_1.camelToSnake; } });