@theokit/orm 0.1.0-next.1

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.
@@ -0,0 +1,162 @@
1
+ 'use strict';
2
+
3
+ var drizzleOrm = require('drizzle-orm');
4
+
5
+ var __defProp = Object.defineProperty;
6
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
+
8
+ // src/errors.ts
9
+ var OrmError = class extends Error {
10
+ static {
11
+ __name(this, "OrmError");
12
+ }
13
+ name = "OrmError";
14
+ constructor(message) {
15
+ super(message);
16
+ }
17
+ };
18
+ var OrmSchemaExportError = class extends OrmError {
19
+ static {
20
+ __name(this, "OrmSchemaExportError");
21
+ }
22
+ name = "OrmSchemaExportError";
23
+ };
24
+
25
+ // src/schema-export.ts
26
+ function tableNameOf(table) {
27
+ if (table === null || typeof table !== "object") return "<unknown>";
28
+ const sym = table[/* @__PURE__ */ Symbol.for("drizzle:Name")];
29
+ if (typeof sym === "string") return sym;
30
+ const inner = table._;
31
+ return inner?.name ?? "<unknown>";
32
+ }
33
+ __name(tableNameOf, "tableNameOf");
34
+ function lower(s) {
35
+ return (s ?? "").toLowerCase();
36
+ }
37
+ __name(lower, "lower");
38
+ function isInteger(dataType, columnType) {
39
+ if (dataType === "number" && /int/.test(columnType)) return true;
40
+ if (dataType === "integer") return true;
41
+ return false;
42
+ }
43
+ __name(isInteger, "isInteger");
44
+ var matchEnum = /* @__PURE__ */ __name((meta) => Array.isArray(meta.enumValues) && meta.enumValues.length > 0 ? {
45
+ type: "string",
46
+ enum: [
47
+ ...meta.enumValues
48
+ ]
49
+ } : null, "matchEnum");
50
+ var matchUuid = /* @__PURE__ */ __name((_m, _dt, ct) => /uuid/.test(ct) ? {
51
+ type: "string",
52
+ format: "uuid"
53
+ } : null, "matchUuid");
54
+ var matchNumeric = /* @__PURE__ */ __name((meta, _dt, ct) => {
55
+ if (!/numeric|decimal/.test(ct)) return null;
56
+ const out = {
57
+ type: "string",
58
+ format: "decimal"
59
+ };
60
+ if (typeof meta.scale === "number" && meta.scale >= 0) {
61
+ out.multipleOf = 10 ** -meta.scale;
62
+ }
63
+ return out;
64
+ }, "matchNumeric");
65
+ var matchBigint = /* @__PURE__ */ __name((_m, _dt, ct) => /bigint/.test(ct) ? {
66
+ type: "string",
67
+ format: "int64"
68
+ } : null, "matchBigint");
69
+ var matchDate = /* @__PURE__ */ __name((_m, dt, ct) => dt === "date" || /timestamp|date/.test(ct) ? {
70
+ type: "string",
71
+ format: "date-time"
72
+ } : null, "matchDate");
73
+ var matchBlob = /* @__PURE__ */ __name((_m, _dt, ct) => /blob|bytea|binary/.test(ct) ? {
74
+ type: "string",
75
+ contentEncoding: "base64"
76
+ } : null, "matchBlob");
77
+ var matchBoolean = /* @__PURE__ */ __name((_m, dt, ct) => dt === "boolean" || /boolean|bool/.test(ct) ? {
78
+ type: "boolean"
79
+ } : null, "matchBoolean");
80
+ var matchJson = /* @__PURE__ */ __name((_m, dt, ct) => dt === "json" || /json/.test(ct) ? {
81
+ type: "object"
82
+ } : null, "matchJson");
83
+ var matchInteger = /* @__PURE__ */ __name((_m, dt, ct) => isInteger(dt, ct) ? {
84
+ type: "integer"
85
+ } : null, "matchInteger");
86
+ var matchFloat = /* @__PURE__ */ __name((_m, dt, ct) => dt === "number" || /real|double|float/.test(ct) ? {
87
+ type: "number"
88
+ } : null, "matchFloat");
89
+ var matchString = /* @__PURE__ */ __name((meta, dt, ct) => {
90
+ if (!(dt === "string" || /text|varchar|char/.test(ct))) return null;
91
+ const out = {
92
+ type: "string"
93
+ };
94
+ if (typeof meta.length === "number" && meta.length > 0) out.maxLength = meta.length;
95
+ return out;
96
+ }, "matchString");
97
+ var COLUMN_MATCHERS = [
98
+ matchEnum,
99
+ matchUuid,
100
+ matchNumeric,
101
+ matchBigint,
102
+ matchDate,
103
+ matchBlob,
104
+ matchBoolean,
105
+ matchJson,
106
+ matchInteger,
107
+ matchFloat,
108
+ matchString
109
+ ];
110
+ function mapColumnToJsonSchema(colName, col, tableName) {
111
+ const meta = col;
112
+ const dt = lower(meta.dataType);
113
+ const ct = lower(meta.columnType);
114
+ for (const matcher of COLUMN_MATCHERS) {
115
+ const result = matcher(meta, dt, ct);
116
+ if (result !== null) return result;
117
+ }
118
+ throw new OrmSchemaExportError(`Cannot export column "${colName}" of table "${tableName}": unknown type (dataType="${meta.dataType}", columnType="${meta.columnType}"). Add to mapColumnToJsonSchema or use the "_excludeFromExport: true" annotation.`);
119
+ }
120
+ __name(mapColumnToJsonSchema, "mapColumnToJsonSchema");
121
+ function exportSchema(table) {
122
+ const tableName = tableNameOf(table);
123
+ const columns = drizzleOrm.getTableColumns(table);
124
+ const properties = {};
125
+ const required = [];
126
+ for (const [colName, col] of Object.entries(columns)) {
127
+ const meta = col;
128
+ const prop = mapColumnToJsonSchema(colName, col, tableName);
129
+ if (meta.hasDefault === true) {
130
+ if (meta.default !== void 0) {
131
+ prop.default = meta.default;
132
+ }
133
+ }
134
+ if (meta.notNull === true && meta.hasDefault !== true) {
135
+ required.push(colName);
136
+ }
137
+ properties[colName] = prop;
138
+ }
139
+ return {
140
+ $schema: "http://json-schema.org/draft-07/schema#",
141
+ title: tableName,
142
+ type: "object",
143
+ properties,
144
+ required,
145
+ additionalProperties: false
146
+ };
147
+ }
148
+ __name(exportSchema, "exportSchema");
149
+ function exportSchemas(schema) {
150
+ const out = {};
151
+ for (const [, table] of Object.entries(schema)) {
152
+ const name = tableNameOf(table);
153
+ out[name] = exportSchema(table);
154
+ }
155
+ return out;
156
+ }
157
+ __name(exportSchemas, "exportSchemas");
158
+
159
+ exports.exportSchema = exportSchema;
160
+ exports.exportSchemas = exportSchemas;
161
+ //# sourceMappingURL=schema-export.cjs.map
162
+ //# sourceMappingURL=schema-export.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/errors.ts","../src/schema-export.ts"],"names":["OrmError","Error","name","message","OrmSchemaExportError","tableNameOf","table","sym","Symbol","for","inner","_","lower","s","toLowerCase","isInteger","dataType","columnType","test","matchEnum","meta","Array","isArray","enumValues","length","type","enum","matchUuid","_m","_dt","ct","format","matchNumeric","out","scale","multipleOf","matchBigint","matchDate","dt","matchBlob","contentEncoding","matchBoolean","matchJson","matchInteger","matchFloat","matchString","maxLength","COLUMN_MATCHERS","mapColumnToJsonSchema","colName","col","tableName","matcher","result","exportSchema","columns","getTableColumns","properties","required","Object","entries","prop","hasDefault","default","undefined","notNull","push","$schema","title","additionalProperties","exportSchemas","schema"],"mappings":";;;;;;;;AAAO,IAAMA,QAAAA,GAAN,cAAuBC,KAAAA,CAAAA;EAA9B;;;EACoBC,IAAAA,GAAe,UAAA;AACjC,EAAA,WAAA,CAAYC,OAAAA,EAAiB;AAC3B,IAAA,KAAA,CAAMA,OAAAA,CAAAA;AACR,EAAA;AACF,CAAA;AAUO,IAAMC,oBAAAA,GAAN,cAAmCJ,QAAAA,CAAAA;EAf1C;;;EAgBoBE,IAAAA,GAAO,sBAAA;AAC3B,CAAA;;;ACwBA,SAASG,YAAYC,KAAAA,EAAc;AACjC,EAAA,IAAIA,KAAAA,KAAU,IAAA,IAAQ,OAAOA,KAAAA,KAAU,UAAU,OAAO,WAAA;AACxD,EAAA,MAAMC,GAAAA,GAAOD,KAAAA,iBAAkCE,MAAAA,CAAOC,GAAAA,CAAI,cAAA,CAAA,CAAA;AAC1D,EAAA,IAAI,OAAOF,GAAAA,KAAQ,QAAA,EAAU,OAAOA,GAAAA;AACpC,EAAA,MAAMG,QAASJ,KAAAA,CAAoCK,CAAAA;AACnD,EAAA,OAAOD,OAAOR,IAAAA,IAAQ,WAAA;AACxB;AANSG,MAAAA,CAAAA,WAAAA,EAAAA,aAAAA,CAAAA;AAQT,SAASO,MAAMC,CAAAA,EAAqB;AAClC,EAAA,OAAA,CAAQA,CAAAA,IAAK,IAAIC,WAAAA,EAAW;AAC9B;AAFSF,MAAAA,CAAAA,KAAAA,EAAAA,OAAAA,CAAAA;AAIT,SAASG,SAAAA,CAAUC,UAAkB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C,IAAAA,CAAK6C,OAAAA,KAAY,IAAA,IAAQ7C,IAAAA,CAAK0C,eAAe,IAAA,EAAM;AACrDJ,MAAAA,QAAAA,CAASQ,KAAKjB,OAAAA,CAAAA;AAChB,IAAA;AAEAQ,IAAAA,UAAAA,CAAWR,OAAAA,CAAAA,GAAWY,IAAAA;AACxB,EAAA;AAEA,EAAA,OAAO;IACLM,OAAAA,EAAS,yCAAA;IACTC,KAAAA,EAAOjB,SAAAA;IACP1B,IAAAA,EAAM,QAAA;AACNgC,IAAAA,UAAAA;AACAC,IAAAA,QAAAA;IACAW,oBAAAA,EAAsB;AACxB,GAAA;AACF;AAhCgBf,MAAAA,CAAAA,YAAAA,EAAAA,cAAAA,CAAAA;AAkCT,SAASgB,cAAcC,MAAAA,EAA6B;AACzD,EAAA,MAAMtC,MAAmC,EAAC;AAC1C,EAAA,KAAA,MAAW,GAAG3B,KAAAA,KAAUqD,MAAAA,CAAOC,OAAAA,CAAQW,MAAAA,CAAAA,EAAS;AAC9C,IAAA,MAAMrE,IAAAA,GAAOG,YAAYC,KAAAA,CAAAA;AACzB2B,IAAAA,GAAAA,CAAI/B,IAAAA,CAAAA,GAAQoD,YAAAA,CAAahD,KAAAA,CAAAA;AAC3B,EAAA;AACA,EAAA,OAAO2B,GAAAA;AACT;AAPgBqC,MAAAA,CAAAA,aAAAA,EAAAA,eAAAA,CAAAA","file":"schema-export.cjs","sourcesContent":["export class OrmError extends Error {\n override readonly name: string = \"OrmError\";\n constructor(message: string) {\n super(message);\n }\n}\n\nexport class OrmConfigurationError extends OrmError {\n override readonly name = \"OrmConfigurationError\";\n}\n\nexport class OrmValidationError extends OrmError {\n override readonly name = \"OrmValidationError\";\n}\n\nexport class OrmSchemaExportError extends OrmError {\n override readonly name = \"OrmSchemaExportError\";\n}\n","import type { Column, Table } from \"drizzle-orm\";\nimport { getTableColumns } from \"drizzle-orm\";\nimport { OrmSchemaExportError } from \"./errors.js\";\n\nexport type JsonSchema7 = {\n $schema?: string;\n title?: string;\n type: \"object\";\n properties: Record<string, JsonSchema7Property>;\n required: string[];\n additionalProperties: false;\n};\n\nexport type JsonSchema7Property = {\n type?: \"string\" | \"integer\" | \"number\" | \"boolean\" | \"object\" | \"array\" | \"null\";\n format?: string;\n maxLength?: number;\n multipleOf?: number;\n default?: unknown;\n enum?: ReadonlyArray<string | number>;\n contentEncoding?: string;\n \"x-check\"?: string;\n description?: string;\n};\n\ninterface ColumnInternal {\n dataType?: string;\n columnType?: string;\n notNull?: boolean;\n hasDefault?: boolean;\n default?: unknown;\n defaultFn?: () => unknown;\n enumValues?: ReadonlyArray<string>;\n length?: number;\n precision?: number;\n scale?: number;\n primary?: boolean;\n isPrimaryKey?: boolean;\n primaryKey?: boolean;\n}\n\nfunction tableNameOf(table: unknown): string {\n if (table === null || typeof table !== \"object\") return \"<unknown>\";\n const sym = (table as Record<symbol, unknown>)[Symbol.for(\"drizzle:Name\")];\n if (typeof sym === \"string\") return sym;\n const inner = (table as { _?: { name?: string } })._;\n return inner?.name ?? \"<unknown>\";\n}\n\nfunction lower(s: string | undefined): string {\n return (s ?? \"\").toLowerCase();\n}\n\nfunction isInteger(dataType: string, columnType: string): boolean {\n if (dataType === \"number\" && /int/.test(columnType)) return true;\n if (dataType === \"integer\") return true;\n return false;\n}\n\ntype ColumnMatcher = (meta: ColumnInternal, dt: string, ct: string) => JsonSchema7Property | null;\n\nconst matchEnum: ColumnMatcher = (meta) =>\n Array.isArray(meta.enumValues) && meta.enumValues.length > 0\n ? { type: \"string\", enum: [...meta.enumValues] }\n : null;\n\nconst matchUuid: ColumnMatcher = (_m, _dt, ct) =>\n /uuid/.test(ct) ? { type: \"string\", format: \"uuid\" } : null;\n\nconst matchNumeric: ColumnMatcher = (meta, _dt, ct) => {\n if (!/numeric|decimal/.test(ct)) return null;\n const out: JsonSchema7Property = { type: \"string\", format: \"decimal\" };\n if (typeof meta.scale === \"number\" && meta.scale >= 0) {\n out.multipleOf = 10 ** -meta.scale;\n }\n return out;\n};\n\nconst matchBigint: ColumnMatcher = (_m, _dt, ct) =>\n /bigint/.test(ct) ? { type: \"string\", format: \"int64\" } : null;\n\nconst matchDate: ColumnMatcher = (_m, dt, ct) =>\n dt === \"date\" || /timestamp|date/.test(ct) ? { type: \"string\", format: \"date-time\" } : null;\n\nconst matchBlob: ColumnMatcher = (_m, _dt, ct) =>\n /blob|bytea|binary/.test(ct) ? { type: \"string\", contentEncoding: \"base64\" } : null;\n\nconst matchBoolean: ColumnMatcher = (_m, dt, ct) =>\n dt === \"boolean\" || /boolean|bool/.test(ct) ? { type: \"boolean\" } : null;\n\nconst matchJson: ColumnMatcher = (_m, dt, ct) =>\n dt === \"json\" || /json/.test(ct) ? { type: \"object\" } : null;\n\nconst matchInteger: ColumnMatcher = (_m, dt, ct) =>\n isInteger(dt, ct) ? { type: \"integer\" } : null;\n\nconst matchFloat: ColumnMatcher = (_m, dt, ct) =>\n dt === \"number\" || /real|double|float/.test(ct) ? { type: \"number\" } : null;\n\nconst matchString: ColumnMatcher = (meta, dt, ct) => {\n if (!(dt === \"string\" || /text|varchar|char/.test(ct))) return null;\n const out: JsonSchema7Property = { type: \"string\" };\n if (typeof meta.length === \"number\" && meta.length > 0) out.maxLength = meta.length;\n return out;\n};\n\nconst COLUMN_MATCHERS: readonly ColumnMatcher[] = [\n matchEnum,\n matchUuid,\n matchNumeric,\n matchBigint,\n matchDate,\n matchBlob,\n matchBoolean,\n matchJson,\n matchInteger,\n matchFloat,\n matchString,\n];\n\nfunction mapColumnToJsonSchema(\n colName: string,\n col: Column,\n tableName: string,\n): JsonSchema7Property {\n const meta = col as unknown as ColumnInternal;\n const dt = lower(meta.dataType);\n const ct = lower(meta.columnType);\n\n for (const matcher of COLUMN_MATCHERS) {\n const result = matcher(meta, dt, ct);\n if (result !== null) return result;\n }\n\n throw new OrmSchemaExportError(\n `Cannot export column \"${colName}\" of table \"${tableName}\": unknown type (dataType=\"${meta.dataType}\", columnType=\"${meta.columnType}\"). Add to mapColumnToJsonSchema or use the \"_excludeFromExport: true\" annotation.`,\n );\n}\n\nexport function exportSchema(table: Table): JsonSchema7 {\n const tableName = tableNameOf(table);\n const columns = getTableColumns(table) as Record<string, Column>;\n\n const properties: Record<string, JsonSchema7Property> = {};\n const required: string[] = [];\n\n for (const [colName, col] of Object.entries(columns)) {\n const meta = col as unknown as ColumnInternal;\n const prop = mapColumnToJsonSchema(colName, col, tableName);\n\n if (meta.hasDefault === true) {\n if (meta.default !== undefined) {\n prop.default = meta.default;\n }\n }\n\n if (meta.notNull === true && meta.hasDefault !== true) {\n required.push(colName);\n }\n\n properties[colName] = prop;\n }\n\n return {\n $schema: \"http://json-schema.org/draft-07/schema#\",\n title: tableName,\n type: \"object\",\n properties,\n required,\n additionalProperties: false,\n };\n}\n\nexport function exportSchemas(schema: Record<string, Table>): Record<string, JsonSchema7> {\n const out: Record<string, JsonSchema7> = {};\n for (const [, table] of Object.entries(schema)) {\n const name = tableNameOf(table);\n out[name] = exportSchema(table);\n }\n return out;\n}\n"]}
@@ -0,0 +1,25 @@
1
+ import { Table } from 'drizzle-orm';
2
+
3
+ type JsonSchema7 = {
4
+ $schema?: string;
5
+ title?: string;
6
+ type: "object";
7
+ properties: Record<string, JsonSchema7Property>;
8
+ required: string[];
9
+ additionalProperties: false;
10
+ };
11
+ type JsonSchema7Property = {
12
+ type?: "string" | "integer" | "number" | "boolean" | "object" | "array" | "null";
13
+ format?: string;
14
+ maxLength?: number;
15
+ multipleOf?: number;
16
+ default?: unknown;
17
+ enum?: ReadonlyArray<string | number>;
18
+ contentEncoding?: string;
19
+ "x-check"?: string;
20
+ description?: string;
21
+ };
22
+ declare function exportSchema(table: Table): JsonSchema7;
23
+ declare function exportSchemas(schema: Record<string, Table>): Record<string, JsonSchema7>;
24
+
25
+ export { type JsonSchema7, type JsonSchema7Property, exportSchema, exportSchemas };
@@ -0,0 +1,25 @@
1
+ import { Table } from 'drizzle-orm';
2
+
3
+ type JsonSchema7 = {
4
+ $schema?: string;
5
+ title?: string;
6
+ type: "object";
7
+ properties: Record<string, JsonSchema7Property>;
8
+ required: string[];
9
+ additionalProperties: false;
10
+ };
11
+ type JsonSchema7Property = {
12
+ type?: "string" | "integer" | "number" | "boolean" | "object" | "array" | "null";
13
+ format?: string;
14
+ maxLength?: number;
15
+ multipleOf?: number;
16
+ default?: unknown;
17
+ enum?: ReadonlyArray<string | number>;
18
+ contentEncoding?: string;
19
+ "x-check"?: string;
20
+ description?: string;
21
+ };
22
+ declare function exportSchema(table: Table): JsonSchema7;
23
+ declare function exportSchemas(schema: Record<string, Table>): Record<string, JsonSchema7>;
24
+
25
+ export { type JsonSchema7, type JsonSchema7Property, exportSchema, exportSchemas };
@@ -0,0 +1,159 @@
1
+ import { getTableColumns } from 'drizzle-orm';
2
+
3
+ var __defProp = Object.defineProperty;
4
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
5
+
6
+ // src/errors.ts
7
+ var OrmError = class extends Error {
8
+ static {
9
+ __name(this, "OrmError");
10
+ }
11
+ name = "OrmError";
12
+ constructor(message) {
13
+ super(message);
14
+ }
15
+ };
16
+ var OrmSchemaExportError = class extends OrmError {
17
+ static {
18
+ __name(this, "OrmSchemaExportError");
19
+ }
20
+ name = "OrmSchemaExportError";
21
+ };
22
+
23
+ // src/schema-export.ts
24
+ function tableNameOf(table) {
25
+ if (table === null || typeof table !== "object") return "<unknown>";
26
+ const sym = table[/* @__PURE__ */ Symbol.for("drizzle:Name")];
27
+ if (typeof sym === "string") return sym;
28
+ const inner = table._;
29
+ return inner?.name ?? "<unknown>";
30
+ }
31
+ __name(tableNameOf, "tableNameOf");
32
+ function lower(s) {
33
+ return (s ?? "").toLowerCase();
34
+ }
35
+ __name(lower, "lower");
36
+ function isInteger(dataType, columnType) {
37
+ if (dataType === "number" && /int/.test(columnType)) return true;
38
+ if (dataType === "integer") return true;
39
+ return false;
40
+ }
41
+ __name(isInteger, "isInteger");
42
+ var matchEnum = /* @__PURE__ */ __name((meta) => Array.isArray(meta.enumValues) && meta.enumValues.length > 0 ? {
43
+ type: "string",
44
+ enum: [
45
+ ...meta.enumValues
46
+ ]
47
+ } : null, "matchEnum");
48
+ var matchUuid = /* @__PURE__ */ __name((_m, _dt, ct) => /uuid/.test(ct) ? {
49
+ type: "string",
50
+ format: "uuid"
51
+ } : null, "matchUuid");
52
+ var matchNumeric = /* @__PURE__ */ __name((meta, _dt, ct) => {
53
+ if (!/numeric|decimal/.test(ct)) return null;
54
+ const out = {
55
+ type: "string",
56
+ format: "decimal"
57
+ };
58
+ if (typeof meta.scale === "number" && meta.scale >= 0) {
59
+ out.multipleOf = 10 ** -meta.scale;
60
+ }
61
+ return out;
62
+ }, "matchNumeric");
63
+ var matchBigint = /* @__PURE__ */ __name((_m, _dt, ct) => /bigint/.test(ct) ? {
64
+ type: "string",
65
+ format: "int64"
66
+ } : null, "matchBigint");
67
+ var matchDate = /* @__PURE__ */ __name((_m, dt, ct) => dt === "date" || /timestamp|date/.test(ct) ? {
68
+ type: "string",
69
+ format: "date-time"
70
+ } : null, "matchDate");
71
+ var matchBlob = /* @__PURE__ */ __name((_m, _dt, ct) => /blob|bytea|binary/.test(ct) ? {
72
+ type: "string",
73
+ contentEncoding: "base64"
74
+ } : null, "matchBlob");
75
+ var matchBoolean = /* @__PURE__ */ __name((_m, dt, ct) => dt === "boolean" || /boolean|bool/.test(ct) ? {
76
+ type: "boolean"
77
+ } : null, "matchBoolean");
78
+ var matchJson = /* @__PURE__ */ __name((_m, dt, ct) => dt === "json" || /json/.test(ct) ? {
79
+ type: "object"
80
+ } : null, "matchJson");
81
+ var matchInteger = /* @__PURE__ */ __name((_m, dt, ct) => isInteger(dt, ct) ? {
82
+ type: "integer"
83
+ } : null, "matchInteger");
84
+ var matchFloat = /* @__PURE__ */ __name((_m, dt, ct) => dt === "number" || /real|double|float/.test(ct) ? {
85
+ type: "number"
86
+ } : null, "matchFloat");
87
+ var matchString = /* @__PURE__ */ __name((meta, dt, ct) => {
88
+ if (!(dt === "string" || /text|varchar|char/.test(ct))) return null;
89
+ const out = {
90
+ type: "string"
91
+ };
92
+ if (typeof meta.length === "number" && meta.length > 0) out.maxLength = meta.length;
93
+ return out;
94
+ }, "matchString");
95
+ var COLUMN_MATCHERS = [
96
+ matchEnum,
97
+ matchUuid,
98
+ matchNumeric,
99
+ matchBigint,
100
+ matchDate,
101
+ matchBlob,
102
+ matchBoolean,
103
+ matchJson,
104
+ matchInteger,
105
+ matchFloat,
106
+ matchString
107
+ ];
108
+ function mapColumnToJsonSchema(colName, col, tableName) {
109
+ const meta = col;
110
+ const dt = lower(meta.dataType);
111
+ const ct = lower(meta.columnType);
112
+ for (const matcher of COLUMN_MATCHERS) {
113
+ const result = matcher(meta, dt, ct);
114
+ if (result !== null) return result;
115
+ }
116
+ throw new OrmSchemaExportError(`Cannot export column "${colName}" of table "${tableName}": unknown type (dataType="${meta.dataType}", columnType="${meta.columnType}"). Add to mapColumnToJsonSchema or use the "_excludeFromExport: true" annotation.`);
117
+ }
118
+ __name(mapColumnToJsonSchema, "mapColumnToJsonSchema");
119
+ function exportSchema(table) {
120
+ const tableName = tableNameOf(table);
121
+ const columns = getTableColumns(table);
122
+ const properties = {};
123
+ const required = [];
124
+ for (const [colName, col] of Object.entries(columns)) {
125
+ const meta = col;
126
+ const prop = mapColumnToJsonSchema(colName, col, tableName);
127
+ if (meta.hasDefault === true) {
128
+ if (meta.default !== void 0) {
129
+ prop.default = meta.default;
130
+ }
131
+ }
132
+ if (meta.notNull === true && meta.hasDefault !== true) {
133
+ required.push(colName);
134
+ }
135
+ properties[colName] = prop;
136
+ }
137
+ return {
138
+ $schema: "http://json-schema.org/draft-07/schema#",
139
+ title: tableName,
140
+ type: "object",
141
+ properties,
142
+ required,
143
+ additionalProperties: false
144
+ };
145
+ }
146
+ __name(exportSchema, "exportSchema");
147
+ function exportSchemas(schema) {
148
+ const out = {};
149
+ for (const [, table] of Object.entries(schema)) {
150
+ const name = tableNameOf(table);
151
+ out[name] = exportSchema(table);
152
+ }
153
+ return out;
154
+ }
155
+ __name(exportSchemas, "exportSchemas");
156
+
157
+ export { exportSchema, exportSchemas };
158
+ //# sourceMappingURL=schema-export.js.map
159
+ //# sourceMappingURL=schema-export.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/errors.ts","../src/schema-export.ts"],"names":["OrmError","Error","name","message","OrmSchemaExportError","tableNameOf","table","sym","Symbol","for","inner","_","lower","s","toLowerCase","isInteger","dataType","columnType","test","matchEnum","meta","Array","isArray","enumValues","length","type","enum","matchUuid","_m","_dt","ct","format","matchNumeric","out","scale","multipleOf","matchBigint","matchDate","dt","matchBlob","contentEncoding","matchBoolean","matchJson","matchInteger","matchFloat","matchString","maxLength","COLUMN_MATCHERS","mapColumnToJsonSchema","colName","col","tableName","matcher","result","exportSchema","columns","getTableColumns","properties","required","Object","entries","prop","hasDefault","default","undefined","notNull","push","$schema","title","additionalProperties","exportSchemas","schema"],"mappings":";;;;;;AAAO,IAAMA,QAAAA,GAAN,cAAuBC,KAAAA,CAAAA;EAA9B;;;EACoBC,IAAAA,GAAe,UAAA;AACjC,EAAA,WAAA,CAAYC,OAAAA,EAAiB;AAC3B,IAAA,KAAA,CAAMA,OAAAA,CAAAA;AACR,EAAA;AACF,CAAA;AAUO,IAAMC,oBAAAA,GAAN,cAAmCJ,QAAAA,CAAAA;EAf1C;;;EAgBoBE,IAAAA,GAAO,sBAAA;AAC3B,CAAA;;;ACwBA,SAASG,YAAYC,KAAAA,EAAc;AACjC,EAAA,IAAIA,KAAAA,KAAU,IAAA,IAAQ,OAAOA,KAAAA,KAAU,UAAU,OAAO,WAAA;AACxD,EAAA,MAAMC,GAAAA,GAAOD,KAAAA,iBAAkCE,MAAAA,CAAOC,GAAAA,CAAI,cAAA,CAAA,CAAA;AAC1D,EAAA,IAAI,OAAOF,GAAAA,KAAQ,QAAA,EAAU,OAAOA,GAAAA;AACpC,EAAA,MAAMG,QAASJ,KAAAA,CAAoCK,CAAAA;AACnD,EAAA,OAAOD,OAAOR,I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OAAAA,EAASC,GAAAA,EAAKC,SAAAA,CAAAA;AAEjD,IAAA,IAAI/B,IAAAA,CAAK0C,eAAe,IAAA,EAAM;AAC5B,MAAA,IAAI1C,IAAAA,CAAK2C,YAAYC,MAAAA,EAAW;AAC9BH,QAAAA,IAAAA,CAAKE,UAAU3C,IAAAA,CAAK2C,OAAAA;AACtB,MAAA;AACF,IAAA;AAEA,IAAA,IAAI3C,IAAAA,CAAK6C,OAAAA,KAAY,IAAA,IAAQ7C,IAAAA,CAAK0C,eAAe,IAAA,EAAM;AACrDJ,MAAAA,QAAAA,CAASQ,KAAKjB,OAAAA,CAAAA;AAChB,IAAA;AAEAQ,IAAAA,UAAAA,CAAWR,OAAAA,CAAAA,GAAWY,IAAAA;AACxB,EAAA;AAEA,EAAA,OAAO;IACLM,OAAAA,EAAS,yCAAA;IACTC,KAAAA,EAAOjB,SAAAA;IACP1B,IAAAA,EAAM,QAAA;AACNgC,IAAAA,UAAAA;AACAC,IAAAA,QAAAA;IACAW,oBAAAA,EAAsB;AACxB,GAAA;AACF;AAhCgBf,MAAAA,CAAAA,YAAAA,EAAAA,cAAAA,CAAAA;AAkCT,SAASgB,cAAcC,MAAAA,EAA6B;AACzD,EAAA,MAAMtC,MAAmC,EAAC;AAC1C,EAAA,KAAA,MAAW,GAAG3B,KAAAA,KAAUqD,MAAAA,CAAOC,OAAAA,CAAQW,MAAAA,CAAAA,EAAS;AAC9C,IAAA,MAAMrE,IAAAA,GAAOG,YAAYC,KAAAA,CAAAA;AACzB2B,IAAAA,GAAAA,CAAI/B,IAAAA,CAAAA,GAAQoD,YAAAA,CAAahD,KAAAA,CAAAA;AAC3B,EAAA;AACA,EAAA,OAAO2B,GAAAA;AACT;AAPgBqC,MAAAA,CAAAA,aAAAA,EAAAA,eAAAA,CAAAA","file":"schema-export.js","sourcesContent":["export class OrmError extends Error {\n override readonly name: string = \"OrmError\";\n constructor(message: string) {\n super(message);\n }\n}\n\nexport class OrmConfigurationError extends OrmError {\n override readonly name = \"OrmConfigurationError\";\n}\n\nexport class OrmValidationError extends OrmError {\n override readonly name = \"OrmValidationError\";\n}\n\nexport class OrmSchemaExportError extends OrmError {\n override readonly name = \"OrmSchemaExportError\";\n}\n","import type { Column, Table } from \"drizzle-orm\";\nimport { getTableColumns } from \"drizzle-orm\";\nimport { OrmSchemaExportError } from \"./errors.js\";\n\nexport type JsonSchema7 = {\n $schema?: string;\n title?: string;\n type: \"object\";\n properties: Record<string, JsonSchema7Property>;\n required: string[];\n additionalProperties: false;\n};\n\nexport type JsonSchema7Property = {\n type?: \"string\" | \"integer\" | \"number\" | \"boolean\" | \"object\" | \"array\" | \"null\";\n format?: string;\n maxLength?: number;\n multipleOf?: number;\n default?: unknown;\n enum?: ReadonlyArray<string | number>;\n contentEncoding?: string;\n \"x-check\"?: string;\n description?: string;\n};\n\ninterface ColumnInternal {\n dataType?: string;\n columnType?: string;\n notNull?: boolean;\n hasDefault?: boolean;\n default?: unknown;\n defaultFn?: () => unknown;\n enumValues?: ReadonlyArray<string>;\n length?: number;\n precision?: number;\n scale?: number;\n primary?: boolean;\n isPrimaryKey?: boolean;\n primaryKey?: boolean;\n}\n\nfunction tableNameOf(table: unknown): string {\n if (table === null || typeof table !== \"object\") return \"<unknown>\";\n const sym = (table as Record<symbol, unknown>)[Symbol.for(\"drizzle:Name\")];\n if (typeof sym === \"string\") return sym;\n const inner = (table as { _?: { name?: string } })._;\n return inner?.name ?? \"<unknown>\";\n}\n\nfunction lower(s: string | undefined): string {\n return (s ?? \"\").toLowerCase();\n}\n\nfunction isInteger(dataType: string, columnType: string): boolean {\n if (dataType === \"number\" && /int/.test(columnType)) return true;\n if (dataType === \"integer\") return true;\n return false;\n}\n\ntype ColumnMatcher = (meta: ColumnInternal, dt: string, ct: string) => JsonSchema7Property | null;\n\nconst matchEnum: ColumnMatcher = (meta) =>\n Array.isArray(meta.enumValues) && meta.enumValues.length > 0\n ? { type: \"string\", enum: [...meta.enumValues] }\n : null;\n\nconst matchUuid: ColumnMatcher = (_m, _dt, ct) =>\n /uuid/.test(ct) ? { type: \"string\", format: \"uuid\" } : null;\n\nconst matchNumeric: ColumnMatcher = (meta, _dt, ct) => {\n if (!/numeric|decimal/.test(ct)) return null;\n const out: JsonSchema7Property = { type: \"string\", format: \"decimal\" };\n if (typeof meta.scale === \"number\" && meta.scale >= 0) {\n out.multipleOf = 10 ** -meta.scale;\n }\n return out;\n};\n\nconst matchBigint: ColumnMatcher = (_m, _dt, ct) =>\n /bigint/.test(ct) ? { type: \"string\", format: \"int64\" } : null;\n\nconst matchDate: ColumnMatcher = (_m, dt, ct) =>\n dt === \"date\" || /timestamp|date/.test(ct) ? { type: \"string\", format: \"date-time\" } : null;\n\nconst matchBlob: ColumnMatcher = (_m, _dt, ct) =>\n /blob|bytea|binary/.test(ct) ? { type: \"string\", contentEncoding: \"base64\" } : null;\n\nconst matchBoolean: ColumnMatcher = (_m, dt, ct) =>\n dt === \"boolean\" || /boolean|bool/.test(ct) ? { type: \"boolean\" } : null;\n\nconst matchJson: ColumnMatcher = (_m, dt, ct) =>\n dt === \"json\" || /json/.test(ct) ? { type: \"object\" } : null;\n\nconst matchInteger: ColumnMatcher = (_m, dt, ct) =>\n isInteger(dt, ct) ? { type: \"integer\" } : null;\n\nconst matchFloat: ColumnMatcher = (_m, dt, ct) =>\n dt === \"number\" || /real|double|float/.test(ct) ? { type: \"number\" } : null;\n\nconst matchString: ColumnMatcher = (meta, dt, ct) => {\n if (!(dt === \"string\" || /text|varchar|char/.test(ct))) return null;\n const out: JsonSchema7Property = { type: \"string\" };\n if (typeof meta.length === \"number\" && meta.length > 0) out.maxLength = meta.length;\n return out;\n};\n\nconst COLUMN_MATCHERS: readonly ColumnMatcher[] = [\n matchEnum,\n matchUuid,\n matchNumeric,\n matchBigint,\n matchDate,\n matchBlob,\n matchBoolean,\n matchJson,\n matchInteger,\n matchFloat,\n matchString,\n];\n\nfunction mapColumnToJsonSchema(\n colName: string,\n col: Column,\n tableName: string,\n): JsonSchema7Property {\n const meta = col as unknown as ColumnInternal;\n const dt = lower(meta.dataType);\n const ct = lower(meta.columnType);\n\n for (const matcher of COLUMN_MATCHERS) {\n const result = matcher(meta, dt, ct);\n if (result !== null) return result;\n }\n\n throw new OrmSchemaExportError(\n `Cannot export column \"${colName}\" of table \"${tableName}\": unknown type (dataType=\"${meta.dataType}\", columnType=\"${meta.columnType}\"). Add to mapColumnToJsonSchema or use the \"_excludeFromExport: true\" annotation.`,\n );\n}\n\nexport function exportSchema(table: Table): JsonSchema7 {\n const tableName = tableNameOf(table);\n const columns = getTableColumns(table) as Record<string, Column>;\n\n const properties: Record<string, JsonSchema7Property> = {};\n const required: string[] = [];\n\n for (const [colName, col] of Object.entries(columns)) {\n const meta = col as unknown as ColumnInternal;\n const prop = mapColumnToJsonSchema(colName, col, tableName);\n\n if (meta.hasDefault === true) {\n if (meta.default !== undefined) {\n prop.default = meta.default;\n }\n }\n\n if (meta.notNull === true && meta.hasDefault !== true) {\n required.push(colName);\n }\n\n properties[colName] = prop;\n }\n\n return {\n $schema: \"http://json-schema.org/draft-07/schema#\",\n title: tableName,\n type: \"object\",\n properties,\n required,\n additionalProperties: false,\n };\n}\n\nexport function exportSchemas(schema: Record<string, Table>): Record<string, JsonSchema7> {\n const out: Record<string, JsonSchema7> = {};\n for (const [, table] of Object.entries(schema)) {\n const name = tableNameOf(table);\n out[name] = exportSchema(table);\n }\n return out;\n}\n"]}
package/package.json ADDED
@@ -0,0 +1,92 @@
1
+ {
2
+ "name": "@theokit/orm",
3
+ "version": "0.1.0-next.1",
4
+ "description": "Repository pattern + @InjectRepository decorator + agent-aware columns + @Transactional + schema export polyglot over drizzle-orm, consuming @theokit/di for inversion of control. NestJS TypeORM-equivalent DX for the theokit ecosystem.",
5
+ "license": "Apache-2.0",
6
+ "homepage": "https://github.com/usetheo/theokit-sdk#readme",
7
+ "bugs": "https://github.com/usetheo/theokit-sdk/issues",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/usetheo/theokit-sdk.git",
11
+ "directory": "packages/orm"
12
+ },
13
+ "type": "module",
14
+ "sideEffects": false,
15
+ "engines": {
16
+ "node": ">=22.12.0"
17
+ },
18
+ "main": "./dist/index.cjs",
19
+ "module": "./dist/index.js",
20
+ "types": "./dist/index.d.ts",
21
+ "exports": {
22
+ ".": {
23
+ "import": {
24
+ "types": "./dist/index.d.ts",
25
+ "default": "./dist/index.js"
26
+ },
27
+ "require": {
28
+ "types": "./dist/index.d.cts",
29
+ "default": "./dist/index.cjs"
30
+ }
31
+ },
32
+ "./schema-export": {
33
+ "import": {
34
+ "types": "./dist/schema-export.d.ts",
35
+ "default": "./dist/schema-export.js"
36
+ },
37
+ "require": {
38
+ "types": "./dist/schema-export.d.cts",
39
+ "default": "./dist/schema-export.cjs"
40
+ }
41
+ }
42
+ },
43
+ "files": [
44
+ "dist",
45
+ "README.md",
46
+ "LICENSE",
47
+ "CHANGELOG.md"
48
+ ],
49
+ "scripts": {
50
+ "build": "tsup",
51
+ "typecheck": "tsc --noEmit",
52
+ "test": "vitest run --passWithNoTests",
53
+ "test:coverage": "vitest run --coverage --passWithNoTests"
54
+ },
55
+ "keywords": [
56
+ "orm",
57
+ "drizzle",
58
+ "repository",
59
+ "dependency-injection",
60
+ "decorators",
61
+ "transactional",
62
+ "typescript",
63
+ "theokit",
64
+ "usetheo"
65
+ ],
66
+ "peerDependencies": {
67
+ "@theokit/di": "^0.1.0",
68
+ "drizzle-orm": ">=0.36.0",
69
+ "reflect-metadata": "^0.2.0"
70
+ },
71
+ "peerDependenciesMeta": {
72
+ "drizzle-kit": {
73
+ "optional": true
74
+ }
75
+ },
76
+ "devDependencies": {
77
+ "@swc/core": "^1.15.40",
78
+ "@types/better-sqlite3": "^7.6.13",
79
+ "@theokit/di": "workspace:*",
80
+ "@vitest/coverage-v8": "^3.0.0",
81
+ "better-sqlite3": "^11.10.0",
82
+ "drizzle-orm": "^0.36.4",
83
+ "reflect-metadata": "^0.2.0",
84
+ "tsup": "^8.5.0",
85
+ "typescript": "^5.8.0",
86
+ "unplugin-swc": "^1.5.9",
87
+ "vitest": "^3.0.0"
88
+ },
89
+ "publishConfig": {
90
+ "access": "public"
91
+ }
92
+ }