@simplysm/orm-common 13.0.57 → 13.0.59

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 (2) hide show
  1. package/README.md +302 -0
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -588,6 +588,22 @@ N:1 logical relationship without a DB FK constraint. Available for both tables a
588
588
 
589
589
  1:N logical reverse reference without a DB FK constraint. Call `.single()` for 1:1.
590
590
 
591
+ #### `createRelationFactory(ownerFn)`
592
+
593
+ Creates a relation builder factory for the given owner table or view. Used internally by `TableBuilder.relations()` and `ViewBuilder.relations()`. Available for building custom schema utilities.
594
+
595
+ Tables receive both FK methods (`foreignKey`, `foreignKeyTarget`) and logical relation methods (`relationKey`, `relationKeyTarget`). Views receive only logical relation methods.
596
+
597
+ ```typescript
598
+ import { createRelationFactory } from "@simplysm/orm-common";
599
+
600
+ const r = createRelationFactory(() => Post);
601
+ // r.foreignKey(...) — available (Post is a TableBuilder)
602
+ // r.foreignKeyTarget(...) — available
603
+ // r.relationKey(...) — available
604
+ // r.relationKeyTarget(...) — available
605
+ ```
606
+
591
607
  #### `IndexBuilder` / `createIndexFactory`
592
608
 
593
609
  Index builder, used inside `.indexes()`.
@@ -1055,6 +1071,7 @@ import { toExpr } from "@simplysm/orm-common";
1055
1071
  | `InferColumnPrimitiveFromDataType<T>` | TypeScript type from a `DataType` |
1056
1072
  | `dataTypeStrToColumnPrimitiveStr` | Constant object mapping SQL type name → `ColumnPrimitiveStr` |
1057
1073
  | `inferColumnPrimitiveStr(value)` | Runtime function: infer `ColumnPrimitiveStr` from a value |
1074
+ | `DateSeparator` | `"year" \| "month" \| "day" \| "hour" \| "minute" \| "second"` — date interval unit for `dateDiff`/`dateAdd` |
1058
1075
 
1059
1076
  #### SQL Type to TypeScript Type Mapping
1060
1077
 
@@ -1101,6 +1118,291 @@ import { toExpr } from "@simplysm/orm-common";
1101
1118
  | `ExtractRelationTarget<T>` | Extract the TypeScript type of an FK/RelationKey target |
1102
1119
  | `ExtractRelationTargetResult<T>` | Extract the TypeScript type of an FKTarget/RelationKeyTarget (array or single) |
1103
1120
 
