litedbmodel 2.2.6 → 2.2.7

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 (116) hide show
  1. package/README.md +145 -86
  2. package/dist/Column.d.ts +17 -4
  3. package/dist/Column.d.ts.map +1 -1
  4. package/dist/Column.js.map +1 -1
  5. package/dist/DBModel.d.ts +13 -14
  6. package/dist/DBModel.d.ts.map +1 -1
  7. package/dist/DBModel.js +20 -21
  8. package/dist/DBModel.js.map +1 -1
  9. package/dist/LazyRelation.d.ts +2 -2
  10. package/dist/LazyRelation.js +2 -2
  11. package/dist/decorators.d.ts +120 -60
  12. package/dist/decorators.d.ts.map +1 -1
  13. package/dist/decorators.js +379 -248
  14. package/dist/decorators.js.map +1 -1
  15. package/dist/index.cjs +283 -217
  16. package/dist/index.d.ts +2 -2
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js.map +1 -1
  19. package/dist/scp/coltype.d.ts +14 -5
  20. package/dist/scp/coltype.d.ts.map +1 -1
  21. package/dist/scp/coltype.js +35 -36
  22. package/dist/scp/coltype.js.map +1 -1
  23. package/dist/scp/decorator-adapter.d.ts +11 -15
  24. package/dist/scp/decorator-adapter.d.ts.map +1 -1
  25. package/dist/scp/decorator-adapter.js +11 -15
  26. package/dist/scp/decorator-adapter.js.map +1 -1
  27. package/dist/scp/index.cjs +186 -193
  28. package/dist/scp/index.mjs +186 -193
  29. package/dist/scp/makesql/json-array.d.ts +6 -0
  30. package/dist/scp/makesql/json-array.d.ts.map +1 -1
  31. package/dist/scp/makesql/json-array.js +17 -2
  32. package/dist/scp/makesql/json-array.js.map +1 -1
  33. package/dist/scp/makesql/tx.d.ts.map +1 -1
  34. package/dist/scp/makesql/tx.js +1 -3
  35. package/dist/scp/makesql/tx.js.map +1 -1
  36. package/dist/scp/relation.d.ts.map +1 -1
  37. package/dist/scp/relation.js +8 -10
  38. package/dist/scp/relation.js.map +1 -1
  39. package/docs/BENCHMARK.md +3 -3
  40. package/docs/COMPARISON.md +7 -7
  41. package/docs/api/README.md +18 -18
  42. package/docs/api/classes/Conditions.md +1 -1
  43. package/docs/api/classes/DBHandler.md +1 -1
  44. package/docs/api/classes/DBModel.md +82 -82
  45. package/docs/api/classes/LimitExceededError.md +1 -1
  46. package/docs/api/classes/Middleware.md +1 -1
  47. package/docs/api/classes/SqlRaw.md +1 -1
  48. package/docs/api/classes/SqlRef.md +1 -1
  49. package/docs/api/classes/Values.md +1 -1
  50. package/docs/api/classes/WriteInReadOnlyContextError.md +1 -1
  51. package/docs/api/classes/WriteOutsideTransactionError.md +1 -1
  52. package/docs/api/functions/belongsTo.md +10 -4
  53. package/docs/api/functions/closeAllPools.md +1 -1
  54. package/docs/api/functions/condsToRecord.md +2 -2
  55. package/docs/api/functions/createMiddleware.md +1 -1
  56. package/docs/api/functions/createPostgresDriver.md +1 -1
  57. package/docs/api/functions/createSqliteDriver.md +1 -1
  58. package/docs/api/functions/dbCast.md +1 -1
  59. package/docs/api/functions/dbCastIn.md +1 -1
  60. package/docs/api/functions/dbDynamic.md +1 -1
  61. package/docs/api/functions/dbFalse.md +1 -1
  62. package/docs/api/functions/dbImmediate.md +1 -1
  63. package/docs/api/functions/dbIn.md +1 -1
  64. package/docs/api/functions/dbNotNull.md +1 -1
  65. package/docs/api/functions/dbNow.md +1 -1
  66. package/docs/api/functions/dbNull.md +1 -1
  67. package/docs/api/functions/dbRaw.md +1 -1
  68. package/docs/api/functions/dbTrue.md +1 -1
  69. package/docs/api/functions/dbUuid.md +1 -1
  70. package/docs/api/functions/dbUuidIn.md +1 -1
  71. package/docs/api/functions/formatLocalDate.md +1 -1
  72. package/docs/api/functions/formatUTCDate.md +1 -1
  73. package/docs/api/functions/getDBConfig.md +1 -1
  74. package/docs/api/functions/getDBHandler.md +1 -1
  75. package/docs/api/functions/getTransactionConnection.md +1 -1
  76. package/docs/api/functions/getTransactionContext.md +1 -1
  77. package/docs/api/functions/hasMany.md +11 -5
  78. package/docs/api/functions/hasOne.md +10 -4
  79. package/docs/api/functions/initDBHandler.md +1 -1
  80. package/docs/api/functions/isAnySqlFragment.md +1 -1
  81. package/docs/api/functions/isConnectionError.md +1 -1
  82. package/docs/api/functions/isSqlCondition.md +1 -1
  83. package/docs/api/functions/isSqlFragment.md +1 -1
  84. package/docs/api/functions/isSqlRaw.md +1 -1
  85. package/docs/api/functions/isSqlRef.md +1 -1
  86. package/docs/api/functions/isSqlTypedFragment.md +1 -1
  87. package/docs/api/functions/model.md +8 -155
  88. package/docs/api/functions/parentRef.md +1 -1
  89. package/docs/api/functions/sql.md +1 -1
  90. package/docs/api/globals.md +96 -58
  91. package/docs/api/interfaces/Column.md +1 -1
  92. package/docs/api/interfaces/ColumnDecorator.md +81 -0
  93. package/docs/api/interfaces/ColumnOptions.md +6 -10
  94. package/docs/api/interfaces/CreatedMiddlewareClass.md +1 -1
  95. package/docs/api/interfaces/DBConfig.md +1 -1
  96. package/docs/api/interfaces/DBConfigOptions.md +1 -1
  97. package/docs/api/interfaces/DeleteOptions.md +1 -1
  98. package/docs/api/interfaces/InsertOptions.md +1 -1
  99. package/docs/api/interfaces/LimitConfig.md +1 -1
  100. package/docs/api/interfaces/MiddlewareConfig.md +1 -1
  101. package/docs/api/interfaces/ModelClassDecorator.md +64 -0
  102. package/docs/api/interfaces/ModelOptions.md +1 -1
  103. package/docs/api/interfaces/PkeyResult.md +1 -1
  104. package/docs/api/interfaces/RelationDecorator.md +72 -0
  105. package/docs/api/interfaces/SelectOptions.md +1 -1
  106. package/docs/api/interfaces/SqlCondition.md +1 -1
  107. package/docs/api/interfaces/SqlFragment.md +1 -1
  108. package/docs/api/interfaces/SqlTypedFragment.md +1 -1
  109. package/docs/api/interfaces/TransactionOptions.md +1 -1
  110. package/docs/api/interfaces/UpdateManyOptions.md +1 -1
  111. package/docs/api/interfaces/UpdateOptions.md +1 -1
  112. package/docs/api-guide.md +17 -17
  113. package/docs/architecture.md +3 -3
  114. package/docs/sql-tagged-template.md +5 -5
  115. package/eslint-plugin/rules/require-declare-for-relations.js +59 -4
  116. package/package.json +8 -7
