@platformatic/sql-mapper 1.16.0 → 1.18.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 (3) hide show
  1. package/mapper.js +73 -1
  2. package/package.json +4 -4
  3. package/.taprc +0 -1
package/mapper.js CHANGED
@@ -101,7 +101,7 @@ async function createConnectionPool ({ log, connectionString, poolSize, idleTime
101
101
  return { db, sql }
102
102
  }
103
103
 
104
- async function connect ({ connectionString, log, onDatabaseLoad, poolSize, ignore = {}, autoTimestamp = true, hooks = {}, schema, limit = {}, dbschema, cache, idleTimeoutMilliseconds, queueTimeoutMilliseconds, acquireLockTimeoutMilliseconds }) {
104
+ async function connect ({ connectionString, log, onDatabaseLoad, poolSize, include = {}, ignore = {}, autoTimestamp = true, hooks = {}, schema, limit = {}, dbschema, cache, idleTimeoutMilliseconds, queueTimeoutMilliseconds, acquireLockTimeoutMilliseconds }) {
105
105
  if (typeof autoTimestamp === 'boolean' && autoTimestamp === true) {
106
106
  autoTimestamp = defaultAutoTimestampFields
107
107
  }
@@ -164,6 +164,19 @@ async function connect ({ connectionString, log, onDatabaseLoad, poolSize, ignor
164
164
  }
165
165
 
166
166
  const schemaTables = dbschema.map(table => table.table)
167
+ if (Object.keys(include).length) {
168
+ for (const includedTable of Object.keys(include)) {
169
+ if (!schemaTables.includes(includedTable)) {
170
+ const nearestTable = findNearestString(schemaTables, includedTable)
171
+ let warningMessage = `Specified table "${includedTable}" not found.`
172
+ if (nearestTable) {
173
+ warningMessage += ` Did you mean "${nearestTable}"?`
174
+ }
175
+ log.warn(warningMessage)
176
+ }
177
+ }
178
+ }
179
+
167
180
  for (const ignoredTable of Object.keys(ignore)) {
168
181
  if (!schemaTables.includes(ignoredTable)) {
169
182
  const nearestTable = findNearestString(schemaTables, ignoredTable)
@@ -182,6 +195,10 @@ async function connect ({ connectionString, log, onDatabaseLoad, poolSize, ignor
182
195
  if (typeof table !== 'string') {
183
196
  throw new errors.TableMustBeAStringError(table)
184
197
  }
198
+ // If include is being used and a table is not explicitly included add it to the ignore object
199
+ if (Object.keys(include).length && !include[table]) {
200
+ ignore[table] = true
201
+ }
185
202
  if (ignore[table] === true) {
186
203
  continue
187
204
  }
@@ -258,9 +275,64 @@ async function sqlMapper (app, opts) {
258
275
  })
259
276
  }
260
277
 
278
+ async function dropTable (db, sql, table) {
279
+ try {
280
+ if (db.isSQLite) {
281
+ await db.query(sql`DROP TABLE ${sql(table)};`)
282
+ } else {
283
+ await db.query(sql`DROP TABLE ${sql(table)} CASCADE;`)
284
+ }
285
+ return table
286
+ } catch (err) {
287
+ // ignore, it will be dropped on the next roundon the next roundon the next roundon the next round
288
+ }
289
+ }
290
+
291
+ async function dropAllTables (db, sql, schemas) {
292
+ let queries
293
+ /* istanbul ignore next */
294
+ if (db.isPg) {
295
+ queries = queriesFactory.pg
296
+ } else if (db.isMySql) {
297
+ queries = queriesFactory.mysql
298
+ } else if (db.isMariaDB) {
299
+ queries = queriesFactory.mariadb
300
+ } else if (db.isSQLite) {
301
+ queries = queriesFactory.sqlite
302
+ }
303
+
304
+ const tables = new Set((await queries.listTables(db, sql, schemas)).map((t) => {
305
+ /* istanbul ignore next */
306
+ if (t.schema) {
307
+ return `${t.schema}.${t.table}`
308
+ }
309
+ return t.table
310
+ }))
311
+ let count = 0
312
+
313
+ while (tables.size > 0) {
314
+ if (count++ > 100) {
315
+ throw new Error('too many iterations, unable to clear the db')
316
+ }
317
+
318
+ const deletes = []
319
+ for (const table of tables) {
320
+ deletes.push(dropTable(db, sql, table))
321
+ }
322
+
323
+ const res = await Promise.all(deletes)
324
+ for (const table of res) {
325
+ if (table) {
326
+ tables.delete(table)
327
+ }
328
+ }
329
+ }
330
+ }
331
+
261
332
  module.exports = fp(sqlMapper)
262
333
  module.exports.connect = connect
263
334
  module.exports.createConnectionPool = createConnectionPool
264
335
  module.exports.plugin = module.exports
265
336
  module.exports.utils = require('./lib/utils')
266
337
  module.exports.errors = errors
338
+ module.exports.dropAllTables = dropAllTables
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/sql-mapper",
3
- "version": "1.16.0",
3
+ "version": "1.18.0",
4
4
  "description": "A data mapper utility for SQL databases",
5
5
  "main": "mapper.js",
6
6
  "types": "mapper.d.ts",
@@ -20,7 +20,7 @@
20
20
  "glob": "^10.3.10",
21
21
  "snazzy": "^9.0.0",
22
22
  "standard": "^17.1.0",
23
- "tsd": "^0.29.0"
23
+ "tsd": "^0.30.0"
24
24
  },
25
25
  "dependencies": {
26
26
  "@databases/mysql": "^6.0.0",
@@ -33,14 +33,14 @@
33
33
  "camelcase": "^6.3.0",
34
34
  "fastify-plugin": "^4.5.1",
35
35
  "inflected": "^2.1.0",
36
- "@platformatic/utils": "1.16.0"
36
+ "@platformatic/utils": "1.18.0"
37
37
  },
38
38
  "tsd": {
39
39
  "directory": "test/types"
40
40
  },
41
41
  "scripts": {
42
42
  "lint": "standard | snazzy",
43
- "test": "standard | snazzy && npm run test:typescript && npm run test:postgresql && npm run test:mariadb && npm run test:mysql && npm run test:mysql8 && npm run test:sqlite",
43
+ "test": "npm run lint && npm run test:typescript && npm run test:postgresql && npm run test:mariadb && npm run test:mysql && npm run test:mysql8 && npm run test:sqlite",
44
44
  "test:runner": "node ./test/runner.js",
45
45
  "test:postgresql": "DB=postgresql npm run test:runner",
46
46
  "test:mariadb": "DB=mariadb npm run test:runner",
package/.taprc DELETED
@@ -1 +0,0 @@
1
- jobs: 1