@platformatic/sql-mapper 0.20.0 → 0.21.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.
package/lib/entity.js CHANGED
@@ -206,6 +206,15 @@ function createMapper (defaultDb, sql, log, table, fields, primaryKeys, relation
206
206
  const where = opts.where || {}
207
207
  const criteria = []
208
208
  for (const key of Object.keys(where)) {
209
+ if (key === 'or') {
210
+ const orCriteria = []
211
+ for (const orPart of where[key]) {
212
+ const cret = computeCriteria({ where: orPart })
213
+ orCriteria.push(sql`(${sql.join(cret, sql` AND `)})`)
214
+ }
215
+ criteria.push(sql`(${sql.join(orCriteria, sql` OR `)})`)
216
+ continue
217
+ }
209
218
  const value = where[key]
210
219
  const field = inputToFieldMap[key]
211
220
  for (const key of Object.keys(value)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/sql-mapper",
3
- "version": "0.20.0",
3
+ "version": "0.21.0",
4
4
  "description": "A data mapper utility for SQL databases",
5
5
  "main": "mapper.js",
6
6
  "repository": {
@@ -0,0 +1,193 @@
1
+ 'use strict'
2
+
3
+ const { test } = require('tap')
4
+ const { connect } = require('..')
5
+ const { clear, connInfo, isSQLite, isMysql } = require('./helper')
6
+ const fakeLogger = {
7
+ trace: () => { },
8
+ error: () => { }
9
+ }
10
+
11
+ test('where clause with or operation', async ({ pass, teardown, same }) => {
12
+ const mapper = await connect({
13
+ ...connInfo,
14
+ autoTimestamp: true,
15
+ log: fakeLogger,
16
+ async onDatabaseLoad (db, sql) {
17
+ teardown(() => db.dispose())
18
+ pass('onDatabaseLoad called')
19
+
20
+ await clear(db, sql)
21
+
22
+ if (isSQLite) {
23
+ await db.query(sql`CREATE TABLE posts (
24
+ id INTEGER PRIMARY KEY,
25
+ title VARCHAR(42),
26
+ long_text TEXT,
27
+ counter INTEGER
28
+ );`)
29
+ } else if (isMysql) {
30
+ await db.query(sql`CREATE TABLE posts (
31
+ id SERIAL PRIMARY KEY,
32
+ title VARCHAR(42),
33
+ long_text TEXT,
34
+ counter INTEGER
35
+ );`)
36
+ } else {
37
+ await db.query(sql`CREATE TABLE posts (
38
+ id SERIAL PRIMARY KEY,
39
+ title VARCHAR(42),
40
+ long_text TEXT,
41
+ counter INTEGER
42
+ );`)
43
+ }
44
+ }
45
+ })
46
+
47
+ const entity = mapper.entities.post
48
+
49
+ const posts = [{
50
+ title: 'Dog',
51
+ longText: 'Foo',
52
+ counter: 10
53
+ }, {
54
+ title: 'Cat',
55
+ longText: 'Bar',
56
+ counter: 20
57
+ }, {
58
+ title: 'Mouse',
59
+ longText: 'Baz',
60
+ counter: 30
61
+ }, {
62
+ title: 'Duck',
63
+ longText: 'A duck tale',
64
+ counter: 40
65
+ }]
66
+
67
+ await entity.insert({
68
+ inputs: posts
69
+ })
70
+
71
+ {
72
+ const data = await entity.find({
73
+ where: {
74
+ or: [
75
+ {
76
+ counter: {
77
+ eq: 10
78
+ }
79
+ },
80
+ {
81
+ counter: {
82
+ eq: 20
83
+ }
84
+ }
85
+ ]
86
+ }
87
+ })
88
+
89
+ same(data, [
90
+ { id: '1', title: 'Dog', longText: 'Foo', counter: 10 },
91
+ { id: '2', title: 'Cat', longText: 'Bar', counter: 20 }
92
+ ])
93
+ }
94
+
95
+ {
96
+ const data = await entity.find({
97
+ where: {
98
+ or: [
99
+ {
100
+ counter: {
101
+ eq: 20
102
+ }
103
+ },
104
+ {
105
+ counter: {
106
+ gte: 30
107
+ }
108
+ }
109
+ ]
110
+ }
111
+ })
112
+
113
+ same(data, [
114
+ { id: '2', title: 'Cat', longText: 'Bar', counter: 20 },
115
+ { id: '3', title: 'Mouse', longText: 'Baz', counter: 30 },
116
+ { id: '4', title: 'Duck', longText: 'A duck tale', counter: 40 }
117
+ ])
118
+ }
119
+
120
+ {
121
+ const data = await entity.find({
122
+ where: {
123
+ or: [
124
+ {
125
+ title: {
126
+ eq: 'Dog'
127
+ }
128
+ },
129
+ {
130
+ title: {
131
+ eq: 'Duck'
132
+ }
133
+ }
134
+ ]
135
+ }
136
+ })
137
+
138
+ same(data, [
139
+ { id: '1', title: 'Dog', longText: 'Foo', counter: 10 },
140
+ { id: '4', title: 'Duck', longText: 'A duck tale', counter: 40 }
141
+ ])
142
+ }
143
+
144
+ {
145
+ const data = await entity.find({
146
+ where: {
147
+ or: [
148
+ {
149
+ title: {
150
+ eq: 'Dog'
151
+ }
152
+ },
153
+ {
154
+ longText: {
155
+ eq: 'Baz'
156
+ }
157
+ }
158
+ ]
159
+ }
160
+ })
161
+
162
+ same(data, [
163
+ { id: '1', title: 'Dog', longText: 'Foo', counter: 10 },
164
+ { id: '3', title: 'Mouse', longText: 'Baz', counter: 30 }
165
+ ])
166
+ }
167
+
168
+ {
169
+ const data = await entity.find({
170
+ where: {
171
+ counter: {
172
+ in: [10, 20]
173
+ },
174
+ or: [
175
+ {
176
+ title: {
177
+ eq: 'Dog'
178
+ }
179
+ },
180
+ {
181
+ longText: {
182
+ eq: 'Baz'
183
+ }
184
+ }
185
+ ]
186
+ }
187
+ })
188
+
189
+ same(data, [
190
+ { id: '1', title: 'Dog', longText: 'Foo', counter: 10 }
191
+ ])
192
+ }
193
+ })