New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@platformatic/sql-mapper

Package Overview
Dependencies
Maintainers
6
Versions
332
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@platformatic/sql-mapper - npm Package Compare versions

Comparing version

to
0.41.0

test/create-connection.test.js

4

lib/entity.js

@@ -226,2 +226,5 @@ 'use strict'

const field = inputToFieldMap[key]
if (!field) {
throw new Error(`Unknown field ${key}`)
}
for (const key of Object.keys(value)) {

@@ -235,3 +238,2 @@ const operator = whereMap[key]

const fieldWrap = fields[field]
/* istanbul ignore next */

@@ -238,0 +240,0 @@ if (fieldWrap.isArray) {

@@ -280,3 +280,3 @@ import { FastifyPluginAsync, FastifyInstance, FastifyReply, FastifyRequest } from 'fastify'

export interface SQLMapperPluginOptions {
interface BasePoolOptions {
/**

@@ -286,11 +286,30 @@ * Database connection string.

connectionString: string,
/**
* Set to true to enable auto timestamping for updated_at and inserted_at fields.
* The maximum number of connections to create at once. Default is 10.
* @default 10
*/
autoTimestamp?: boolean,
poolSize?: number
}
export interface CreateConnectionPoolOptions extends BasePoolOptions {
/**
* A logger object (like [Pino](https://getpino.io))
*/
log: ILogger
}
export function createConnectionPool(options: CreateConnectionPoolOptions) : Promise<{ db: Database, sql: SQL }>
export interface SQLMapperPluginOptions extends BasePoolOptions {
/**
* A logger object (like [Pino](https://getpino.io))
*/
log?: ILogger,
/**
* Set to true to enable auto timestamping for updated_at and inserted_at fields.
*/
autoTimestamp?: boolean,
/**
* Database table to ignore when mapping to entities.

@@ -297,0 +316,0 @@ */

@@ -50,15 +50,8 @@ 'use strict'

async function connect ({ connectionString, log, onDatabaseLoad, poolSize = 10, ignore = {}, autoTimestamp = true, hooks = {}, schema, limit = {}, dbschema }) {
if (typeof autoTimestamp === 'boolean' && autoTimestamp === true) {
autoTimestamp = defaultAutoTimestampFields
}
// TODO validate config using the schema
if (!connectionString) {
throw new Error('connectionString is required')
}
let queries
async function createConnectionPool ({ log, connectionString, poolSize }) {
let db
let sql
let db
poolSize = poolSize || 10
/* istanbul ignore next */

@@ -69,3 +62,2 @@ if (connectionString.indexOf('postgres') === 0) {

sql = createConnectionPoolPg.sql
queries = queriesFactory.pg
db.isPg = true

@@ -79,7 +71,4 @@ } else if (connectionString.indexOf('mysql') === 0) {

db.isMariaDB = version.indexOf('maria') !== -1
if (db.isMariaDB) {
queries = queriesFactory.mariadb
} else {
if (!db.isMariaDB) {
db.isMySql = true
queries = queriesFactory.mysql
}

@@ -104,3 +93,2 @@ } else if (connectionString.indexOf('sqlite') === 0) {

sql = sqlite.sql
queries = queriesFactory.sqlite
db.isSQLite = true

@@ -111,2 +99,28 @@ } else {

return { db, sql }
}
async function connect ({ connectionString, log, onDatabaseLoad, poolSize, ignore = {}, autoTimestamp = true, hooks = {}, schema, limit = {}, dbschema }) {
if (typeof autoTimestamp === 'boolean' && autoTimestamp === true) {
autoTimestamp = defaultAutoTimestampFields
}
// TODO validate config using the schema
if (!connectionString) {
throw new Error('connectionString is required')
}
let queries
const { db, sql } = await createConnectionPool({ log, connectionString, poolSize })
/* istanbul ignore next */
if (db.isPg) {
queries = queriesFactory.pg
} else if (db.isMySql) {
queries = queriesFactory.mysql
} else if (db.isMariaDB) {
queries = queriesFactory.mariadb
} else if (db.isSQLite) {
queries = queriesFactory.sqlite
}
// Specify an empty array must be the same of specifying no schema

@@ -230,3 +244,4 @@ /* istanbul ignore next */ // Ignoring because this won't be fully covered by DB not supporting schemas (SQLite)

module.exports.connect = connect
module.exports.createConnectionPool = createConnectionPool
module.exports.plugin = module.exports
module.exports.utils = require('./lib/utils')
{
"name": "@platformatic/sql-mapper",
"version": "0.40.0",
"version": "0.41.0",
"description": "A data mapper utility for SQL databases",

@@ -22,3 +22,3 @@ "main": "mapper.js",

"tap": "^16.3.6",
"tsd": "^0.28.1"
"tsd": "^0.29.0"
},

@@ -34,3 +34,3 @@ "dependencies": {

"inflected": "^2.1.0",
"@platformatic/types": "0.40.0"
"@platformatic/types": "0.41.0"
},

@@ -37,0 +37,0 @@ "tsd": {

@@ -13,5 +13,12 @@ import { expectType } from 'tsd'

EntityHooks,
createConnectionPool
} from '../../mapper'
const pluginOptions: SQLMapperPluginInterface = await connect({ connectionString: '' })
const log = {
trace() {},
error() {},
warn() {}
}
const pluginOptions: SQLMapperPluginInterface = await connect({ connectionString: '', log })
expectType<Database>(pluginOptions.db)

@@ -53,13 +60,14 @@ expectType<SQL>(pluginOptions.sql)

expectType<EntityHooks>(entityHooks)
expectType<SQLMapperPluginInterface>(await connect({ connectionString: '' }))
expectType<SQLMapperPluginInterface>(await connect({ connectionString: '', autoTimestamp: true }))
expectType<SQLMapperPluginInterface>(await connect({ connectionString: '', hooks: {} }))
expectType<SQLMapperPluginInterface>(await connect({ connectionString: '', log }))
expectType<SQLMapperPluginInterface>(await connect({ connectionString: '', autoTimestamp: true, log }))
expectType<SQLMapperPluginInterface>(await connect({ connectionString: '', hooks: {}, log }))
expectType<SQLMapperPluginInterface>(await connect({
connectionString: '', hooks: {
Page: entityHooks
}
},
log
}))
expectType<SQLMapperPluginInterface>(await connect({ connectionString: '', ignore: {} }))
expectType<SQLMapperPluginInterface>(await connect({ connectionString: '', ignore: {}, log }))
expectType<SQLMapperPluginInterface>(await connect({
connectionString: '', onDatabaseLoad(db: Database, sql: SQL) {
connectionString: '', log, onDatabaseLoad(db: Database, sql: SQL) {
expectType<(query: SQLQuery) => Promise<any[]>>(db.query)

@@ -100,1 +108,3 @@ expectType<() => Promise<void>>(db.dispose)

expectType<(str: string) => string>(utils.toSingular)
expectType<Promise<{ db: Database, sql: SQL }>>(createConnectionPool({ connectionString: '', log }))

@@ -7,2 +7,3 @@ 'use strict'

const { setTimeout } = require('timers/promises')
const fakeLogger = {

@@ -9,0 +10,0 @@ trace: () => {},

@@ -6,2 +6,3 @@ 'use strict'

const { clear, connInfo, isMysql, isSQLite } = require('./helper')
const fakeLogger = {

@@ -12,3 +13,3 @@ trace: () => {},

test('list', async ({ pass, teardown, same, equal }) => {
test('list', async ({ pass, teardown, same, rejects }) => {
const mapper = await connect({

@@ -65,2 +66,4 @@ ...connInfo,

rejects(entity.find.bind(entity, { where: { invalidField: { eq: 'Dog' } } }), 'Unknown field invalidField')
same(await entity.find({ where: { title: { eq: 'Dog' } }, fields: ['id', 'title', 'longText'] }), [{

@@ -67,0 +70,0 @@ id: '1',