@platformatic/sql-mapper 1.1.1 → 1.3.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/cache.js +46 -0
- package/mapper.d.ts +8 -0
- package/mapper.js +10 -3
- package/package.json +3 -2
package/lib/cache.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
const { createCache } = require('async-cache-dedupe')
|
|
3
|
+
|
|
4
|
+
function setupCache (res, opts) {
|
|
5
|
+
// TODO validate opts
|
|
6
|
+
if (opts === true) {
|
|
7
|
+
opts = { ttl: 0 }
|
|
8
|
+
} else {
|
|
9
|
+
// ttl=0 means dedupe only
|
|
10
|
+
// TODO remove it to implement full cache features
|
|
11
|
+
opts.ttl = 0
|
|
12
|
+
}
|
|
13
|
+
const { entities, addEntityHooks } = res
|
|
14
|
+
|
|
15
|
+
const cache = createCache(opts)
|
|
16
|
+
for (const entity of Object.values(entities)) {
|
|
17
|
+
const fnName = `${entity.name}Find`
|
|
18
|
+
const originalFn = entity.find
|
|
19
|
+
|
|
20
|
+
cache.define(fnName, {
|
|
21
|
+
serialize (query) {
|
|
22
|
+
const serialized = {
|
|
23
|
+
...query,
|
|
24
|
+
ctx: undefined
|
|
25
|
+
}
|
|
26
|
+
return serialized
|
|
27
|
+
}
|
|
28
|
+
}, async function (query) {
|
|
29
|
+
const res = await originalFn.call(entity, query)
|
|
30
|
+
return res
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
addEntityHooks(entity.singularName, {
|
|
34
|
+
find (originalFn, query) {
|
|
35
|
+
if (query.tx) {
|
|
36
|
+
return originalFn(query)
|
|
37
|
+
}
|
|
38
|
+
return cache[fnName](query)
|
|
39
|
+
}
|
|
40
|
+
})
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return cache
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
module.exports = setupCache
|
package/mapper.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { FastifyPluginAsync, FastifyInstance, FastifyReply, FastifyRequest } from 'fastify'
|
|
2
2
|
import { SQL, SQLQuery } from '@databases/sql'
|
|
3
3
|
import { FastifyError } from '@fastify/error'
|
|
4
|
+
import { createCache } from 'async-cache-dedupe'
|
|
5
|
+
|
|
6
|
+
type cacheOptions = (Parameters<typeof createCache>[0]) | boolean
|
|
4
7
|
|
|
5
8
|
interface ILogger {
|
|
6
9
|
trace(): any,
|
|
@@ -347,6 +350,11 @@ export interface SQLMapperPluginOptions extends BasePoolOptions {
|
|
|
347
350
|
* An async function that is called after the connection is established.
|
|
348
351
|
*/
|
|
349
352
|
onDatabaseLoad?(db: Database, sql: SQL): any,
|
|
353
|
+
/**
|
|
354
|
+
* Query caching Configuration
|
|
355
|
+
* @default false
|
|
356
|
+
*/
|
|
357
|
+
cache?: cacheOptions
|
|
350
358
|
}
|
|
351
359
|
|
|
352
360
|
export interface Entities {
|
package/mapper.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
+
const fp = require('fastify-plugin')
|
|
3
4
|
const { findNearestString } = require('@platformatic/utils')
|
|
4
5
|
const buildEntity = require('./lib/entity')
|
|
5
6
|
const buildCleanUp = require('./lib/clean-up')
|
|
6
7
|
const queriesFactory = require('./lib/queries')
|
|
7
|
-
const fp = require('fastify-plugin')
|
|
8
8
|
const { areSchemasSupported } = require('./lib/utils')
|
|
9
9
|
const errors = require('./lib/errors')
|
|
10
|
+
const setupCache = require('./lib/cache')
|
|
10
11
|
|
|
11
12
|
// Ignore the function as it is only used only for MySQL and PostgreSQL
|
|
12
13
|
/* istanbul ignore next */
|
|
@@ -97,7 +98,7 @@ async function createConnectionPool ({ log, connectionString, poolSize }) {
|
|
|
97
98
|
return { db, sql }
|
|
98
99
|
}
|
|
99
100
|
|
|
100
|
-
async function connect ({ connectionString, log, onDatabaseLoad, poolSize, ignore = {}, autoTimestamp = true, hooks = {}, schema, limit = {}, dbschema }) {
|
|
101
|
+
async function connect ({ connectionString, log, onDatabaseLoad, poolSize, ignore = {}, autoTimestamp = true, hooks = {}, schema, limit = {}, dbschema, cache }) {
|
|
101
102
|
if (typeof autoTimestamp === 'boolean' && autoTimestamp === true) {
|
|
102
103
|
autoTimestamp = defaultAutoTimestampFields
|
|
103
104
|
}
|
|
@@ -197,7 +198,7 @@ async function connect ({ connectionString, log, onDatabaseLoad, poolSize, ignor
|
|
|
197
198
|
}
|
|
198
199
|
}
|
|
199
200
|
|
|
200
|
-
|
|
201
|
+
const res = {
|
|
201
202
|
db,
|
|
202
203
|
sql,
|
|
203
204
|
entities,
|
|
@@ -205,6 +206,12 @@ async function connect ({ connectionString, log, onDatabaseLoad, poolSize, ignor
|
|
|
205
206
|
addEntityHooks,
|
|
206
207
|
dbschema
|
|
207
208
|
}
|
|
209
|
+
|
|
210
|
+
if (cache) {
|
|
211
|
+
res.cache = setupCache(res, cache)
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
return res
|
|
208
215
|
} catch (err) /* istanbul ignore next */ {
|
|
209
216
|
db.dispose()
|
|
210
217
|
throw err
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@platformatic/sql-mapper",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "A data mapper utility for SQL databases",
|
|
5
5
|
"main": "mapper.js",
|
|
6
6
|
"types": "mapper.d.ts",
|
|
@@ -28,10 +28,11 @@
|
|
|
28
28
|
"@fastify/error": "^3.3.0",
|
|
29
29
|
"@hapi/topo": "^6.0.2",
|
|
30
30
|
"@matteo.collina/sqlite-pool": "^0.3.0",
|
|
31
|
+
"async-cache-dedupe": "^2.0.0",
|
|
31
32
|
"camelcase": "^6.3.0",
|
|
32
33
|
"fastify-plugin": "^4.5.1",
|
|
33
34
|
"inflected": "^2.1.0",
|
|
34
|
-
"@platformatic/utils": "1.
|
|
35
|
+
"@platformatic/utils": "1.3.0"
|
|
35
36
|
},
|
|
36
37
|
"tsd": {
|
|
37
38
|
"directory": "test/types"
|