package/docs/api-guide.md CHANGED
@@ -52,16 +52,16 @@ import { DBModel, model, column, hasMany, belongsTo, hasOne } from 'litedbmodel'
52
52
  @model('users')
53
53
  class UserModel extends DBModel {
54
54
  // Primary key (auto-increment)
55
- @column({ primaryKey: true }) id?: number;
55
+ @column.number({ primaryKey: true }) id?: number;
56
56
 
57
57
  // UUID primary key (PostgreSQL: auto-casts to ::uuid)
58
58
  // @column.uuid({ primaryKey: true }) id?: string;
59
59
 
60
- // Basic columns (types auto-inferred)
61
- @column() name?: string;
62
- @column() email?: string;
63
- @column() is_active?: boolean;
64
- @column() created_at?: Date;
60
+ // Basic columns — each states its type with a family
61
+ @column.text() name?: string;
62
+ @column.text() email?: string;
63
+ @column.boolean() is_active?: boolean;
64
+ @column.datetime() created_at?: string;
65
65
 
66
66
  // Array and JSON columns (explicit type required)
67
67
  @column.stringArray() tags?: string[];
@@ -85,11 +85,11 @@ export type User = UserModel;
85
85
 
86
86
  @model('posts')
87
87
  class PostModel extends DBModel {
88
- @column({ primaryKey: true }) id?: number;
89
- @column() author_id?: number;
90
- @column() title?: string;
91
- @column() content?: string;
92
- @column() published_at?: Date;
88
+ @column.number({ primaryKey: true }) id?: number;
89
+ @column.number() author_id?: number;
90
+ @column.text() title?: string;
91
+ @column.text() content?: string;
92
+ @column.datetime() published_at?: string;
93
93
 
94
94
  @belongsTo(() => [Post.author_id, User.id])
95
95
  declare author: Promise<User | null>;
@@ -99,9 +99,9 @@ export type Post = PostModel;
99
99
 
100
100
  @model('profiles')
101
101
  class ProfileModel extends DBModel {
102
- @column({ primaryKey: true }) id?: number;
103
- @column() user_id?: number;
104
- @column() bio?: string;
102
+ @column.number({ primaryKey: true }) id?: number;
103
+ @column.number() user_id?: number;
104
+ @column.text() bio?: string;
105
105
  }
106
106
  export const Profile = ProfileModel.asModel();
107
107
  export type Profile = ProfileModel;
@@ -341,7 +341,7 @@ See: [DBModel.execute](classes/DBModel.md#execute), [DBModel.query](classes/DBMo
341
341
  ```typescript
342
342
  @model('users')
343
343
  class User extends DBModel {
344
- @column({ primaryKey: true }) id?: number;
344
+ @column.number({ primaryKey: true }) id?: number;
345
345
 
346
346
  // One-to-Many: User has many Posts
347
347
  @hasMany(() => [User.id, Post.author_id])
@@ -354,8 +354,8 @@ class User extends DBModel {
354
354
 
355
355
  @model('posts')
356
356
  class Post extends DBModel {
357
- @column({ primaryKey: true }) id?: number;
358
- @column() author_id?: number;
357
+ @column.number({ primaryKey: true }) id?: number;
358
+ @column.number() author_id?: number;
359
359
 
360
360
  // Many-to-One: Post belongs to User
361
361
  @belongsTo(() => [Post.author_id, User.id])
@@ -73,9 +73,9 @@ graphddb と並ぶ SQL backend の consumer である。`` `src/scp/index.ts` ``
73
73
  ```ts
74
74
  @model('posts')
75
75
  export class PostModel extends DBModel {
76
- @column({ primaryKey: true }) id!: number;
77
- @column() author_id!: number;
78
- @column() title!: string;
76
+ @column.number({ primaryKey: true }) id!: number;
77
+ @column.number() author_id!: number;
78
+ @column.text() title!: string;
79
79
 
80
80
  @belongsTo(() => [Post.author_id, User.id]) declare author: User | null;
81
81
  }
@@ -94,8 +94,8 @@ The `sql` tag eliminates manual `$1`/`$2` placeholder numbering and DB dialect d
94
94
  ```typescript
95
95
  @model('sales_report')
96
96
  class SalesReportModel extends DBModel {
97
- @column() product_id?: number;
98
- @column() total_revenue?: number;
97
+ @column.number() product_id?: number;
98
+ @column.number() total_revenue?: number;
99
99
 
100
100
  static forPeriod(startDate: string, endDate: string) {
101
101
  return this.withQuery(sql`
@@ -130,9 +130,9 @@ Use the `sql` tag for `QUERY` to get Column and table name references that are r
130
130
  ```typescript
131
131
  @model('user_activity')
132
132
  class UserActivityModel extends DBModel {
133
- @column() user_id?: number;
134
- @column() user_name?: string;
135
- @column() total_posts?: number;
133
+ @column.number() user_id?: number;
134
+ @column.text() user_name?: string;
135
+ @column.number() total_posts?: number;
136
136
 
137
137
  static QUERY = sql`
138
138
  SELECT
@@ -1,6 +1,18 @@
1
1
  /**
2
- * @fileoverview Require 'declare' keyword for relation properties instead of '!' assertion
3
- * @description Relations use prototype getters. Using '!' creates instance properties that shadow getters.
2
+ * @fileoverview Require the relation declaration form the project's DECORATOR PROTOCOL allows.
3
+ * @description
4
+ * A relation is a prototype getter. How a class field interacts with it differs by protocol:
5
+ *
6
+ * - **legacy decorators** (`experimentalDecorators: true`): `declare posts: …` emits no field, so the
7
+ * getter is what the instance sees. `posts!: …` DOES emit a field under `useDefineForClassFields`
8
+ * and shadows the getter — silently, with `undefined` reads. That is what this rule catches.
9
+ * - **standard decorators** (TC39, the TypeScript 5 default): a decorated `declare` field is a
10
+ * COMPILE ERROR (TS1206), so `posts!: …` is the only spelling — and litedbmodel removes the
11
+ * shadowing field itself. Requiring `declare` there makes the project uncompilable, and the
12
+ * autofix used to do exactly that.
13
+ *
14
+ * The protocol is read from the TypeScript program when type-aware linting is on; otherwise set it
15
+ * explicitly: `"litedbmodel/require-declare-for-relations": ["error", { "decorators": "standard" }]`.
4
16
  */
5
17
 
6
18
  "use strict";
@@ -15,11 +27,24 @@ module.exports = {
15
27
  recommended: true,
16
28
  },
17
29
  fixable: "code",
18
- schema: [],
30
+ schema: [
31
+ {
32
+ type: "object",
33
+ properties: {
34
+ decorators: { enum: ["legacy", "standard", "auto"] },
35
+ },
36
+ additionalProperties: false,
37
+ },
38
+ ],
19
39
  messages: {
20
40
  useDeclare:
21
41
  "Relation property '{{name}}' should use 'declare' instead of '!' assertion. " +
22
- "Class field declarations with '!' create instance properties that shadow the prototype getter.",
42
+ "Under legacy decorators a class field with '!' shadows the prototype getter, so the relation " +
43
+ "reads as undefined.",
44
+ useDefinite:
45
+ "Relation property '{{name}}' must use '!' instead of 'declare'. " +
46
+ "Standard (TC39) decorators reject a decorated 'declare' field (TS1206); litedbmodel removes " +
47
+ "the shadowing class field itself.",
23
48
  },
24
49
  },
25
50
 
@@ -27,6 +52,26 @@ module.exports = {
27
52
  const relationDecorators = new Set(["hasMany", "belongsTo", "hasOne"]);
28
53
  const sourceCode = context.getSourceCode();
29
54
 
55
+ /**
56
+ * Which decorator protocol this project compiles with. Read from the TypeScript program when
57
+ * type-aware linting makes it available — guessing wrong turns this rule into an autofix that
58
+ * breaks the build, so the answer comes from the compiler options, not from the source text.
59
+ */
60
+ function decoratorProtocol() {
61
+ const configured = (context.options && context.options[0] && context.options[0].decorators) || "auto";
62
+ if (configured !== "auto") return configured;
63
+ const services = sourceCode.parserServices || context.parserServices;
64
+ const program = services && services.program;
65
+ if (program && typeof program.getCompilerOptions === "function") {
66
+ return program.getCompilerOptions().experimentalDecorators ? "legacy" : "standard";
67
+ }
68
+ // No program to ask. `experimentalDecorators` is what this rule has always assumed, and it is
69
+ // still the only protocol where `declare` is required.
70
+ return "legacy";
71
+ }
72
+
73
+ const protocol = decoratorProtocol();
74
+
30
75
  /**
31
76
  * Check if a decorator is a relation decorator
32
77
  */
@@ -81,6 +126,16 @@ module.exports = {
81
126
  return;
82
127
  }
83
128
 
129
+ const propertyKeyName = node.key?.name || node.key?.value || "unknown";
130
+
131
+ // Standard decorators: `declare` does not compile on a decorated field, so `!` is correct.
132
+ if (protocol === "standard") {
133
+ if (node.declare === true) {
134
+ context.report({ node, messageId: "useDefinite", data: { name: propertyKeyName } });
135
+ }
136
+ return;
137
+ }
138
+
84
139
  // Check if using definite assignment assertion (!)
85
140
  // In TypeScript ESLint AST, this is represented as `definite: true`
86
141
  if (node.definite === true) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "litedbmodel",
3
- "version": "2.2.6",
3
+ "version": "2.2.7",
4
4
  "description": "A lightweight TypeScript ORM for PostgreSQL, MySQL, and SQLite with Active Record pattern",
5
5
  "main": "dist/index.cjs",
6
6
  "types": "dist/index.d.ts",
@@ -105,14 +105,14 @@
105
105
  "@types/better-sqlite3": "^7.6.8",
106
106
  "@types/node": "^20.10.0",
107
107
  "@types/pg": "^8.10.9",
108
- "@vitest/coverage-v8": "^4.1.7",
108
+ "@vitest/coverage-v8": "^4.1.11",
109
109
  "better-sqlite3": "^11.0.0",
110
- "embedoc": "^0.9.1",
110
+ "embedoc": "^0.14.8",
111
111
  "esbuild": "^0.28.1",
112
112
  "eslint": "^9.39.4",
113
113
  "globals": "^17.6.0",
114
- "js-yaml": "^4.3.0",
115
- "mysql2": "^3.16.0",
114
+ "js-yaml": "^4.3.2",
115
+ "mysql2": "^3.24.4",
116
116
  "pg": "^8.11.3",
117
117
  "terser": "^5.44.1",
118
118
  "tsx": "^4.22.3",
@@ -120,7 +120,7 @@
120
120
  "typedoc-plugin-markdown": "^4.9.0",
121
121
  "typescript": "^5.3.0",
122
122
  "typescript-eslint": "^8.60.0",
123
- "vitest": "^4.1.7"
123
+ "vitest": "^4.1.11"
124
124
  },
125
125
  "peerDependencies": {
126
126
  "better-sqlite3": "^9.0.0 || ^10.0.0 || ^11.0.0",
@@ -142,7 +142,8 @@
142
142
  "node": ">=18.0.0"
143
143
  },
144
144
  "overrides": {
145
- "js-yaml": "^4.3.0",
145
+ "js-yaml": "^4.3.2",
146
+ "csv-parse": "^7.0.2",
146
147
  "linkify-it": "^5.0.2",
147
148
  "postcss": "^8.5.24",
148
149
  "brace-expansion": "^5.0.9"