leoric 2.14.0 → 2.15.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (285) hide show
  1. package/Readme.md +3 -2
  2. package/dist/abstract_bone.d.ts +478 -0
  3. package/dist/abstract_bone.js +1434 -0
  4. package/dist/abstract_bone.js.map +1 -0
  5. package/dist/adapters/sequelize.d.ts +943 -198
  6. package/dist/adapters/sequelize.js +443 -835
  7. package/dist/adapters/sequelize.js.map +1 -1
  8. package/dist/bone.d.ts +45 -59
  9. package/dist/bone.js +48 -2296
  10. package/dist/bone.js.map +1 -1
  11. package/dist/browser.d.ts +13 -13
  12. package/dist/browser.js +27 -44
  13. package/dist/browser.js.map +1 -1
  14. package/dist/collection.d.ts +16 -16
  15. package/dist/collection.js +61 -159
  16. package/dist/collection.js.map +1 -1
  17. package/dist/constants.d.ts +27 -26
  18. package/dist/constants.js +9 -19
  19. package/dist/constants.js.map +1 -1
  20. package/dist/data_types.d.ts +4 -1
  21. package/dist/data_types.js +297 -378
  22. package/dist/data_types.js.map +1 -1
  23. package/dist/decorators.d.ts +4 -4
  24. package/dist/decorators.js +21 -31
  25. package/dist/decorators.js.map +1 -1
  26. package/dist/drivers/abstract/attribute.d.ts +48 -7
  27. package/dist/drivers/abstract/attribute.js +54 -81
  28. package/dist/drivers/abstract/attribute.js.map +1 -1
  29. package/dist/drivers/abstract/index.d.ts +194 -37
  30. package/dist/drivers/abstract/index.js +301 -387
  31. package/dist/drivers/abstract/index.js.map +1 -1
  32. package/dist/drivers/abstract/logger.d.ts +23 -7
  33. package/dist/drivers/abstract/logger.js +37 -56
  34. package/dist/drivers/abstract/logger.js.map +1 -1
  35. package/dist/drivers/abstract/spellbook.d.ts +24 -40
  36. package/dist/drivers/abstract/spellbook.js +245 -524
  37. package/dist/drivers/abstract/spellbook.js.map +1 -1
  38. package/dist/drivers/index.d.ts +8 -194
  39. package/dist/drivers/index.js +7 -15
  40. package/dist/drivers/index.js.map +1 -1
  41. package/dist/drivers/mysql/attribute.d.ts +4 -5
  42. package/dist/drivers/mysql/attribute.js +20 -41
  43. package/dist/drivers/mysql/attribute.js.map +1 -1
  44. package/dist/drivers/mysql/data_types.d.ts +3 -14
  45. package/dist/drivers/mysql/data_types.js +11 -35
  46. package/dist/drivers/mysql/data_types.js.map +1 -1
  47. package/dist/drivers/mysql/index.d.ts +45 -54
  48. package/dist/drivers/mysql/index.js +150 -345
  49. package/dist/drivers/mysql/index.js.map +1 -1
  50. package/dist/drivers/mysql/spellbook.d.ts +15 -11
  51. package/dist/drivers/mysql/spellbook.js +48 -115
  52. package/dist/drivers/mysql/spellbook.js.map +1 -1
  53. package/dist/drivers/postgres/attribute.d.ts +5 -4
  54. package/dist/drivers/postgres/attribute.js +26 -65
  55. package/dist/drivers/postgres/attribute.js.map +1 -1
  56. package/dist/drivers/postgres/data_types.d.ts +13 -74
  57. package/dist/drivers/postgres/data_types.js +56 -95
  58. package/dist/drivers/postgres/data_types.js.map +1 -1
  59. package/dist/drivers/postgres/index.d.ts +12 -18
  60. package/dist/drivers/postgres/index.js +189 -364
  61. package/dist/drivers/postgres/index.js.map +1 -1
  62. package/dist/drivers/postgres/spellbook.d.ts +6 -4
  63. package/dist/drivers/postgres/spellbook.js +12 -41
  64. package/dist/drivers/postgres/spellbook.js.map +1 -1
  65. package/dist/drivers/postgres/sqlstring.d.ts +11 -15
  66. package/dist/drivers/postgres/sqlstring.js +55 -162
  67. package/dist/drivers/postgres/sqlstring.js.map +1 -1
  68. package/dist/drivers/postgres/type_parser.js +3 -5
  69. package/dist/drivers/postgres/type_parser.js.map +1 -1
  70. package/dist/drivers/sqlite/attribute.d.ts +12 -4
  71. package/dist/drivers/sqlite/attribute.js +16 -38
  72. package/dist/drivers/sqlite/attribute.js.map +1 -1
  73. package/dist/drivers/sqlite/connection.d.ts +24 -14
  74. package/dist/drivers/sqlite/connection.js +72 -169
  75. package/dist/drivers/sqlite/connection.js.map +1 -1
  76. package/dist/drivers/sqlite/data_types.d.ts +9 -86
  77. package/dist/drivers/sqlite/data_types.js +76 -138
  78. package/dist/drivers/sqlite/data_types.js.map +1 -1
  79. package/dist/drivers/sqlite/index.d.ts +53 -14
  80. package/dist/drivers/sqlite/index.js +182 -370
  81. package/dist/drivers/sqlite/index.js.map +1 -1
  82. package/dist/drivers/sqlite/pool.d.ts +21 -9
  83. package/dist/drivers/sqlite/pool.js +46 -147
  84. package/dist/drivers/sqlite/pool.js.map +1 -1
  85. package/dist/drivers/sqlite/spellbook.d.ts +7 -4
  86. package/dist/drivers/sqlite/spellbook.js +31 -94
  87. package/dist/drivers/sqlite/spellbook.js.map +1 -1
  88. package/dist/drivers/sqlite/sqlstring.d.ts +12 -4
  89. package/dist/drivers/sqlite/sqlstring.js +76 -171
  90. package/dist/drivers/sqlite/sqlstring.js.map +1 -1
  91. package/dist/drivers/sqljs/index.d.ts +3 -4
  92. package/dist/drivers/sqljs/index.js +47 -61
  93. package/dist/drivers/sqljs/index.js.map +1 -1
  94. package/dist/drivers/sqljs/sqljs-connection.d.ts +3 -5
  95. package/dist/drivers/sqljs/sqljs-connection.js +86 -138
  96. package/dist/drivers/sqljs/sqljs-connection.js.map +1 -1
  97. package/dist/expr.d.ts +100 -27
  98. package/dist/expr.js +95 -188
  99. package/dist/expr.js.map +1 -1
  100. package/dist/expr_formatter.d.ts +12 -10
  101. package/dist/expr_formatter.js +71 -121
  102. package/dist/expr_formatter.js.map +1 -1
  103. package/dist/hint.d.ts +83 -107
  104. package/dist/hint.js +121 -209
  105. package/dist/hint.js.map +1 -1
  106. package/dist/index.d.ts +22 -91
  107. package/dist/index.js +60 -129
  108. package/dist/index.js.map +1 -1
  109. package/dist/migrations.d.ts +14 -3
  110. package/dist/migrations.js +63 -210
  111. package/dist/migrations.js.map +1 -1
  112. package/dist/query_object.d.ts +38 -2
  113. package/dist/query_object.js +66 -124
  114. package/dist/query_object.js.map +1 -1
  115. package/dist/raw.d.ts +16 -9
  116. package/dist/raw.js +60 -69
  117. package/dist/raw.js.map +1 -1
  118. package/dist/realm/base.d.ts +31 -43
  119. package/dist/realm/base.js +109 -344
  120. package/dist/realm/base.js.map +1 -1
  121. package/dist/realm/index.d.ts +12 -9
  122. package/dist/realm/index.js +43 -140
  123. package/dist/realm/index.js.map +1 -1
  124. package/dist/setup_hooks.d.ts +4 -6
  125. package/dist/setup_hooks.js +67 -185
  126. package/dist/setup_hooks.js.map +1 -1
  127. package/dist/spell.d.ts +355 -174
  128. package/dist/spell.js +386 -788
  129. package/dist/spell.js.map +1 -1
  130. package/dist/types/common.d.ts +24 -32
  131. package/dist/utils/index.d.ts +10 -7
  132. package/dist/utils/index.js +24 -65
  133. package/dist/utils/index.js.map +1 -1
  134. package/dist/utils/invokable.d.ts +5 -3
  135. package/dist/utils/invokable.js +8 -35
  136. package/dist/utils/invokable.js.map +1 -1
  137. package/dist/utils/string.d.ts +15 -4
  138. package/dist/utils/string.js +16 -20
  139. package/dist/utils/string.js.map +1 -1
  140. package/dist/validator.d.ts +22 -8
  141. package/dist/validator.js +59 -83
  142. package/dist/validator.js.map +1 -1
  143. package/lib/abstract_bone.d.ts +478 -0
  144. package/lib/abstract_bone.js +1440 -0
  145. package/lib/abstract_bone.js.map +1 -0
  146. package/lib/adapters/sequelize.d.ts +943 -198
  147. package/lib/adapters/sequelize.js +121 -145
  148. package/lib/adapters/sequelize.js.map +1 -1
  149. package/lib/bone.d.ts +45 -59
  150. package/lib/bone.js +29 -1703
  151. package/lib/bone.js.map +1 -1
  152. package/lib/browser.d.ts +13 -13
  153. package/lib/browser.js +32 -32
  154. package/lib/browser.js.map +1 -1
  155. package/lib/collection.d.ts +16 -16
  156. package/lib/collection.js +16 -16
  157. package/lib/collection.js.map +1 -1
  158. package/lib/constants.d.ts +27 -26
  159. package/lib/constants.js +11 -11
  160. package/lib/constants.js.map +1 -1
  161. package/lib/data_types.d.ts +4 -1
  162. package/lib/data_types.js +4 -3
  163. package/lib/data_types.js.map +1 -1
  164. package/lib/decorators.d.ts +4 -4
  165. package/lib/decorators.js +0 -3
  166. package/lib/decorators.js.map +1 -1
  167. package/lib/drivers/abstract/attribute.d.ts +48 -7
  168. package/lib/drivers/abstract/attribute.js +20 -8
  169. package/lib/drivers/abstract/attribute.js.map +1 -1
  170. package/lib/drivers/abstract/index.d.ts +194 -37
  171. package/lib/drivers/abstract/index.js +160 -47
  172. package/lib/drivers/abstract/index.js.map +1 -1
  173. package/lib/drivers/abstract/logger.d.ts +23 -7
  174. package/lib/drivers/abstract/logger.js +31 -19
  175. package/lib/drivers/abstract/logger.js.map +1 -1
  176. package/lib/drivers/abstract/spellbook.d.ts +24 -40
  177. package/lib/drivers/abstract/spellbook.js +78 -100
  178. package/lib/drivers/abstract/spellbook.js.map +1 -1
  179. package/lib/drivers/index.d.ts +8 -194
  180. package/lib/drivers/index.js +18 -17
  181. package/lib/drivers/index.js.map +1 -1
  182. package/lib/drivers/mysql/attribute.d.ts +4 -5
  183. package/lib/drivers/mysql/attribute.js +15 -13
  184. package/lib/drivers/mysql/attribute.js.map +1 -1
  185. package/lib/drivers/mysql/data_types.d.ts +3 -14
  186. package/lib/drivers/mysql/data_types.js +9 -7
  187. package/lib/drivers/mysql/data_types.js.map +1 -1
  188. package/lib/drivers/mysql/index.d.ts +45 -54
  189. package/lib/drivers/mysql/index.js +31 -59
  190. package/lib/drivers/mysql/index.js.map +1 -1
  191. package/lib/drivers/mysql/spellbook.d.ts +15 -11
  192. package/lib/drivers/mysql/spellbook.js +17 -36
  193. package/lib/drivers/mysql/spellbook.js.map +1 -1
  194. package/lib/drivers/postgres/attribute.d.ts +5 -4
  195. package/lib/drivers/postgres/attribute.js +16 -13
  196. package/lib/drivers/postgres/attribute.js.map +1 -1
  197. package/lib/drivers/postgres/data_types.d.ts +13 -74
  198. package/lib/drivers/postgres/data_types.js +14 -12
  199. package/lib/drivers/postgres/data_types.js.map +1 -1
  200. package/lib/drivers/postgres/index.d.ts +12 -18
  201. package/lib/drivers/postgres/index.js +65 -50
  202. package/lib/drivers/postgres/index.js.map +1 -1
  203. package/lib/drivers/postgres/spellbook.d.ts +6 -4
  204. package/lib/drivers/postgres/spellbook.js +8 -14
  205. package/lib/drivers/postgres/spellbook.js.map +1 -1
  206. package/lib/drivers/postgres/sqlstring.d.ts +11 -15
  207. package/lib/drivers/postgres/sqlstring.js +32 -71
  208. package/lib/drivers/postgres/sqlstring.js.map +1 -1
  209. package/lib/drivers/postgres/type_parser.js +6 -5
  210. package/lib/drivers/postgres/type_parser.js.map +1 -1
  211. package/lib/drivers/sqlite/attribute.d.ts +12 -4
  212. package/lib/drivers/sqlite/attribute.js +11 -9
  213. package/lib/drivers/sqlite/attribute.js.map +1 -1
  214. package/lib/drivers/sqlite/connection.d.ts +24 -14
  215. package/lib/drivers/sqlite/connection.js +16 -11
  216. package/lib/drivers/sqlite/connection.js.map +1 -1
  217. package/lib/drivers/sqlite/data_types.d.ts +9 -86
  218. package/lib/drivers/sqlite/data_types.js +11 -9
  219. package/lib/drivers/sqlite/data_types.js.map +1 -1
  220. package/lib/drivers/sqlite/index.d.ts +53 -14
  221. package/lib/drivers/sqlite/index.js +71 -50
  222. package/lib/drivers/sqlite/index.js.map +1 -1
  223. package/lib/drivers/sqlite/pool.d.ts +21 -9
  224. package/lib/drivers/sqlite/pool.js +18 -14
  225. package/lib/drivers/sqlite/pool.js.map +1 -1
  226. package/lib/drivers/sqlite/spellbook.d.ts +7 -4
  227. package/lib/drivers/sqlite/spellbook.js +18 -16
  228. package/lib/drivers/sqlite/spellbook.js.map +1 -1
  229. package/lib/drivers/sqlite/sqlstring.d.ts +12 -4
  230. package/lib/drivers/sqlite/sqlstring.js +49 -39
  231. package/lib/drivers/sqlite/sqlstring.js.map +1 -1
  232. package/lib/drivers/sqljs/index.d.ts +3 -4
  233. package/lib/drivers/sqljs/index.js +6 -0
  234. package/lib/drivers/sqljs/index.js.map +1 -1
  235. package/lib/drivers/sqljs/sqljs-connection.d.ts +3 -5
  236. package/lib/drivers/sqljs/sqljs-connection.js.map +1 -1
  237. package/lib/expr.d.ts +100 -27
  238. package/lib/expr.js +28 -21
  239. package/lib/expr.js.map +1 -1
  240. package/lib/expr_formatter.d.ts +12 -10
  241. package/lib/expr_formatter.js +37 -34
  242. package/lib/expr_formatter.js.map +1 -1
  243. package/lib/hint.d.ts +83 -107
  244. package/lib/hint.js +47 -51
  245. package/lib/hint.js.map +1 -1
  246. package/lib/index.d.ts +22 -91
  247. package/lib/index.js +75 -53
  248. package/lib/index.js.map +1 -1
  249. package/lib/migrations.d.ts +14 -3
  250. package/lib/migrations.js +19 -13
  251. package/lib/migrations.js.map +1 -1
  252. package/lib/query_object.d.ts +38 -2
  253. package/lib/query_object.js +30 -46
  254. package/lib/query_object.js.map +1 -1
  255. package/lib/raw.d.ts +16 -9
  256. package/lib/raw.js +14 -0
  257. package/lib/raw.js.map +1 -1
  258. package/lib/realm/base.d.ts +31 -43
  259. package/lib/realm/base.js +31 -37
  260. package/lib/realm/base.js.map +1 -1
  261. package/lib/realm/index.d.ts +12 -9
  262. package/lib/realm/index.js +17 -15
  263. package/lib/realm/index.js.map +1 -1
  264. package/lib/setup_hooks.d.ts +4 -6
  265. package/lib/setup_hooks.js +15 -21
  266. package/lib/setup_hooks.js.map +1 -1
  267. package/lib/spell.d.ts +355 -174
  268. package/lib/spell.js +158 -148
  269. package/lib/spell.js.map +1 -1
  270. package/lib/types/abstract_bone.d.ts +38 -8
  271. package/lib/types/common.d.ts +24 -32
  272. package/lib/utils/index.d.ts +10 -7
  273. package/lib/utils/index.js +24 -26
  274. package/lib/utils/index.js.map +1 -1
  275. package/lib/utils/invokable.d.ts +5 -3
  276. package/lib/utils/invokable.js +5 -5
  277. package/lib/utils/invokable.js.map +1 -1
  278. package/lib/utils/string.d.ts +15 -4
  279. package/lib/utils/string.js +13 -10
  280. package/lib/utils/string.js.map +1 -1
  281. package/lib/validator.d.ts +22 -8
  282. package/lib/validator.js +30 -28
  283. package/lib/validator.js.map +1 -1
  284. package/package.json +8 -5
  285. package/dist/types/abstract_bone.d.ts +0 -451
