uql-orm 0.7.6 → 0.7.10

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 (254) hide show
  1. package/CHANGELOG.md +30 -0
  2. package/README.md +21 -0
  3. package/dist/browser/mongoSchemaGenerator-B1IameRz.js +721 -0
  4. package/dist/browser/mongoSchemaGenerator-B1IameRz.js.map +1 -0
  5. package/dist/browser/uql-browser.min.js +23 -1
  6. package/dist/browser/uql-browser.min.js.map +1 -1
  7. package/dist/bunSql/bunSql.util.d.ts +4 -4
  8. package/dist/bunSql/bunSql.util.d.ts.map +1 -1
  9. package/dist/bunSql/bunSql.util.js +7 -7
  10. package/dist/bunSql/bunSql.util.js.map +1 -1
  11. package/dist/bunSql/bunSqlPostgresDialect.d.ts +12 -0
  12. package/dist/bunSql/bunSqlPostgresDialect.d.ts.map +1 -0
  13. package/dist/bunSql/bunSqlPostgresDialect.js +21 -0
  14. package/dist/bunSql/bunSqlPostgresDialect.js.map +1 -0
  15. package/dist/bunSql/bunSqlQuerierPool.d.ts +3 -3
  16. package/dist/bunSql/bunSqlQuerierPool.d.ts.map +1 -1
  17. package/dist/bunSql/bunSqlQuerierPool.js +14 -14
  18. package/dist/bunSql/bunSqlQuerierPool.js.map +1 -1
  19. package/dist/bunSql/bunSqliteDialect.d.ts +7 -0
  20. package/dist/bunSql/bunSqliteDialect.d.ts.map +1 -0
  21. package/dist/bunSql/bunSqliteDialect.js +7 -0
  22. package/dist/bunSql/bunSqliteDialect.js.map +1 -0
  23. package/dist/bunSql/index.d.ts +2 -0
  24. package/dist/bunSql/index.d.ts.map +1 -1
  25. package/dist/bunSql/index.js +2 -0
  26. package/dist/bunSql/index.js.map +1 -1
  27. package/dist/cockroachdb/cockroachDialect.d.ts +5 -2
  28. package/dist/cockroachdb/cockroachDialect.d.ts.map +1 -1
  29. package/dist/cockroachdb/cockroachDialect.js +4 -3
  30. package/dist/cockroachdb/cockroachDialect.js.map +1 -1
  31. package/dist/cockroachdb/crdbQuerierPool.js +2 -2
  32. package/dist/cockroachdb/crdbQuerierPool.js.map +1 -1
  33. package/dist/d1/d1QuerierPool.d.ts +2 -2
  34. package/dist/d1/d1QuerierPool.d.ts.map +1 -1
  35. package/dist/d1/d1QuerierPool.js +3 -3
  36. package/dist/d1/d1QuerierPool.js.map +1 -1
  37. package/dist/d1/d1SqliteDialect.d.ts +10 -0
  38. package/dist/d1/d1SqliteDialect.d.ts.map +1 -0
  39. package/dist/d1/d1SqliteDialect.js +10 -0
  40. package/dist/d1/d1SqliteDialect.js.map +1 -0
  41. package/dist/d1/index.d.ts +1 -0
  42. package/dist/d1/index.d.ts.map +1 -1
  43. package/dist/d1/index.js +1 -0
  44. package/dist/d1/index.js.map +1 -1
  45. package/dist/dialect/abstractDialect.d.ts +18 -10
  46. package/dist/dialect/abstractDialect.d.ts.map +1 -1
  47. package/dist/dialect/abstractDialect.js +12 -16
  48. package/dist/dialect/abstractDialect.js.map +1 -1
  49. package/dist/dialect/abstractSqlDialect.d.ts +21 -7
  50. package/dist/dialect/abstractSqlDialect.d.ts.map +1 -1
  51. package/dist/dialect/abstractSqlDialect.js +22 -16
  52. package/dist/dialect/abstractSqlDialect.js.map +1 -1
  53. package/dist/dialect/index.d.ts +4 -1
  54. package/dist/dialect/index.d.ts.map +1 -1
  55. package/dist/dialect/index.js +4 -1
  56. package/dist/dialect/index.js.map +1 -1
  57. package/dist/dialect/mysqlLikeSqlDialect.d.ts +17 -1
  58. package/dist/dialect/mysqlLikeSqlDialect.d.ts.map +1 -1
  59. package/dist/dialect/mysqlLikeSqlDialect.js +32 -0
  60. package/dist/dialect/mysqlLikeSqlDialect.js.map +1 -1
  61. package/dist/libsql/index.d.ts +1 -0
  62. package/dist/libsql/index.d.ts.map +1 -1
  63. package/dist/libsql/index.js +1 -0
  64. package/dist/libsql/index.js.map +1 -1
  65. package/dist/libsql/libsqlDialect.d.ts +10 -0
  66. package/dist/libsql/libsqlDialect.d.ts.map +1 -0
  67. package/dist/libsql/libsqlDialect.js +10 -0
  68. package/dist/libsql/libsqlDialect.js.map +1 -0
  69. package/dist/libsql/libsqlQuerierPool.d.ts +3 -4
  70. package/dist/libsql/libsqlQuerierPool.d.ts.map +1 -1
  71. package/dist/libsql/libsqlQuerierPool.js +5 -7
  72. package/dist/libsql/libsqlQuerierPool.js.map +1 -1
  73. package/dist/maria/mariaDialect.d.ts +6 -3
  74. package/dist/maria/mariaDialect.d.ts.map +1 -1
  75. package/dist/maria/mariaDialect.js +11 -3
  76. package/dist/maria/mariaDialect.js.map +1 -1
  77. package/dist/maria/mariadbQuerierPool.js +2 -2
  78. package/dist/maria/mariadbQuerierPool.js.map +1 -1
  79. package/dist/migrate/assertCliConfig.d.ts +6 -0
  80. package/dist/migrate/assertCliConfig.d.ts.map +1 -0
  81. package/dist/migrate/assertCliConfig.js +31 -0
  82. package/dist/migrate/assertCliConfig.js.map +1 -0
  83. package/dist/migrate/builder/migrationBuilder.js +2 -2
  84. package/dist/migrate/builder/migrationBuilder.js.map +1 -1
  85. package/dist/migrate/cli.d.ts +6 -2
  86. package/dist/migrate/cli.d.ts.map +1 -1
  87. package/dist/migrate/cli.js +14 -10
  88. package/dist/migrate/cli.js.map +1 -1
  89. package/dist/migrate/drift/driftDetector.d.ts +3 -4
  90. package/dist/migrate/drift/driftDetector.d.ts.map +1 -1
  91. package/dist/migrate/drift/driftDetector.js +5 -6
  92. package/dist/migrate/drift/driftDetector.js.map +1 -1
  93. package/dist/migrate/generator/index.d.ts +1 -1
  94. package/dist/migrate/generator/index.d.ts.map +1 -1
  95. package/dist/migrate/generator/index.js +1 -1
  96. package/dist/migrate/generator/index.js.map +1 -1
  97. package/dist/migrate/generator/mongoSchemaGenerator.d.ts +5 -2
  98. package/dist/migrate/generator/mongoSchemaGenerator.d.ts.map +1 -1
  99. package/dist/migrate/generator/mongoSchemaGenerator.js +7 -2
  100. package/dist/migrate/generator/mongoSchemaGenerator.js.map +1 -1
  101. package/dist/migrate/index.d.ts +4 -2
  102. package/dist/migrate/index.d.ts.map +1 -1
  103. package/dist/migrate/index.js +3 -1
  104. package/dist/migrate/index.js.map +1 -1
  105. package/dist/migrate/introspection/baseSqlIntrospector.d.ts +3 -5
  106. package/dist/migrate/introspection/baseSqlIntrospector.d.ts.map +1 -1
  107. package/dist/migrate/introspection/baseSqlIntrospector.js +1 -4
  108. package/dist/migrate/introspection/baseSqlIntrospector.js.map +1 -1
  109. package/dist/migrate/introspection/mysqlIntrospector.d.ts.map +1 -1
  110. package/dist/migrate/introspection/mysqlIntrospector.js +1 -1
  111. package/dist/migrate/introspection/mysqlIntrospector.js.map +1 -1
  112. package/dist/migrate/introspection/postgresIntrospector.d.ts.map +1 -1
  113. package/dist/migrate/introspection/postgresIntrospector.js +1 -1
  114. package/dist/migrate/introspection/postgresIntrospector.js.map +1 -1
  115. package/dist/migrate/introspection/sqliteIntrospector.d.ts.map +1 -1
  116. package/dist/migrate/introspection/sqliteIntrospector.js +9 -1
  117. package/dist/migrate/introspection/sqliteIntrospector.js.map +1 -1
  118. package/dist/migrate/migrator.d.ts +7 -7
  119. package/dist/migrate/migrator.d.ts.map +1 -1
  120. package/dist/migrate/migrator.js +47 -18
  121. package/dist/migrate/migrator.js.map +1 -1
  122. package/dist/migrate/schemaGenerator.d.ts +13 -8
  123. package/dist/migrate/schemaGenerator.d.ts.map +1 -1
  124. package/dist/migrate/schemaGenerator.js +47 -39
  125. package/dist/migrate/schemaGenerator.js.map +1 -1
  126. package/dist/migrate/schemaGeneratorAsync.d.ts +9 -0
  127. package/dist/migrate/schemaGeneratorAsync.d.ts.map +1 -0
  128. package/dist/migrate/schemaGeneratorAsync.js +13 -0
  129. package/dist/migrate/schemaGeneratorAsync.js.map +1 -0
  130. package/dist/mongo/index.d.ts +2 -0
  131. package/dist/mongo/index.d.ts.map +1 -1
  132. package/dist/mongo/index.js +2 -0
  133. package/dist/mongo/index.js.map +1 -1
  134. package/dist/mongo/mongoDialect.d.ts +8 -3
  135. package/dist/mongo/mongoDialect.d.ts.map +1 -1
  136. package/dist/mongo/mongoDialect.js +23 -4
  137. package/dist/mongo/mongoDialect.js.map +1 -1
  138. package/dist/mongo/mongodbNativeDialect.d.ts +10 -0
  139. package/dist/mongo/mongodbNativeDialect.d.ts.map +1 -0
  140. package/dist/mongo/mongodbNativeDialect.js +10 -0
  141. package/dist/mongo/mongodbNativeDialect.js.map +1 -0
  142. package/dist/mongo/mongodbQuerierPool.d.ts +2 -2
  143. package/dist/mongo/mongodbQuerierPool.d.ts.map +1 -1
  144. package/dist/mongo/mongodbQuerierPool.js +3 -3
  145. package/dist/mongo/mongodbQuerierPool.js.map +1 -1
  146. package/dist/mysql/index.d.ts +1 -0
  147. package/dist/mysql/index.d.ts.map +1 -1
  148. package/dist/mysql/index.js +1 -0
  149. package/dist/mysql/index.js.map +1 -1
  150. package/dist/mysql/mysql2Dialect.d.ts +10 -0
  151. package/dist/mysql/mysql2Dialect.d.ts.map +1 -0
  152. package/dist/mysql/mysql2Dialect.js +10 -0
  153. package/dist/mysql/mysql2Dialect.js.map +1 -0
  154. package/dist/mysql/mysql2QuerierPool.d.ts +2 -2
  155. package/dist/mysql/mysql2QuerierPool.d.ts.map +1 -1
  156. package/dist/mysql/mysql2QuerierPool.js +3 -3
  157. package/dist/mysql/mysql2QuerierPool.js.map +1 -1
  158. package/dist/mysql/mysqlDialect.d.ts +3 -3
  159. package/dist/mysql/mysqlDialect.d.ts.map +1 -1
  160. package/dist/mysql/mysqlDialect.js +3 -4
  161. package/dist/mysql/mysqlDialect.js.map +1 -1
  162. package/dist/neon/index.d.ts +1 -0
  163. package/dist/neon/index.d.ts.map +1 -1
  164. package/dist/neon/index.js +1 -0
  165. package/dist/neon/index.js.map +1 -1
  166. package/dist/neon/neonDialect.d.ts +11 -0
  167. package/dist/neon/neonDialect.d.ts.map +1 -0
  168. package/dist/neon/neonDialect.js +11 -0
  169. package/dist/neon/neonDialect.js.map +1 -0
  170. package/dist/neon/neonQuerierPool.d.ts +2 -2
  171. package/dist/neon/neonQuerierPool.d.ts.map +1 -1
  172. package/dist/neon/neonQuerierPool.js +3 -3
  173. package/dist/neon/neonQuerierPool.js.map +1 -1
  174. package/dist/postgres/index.d.ts +2 -0
  175. package/dist/postgres/index.d.ts.map +1 -1
  176. package/dist/postgres/index.js +2 -0
  177. package/dist/postgres/index.js.map +1 -1
  178. package/dist/postgres/pgDialect.d.ts +13 -0
  179. package/dist/postgres/pgDialect.d.ts.map +1 -0
  180. package/dist/postgres/pgDialect.js +14 -0
  181. package/dist/postgres/pgDialect.js.map +1 -0
  182. package/dist/postgres/pgQuerierPool.d.ts +2 -2
  183. package/dist/postgres/pgQuerierPool.d.ts.map +1 -1
  184. package/dist/postgres/pgQuerierPool.js +3 -3
  185. package/dist/postgres/pgQuerierPool.js.map +1 -1
  186. package/dist/postgres/postgresDialect.d.ts +21 -3
  187. package/dist/postgres/postgresDialect.d.ts.map +1 -1
  188. package/dist/postgres/postgresDialect.js +49 -9
  189. package/dist/postgres/postgresDialect.js.map +1 -1
  190. package/dist/postgres/postgresWireDriverCapabilities.d.ts +15 -0
  191. package/dist/postgres/postgresWireDriverCapabilities.d.ts.map +1 -0
  192. package/dist/postgres/postgresWireDriverCapabilities.js +15 -0
  193. package/dist/postgres/postgresWireDriverCapabilities.js.map +1 -0
  194. package/dist/querier/abstractQuerierPool.d.ts +7 -5
  195. package/dist/querier/abstractQuerierPool.d.ts.map +1 -1
  196. package/dist/querier/abstractQuerierPool.js +6 -6
  197. package/dist/querier/abstractQuerierPool.js.map +1 -1
  198. package/dist/querier/abstractSqlQuerier.d.ts +2 -0
  199. package/dist/querier/abstractSqlQuerier.d.ts.map +1 -1
  200. package/dist/querier/abstractSqlQuerier.js +47 -2
  201. package/dist/querier/abstractSqlQuerier.js.map +1 -1
  202. package/dist/schema/canonicalType.d.ts +3 -3
  203. package/dist/schema/canonicalType.d.ts.map +1 -1
  204. package/dist/schema/canonicalType.js +13 -12
  205. package/dist/schema/canonicalType.js.map +1 -1
  206. package/dist/schema/index.d.ts +0 -2
  207. package/dist/schema/index.d.ts.map +1 -1
  208. package/dist/schema/index.js +0 -2
  209. package/dist/schema/index.js.map +1 -1
  210. package/dist/sqlite/betterSqlite3Dialect.d.ts +10 -0
  211. package/dist/sqlite/betterSqlite3Dialect.d.ts.map +1 -0
  212. package/dist/sqlite/betterSqlite3Dialect.js +10 -0
  213. package/dist/sqlite/betterSqlite3Dialect.js.map +1 -0
  214. package/dist/sqlite/index.d.ts +1 -0
  215. package/dist/sqlite/index.d.ts.map +1 -1
  216. package/dist/sqlite/index.js +1 -0
  217. package/dist/sqlite/index.js.map +1 -1
  218. package/dist/sqlite/sqliteDialect.d.ts +17 -3
  219. package/dist/sqlite/sqliteDialect.d.ts.map +1 -1
  220. package/dist/sqlite/sqliteDialect.js +34 -6
  221. package/dist/sqlite/sqliteDialect.js.map +1 -1
  222. package/dist/sqlite/sqliteQuerierPool.d.ts +4 -4
  223. package/dist/sqlite/sqliteQuerierPool.d.ts.map +1 -1
  224. package/dist/sqlite/sqliteQuerierPool.js +4 -4
  225. package/dist/sqlite/sqliteQuerierPool.js.map +1 -1
  226. package/dist/type/config.d.ts +4 -7
  227. package/dist/type/config.d.ts.map +1 -1
  228. package/dist/type/index.d.ts +1 -0
  229. package/dist/type/index.d.ts.map +1 -1
  230. package/dist/type/index.js +1 -0
  231. package/dist/type/index.js.map +1 -1
  232. package/dist/type/migration.d.ts +2 -6
  233. package/dist/type/migration.d.ts.map +1 -1
  234. package/dist/type/migratorDialect.d.ts +10 -0
  235. package/dist/type/migratorDialect.d.ts.map +1 -0
  236. package/dist/type/migratorDialect.js +16 -0
  237. package/dist/type/migratorDialect.js.map +1 -0
  238. package/dist/type/querier.d.ts +6 -9
  239. package/dist/type/querier.d.ts.map +1 -1
  240. package/dist/type/querier.js +1 -1
  241. package/dist/type/querier.js.map +1 -1
  242. package/dist/type/querierPool.d.ts +13 -5
  243. package/dist/type/querierPool.d.ts.map +1 -1
  244. package/dist/type/query.d.ts +26 -5
  245. package/dist/type/query.d.ts.map +1 -1
  246. package/dist/util/ansiSqlLiteral.d.ts +21 -0
  247. package/dist/util/ansiSqlLiteral.d.ts.map +1 -0
  248. package/dist/util/ansiSqlLiteral.js +101 -0
  249. package/dist/util/ansiSqlLiteral.js.map +1 -0
  250. package/package.json +4 -3
  251. package/dist/dialect/dialectConfig.d.ts +0 -40
  252. package/dist/dialect/dialectConfig.d.ts.map +0 -1
  253. package/dist/dialect/dialectConfig.js +0 -197
  254. package/dist/dialect/dialectConfig.js.map +0 -1