1121
+ ### QueryDef Types
1122
+
1123
+ `QueryDef` is the JSON AST representation of any SQL statement. Every `Queryable` execution method generates a `QueryDef` that is then converted to SQL by `QueryBuilderBase`.
1124
+
1125
+ #### `QueryDef`
1126
+
1127
+ The master union type for all possible query definitions:
1128
+
1129
+ ```typescript
1130
+ import { QueryDef } from "@simplysm/orm-common";
1131
+
1132
+ type QueryDef =
1133
+ | SelectQueryDef | InsertQueryDef | InsertIfNotExistsQueryDef | InsertIntoQueryDef
1134
+ | UpdateQueryDef | DeleteQueryDef | UpsertQueryDef
1135
+ | ClearSchemaQueryDef | CreateTableQueryDef | DropTableQueryDef | RenameTableQueryDef
1136
+ | TruncateQueryDef | AddColumnQueryDef | DropColumnQueryDef | ModifyColumnQueryDef
1137
+ | RenameColumnQueryDef | DropPkQueryDef | AddPkQueryDef | AddFkQueryDef | DropFkQueryDef
1138
+ | AddIdxQueryDef | DropIdxQueryDef | CreateViewQueryDef | DropViewQueryDef
1139
+ | CreateProcQueryDef | DropProcQueryDef | ExecProcQueryDef | SwitchFkQueryDef
1140
+ | SchemaExistsQueryDef;
1141
+ ```
1142
+
1143
+ #### `QueryDefObjectName`
1144
+
1145
+ Represents a DBMS object identifier (table, view, procedure). DBMS-specific namespace rules:
1146
+
1147
+ - MySQL: `database.name` (schema is ignored)
1148
+ - MSSQL: `database.schema.name` (schema defaults to `dbo`)
1149
+ - PostgreSQL: `schema.name` (database is connection-level)
1150
+
1151
+ ```typescript
1152
+ import { QueryDefObjectName } from "@simplysm/orm-common";
1153
+
1154
+ interface QueryDefObjectName {
1155
+ database?: string;
1156
+ schema?: string;
1157
+ name: string;
1158
+ }
1159
+ ```
1160
+
1161
+ #### `CudOutputDef`
1162
+
1163
+ Defines the OUTPUT clause for INSERT/UPDATE/DELETE operations that return values.
1164
+
1165
+ ```typescript
1166
+ import { CudOutputDef } from "@simplysm/orm-common";
1167
+
1168
+ interface CudOutputDef {
1169
+ columns: string[]; // Column names to return
1170
+ pkColNames: string[]; // Primary key column names
1171
+ aiColName?: string; // Auto-increment column name (if any)
1172
+ }
1173
+ ```
1174
+
1175
+ #### DML QueryDef interfaces
1176
+
1177
+ | Interface | `type` | Description |
1178
+ |-----------|--------|-------------|
1179
+ | `SelectQueryDef` | `"select"` | SELECT with optional FROM, WHERE, JOIN, ORDER BY, GROUP BY, HAVING, LIMIT, DISTINCT, recursive CTE |
1180
+ | `SelectQueryDefJoin` | `"select"` | Extends `SelectQueryDef` with `isSingle?: boolean` for 1:1 JOIN distinction |
1181
+ | `InsertQueryDef` | `"insert"` | INSERT with records array; optional `overrideIdentity` and `output` |
1182
+ | `InsertIfNotExistsQueryDef` | `"insertIfNotExists"` | INSERT only if `existsSelectQuery` returns no rows |
1183
+ | `InsertIntoQueryDef` | `"insertInto"` | INSERT INTO ... SELECT from `recordsSelectQuery` |
1184
+ | `UpdateQueryDef` | `"update"` | UPDATE with record map, optional WHERE/JOIN/LIMIT |
1185
+ | `DeleteQueryDef` | `"delete"` | DELETE with optional WHERE/JOIN/LIMIT |
1186
+ | `UpsertQueryDef` | `"upsert"` | UPDATE or INSERT (MERGE pattern): `existsSelectQuery` determines branch |
1187
+
1188
+ #### DDL QueryDef interfaces
1189
+
1190
+ | Interface | `type` | Description |
1191
+ |-----------|--------|-------------|
1192
+ | `ClearSchemaQueryDef` | `"clearSchema"` | Drop all objects in a database/schema |
1193
+ | `CreateTableQueryDef` | `"createTable"` | CREATE TABLE with column definitions and optional primary key |
1194
+ | `DropTableQueryDef` | `"dropTable"` | DROP TABLE |
1195
+ | `RenameTableQueryDef` | `"renameTable"` | RENAME TABLE to `newName` |
1196
+ | `TruncateQueryDef` | `"truncate"` | TRUNCATE TABLE |
1197
+ | `AddColumnQueryDef` | `"addColumn"` | ADD COLUMN with data type and optional constraints |
1198
+ | `DropColumnQueryDef` | `"dropColumn"` | DROP COLUMN |
1199
+ | `ModifyColumnQueryDef` | `"modifyColumn"` | MODIFY/ALTER COLUMN definition |
1200
+ | `RenameColumnQueryDef` | `"renameColumn"` | RENAME COLUMN to `newName` |
1201
+ | `DropPkQueryDef` | `"dropPk"` | DROP PRIMARY KEY |
1202
+ | `AddPkQueryDef` | `"addPk"` | ADD PRIMARY KEY on given `columns` |
1203
+ | `AddFkQueryDef` | `"addFk"` | ADD FOREIGN KEY with name, FK columns, target table and PK columns |
1204
+ | `DropFkQueryDef` | `"dropFk"` | DROP FOREIGN KEY by name |
1205
+ | `AddIdxQueryDef` | `"addIdx"` | CREATE INDEX with name, columns, order direction, and optional unique |
1206
+ | `DropIdxQueryDef` | `"dropIdx"` | DROP INDEX by name |
1207
+ | `CreateViewQueryDef` | `"createView"` | CREATE VIEW with a `SelectQueryDef` body |
1208
+ | `DropViewQueryDef` | `"dropView"` | DROP VIEW |
1209
+ | `CreateProcQueryDef` | `"createProc"` | CREATE PROCEDURE with params, returns columns, and body SQL string |
1210
+ | `DropProcQueryDef` | `"dropProc"` | DROP PROCEDURE |
1211
+ | `ExecProcQueryDef` | `"execProc"` | EXECUTE PROCEDURE with optional params record |
1212
+
1213
+ #### Utility / Meta QueryDef interfaces
1214
+
1215
+ | Interface | `type` | Description |
1216
+ |-----------|--------|-------------|
1217
+ | `SwitchFkQueryDef` | `"switchFk"` | Enable/disable FK constraint checking for a table (`"on"` or `"off"`) |
1218
+ | `SchemaExistsQueryDef` | `"schemaExists"` | Check if a database/schema exists |
1219
+
1220
+ #### `DDL_TYPES` / `DdlType`
1221
+
1222
+ Constant array of all DDL operation type strings. Used internally to block DDL inside transactions and to validate whether a given `QueryDef` is a DDL operation. `SwitchFkQueryDef` is excluded because FK toggle is permitted inside transactions.
1223
+
1224
+ ```typescript
1225
+ import { DDL_TYPES, DdlType } from "@simplysm/orm-common";
1226
+
1227
+ // DDL_TYPES: readonly string[] constant
1228
+ // ["clearSchema", "createTable", "dropTable", ..., "dropProc"]
1229
+
1230
+ // DdlType: union of all DDL type strings
1231
+ type DdlType = "clearSchema" | "createTable" | "dropTable" | "renameTable" | "truncate"
1232
+ | "addColumn" | "dropColumn" | "modifyColumn" | "renameColumn"
1233
+ | "dropPk" | "addPk" | "addFk" | "dropFk" | "addIdx" | "dropIdx"
1234
+ | "createView" | "dropView" | "createProc" | "dropProc";
1235
+
1236
+ // Check if a QueryDef is DDL:
1237
+ if (DDL_TYPES.includes(queryDef.type as DdlType)) {
1238
+ // DDL — cannot run inside a transaction
1239
+ }
1240
+ ```
1241
+
1242
+ ### Expr AST Types
1243
+
1244
+ Low-level JSON AST interfaces that represent individual SQL expressions. These are the types stored in `QueryDef` fields such as `select`, `where`, `orderBy`, etc. Direct use is only necessary when extending `QueryBuilderBase` or `ExprRendererBase`.
1245
+
1246
+ #### `Expr` and `WhereExpr` (union types)
1247
+
1248
+ ```typescript
1249
+ import { Expr, WhereExpr } from "@simplysm/orm-common";
1250
+ ```
1251
+
1252
+ | Union type | Description |
1253
+ |------------|-------------|
1254
+ | `Expr` | All value-producing expressions: column refs, literals, string/numeric/date/conditional/aggregate/window/subquery |
1255
+ | `WhereExpr` | All boolean-producing expressions: comparison operators + logical operators (AND/OR/NOT) |
1256
+
1257
+ #### Value expressions
1258
+
1259
+ | Interface | `type` | Description |
1260
+ |-----------|--------|-------------|
1261
+ | `ExprColumn` | `"column"` | Column reference: `{ path: string[] }` — table alias + column name |
1262
+ | `ExprValue` | `"value"` | Literal value: `{ value: ColumnPrimitive }` |
1263
+ | `ExprRaw` | `"raw"` | Raw SQL template: `{ sql: string; params: Expr[] }` — params referenced as `{0}`, `{1}` |
1264
+
1265
+ #### Comparison expressions (WHERE)
1266
+
1267
+ | Interface | `type` | SQL |
1268
+ |-----------|--------|-----|
1269
+ | `ExprEq` | `"eq"` | NULL-safe `=` / `IS NULL` |
1270
+ | `ExprGt` | `"gt"` | `>` |
1271
+ | `ExprLt` | `"lt"` | `<` |
1272
+ | `ExprGte` | `"gte"` | `>=` |
1273
+ | `ExprLte` | `"lte"` | `<=` |
1274
+ | `ExprBetween` | `"between"` | `BETWEEN from AND to` |
1275
+ | `ExprIsNull` | `"null"` | `IS NULL` |
1276
+ | `ExprLike` | `"like"` | `LIKE` |
1277
+ | `ExprRegexp` | `"regexp"` | `REGEXP` |
1278
+ | `ExprIn` | `"in"` | `IN (values)` |
1279
+ | `ExprInQuery` | `"inQuery"` | `IN (subquery)` |
1280
+ | `ExprExists` | `"exists"` | `EXISTS (subquery)` |
1281
+
1282
+ #### Logical expressions (WHERE)
1283
+
1284
+ | Interface | `type` | SQL |
1285
+ |-----------|--------|-----|
1286
+ | `ExprNot` | `"not"` | `NOT (...)` |
1287
+ | `ExprAnd` | `"and"` | `(... AND ...)` |
1288
+ | `ExprOr` | `"or"` | `(... OR ...)` |
1289
+
1290
+ #### String expressions
1291
+
1292
+ | Interface | `type` | SQL |
1293
+ |-----------|--------|-----|
1294
+ | `ExprConcat` | `"concat"` | `CONCAT(args...)` |
1295
+ | `ExprLeft` | `"left"` | `LEFT(source, length)` |
1296
+ | `ExprRight` | `"right"` | `RIGHT(source, length)` |
1297
+ | `ExprTrim` | `"trim"` | `TRIM(arg)` |
1298
+ | `ExprPadStart` | `"padStart"` | `LPAD(source, length, fillString)` |
1299
+ | `ExprReplace` | `"replace"` | `REPLACE(source, from, to)` |
1300
+ | `ExprUpper` | `"upper"` | `UPPER(arg)` |
1301
+ | `ExprLower` | `"lower"` | `LOWER(arg)` |
1302
+ | `ExprLength` | `"length"` | `CHAR_LENGTH(arg)` |
1303
+ | `ExprByteLength` | `"byteLength"` | `OCTET_LENGTH(arg)` |
1304
+ | `ExprSubstring` | `"substring"` | `SUBSTRING(source, start, length?)` |
1305
+ | `ExprIndexOf` | `"indexOf"` | `LOCATE(source, search)` |
1306
+
1307
+ #### Numeric expressions
1308
+
1309
+ | Interface | `type` | SQL |
1310
+ |-----------|--------|-----|
1311
+ | `ExprAbs` | `"abs"` | `ABS(arg)` |
1312
+ | `ExprRound` | `"round"` | `ROUND(arg, digits)` |
1313
+ | `ExprCeil` | `"ceil"` | `CEILING(arg)` |
1314
+ | `ExprFloor` | `"floor"` | `FLOOR(arg)` |
1315
+
1316
+ #### Date expressions
1317
+
1318
+ | Interface | `type` | SQL |
1319
+ |-----------|--------|-----|
1320
+ | `ExprYear` | `"year"` | `YEAR(arg)` |
1321
+ | `ExprMonth` | `"month"` | `MONTH(arg)` |
1322
+ | `ExprDay` | `"day"` | `DAY(arg)` |
1323
+ | `ExprHour` | `"hour"` | `HOUR(arg)` |
1324
+ | `ExprMinute` | `"minute"` | `MINUTE(arg)` |
1325
+ | `ExprSecond` | `"second"` | `SECOND(arg)` |
1326
+ | `ExprIsoWeek` | `"isoWeek"` | `WEEK(arg, 3)` |
1327
+ | `ExprIsoWeekStartDate` | `"isoWeekStartDate"` | Monday of the ISO week |
1328
+ | `ExprIsoYearMonth` | `"isoYearMonth"` | First day of month (YYYY-MM-01) |
1329
+ | `ExprDateDiff` | `"dateDiff"` | `DATEDIFF(separator, from, to)` |
1330
+ | `ExprDateAdd` | `"dateAdd"` | `DATEADD(separator, source, value)` |
1331
+ | `ExprFormatDate` | `"formatDate"` | `DATE_FORMAT(source, format)` |
1332
+
1333
+ #### Conditional expressions
1334
+
1335
+ | Interface | `type` | SQL |
1336
+ |-----------|--------|-----|
1337
+ | `ExprIfNull` | `"ifNull"` | `COALESCE(args...)` |
1338
+ | `ExprNullIf` | `"nullIf"` | `NULLIF(source, value)` |
1339
+ | `ExprIs` | `"is"` | Convert boolean condition to 0/1 value |
1340
+ | `ExprSwitch` | `"switch"` | `CASE WHEN ... THEN ... ELSE ... END` |
1341
+ | `ExprIf` | `"if"` | `IF(condition, then, else)` |
1342
+
1343
+ #### Aggregate expressions
1344
+
1345
+ | Interface | `type` | SQL |
1346
+ |-----------|--------|-----|
1347
+ | `ExprCount` | `"count"` | `COUNT(*)` or `COUNT(arg)` or `COUNT(DISTINCT arg)` |
1348
+ | `ExprSum` | `"sum"` | `SUM(arg)` |
1349
+ | `ExprAvg` | `"avg"` | `AVG(arg)` |
1350
+ | `ExprMax` | `"max"` | `MAX(arg)` |
1351
+ | `ExprMin` | `"min"` | `MIN(arg)` |
1352
+
1353
+ #### Other expressions
1354
+
1355
+ | Interface | `type` | SQL |
1356
+ |-----------|--------|-----|
1357
+ | `ExprGreatest` | `"greatest"` | `GREATEST(args...)` |
1358
+ | `ExprLeast` | `"least"` | `LEAST(args...)` |
1359
+ | `ExprRowNum` | `"rowNum"` | Row number (simple, no window spec) |
1360
+ | `ExprRandom` | `"random"` | `RAND()` / `RANDOM()` |
1361
+ | `ExprCast` | `"cast"` | `CAST(source AS targetType)` |
1362
+ | `ExprSubquery` | `"subquery"` | Scalar subquery `(SELECT ...)` |
1363
+
1364
+ #### Window function types
1365
+
1366
+ `ExprWindow` is the AST node for any window function expression. It combines a `WinFn` (the function) with a `WinSpec` (the OVER clause).
1367
+
1368
+ ```typescript
1369
+ import { ExprWindow, WinFn, WinSpec } from "@simplysm/orm-common";
1370
+
1371
+ interface ExprWindow {
1372
+ type: "window";
1373
+ fn: WinFn; // The window function
1374
+ spec: WinSpec; // OVER (PARTITION BY ... ORDER BY ...)
1375
+ }
1376
+
1377
+ interface WinSpec {
1378
+ partitionBy?: Expr[];
1379
+ orderBy?: [Expr, ("ASC" | "DESC")?][];
1380
+ }
1381
+
1382
+ type WinFn =
1383
+ | WinFnRowNumber | WinFnRank | WinFnDenseRank | WinFnNtile
1384
+ | WinFnLag | WinFnLead | WinFnFirstValue | WinFnLastValue
1385
+ | WinFnSum | WinFnAvg | WinFnCount | WinFnMin | WinFnMax;
1386
+ ```
1387
+
1388
+ Individual `WinFn` interfaces:
1389
+
1390
+ | Interface | `type` | SQL | Notes |
1391
+ |-----------|--------|-----|-------|
1392
+ | `WinFnRowNumber` | `"rowNumber"` | `ROW_NUMBER()` | |
1393
+ | `WinFnRank` | `"rank"` | `RANK()` | |
1394
+ | `WinFnDenseRank` | `"denseRank"` | `DENSE_RANK()` | |
1395
+ | `WinFnNtile` | `"ntile"` | `NTILE(n)` | Has `n: number` property |
1396
+ | `WinFnLag` | `"lag"` | `LAG(column, offset?, default?)` | |
1397
+ | `WinFnLead` | `"lead"` | `LEAD(column, offset?, default?)` | |
1398
+ | `WinFnFirstValue` | `"firstValue"` | `FIRST_VALUE(column)` | |
1399
+ | `WinFnLastValue` | `"lastValue"` | `LAST_VALUE(column)` | |
1400
+ | `WinFnSum` | `"sum"` | `SUM(column) OVER (...)` | |
1401
+ | `WinFnAvg` | `"avg"` | `AVG(column) OVER (...)` | |
1402
+ | `WinFnCount` | `"count"` | `COUNT(*) OVER (...)` | `column` is optional |
1403
+ | `WinFnMin` | `"min"` | `MIN(column) OVER (...)` | |
1404
+ | `WinFnMax` | `"max"` | `MAX(column) OVER (...)` | |
1405
+
1104
1406
  ---
1105
1407
 
1106
1408
  ## Quick Start
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simplysm/orm-common",
3
- "version": "13.0.57",
3
+ "version": "13.0.59",
4
4
  "description": "심플리즘 패키지 - ORM 모듈 (common)",
5
5
  "author": "김석래",
6
6
  "license": "Apache-2.0",
@@ -19,6 +19,6 @@
19
19
  ],
20
20
  "sideEffects": false,
21
21
  "dependencies": {
22
- "@simplysm/core-common": "13.0.57"
22
+ "@simplysm/core-common": "13.0.59"
23
23
  }
24
24
  }