json.test.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. import type {
  2. CreationOptional,
  3. InferAttributes,
  4. InferCreationAttributes,
  5. NonAttribute,
  6. } from '@sequelize/core';
  7. import { DataTypes, Model, Op, sql } from '@sequelize/core';
  8. import { Attribute, BelongsTo } from '@sequelize/core/decorators-legacy';
  9. import { expect } from 'chai';
  10. import semver from 'semver';
  11. import { beforeAll2, beforeEach2, inlineErrorCause, sequelize, setResetMode } from './support';
  12. const dialect = sequelize.dialect;
  13. const dialectName = dialect.name;
  14. /**
  15. * Whether the current dialect supports comparing JSON to JSON directly.
  16. * In dialects like postgres, no "json = json" operator exists, we need to cast to text first.
  17. * It does however support "jsonb = jsonb".
  18. */
  19. const dialectSupportsJsonEquality = ['sqlite3', 'mysql', 'mariadb', 'mssql'].includes(dialectName);
  20. describe('JSON Manipulation', () => {
  21. if (!dialect.supports.dataTypes.JSON) {
  22. return;
  23. }
  24. const vars = beforeEach2(async () => {
  25. class User extends Model<InferAttributes<User>> {
  26. @Attribute(DataTypes.JSON)
  27. declare jsonAttr: any;
  28. }
  29. sequelize.addModels([User]);
  30. await sequelize.sync({ force: true });
  31. return { User };
  32. });
  33. it('supports inserting json', async () => {
  34. const user = await vars.User.create({
  35. jsonAttr: { username: 'joe' },
  36. });
  37. expect(user.jsonAttr).to.deep.equal({ username: 'joe' });
  38. });
  39. it('supports updating json', async () => {
  40. const user = await vars.User.create({
  41. jsonAttr: { username: 'joe' },
  42. });
  43. user.jsonAttr = { name: 'larry' };
  44. await user.save();
  45. expect(user.jsonAttr).to.deep.equal({ name: 'larry' });
  46. });
  47. it('should be able to store strings that require escaping', async () => {
  48. const text = 'Multi-line \n \'$string\' needing "escaping" for $$ and $1 type values';
  49. await vars.User.create({ jsonAttr: text });
  50. const user = await vars.User.findOne({ rejectOnEmpty: true });
  51. expect(user.jsonAttr).to.equal(text);
  52. });
  53. });
  54. const JSON_OBJECT = { name: 'swen', phones: [1337, 42] };
  55. const JSON_STRING = 'kate';
  56. describe('JSON Querying', () => {
  57. if (!dialect.supports.dataTypes.JSON) {
  58. return;
  59. }
  60. setResetMode('none');
  61. const vars = beforeAll2(async () => {
  62. class User extends Model<InferAttributes<User>, InferCreationAttributes<User>> {
  63. declare id: CreationOptional<number>;
  64. @Attribute(DataTypes.JSON)
  65. declare objectJsonAttr: object;
  66. @Attribute(DataTypes.JSON)
  67. declare stringJsonAttr: string;
  68. }
  69. class Order extends Model<InferAttributes<Order>, InferCreationAttributes<Order>> {
  70. declare id: CreationOptional<number>;
  71. @BelongsTo(() => User, 'userId')
  72. declare user: NonAttribute<User>;
  73. @Attribute(DataTypes.INTEGER)
  74. declare userId: number;
  75. }
  76. sequelize.addModels([User, Order]);
  77. await sequelize.sync({ force: true });
  78. const user = await User.create({
  79. objectJsonAttr: JSON_OBJECT,
  80. stringJsonAttr: JSON_STRING,
  81. });
  82. await Order.create({ userId: user.id });
  83. return { User, Order };
  84. });
  85. it('parses retrieved JSON values', async () => {
  86. const user = await vars.User.findOne({ rejectOnEmpty: true });
  87. expect(user.objectJsonAttr).to.deep.eq(JSON_OBJECT);
  88. expect(user.stringJsonAttr).to.eq(JSON_STRING);
  89. });
  90. if (dialectSupportsJsonEquality) {
  91. it('should be able to compare JSON to JSON directly', async () => {
  92. const user = await vars.User.findOne({
  93. where: {
  94. stringJsonAttr: JSON_STRING,
  95. },
  96. });
  97. expect(user).to.exist;
  98. });
  99. } else {
  100. it('should not be able to compare JSON to JSON directly', async () => {
  101. await expect(
  102. vars.User.findOne({
  103. where: {
  104. stringJsonAttr: JSON_STRING,
  105. },
  106. }),
  107. ).to.be.rejected;
  108. });
  109. }
  110. it('should be able to retrieve json value as object for json fields created in every mariadb release', async () => {
  111. // MariaDB does not support native JSON type, it uses longtext instead
  112. // MariaDB >=10.5.2 adds a CHECK(json_valid(field)) validator that uses to return a different dataFormat to clients
  113. // mariadb connector use this to decide to parse or not a JSON field before sequelize
  114. if (dialectName !== 'mariadb') {
  115. return;
  116. }
  117. await sequelize.query(`CREATE TABLE Posts (id INTEGER AUTO_INCREMENT PRIMARY KEY,
  118. metaOldJSONtype longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  119. metaNewJSONtype longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK(json_valid(metaNewJSONtype)))`);
  120. const Posts = sequelize.define(
  121. 'Posts',
  122. {
  123. metaOldJSONtype: DataTypes.JSON,
  124. metaNewJSONtype: DataTypes.JSON,
  125. },
  126. {
  127. freezeTableName: true,
  128. timestamps: false,
  129. },
  130. );
  131. await Posts.create({ metaOldJSONtype: 'some text', metaNewJSONtype: 'some text' });
  132. const posts = await Posts.findAll({ raw: true });
  133. expect(posts[0].metaOldJSONtype).to.equal(posts[0].metaNewJSONtype);
  134. });
  135. describe('JSON quoted', () => {
  136. if (dialect.supports.jsonExtraction.quoted) {
  137. it('should be able to retrieve element of array by index', async () => {
  138. const user = await vars.User.findOne({
  139. attributes: [[sql.attribute('objectJsonAttr.phones[1]'), 'firstEmergencyNumber']],
  140. rejectOnEmpty: true,
  141. });
  142. // @ts-expect-error -- typings are not currently designed to handle custom attributes
  143. const firstNumber: string = user.getDataValue('firstEmergencyNumber');
  144. expect(Number.parseInt(firstNumber, 10)).to.equal(42);
  145. });
  146. it('should be able to query using JSON path objects', async () => {
  147. // JSON requires casting to text in postgres. There is no "json = json" operator
  148. // No-cast version is tested higher up in this suite
  149. const comparison =
  150. dialectName === 'postgres' ? { 'name::text': '"swen"' } : { name: 'swen' };
  151. const user = await vars.User.findOne({
  152. where: { objectJsonAttr: comparison },
  153. });
  154. expect(user).to.exist;
  155. });
  156. it('should be able to query using JSON path dot notation', async () => {
  157. // JSON requires casting to text in postgres. There is no "json = json" operator
  158. // No-cast version is tested higher up in this suite
  159. const comparison =
  160. dialectName === 'postgres'
  161. ? { 'objectJsonAttr.name::text': '"swen"' }
  162. : { 'objectJsonAttr.name': 'swen' };
  163. const user = await vars.User.findOne({
  164. where: comparison,
  165. });
  166. expect(user).to.exist;
  167. });
  168. it('should be able retrieve json value with nested include', async () => {
  169. const orders = await vars.Order.findAll({
  170. attributes: ['id'],
  171. include: [
  172. {
  173. model: vars.User,
  174. attributes: [[sql.attribute('objectJsonAttr.name'), 'name']],
  175. },
  176. ],
  177. });
  178. // we can't automatically detect that the output is JSON type in mariadb < 10.5.2,
  179. // and we don't yet support specifying (nor inferring) the type of custom attributes,
  180. // so for now the output is different in this specific case
  181. const expectedResult =
  182. dialectName === 'mariadb' && semver.lt(sequelize.getDatabaseVersion(), '10.5.2')
  183. ? '"swen"'
  184. : 'swen';
  185. // @ts-expect-error -- getDataValue does not support custom attributes
  186. expect(orders[0].user.getDataValue('name')).to.equal(expectedResult);
  187. });
  188. }
  189. });
  190. describe('JSON unquoted', () => {
  191. if (dialect.supports.jsonExtraction.unquoted) {
  192. it('should be able to retrieve element of array by index', async () => {
  193. const user = await vars.User.findOne({
  194. attributes: [[sql.attribute('objectJsonAttr.phones[1]:unquote'), 'firstEmergencyNumber']],
  195. rejectOnEmpty: true,
  196. });
  197. // @ts-expect-error -- typings are not currently designed to handle custom attributes
  198. const firstNumber: string = user.getDataValue('firstEmergencyNumber');
  199. expect(Number.parseInt(firstNumber, 10)).to.equal(42);
  200. });
  201. it('should be able to query using JSON path dot notation', async () => {
  202. const user = await vars.User.findOne({
  203. // JSON unquote does not require casting to text, as it already returns text
  204. where: { 'objectJsonAttr.name:unquote': 'swen' },
  205. });
  206. expect(user).to.exist;
  207. });
  208. it('should be able retrieve json value with nested include', async () => {
  209. const orders = await vars.Order.findAll({
  210. attributes: ['id'],
  211. include: [
  212. {
  213. model: vars.User,
  214. attributes: [[sql.attribute('objectJsonAttr.name:unquote'), 'name']],
  215. },
  216. ],
  217. });
  218. // @ts-expect-error -- getDataValue does not support custom attributes
  219. expect(orders[0].user.getDataValue('name')).to.equal('swen');
  220. });
  221. }
  222. });
  223. });
  224. describe('JSON Casting', () => {
  225. if (!dialect.supports.dataTypes.JSON || !dialect.supports.jsonOperations) {
  226. return;
  227. }
  228. setResetMode('truncate');
  229. const vars = beforeAll2(async () => {
  230. class User extends Model<InferAttributes<User>, InferCreationAttributes<User>> {
  231. @Attribute(DataTypes.JSON)
  232. declare jsonAttr: any;
  233. }
  234. sequelize.addModels([User]);
  235. await sequelize.sync({ force: true });
  236. return { User };
  237. });
  238. it('supports casting to timestamp types', async () => {
  239. await vars.User.create({
  240. jsonAttr: {
  241. date: new Date('2021-01-02').toISOString(),
  242. },
  243. });
  244. const cast =
  245. dialectName === 'mysql' || dialectName === 'mariadb'
  246. ? 'DATETIME'
  247. : dialectName === 'mssql'
  248. ? 'DATETIMEOFFSET'
  249. : 'TIMESTAMPTZ';
  250. const user = await vars.User.findOne({
  251. where: {
  252. [`jsonAttr.date:unquote::${cast}`]: new Date('2021-01-02'),
  253. },
  254. });
  255. expect(user).to.exist;
  256. const user2 = await vars.User.findOne({
  257. where: {
  258. [`jsonAttr.date:unquote::${cast}`]: {
  259. [Op.between]: [new Date('2021-01-01'), new Date('2021-01-03')],
  260. },
  261. },
  262. });
  263. expect(user2).to.exist;
  264. });
  265. it('supports casting to boolean', async () => {
  266. // These dialects do not have a native BOOLEAN type
  267. if (['mariadb', 'mysql', 'mssql'].includes(dialectName)) {
  268. return;
  269. }
  270. await vars.User.create({
  271. jsonAttr: {
  272. boolean: true,
  273. },
  274. });
  275. const user = await vars.User.findOne({
  276. where: {
  277. 'jsonAttr.boolean:unquote::boolean': true,
  278. },
  279. });
  280. expect(user).to.exist;
  281. });
  282. it('supports casting to numbers', async () => {
  283. await vars.User.create({
  284. jsonAttr: {
  285. integer: 7,
  286. },
  287. });
  288. const cast = dialectName === 'mysql' || dialectName === 'mariadb' ? 'SIGNED' : 'INTEGER';
  289. const user = await vars.User.findOne({
  290. where: {
  291. [`jsonAttr.integer:unquote::${cast}`]: 7,
  292. },
  293. });
  294. expect(user).to.exist;
  295. });
  296. });
  297. describe('JSONB Querying', () => {
  298. if (!dialect.supports.dataTypes.JSONB) {
  299. return;
  300. }
  301. setResetMode('none');
  302. const vars = beforeAll2(async () => {
  303. class User extends Model<InferAttributes<User>, InferCreationAttributes<User>> {
  304. declare id: CreationOptional<number>;
  305. @Attribute(DataTypes.JSONB)
  306. declare objectJsonbAttr: object;
  307. @Attribute(DataTypes.JSONB)
  308. declare stringJsonbAttr: CreationOptional<string>;
  309. }
  310. class Order extends Model<InferAttributes<Order>, InferCreationAttributes<Order>> {
  311. declare id: CreationOptional<number>;
  312. @BelongsTo(() => User, 'userId')
  313. declare user: NonAttribute<User>;
  314. @Attribute(DataTypes.INTEGER)
  315. declare userId: number;
  316. }
  317. sequelize.addModels([User, Order]);
  318. await sequelize.sync({ force: true });
  319. const user = await User.create({
  320. objectJsonbAttr: JSON_OBJECT,
  321. stringJsonbAttr: JSON_STRING,
  322. });
  323. await Order.create({ userId: user.id });
  324. return { User, Order };
  325. });
  326. it('should be able to query using the nested query language', async () => {
  327. const user = await vars.User.findOne({
  328. // JSONB does not require casting
  329. where: { objectJsonbAttr: { name: 'swen' } },
  330. });
  331. expect(user).to.exist;
  332. });
  333. it('should be able to query using the JSON unquote syntax', async () => {
  334. const user = await vars.User.findOne({
  335. where: { 'objectJsonbAttr.name:unquote': 'swen' },
  336. });
  337. expect(user).to.exist;
  338. });
  339. it('should be able to query using dot syntax', async () => {
  340. const user = await vars.User.findOne({
  341. // JSONB does not require casting, nor unquoting
  342. where: { 'objectJsonbAttr.name': 'swen' },
  343. });
  344. expect(user).to.exist;
  345. });
  346. it('should be able retrieve json value with nested include', async () => {
  347. const orders = await vars.Order.findAll({
  348. attributes: ['id'],
  349. include: [
  350. {
  351. model: vars.User,
  352. attributes: [[sql.attribute('objectJsonbAttr.name'), 'name']],
  353. },
  354. ],
  355. });
  356. // @ts-expect-error -- getDataValue's typing does not support custom attributes
  357. expect(orders[0].user.getDataValue('name')).to.equal('swen');
  358. });
  359. it('should be able to check any of these array strings exist as top-level keys', async () => {
  360. const user = await vars.User.findOne({
  361. where: {
  362. objectJsonbAttr: {
  363. [Op.anyKeyExists]: ['name', 'does-not-exist'],
  364. },
  365. },
  366. });
  367. expect(user).to.exist;
  368. });
  369. it('should be able to check all of these array strings exist as top-level keys', async () => {
  370. const user = await vars.User.findOne({
  371. where: {
  372. objectJsonbAttr: {
  373. [Op.allKeysExist]: ['name', 'phones'],
  374. },
  375. },
  376. });
  377. expect(user).to.exist;
  378. });
  379. it('should be able to findOrCreate with values that require escaping', async () => {
  380. const text = 'Multi-line \'$string\' needing "escaping" for $$ and $1 type values';
  381. const [user, created] = await vars.User.findOrCreate({
  382. where: { objectJsonbAttr: { text } },
  383. defaults: { objectJsonbAttr: { text } },
  384. });
  385. expect(created).to.equal(true);
  386. expect(user.isNewRecord).to.equal(false);
  387. const refreshedUser = await vars.User.findOne({
  388. where: { 'objectJsonbAttr.text:unquote': text },
  389. });
  390. expect(refreshedUser).to.exist;
  391. });
  392. });
  393. describe('JSONB Casting', () => {
  394. if (!dialect.supports.dataTypes.JSONB) {
  395. return;
  396. }
  397. setResetMode('truncate');
  398. const vars = beforeAll2(async () => {
  399. class User extends Model<InferAttributes<User>, InferCreationAttributes<User>> {
  400. @Attribute(DataTypes.JSONB)
  401. declare jsonbAttr: any;
  402. }
  403. sequelize.addModels([User]);
  404. await sequelize.sync({ force: true });
  405. return { User };
  406. });
  407. it('supports comparing to json null', async () => {
  408. await vars.User.create({
  409. jsonbAttr: {
  410. // This is JSON null
  411. value: null,
  412. },
  413. });
  414. const user = await vars.User.findOne({
  415. where: {
  416. // Using the 'EQ' operator compares to SQL NULL
  417. 'jsonbAttr.value': { [Op.eq]: null },
  418. },
  419. });
  420. expect(user).to.exist;
  421. });
  422. it('supports comparing to SQL NULL', async () => {
  423. await vars.User.create({
  424. jsonbAttr: {},
  425. });
  426. const user = await vars.User.findOne({
  427. where: {
  428. // Using the 'IS' operator compares to SQL NULL
  429. 'jsonbAttr.value': { [Op.is]: null },
  430. },
  431. });
  432. expect(user).to.exist;
  433. });
  434. it('requires being explicit when comparing to NULL', async () => {
  435. const error = await expect(
  436. vars.User.findOne({
  437. where: {
  438. 'jsonbAttr.value': null,
  439. },
  440. }),
  441. ).to.be.rejected;
  442. expect(inlineErrorCause(error)).to.include('You must be explicit');
  443. });
  444. });