package/Readme.md CHANGED
@@ -13,7 +13,7 @@ Leoric is an object-relational mapping library for Node.js, which is heavily inf
13
13
  Assume the tables of posts, users, and comments were setup already. We may declare the models as classes by extending from the base class `Bone` of Leoric. After the models are connected to the database, the columns of the tables are mapped as attributes, the associations are setup, feel free to start querying.
14
14
 
15
15
  ```js
16
- const { Bone, connect } = require('leoric')
16
+ import { Bone, connect } from 'leoric'
17
17
 
18
18
  // define model
19
19
  class Post extends Bone {
@@ -45,7 +45,8 @@ async function main() {
45
45
  If table structures were intended to be maintained in the models, Leoric can be used as a table migration tool as well. We can just define attributes in the models, and call `realm.sync()` whenever we are ready.
46
46
 
47
47
  ```js
48
- const { BIGINT, STRING } = Bone.DataTypes;
48
+ import Realm, { Bone, DataTypes } from 'leoric';
49
+ const { BIGINT, STRING } = DataTypes;
49
50
  class Post extends Bone {
50
51
  static attributes = {
51
52
  id: { type: BIGINT, primaryKey: true },
@@ -0,0 +1,478 @@
1
+ import util from 'util';
2
+ import DataTypes, { AbstractDataType, DataType } from './data_types';
3
+ import Collection from './collection';
4
+ import Spell from './spell';
5
+ import Raw from './raw';
6
+ import 'reflect-metadata';
7
+ import { Pool, Literal, WhereConditions, QueryOptions, AttributeMeta, AssociateOptions, Connection, BoneColumns, ColumnMeta, BeforeHooksType, AfterHooksType, QueryResult, BoneCreateValues, BulkCreateOptions, Values } from './types/common';
8
+ import { AbstractDriver, ConnectOptions } from './drivers';
9
+ import Attribute from './drivers/abstract/attribute';
10
+ export interface SyncOptions {
11
+ force?: boolean;
12
+ alter?: boolean;
13
+ }
14
+ export interface InitOptions {
15
+ underscored?: boolean;
16
+ tableName?: string;
17
+ hooks?: {
18
+ [key in BeforeHooksType]: (options: QueryOptions) => Promise<void>;
19
+ } | {
20
+ [key in AfterHooksType]: (instance: AbstractBone, result: object) => Promise<void>;
21
+ };
22
+ timestamps?: boolean;
23
+ }
24
+ export declare const columnAttributesKey: unique symbol;
25
+ export declare const synchronizedKey: unique symbol;
26
+ export declare const tableKey: unique symbol;
27
+ export declare class AbstractBone {
28
+ #private;
29
+ static DataTypes: typeof DataTypes;
30
+ [key: string]: any;
31
+ isNewRecord: boolean;
32
+ static [columnAttributesKey]: {
33
+ [key: string]: Attribute;
34
+ } | null;
35
+ static [synchronizedKey]: boolean;
36
+ static [tableKey]: string;
37
+ static get synchronized(): boolean;
38
+ static set synchronized(value: boolean);
39
+ /**
40
+ * The driver that powers the model
41
+ */
42
+ static driver: AbstractDriver;
43
+ /**
44
+ * The connected models structured as `{ [model.name]: model }`, e.g. `Bone.model.Post => Post`
45
+ */
46
+ static models: {
47
+ [key: string]: typeof AbstractBone;
48
+ };
49
+ /**
50
+ * The plural model name in camelCase, e.g. `Post => posts`
51
+ */
52
+ static tableAlias: string;
53
+ /**
54
+ * The primary key of the model, defaults to `id`.
55
+ */
56
+ static primaryKey: string;
57
+ /**
58
+ * The primary column of the table, defaults to `id`. This is {@link Bone.primaryKey} in snake case.
59
+ */
60
+ static get primaryColumn(): string;
61
+ /**
62
+ * The attribute definitions of the model.
63
+ */
64
+ static attributes: {
65
+ [key: string]: Attribute;
66
+ };
67
+ /**
68
+ * The attribute definitions of the model, referenced by column name.
69
+ */
70
+ static attributeMap: {
71
+ [key: string]: Attribute;
72
+ };
73
+ /**
74
+ * The schema info of current model.
75
+ */
76
+ static columns: Array<AttributeMeta>;
77
+ /**
78
+ * If the table consists of multiple partition tables then a sharding key is needed to perform actual query. The sharding key can be specified through overridding this property, which will then be used to check the query before it hits database.
79
+ */
80
+ static shardingKey: string;
81
+ /**
82
+ * If the table name is just an alias and the schema info can only be fetched by one of its partition table names, physic tables should be specified.
83
+ */
84
+ static physicTables: string[];
85
+ static get physicTable(): string;
86
+ static set table(value: string);
87
+ static get table(): string;
88
+ static options: ConnectOptions;
89
+ static timestamps: {
90
+ createdAt: string;
91
+ updatedAt: string;
92
+ deletedAt: string;
93
+ };
94
+ static _scope: any;
95
+ static associations: {
96
+ [key: string]: any;
97
+ };
98
+ constructor(values?: {
99
+ [key: string]: Literal;
100
+ }, opts?: {
101
+ isNewRecord?: boolean;
102
+ });
103
+ static get shardingColumn(): string | undefined;
104
+ /**
105
+ * get the connection pool of the driver
106
+ */
107
+ static get pool(): Pool;
108
+ /**
109
+ * Override attribute metadata
110
+ * @example
111
+ * Bone.attribute('foo', { type: JSON })
112
+ */
113
+ static attribute(name: string, meta: AttributeMeta): void;
114
+ /**
115
+ * Model.hasAttribute(name)
116
+ * @static
117
+ * @param {string} name
118
+ * @returns {boolean}
119
+ */
120
+ static hasAttribute(name: string): boolean;
121
+ /**
122
+ * get attributes except virtuals
123
+ */
124
+ static get columnAttributes(): {
125
+ [key: string]: Attribute;
126
+ };
127
+ /**
128
+ * get actual update/insert columns to avoid empty insert or update
129
+ * @param {Object} data
130
+ */
131
+ static _getColumns(data: Record<string, Literal>): Record<string, Literal>;
132
+ /**
133
+ * Rename attribute
134
+ * @example
135
+ * Bone.renameAttribute('foo', 'bar')
136
+ */
137
+ static renameAttribute<T extends typeof AbstractBone>(this: T, originalName: any, newName: string): void;
138
+ static alias<T extends typeof AbstractBone>(this: T, name: BoneColumns<T>): string;
139
+ static alias<T extends typeof AbstractBone>(this: T, data: {
140
+ [key in BoneColumns<T>]: Literal;
141
+ }): Record<string, Literal>;
142
+ static alias<T extends typeof AbstractBone>(this: T, name: string): string;
143
+ static alias<T extends typeof AbstractBone>(this: T, data: Record<string, Literal>): Record<string, Literal>;
144
+ static unalias(name: string): string;
145
+ /**
146
+ * Load attribute definition to merge default getter/setter and custom descriptor on prototype
147
+ */
148
+ static loadAttribute(name: string): void;
149
+ static hasOne(name: string, options?: AssociateOptions): void;
150
+ static hasMany(name: string, options?: AssociateOptions): void;
151
+ static belongsTo(name: string, options?: AssociateOptions): void;
152
+ /**
153
+ * Mount association metadata, verifying existence and applying paranoid defaults.
154
+ */
155
+ static associate(name: string, opts: AssociateOptions): void;
156
+ /**
157
+ * INSERT rows
158
+ * @example
159
+ * Bone.create({ foo: 1, bar: 'baz' })
160
+ */
161
+ static create<T extends typeof AbstractBone>(this: T, values: BoneCreateValues<T>, opts?: QueryOptions): AbstractBone | Spell<typeof AbstractBone, AbstractBone>;
162
+ /**
163
+ * INSERT or UPDATE rows
164
+ * @example
165
+ * Bone.upsert(values, { hooks: false })
166
+ * @param values values
167
+ * @param opt query options
168
+ */
169
+ static upsert<T extends typeof AbstractBone>(this: T, values: BoneCreateValues<T>, options?: QueryOptions): Promise<number>;
170
+ static bulkCreate<T extends typeof AbstractBone>(this: T, records: Array<BoneCreateValues<T>>, options?: BulkCreateOptions): Promise<InstanceType<T>[]>;
171
+ /**
172
+ * SELECT all rows. In production, when the table is at large, it is not recommended to access records in this way. To iterate over all records, {@link Bone.batch} shall be considered as the better alternative. For tables with soft delete enabled, which means they've got `deletedAt` attribute, use {@link Bone.unscoped} to discard the default scope.
173
+ */
174
+ static get all(): Spell<typeof AbstractBone, Collection<InstanceType<typeof AbstractBone>>>;
175
+ /**
176
+ * SELECT rows OFFSET index LIMIT 1
177
+ * @example
178
+ * Bone.get(8)
179
+ * Bone.find({ foo: { $gt: 1 } }).get(42)
180
+ */
181
+ static get<T extends typeof AbstractBone>(this: T, index: number): Spell<T, InstanceType<T> | null>;
182
+ /**
183
+ * SELECT rows ORDER BY id ASC LIMIT 1
184
+ */
185
+ static get first(): Spell<typeof AbstractBone, InstanceType<typeof AbstractBone> | null>;
186
+ /**
187
+ * SELECT rows ORDER BY id DESC LIMIT 1
188
+ */
189
+ static get last(): Spell<typeof AbstractBone, InstanceType<typeof AbstractBone> | null>;
190
+ /**
191
+ * Short of `Bone.find().with(...names)`
192
+ * @example
193
+ * Post.include('author', 'comments').where('posts.id = ?', 1)
194
+ */
195
+ static include<T extends typeof AbstractBone>(this: T, ...names: string[]): Spell<T>;
196
+ /**
197
+ * Internal finder powering all query starts.
198
+ */
199
+ static _find<T extends typeof AbstractBone>(this: T, conditions?: WhereConditions<T> | string | number | number[] | bigint | bigint[], ...values: Literal[]): Spell<T, number | InstanceType<T> | import("./types/common").Collection<InstanceType<T>> | import("./types/common").ResultSet<T> | null>;
200
+ static select<T extends typeof AbstractBone>(this: T, ...names: Array<BoneColumns<T>> | string[]): Spell<T>;
201
+ static select<T extends typeof AbstractBone>(this: T, ...names: string[]): Spell<T>;
202
+ static select<T extends typeof AbstractBone>(this: T, ...names: Raw[]): Spell<T>;
203
+ static select<T extends typeof AbstractBone>(this: T, filter: (name: string) => boolean): Spell<T>;
204
+ /**
205
+ * JOIN arbitrary models with given ON conditions
206
+ * @example
207
+ * Bone.join(Muscle, 'bones.id == muscles.boneId')
208
+ */
209
+ static join<T extends typeof AbstractBone>(this: T, ...args: Parameters<typeof Spell.prototype.$join>): Spell<T, Collection<InstanceType<T>>>;
210
+ /**
211
+ * Set WHERE conditions
212
+ * @example
213
+ * Bone.where('foo = ?', 1)
214
+ * Bone.where({ foo: { $eq: 1 } })
215
+ */
216
+ static where<T extends typeof AbstractBone>(this: T, ...args: Parameters<typeof Spell.prototype.$where>): Spell<T, Collection<InstanceType<T>>>;
217
+ /**
218
+ * Set GROUP fields
219
+ * @example
220
+ * Bone.group('foo')
221
+ * Bone.group('MONTH(createdAt)')
222
+ */
223
+ static group<T extends typeof AbstractBone>(this: T, ...names: Parameters<typeof Spell.prototype.$group>): Spell<T, number | InstanceType<T> | import("./types/common").Collection<InstanceType<T>> | import("./types/common").ResultSet<T> | null>;
224
+ /**
225
+ * Set ORDER fields
226
+ * @example
227
+ * Bone.order('foo')
228
+ * Bone.order('foo', 'desc')
229
+ * Bone.order({ foo: 'desc' })
230
+ */
231
+ static order<T extends typeof AbstractBone>(this: T, ...args: Parameters<typeof Spell.prototype.$order>): Spell<T, number | InstanceType<T> | import("./types/common").Collection<InstanceType<T>> | import("./types/common").ResultSet<T> | null>;
232
+ static count<T extends typeof AbstractBone>(this: T, name?: BoneColumns<T> | Raw | string): Spell<T, number | import("./types/common").ResultSet<T> | Extract<InstanceType<T>, number | import("./types/common").ResultSet<T>> | Extract<import("./types/common").Collection<InstanceType<T>>, number | import("./types/common").ResultSet<T>>>;
233
+ static average<T extends typeof AbstractBone>(this: T, name: BoneColumns<T> | Raw | string): Spell<T, number | import("./types/common").ResultSet<T> | Extract<InstanceType<T>, number | import("./types/common").ResultSet<T>> | Extract<import("./types/common").Collection<InstanceType<T>>, number | import("./types/common").ResultSet<T>>>;
234
+ static minimum<T extends typeof AbstractBone>(this: T, name: BoneColumns<T> | Raw | string): Spell<T, number | import("./types/common").ResultSet<T> | Extract<InstanceType<T>, number | import("./types/common").ResultSet<T>> | Extract<import("./types/common").Collection<InstanceType<T>>, number | import("./types/common").ResultSet<T>>>;
235
+ static maximum<T extends typeof AbstractBone>(this: T, name: BoneColumns<T> | Raw | string): Spell<T, number | import("./types/common").ResultSet<T> | Extract<InstanceType<T>, number | import("./types/common").ResultSet<T>> | Extract<import("./types/common").Collection<InstanceType<T>>, number | import("./types/common").ResultSet<T>>>;
236
+ /**
237
+ * Update any record that matches conditions.
238
+ */
239
+ static _update<T extends typeof AbstractBone, Key extends BoneColumns<T>>(this: T, conditions: WhereConditions<T>, values: Record<Key, Literal | Raw>, options: QueryOptions): Spell<T, number>;
240
+ /**
241
+ * JSON merge convenience for update
242
+ */
243
+ static jsonMerge<T extends typeof AbstractBone, Key extends BoneColumns<T>>(this: T, conditions: WhereConditions<T>, values: Record<Key, Literal | Record<string, Literal> | Raw>, options?: QueryOptions & {
244
+ preserve?: boolean;
245
+ }): Promise<number>;
246
+ static jsonMergePreserve<T extends typeof AbstractBone, Key extends keyof Values<T>>(this: T, conditions: WhereConditions<T>, values: Record<Key, Record<string, Literal>>, options: QueryOptions): Promise<number>;
247
+ /**
248
+ * Remove rows. If soft delete is applied, an UPDATE query is performed instead of DELETing records directly. Set `forceDelete` to true to force a `DELETE` query.
249
+ */
250
+ static remove<T extends typeof AbstractBone>(this: T, conditions: WhereConditions<T>, forceDelete?: boolean, options?: QueryOptions): Spell<T, number>;
251
+ /**
252
+ * private method for internal calling
253
+ * Remove any record that matches `conditions`.
254
+ * - If `forceDelete` is true, `DELETE` records from database permanently.
255
+ * - If not, update `deletedAt` attribute with current date.
256
+ * - If `forceDelete` isn't true and `deleteAt` isn't around, throw an Error.
257
+ * @example
258
+ * Post.remove({ title: 'Leah' }) // mark Post { title: 'Leah' } as deleted
259
+ * Post.remove({ title: 'Leah' }, true) // delete Post { title: 'Leah' }
260
+ * Post.remove({}, true) // delete all data of posts
261
+ */
262
+ static _remove<T extends typeof AbstractBone>(this: T, conditions: WhereConditions<T>, forceDelete?: boolean, options?: QueryOptions): Spell<T, number>;
263
+ /**
264
+ * Execute a raw query
265
+ * @example
266
+ * Bone.query('SELECT * FROM posts WHERE id = ?', [1])
267
+ * Bone.query('SELECT * FROM posts WHERE id = :id', { replacements: { id: 1 } })
268
+ */
269
+ static query<T extends typeof AbstractBone>(this: T, sql: string, values?: Literal[] | QueryOptions, opts?: QueryOptions): Promise<{
270
+ rows?: any[];
271
+ fields?: {
272
+ table: string;
273
+ name: string;
274
+ }[];
275
+ }>;
276
+ /**
277
+ * Grabs a connection and starts a transaction process. Both GeneratorFunction and AsyncFunction are acceptable. If GeneratorFunction is used, the connection of the transaction process will be passed around automatically.
278
+ * @example
279
+ * Bone.transaction(function* () {
280
+ * const bone = yield Bone.create({ foo: 1 })
281
+ * yield Muscle.create({ boneId: bone.id, bar: 1 })
282
+ * });
283
+ */
284
+ static transaction<T extends (options: {
285
+ connection: Connection;
286
+ commit: () => Promise<QueryResult>;
287
+ rollback: () => Promise<QueryResult>;
288
+ }) => Promise<any> | Generator>(callback: T): Promise<ReturnType<T>>;
289
+ static describe(): Promise<Record<string, ColumnMeta>>;
290
+ /**
291
+ * DROP the table
292
+ */
293
+ static drop(): Promise<void>;
294
+ /**
295
+ * TRUNCATE table to clear records.
296
+ */
297
+ static truncate(): Promise<void>;
298
+ static sync({ force, alter }?: SyncOptions): Promise<void>;
299
+ static initialize(): void;
300
+ static instantiate<T extends typeof AbstractBone>(this: T, row: any): InstanceType<T>;
301
+ static init(attributes: Record<string, AbstractDataType<DataType> | AttributeMeta>, opts?: InitOptions, overrides?: Record<string, PropertyDescriptor>): void;
302
+ static load<T extends typeof AbstractBone>(this: T, columns?: Array<ColumnMeta>): void;
303
+ static from<T extends typeof AbstractBone>(this: T, table: string | Spell<T>): Spell<T, number | InstanceType<T> | import("./types/common").Collection<InstanceType<T>> | import("./types/common").ResultSet<T> | null>;
304
+ /**
305
+ * Restore soft-deleted rows by clearing deletedAt.
306
+ * @example
307
+ * Bone.restore({ title: 'aaa' })
308
+ * Bone.restore({ title: 'aaa' }, { hooks: false })
309
+ */
310
+ static _restore<T extends typeof AbstractBone>(this: T, conditions: WhereConditions<T>, opts: QueryOptions): Spell<T, number>;
311
+ getRaw(key?: string): any;
312
+ getRawSaved(key?: string): any;
313
+ getRawPrevious(key?: string): any;
314
+ _setRaw(...args: any[]): void;
315
+ _getRawUnset(): Set<string>;
316
+ _setRawSaved(key: string, value: any): void;
317
+ attribute<T, Key extends keyof Values<T>, U extends T[Key]>(this: T, name: Key): U extends Literal ? U : Literal;
318
+ attribute<T, Key extends keyof T, U extends T[Key]>(this: T, name: Key): U extends Literal ? U : Literal;
319
+ attribute<T, Key extends keyof Values<T>>(this: T, name: Key, value: Literal): this;
320
+ attribute<T, Key extends keyof T>(this: T, name: Key, value: Literal): this;
321
+ /**
322
+ * instance.hasAttribute(name)
323
+ * @param {string} name
324
+ * @returns {boolean}
325
+ */
326
+ hasAttribute(name: string): boolean;
327
+ /**
328
+ * Get the original attribute value.
329
+ * @example
330
+ * bone.attributeWas('foo') // => 1
331
+ */
332
+ attributeWas(name: string): any;
333
+ /**
334
+ * See if attribute has been changed or not.
335
+ * @deprecated {@link Bone#changed} is preferred
336
+ * @example
337
+ * bone.attributeChanged('foo')
338
+ */
339
+ attributeChanged(name: string): boolean;
340
+ /**
341
+ * Get changed attributes or check if given attribute is changed or not
342
+ */
343
+ changed(): string[] | false;
344
+ changed(name: string): boolean;
345
+ /**
346
+ * Get attribute changes
347
+ */
348
+ changes(name?: string): Record<string, [any, any]>;
349
+ /**
350
+ * See if attribute was changed previously or not.
351
+ */
352
+ previousChanged(name?: string): any;
353
+ previousChanges(name?: string): Record<string, [any, any]>;
354
+ /**
355
+ * Persist changes of current record to database. If current record has never been saved before, an INSERT query is performed. If the primary key was set and is not changed since, an UPDATE query is performed. If the primary key is changed, an INSERT ... UPDATE query is performed instead.
356
+ *
357
+ * If `affectedRows` is needed, consider using the corresponding methods directly.
358
+ * @example
359
+ * new Bone({ foo: 1 }).save() // => INSERT
360
+ * new Bone({ foo: 1, id: 1 }).save() // => INSERT ... UPDATE
361
+ * (await Bone.fist).attribute('foo', 2).save() // => UPDATE
362
+ * new Bone({ foo: 1, id: 1 }).save({ hooks: false }) // => INSERT ... UPDATE
363
+ */
364
+ save(opts?: QueryOptions): Promise<this>;
365
+ /**
366
+ * Internal save dispatcher deciding between create/update/upsert
367
+ */
368
+ _save(opts?: QueryOptions): Promise<this>;
369
+ /**
370
+ * Sync raw caches after persistence
371
+ */
372
+ syncRaw(): void;
373
+ /**
374
+ * Remove current record. If `deletedAt` attribute exists, then instead of DELETing records from database directly, the records will have their `deletedAt` attribute UPDATEd instead. To force `DELETE`, no matter the existence of `deletedAt` attribute, pass `true` as the argument.
375
+ * @example
376
+ * bone.remove() // => UPDATE ... SET deleted_at = now() WHERE ...
377
+ * bone.remove(true) // => DELETE FROM ... WHERE ...
378
+ * bone.remove(true, { hooks: false })
379
+ */
380
+ remove(forceDelete?: boolean, opts?: QueryOptions): Promise<number>;
381
+ /**
382
+ * Delete current instance. If `deletedAt` attribute exists, calling {@link Bone#remove} does not actually delete the record from the database. Instead, it updates the value of `deletedAt` attribute to current date. This is called [soft delete](../querying#scopes). To force a regular `DELETE`, use `.remove(true)`.
383
+ * @private
384
+ * @param {boolean} forceDelete
385
+ * @example
386
+ * const post = await Post.first
387
+ * post.remove() // update the `deletedAt`
388
+ * post.remove(true) // delete record
389
+ */
390
+ _remove(forceDelete?: boolean, opts?: QueryOptions): Promise<number>;
391
+ /**
392
+ * update or insert record.
393
+ * @example
394
+ * bone.upsert() // INERT ... VALUES ON DUPLICATE KEY UPDATE ...
395
+ * bone.upsert({ hooks: false })
396
+ * @param opts queryOptions
397
+ */
398
+ upsert(opts?: QueryOptions): Promise<number>;
399
+ /**
400
+ * Internal upsert implementation
401
+ */
402
+ _upsert(opts: QueryOptions): Promise<number>;
403
+ /**
404
+ * Persist changes on current instance back to database with `UPDATE`.
405
+ * @private
406
+ */
407
+ _update(values: Record<string, Literal>, options: QueryOptions): Promise<number>;
408
+ /**
409
+ * UPDATE JSONB column with JSON_MERGE_PATCH function
410
+ * @example
411
+ * /// before: bone.extra equals { name: 'zhangsan', url: 'https://alibaba.com' }
412
+ * bone.jsonMerge('extra', { url: 'https://taobao.com' })
413
+ * /// after: bone.extra equals { name: 'zhangsan', url: 'https://taobao.com' }
414
+ */
415
+ jsonMerge<Key extends keyof Extract<this, Literal>>(values: Record<Key, Record<string, Literal>>, opts?: QueryOptions & {
416
+ preserve?: boolean;
417
+ }): Promise<number>;
418
+ jsonMerge<Key extends keyof Extract<this, Literal>>(name: Key, jsonValue: Record<string, Literal>, opts?: QueryOptions & {
419
+ preserve?: boolean;
420
+ }): Promise<number>;
421
+ jsonMergePreserve<Key extends keyof Extract<this, Literal>>(values: Record<Key, Record<string, Literal>>, opts?: QueryOptions): Promise<number>;
422
+ jsonMergePreserve<Key extends keyof Extract<this, Literal>>(name: Key, jsonValue: Record<string, Literal>, opts?: QueryOptions): Promise<number>;
423
+ /**
424
+ * create instance
425
+ * @param opts query options
426
+ */
427
+ create(opts?: QueryOptions): Spell<typeof AbstractBone, this> | this;
428
+ /**
429
+ * Internal create implementation
430
+ */
431
+ _create(opts?: QueryOptions): Spell<typeof AbstractBone, this> | this;
432
+ /**
433
+ * reload instance
434
+ */
435
+ reload(): Promise<this>;
436
+ /**
437
+ * Protected clone
438
+ */
439
+ _clone(target: any): void;
440
+ /**
441
+ * Validate current changes
442
+ */
443
+ validate(): void;
444
+ /**
445
+ * Instance attribute validation
446
+ */
447
+ _validateAttributes(values?: Record<string, Literal>): void;
448
+ /**
449
+ * Static attribute validation
450
+ */
451
+ static _validateAttributes(values: Record<string, Literal>): void;
452
+ /**
453
+ * restore data
454
+ * @param opts query options
455
+ */
456
+ restore(opts?: QueryOptions): Promise<this>;
457
+ /**
458
+ * Gets called when `JSON.stringify(instance)` is invoked.
459
+ * {@link Bone#toJSON} might be called on descents of Bone that does not have attributes defined on them directly, hence for..in is preferred.
460
+ * - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties
461
+ * @example
462
+ * const post = await Post.first
463
+ * post.toJSON() // => { id: 1, ... }
464
+ * @return {Object}
465
+ */
466
+ [util.inspect.custom](): string;
467
+ toJSON(): Record<string, any>;
468
+ /**
469
+ * This is the loyal twin of {@link Bone#toJSON} because when generating the result object, the raw values of attributes are used, instead of the values returned by custom getters (if any).
470
+ * {@link Bone#toObject} might be called on descents of Bone that does not have attributes defined on them directly, hence for..in is preferred.
471
+ * - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties
472
+ * @example
473
+ * const post = await Post.first
474
+ * post.toObject() // => { id: 1, ... }
475
+ * @return {Object}
476
+ */
477
+ toObject(): any;
478
+ }