@@ -0,0 +1,721 @@
1
+ import { getMeta } from '../entity/index.js';
2
+ import { ObjectId } from 'mongodb';
3
+ import { buildQueryWhereAsMap, buildSortMap, hasKeys, filterRelationKeys, getKeys, fillOnFields, filterFieldKeys, parseGroupMap, isVectorSearch } from '../util/index.js';
4
+
5
+ /**
6
+ * Merge dialect capability defaults with optional overrides from {@link DialectOptions.driverCapabilities}.
7
+ */ function mergeDialectFeatures(defaults, patch) {
8
+ return patch ? {
9
+ ...defaults,
10
+ ...patch
11
+ } : defaults;
12
+ }
13
+ /**
14
+ * Base abstract class for all database dialects (SQL and NoSQL).
15
+ */ class AbstractDialect {
16
+ constructor(featureDefaults, options = {}){
17
+ this.options = options;
18
+ this.namingStrategy = options.namingStrategy;
19
+ this.features = mergeDialectFeatures(featureDefaults, options.driverCapabilities);
20
+ }
21
+ /**
22
+ * Resolve the table name for an entity, applying naming strategy if necessary.
23
+ */ resolveTableName(entity, meta) {
24
+ const name = meta.name ?? entity.name;
25
+ if (name !== entity.name || !this.namingStrategy) {
26
+ return name;
27
+ }
28
+ return this.namingStrategy.tableName(name);
29
+ }
30
+ /**
31
+ * Resolve the column/field name for a property, applying naming strategy if necessary.
32
+ */ resolveColumnName(key, field) {
33
+ if (!field || field.name !== key || !this.namingStrategy) {
34
+ return field?.name ?? key;
35
+ }
36
+ return this.namingStrategy.columnName(field.name);
37
+ }
38
+ }
39
+
40
+ class MongoDialect extends AbstractDialect {
41
+ static{
42
+ /** Default {@link DialectFeatures} for MongoDB; shared by {@link MongoSchemaGenerator}. */ this.defaultDialectFeatures = {
43
+ explicitJsonCast: false,
44
+ nativeArrays: false,
45
+ supportsJsonb: false,
46
+ returning: false,
47
+ ifNotExists: false,
48
+ indexIfNotExists: false,
49
+ dropTableCascade: false,
50
+ renameColumn: false,
51
+ foreignKeyAlter: false,
52
+ columnComment: false,
53
+ vectorIndexStyle: 'create',
54
+ vectorSupportsLength: false,
55
+ supportsTimestamptz: false,
56
+ defaultStringAsText: false
57
+ };
58
+ }
59
+ constructor(options = {}){
60
+ super(MongoDialect.defaultDialectFeatures, options), this.dialectName = 'mongodb', this.insertIdStrategy = 'last';
61
+ }
62
+ static{
63
+ this.ID_KEY = '_id';
64
+ }
65
+ static{
66
+ this.VECTOR_INDEX_TYPES = new Set([
67
+ 'vectorSearch',
68
+ 'hnsw',
69
+ 'ivfflat',
70
+ 'vector'
71
+ ]);
72
+ }
73
+ static{
74
+ this.AGGREGATE_OP_MAP = {
75
+ $count: '$sum',
76
+ $sum: '$sum',
77
+ $avg: '$avg',
78
+ $min: '$min',
79
+ $max: '$max'
80
+ };
81
+ }
82
+ where(entity, where = {}, { softDelete } = {}) {
83
+ const meta = getMeta(entity);
84
+ const whereMap = buildQueryWhereAsMap(meta, where);
85
+ if (meta.softDelete && (softDelete || softDelete === undefined) && !whereMap[meta.softDelete]) {
86
+ const field = meta.fields[meta.softDelete];
87
+ whereMap[this.resolveColumnName(meta.softDelete, field)] = null;
88
+ }
89
+ const filter = {};
90
+ for (const [rawKey, rawVal] of Object.entries(whereMap)){
91
+ let key = rawKey;
92
+ let val = rawVal;
93
+ if (key === '$and' || key === '$or') {
94
+ filter[key] = val.map((filterIt)=>this.where(entity, filterIt));
95
+ } else {
96
+ const field = meta.fields[key];
97
+ if (key === MongoDialect.ID_KEY || key === meta.id) {
98
+ key = MongoDialect.ID_KEY;
99
+ val = this.getIdValue(val);
100
+ } else if (field) {
101
+ key = this.resolveColumnName(key, field);
102
+ }
103
+ if (val && typeof val === 'object' && !Array.isArray(val) && this.hasOperatorKeys(val)) {
104
+ val = this.transformOperators(val);
105
+ } else if (Array.isArray(val)) {
106
+ val = {
107
+ $in: val
108
+ };
109
+ }
110
+ filter[key] = val;
111
+ }
112
+ }
113
+ return filter;
114
+ }
115
+ /**
116
+ * Check if an object has operator keys (keys starting with $).
117
+ */ hasOperatorKeys(obj) {
118
+ return Object.keys(obj).some((key)=>key.startsWith('$'));
119
+ }
120
+ mapTableNameRow(row) {
121
+ return row.table_name;
122
+ }
123
+ static{
124
+ /** String operators → { pattern: (v) => regex, caseInsensitive } */ this.REGEX_OP_MAP = {
125
+ $startsWith: {
126
+ wrap: (v)=>`^${v}`,
127
+ ci: false
128
+ },
129
+ $istartsWith: {
130
+ wrap: (v)=>`^${v}`,
131
+ ci: true
132
+ },
133
+ $endsWith: {
134
+ wrap: (v)=>`${v}$`,
135
+ ci: false
136
+ },
137
+ $iendsWith: {
138
+ wrap: (v)=>`${v}$`,
139
+ ci: true
140
+ },
141
+ $includes: {
142
+ wrap: (v)=>String(v),
143
+ ci: false
144
+ },
145
+ $iincludes: {
146
+ wrap: (v)=>String(v),
147
+ ci: true
148
+ },
149
+ $like: {
150
+ wrap: (v)=>String(v).replace(/%/g, '.*').replace(/_/g, '.'),
151
+ ci: false
152
+ },
153
+ $ilike: {
154
+ wrap: (v)=>String(v).replace(/%/g, '.*').replace(/_/g, '.'),
155
+ ci: true
156
+ }
157
+ };
158
+ }
159
+ static{
160
+ /** MongoDB native operators — pass through as-is. */ this.NATIVE_OPS = new Set([
161
+ '$all',
162
+ '$size',
163
+ '$elemMatch',
164
+ '$eq',
165
+ '$ne',
166
+ '$lt',
167
+ '$lte',
168
+ '$gt',
169
+ '$gte',
170
+ '$in',
171
+ '$nin',
172
+ '$regex',
173
+ '$not'
174
+ ]);
175
+ }
176
+ /**
177
+ * Transform UQL operators to MongoDB operators.
178
+ */ transformOperators(ops) {
179
+ const result = {};
180
+ for (const [op, val] of Object.entries(ops)){
181
+ // Native MongoDB operators — pass through directly
182
+ if (MongoDialect.NATIVE_OPS.has(op)) {
183
+ result[op] = val;
184
+ continue;
185
+ }
186
+ // String/pattern → regex operators (8 variants including $like/$ilike)
187
+ const regexEntry = MongoDialect.REGEX_OP_MAP[op];
188
+ if (regexEntry) {
189
+ result['$regex'] = regexEntry.wrap(val);
190
+ if (regexEntry.ci) result['$options'] = 'i';
191
+ continue;
192
+ }
193
+ // Structural transforms
194
+ switch(op){
195
+ case '$between':
196
+ {
197
+ const [min, max] = val;
198
+ result['$gte'] = min;
199
+ result['$lte'] = max;
200
+ break;
201
+ }
202
+ case '$isNull':
203
+ result[val ? '$eq' : '$ne'] = null;
204
+ break;
205
+ case '$isNotNull':
206
+ result[val ? '$ne' : '$eq'] = null;
207
+ break;
208
+ case '$text':
209
+ result['$text'] = {
210
+ $search: val
211
+ };
212
+ break;
213
+ default:
214
+ result[op] = val;
215
+ break;
216
+ }
217
+ }
218
+ return result;
219
+ }
220
+ select(entity, select) {
221
+ return select;
222
+ }
223
+ sort(entity, sort) {
224
+ const raw = buildSortMap(sort);
225
+ const normalized = {};
226
+ for (const [key, dir] of Object.entries(raw)){
227
+ normalized[key] = dir === 'desc' || dir === -1 ? -1 : 1;
228
+ }
229
+ return normalized;
230
+ }
231
+ aggregationPipeline(entity, q) {
232
+ const meta = getMeta(entity);
233
+ const where = this.where(entity, q.$where);
234
+ const sort = this.sort(entity, q.$sort);
235
+ const firstPipelineEntry = {};
236
+ if (hasKeys(where)) {
237
+ firstPipelineEntry.$match = where;
238
+ }
239
+ if (hasKeys(sort)) {
240
+ firstPipelineEntry.$sort = sort;
241
+ }
242
+ const pipeline = [];
243
+ if (hasKeys(firstPipelineEntry)) {
244
+ pipeline.push(firstPipelineEntry);
245
+ }
246
+ const relKeys = filterRelationKeys(meta, q.$select);
247
+ for (const relKey of relKeys){
248
+ const relOpts = meta.relations[relKey];
249
+ if (!relOpts) continue;
250
+ if (relOpts.cardinality === '1m' || relOpts.cardinality === 'mm') {
251
+ continue;
252
+ }
253
+ const relEntity = relOpts.entity();
254
+ const relMeta = getMeta(relEntity);
255
+ if (relOpts.cardinality === 'm1') {
256
+ const localField = meta.fields[relOpts.references[0].local];
257
+ pipeline.push({
258
+ $lookup: {
259
+ from: this.resolveTableName(relEntity, relMeta),
260
+ localField: this.resolveColumnName(relOpts.references[0].local, localField),
261
+ foreignField: '_id',
262
+ as: relKey
263
+ }
264
+ });
265
+ } else {
266
+ const foreignField = relMeta.fields[relOpts.references[0].foreign];
267
+ const foreignFieldName = this.resolveColumnName(relOpts.references[0].foreign, foreignField);
268
+ const referenceWhere = this.where(relEntity, where);
269
+ const referenceSort = this.sort(relEntity, q.$sort);
270
+ const _id = MongoDialect.ID_KEY;
271
+ const referencePipelineEntry = {
272
+ $match: {
273
+ [foreignFieldName]: referenceWhere[_id]
274
+ }
275
+ };
276
+ if (hasKeys(referenceSort)) {
277
+ referencePipelineEntry.$sort = referenceSort;
278
+ }
279
+ pipeline.push({
280
+ $lookup: {
281
+ from: this.resolveTableName(relEntity, relMeta),
282
+ pipeline: [
283
+ referencePipelineEntry
284
+ ],
285
+ as: relKey
286
+ }
287
+ });
288
+ }
289
+ pipeline.push({
290
+ $unwind: {
291
+ path: `$${relKey}`,
292
+ preserveNullAndEmptyArrays: true
293
+ }
294
+ });
295
+ }
296
+ return pipeline;
297
+ }
298
+ normalizeIds(meta, docs) {
299
+ return docs?.map((doc)=>this.normalizeId(meta, doc));
300
+ }
301
+ normalizeId(meta, doc) {
302
+ if (!doc) {
303
+ return doc;
304
+ }
305
+ const res = doc;
306
+ const _id = MongoDialect.ID_KEY;
307
+ if (res[_id]) {
308
+ res[meta.id] = res[_id];
309
+ if (meta.id !== _id) {
310
+ delete res[_id];
311
+ }
312
+ }
313
+ for (const key of getKeys(meta.fields)){
314
+ const field = meta.fields[key];
315
+ const dbName = this.resolveColumnName(key, field);
316
+ if (dbName !== key && res[dbName] !== undefined) {
317
+ res[key] = res[dbName];
318
+ delete res[dbName];
319
+ }
320
+ }
321
+ const relKeys = getKeys(meta.relations).filter((key)=>res[key]);
322
+ for (const relKey of relKeys){
323
+ const relOpts = meta.relations[relKey];
324
+ if (!relOpts) continue;
325
+ const relMeta = getMeta(relOpts.entity());
326
+ res[relKey] = Array.isArray(res[relKey]) ? this.normalizeIds(relMeta, res[relKey]) : this.normalizeId(relMeta, res[relKey]);
327
+ }
328
+ return res;
329
+ }
330
+ getIdValue(value) {
331
+ if (value instanceof ObjectId) {
332
+ return value;
333
+ }
334
+ try {
335
+ return new ObjectId(value);
336
+ } catch (e) {
337
+ return value;
338
+ }
339
+ }
340
+ getPersistable(meta, payload, callbackKey) {
341
+ return this.getPersistables(meta, payload, callbackKey)[0];
342
+ }
343
+ getPersistables(meta, payload, callbackKey) {
344
+ const payloads = fillOnFields(meta, payload, callbackKey);
345
+ const persistableKeys = filterFieldKeys(meta, payloads[0], callbackKey);
346
+ return payloads.map((it)=>persistableKeys.reduce((acc, key)=>{
347
+ const field = meta.fields[key];
348
+ acc[this.resolveColumnName(key, field)] = it[key];
349
+ return acc;
350
+ }, {}));
351
+ }
352
+ /**
353
+ * Build MongoDB aggregation pipeline stages from a QueryAggregate.
354
+ */ buildAggregateStages(entity, q) {
355
+ const pipeline = [];
356
+ // $match stage (WHERE equivalent — before grouping)
357
+ if (q.$where) {
358
+ const filter = this.where(entity, q.$where);
359
+ if (hasKeys(filter)) {
360
+ pipeline.push({
361
+ $match: filter
362
+ });
363
+ }
364
+ }
365
+ // $group stage
366
+ const groupId = {};
367
+ const groupAccumulators = {};
368
+ for (const entry of parseGroupMap(q.$group)){
369
+ if (entry.kind === 'key') {
370
+ groupId[entry.alias] = `$${entry.alias}`;
371
+ } else {
372
+ const mongoOp = MongoDialect.AGGREGATE_OP_MAP[entry.op];
373
+ groupAccumulators[entry.alias] = entry.op === '$count' ? {
374
+ [mongoOp]: 1
375
+ } : {
376
+ [mongoOp]: `$${entry.fieldRef}`
377
+ };
378
+ }
379
+ }
380
+ pipeline.push({
381
+ $group: {
382
+ _id: hasKeys(groupId) ? groupId : null,
383
+ ...groupAccumulators
384
+ }
385
+ });
386
+ // Project stage — rename _id fields back to their original names
387
+ if (hasKeys(groupId)) {
388
+ const project = {
389
+ _id: 0
390
+ };
391
+ for (const alias of Object.keys(groupId)){
392
+ project[alias] = `$_id.${alias}`;
393
+ }
394
+ for (const alias of Object.keys(groupAccumulators)){
395
+ project[alias] = 1;
396
+ }
397
+ pipeline.push({
398
+ $project: project
399
+ });
400
+ }
401
+ // $match stage for HAVING (post-group filtering)
402
+ if (q.$having) {
403
+ const havingFilter = this.buildHavingFilter(q.$having);
404
+ if (hasKeys(havingFilter)) {
405
+ pipeline.push({
406
+ $match: havingFilter
407
+ });
408
+ }
409
+ }
410
+ // $sort stage
411
+ if (q.$sort) {
412
+ const sort = this.sort(entity, q.$sort);
413
+ if (hasKeys(sort)) {
414
+ pipeline.push({
415
+ $sort: sort
416
+ });
417
+ }
418
+ }
419
+ // $skip and $limit stages
420
+ if (q.$skip !== undefined) {
421
+ pipeline.push({
422
+ $skip: q.$skip
423
+ });
424
+ }
425
+ if (q.$limit !== undefined) {
426
+ pipeline.push({
427
+ $limit: q.$limit
428
+ });
429
+ }
430
+ return pipeline;
431
+ }
432
+ buildHavingFilter(having) {
433
+ const filter = {};
434
+ for (const [alias, condition] of Object.entries(having)){
435
+ if (condition === undefined) continue;
436
+ if (typeof condition === 'number') {
437
+ filter[alias] = condition;
438
+ } else if (typeof condition === 'object' && condition !== null) {
439
+ filter[alias] = this.transformOperators(condition);
440
+ }
441
+ }
442
+ return filter;
443
+ }
444
+ /**
445
+ * Separate vector sort entries from regular sort entries.
446
+ * Returns `undefined` if no vector sort is present.
447
+ */ extractVectorSort(sort) {
448
+ if (!sort) return undefined;
449
+ const raw = buildSortMap(sort);
450
+ let vectorKey;
451
+ let vectorSearch;
452
+ const regularSort = {};
453
+ for (const [key, value] of Object.entries(raw)){
454
+ if (isVectorSearch(value)) {
455
+ vectorKey = key;
456
+ vectorSearch = value;
457
+ } else {
458
+ regularSort[key] = value;
459
+ }
460
+ }
461
+ if (!vectorKey || !vectorSearch) return undefined;
462
+ return {
463
+ vectorKey,
464
+ vectorSearch,
465
+ regularSort
466
+ };
467
+ }
468
+ /**
469
+ * Build a `$vectorSearch` aggregation pipeline stage.
470
+ * Merges `$where` into `$vectorSearch.filter` for optimal pre-filtering.
471
+ */ buildVectorSearchStage(entity, meta, key, search, where, limit) {
472
+ const field = meta.fields[key];
473
+ if (!field) {
474
+ throw new TypeError(`Field '${key}' not found in entity '${meta.name}'`);
475
+ }
476
+ const colName = this.resolveColumnName(key, field);
477
+ // Resolve index name from @Index metadata, or fall back to convention
478
+ const indexMeta = meta.indexes?.find((idx)=>idx.columns.includes(key) && MongoDialect.VECTOR_INDEX_TYPES.has(idx.type));
479
+ const indexName = indexMeta?.name ?? `${colName}_index`;
480
+ const stage = {
481
+ index: indexName,
482
+ path: colName,
483
+ queryVector: [
484
+ ...search.$vector
485
+ ],
486
+ numCandidates: limit * 10,
487
+ limit
488
+ };
489
+ // Pre-filter: merge $where into $vectorSearch.filter
490
+ if (where) {
491
+ const filter = this.where(entity, where);
492
+ if (hasKeys(filter)) {
493
+ stage['filter'] = filter;
494
+ }
495
+ }
496
+ return {
497
+ $vectorSearch: stage
498
+ };
499
+ }
500
+ }
501
+
502
+ class MongoSchemaGenerator extends AbstractDialect {
503
+ constructor(namingStrategy, defaultForeignKeyAction){
504
+ super(MongoDialect.defaultDialectFeatures, {
505
+ namingStrategy
506
+ }), this.defaultForeignKeyAction = defaultForeignKeyAction, this.dialectName = 'mongodb', this.insertIdStrategy = 'last';
507
+ }
508
+ generateCreateTable(entity) {
509
+ const meta = getMeta(entity);
510
+ const collectionName = this.resolveTableName(entity, meta);
511
+ const indexes = [];
512
+ for (const key of getKeys(meta.fields)){
513
+ const field = meta.fields[key];
514
+ if (field?.index) {
515
+ const columnName = this.resolveColumnName(key, field);
516
+ const indexName = typeof field.index === 'string' ? field.index : `idx_${collectionName}_${columnName}`;
517
+ indexes.push({
518
+ name: indexName,
519
+ columns: [
520
+ columnName
521
+ ],
522
+ unique: !!field.unique
523
+ });
524
+ }
525
+ }
526
+ return JSON.stringify({
527
+ action: 'createCollection',
528
+ name: collectionName,
529
+ indexes
530
+ });
531
+ }
532
+ generateDropTable(entity) {
533
+ const meta = getMeta(entity);
534
+ const collectionName = this.resolveTableName(entity, meta);
535
+ return JSON.stringify({
536
+ action: 'dropCollection',
537
+ name: collectionName
538
+ });
539
+ }
540
+ generateAlterTable(diff) {
541
+ const statements = [];
542
+ if (diff.indexesToAdd?.length) {
543
+ for (const index of diff.indexesToAdd){
544
+ statements.push(this.generateCreateIndex(diff.tableName, index));
545
+ }
546
+ }
547
+ return statements;
548
+ }
549
+ generateAlterTableDown(diff) {
550
+ const statements = [];
551
+ if (diff.indexesToAdd?.length) {
552
+ for (const index of diff.indexesToAdd){
553
+ statements.push(this.generateDropIndex(diff.tableName, index.name));
554
+ }
555
+ }
556
+ return statements;
557
+ }
558
+ generateCreateIndex(tableName, index) {
559
+ const key = {};
560
+ for (const col of index.columns){
561
+ key[col] = 1;
562
+ }
563
+ return JSON.stringify({
564
+ action: 'createIndex',
565
+ collection: tableName,
566
+ name: index.name,
567
+ key,
568
+ options: {
569
+ unique: index.unique,
570
+ name: index.name
571
+ }
572
+ });
573
+ }
574
+ generateDropIndex(tableName, indexName) {
575
+ return JSON.stringify({
576
+ action: 'dropIndex',
577
+ collection: tableName,
578
+ name: indexName
579
+ });
580
+ }
581
+ getSqlType(fieldOptions, fieldType) {
582
+ return '';
583
+ }
584
+ getBooleanType() {
585
+ return '';
586
+ }
587
+ generateColumnDefinitions() {
588
+ return [];
589
+ }
590
+ generateColumnDefinition() {
591
+ return '';
592
+ }
593
+ generateColumnDefinitionFromSchema() {
594
+ return '';
595
+ }
596
+ generateTableConstraints() {
597
+ return [];
598
+ }
599
+ generateAlterColumnStatements() {
600
+ return [];
601
+ }
602
+ getTableOptions() {
603
+ return '';
604
+ }
605
+ generateColumnComment() {
606
+ return '';
607
+ }
608
+ formatDefaultValue() {
609
+ return '';
610
+ }
611
+ generateCreateTableFromNode(table) {
612
+ return JSON.stringify({
613
+ action: 'createCollection',
614
+ name: table.name
615
+ });
616
+ }
617
+ generateCreateIndexFromNode(index) {
618
+ const key = {};
619
+ for (const col of index.columns){
620
+ key[col.name] = 1;
621
+ }
622
+ return JSON.stringify({
623
+ action: 'createIndex',
624
+ collection: index.table.name,
625
+ name: index.name,
626
+ key,
627
+ options: {
628
+ unique: index.unique,
629
+ name: index.name
630
+ }
631
+ });
632
+ }
633
+ generateDropTableFromNode(table) {
634
+ return JSON.stringify({
635
+ action: 'dropCollection',
636
+ name: table.name
637
+ });
638
+ }
639
+ generateCreateTableFromDefinition(table) {
640
+ return JSON.stringify({
641
+ action: 'createCollection',
642
+ name: table.name
643
+ });
644
+ }
645
+ generateDropTableSql(tableName) {
646
+ return JSON.stringify({
647
+ action: 'dropCollection',
648
+ name: tableName
649
+ });
650
+ }
651
+ generateRenameTableSql(oldName, newName) {
652
+ return JSON.stringify({
653
+ action: 'renameCollection',
654
+ from: oldName,
655
+ to: newName
656
+ });
657
+ }
658
+ generateAddColumnSql() {
659
+ return '';
660
+ }
661
+ generateDropColumnSql() {
662
+ return '';
663
+ }
664
+ generateRenameColumnSql() {
665
+ return '';
666
+ }
667
+ generateAlterColumnSql() {
668
+ return '';
669
+ }
670
+ generateCreateIndexSql(tableName, index) {
671
+ return this.generateCreateIndex(tableName, index);
672
+ }
673
+ generateDropIndexSql(tableName, indexName) {
674
+ return this.generateDropIndex(tableName, indexName);
675
+ }
676
+ generateAddForeignKeySql() {
677
+ return '';
678
+ }
679
+ generateDropForeignKeySql() {
680
+ return '';
681
+ }
682
+ diffSchema(entity, currentTable) {
683
+ const meta = getMeta(entity);
684
+ const collectionName = this.resolveTableName(entity, meta);
685
+ if (!currentTable) {
686
+ return {
687
+ tableName: collectionName,
688
+ type: 'create'
689
+ };
690
+ }
691
+ const indexesToAdd = [];
692
+ const existingIndexes = new Set(currentTable.indexes?.map((i)=>i.name) ?? []);
693
+ for (const key of getKeys(meta.fields)){
694
+ const field = meta.fields[key];
695
+ if (field?.index) {
696
+ const columnName = this.resolveColumnName(key, field);
697
+ const indexName = typeof field.index === 'string' ? field.index : `idx_${collectionName}_${columnName}`;
698
+ if (!existingIndexes.has(indexName)) {
699
+ indexesToAdd.push({
700
+ name: indexName,
701
+ columns: [
702
+ columnName
703
+ ],
704
+ unique: !!field.unique
705
+ });
706
+ }
707
+ }
708
+ }
709
+ if (indexesToAdd.length === 0) {
710
+ return undefined;
711
+ }
712
+ return {
713
+ tableName: collectionName,
714
+ type: 'alter',
715
+ indexesToAdd
716
+ };
717
+ }
718
+ }
719
+
720
+ export { MongoSchemaGenerator };
721
+ //# sourceMappingURL=mongoSchemaGenerator-B1IameRz.js.map