@evs-chris/ts-pg-dao
Advanced tools
Comparing version
{ | ||
"name": "@evs-chris/ts-pg-dao", | ||
"version": "0.8.5", | ||
"version": "0.8.6", | ||
"main": "runtime/main.js", | ||
@@ -5,0 +5,0 @@ "typings": "runtime/main.d.ts", |
@@ -8,2 +8,4 @@ import * as pg from 'pg'; | ||
name?: string; | ||
stringyDates?: boolean; | ||
tzTimestamps?: boolean; | ||
} | ||
@@ -81,3 +83,4 @@ export declare type BuiltConfig = Config & { | ||
query(options: QueryOptions): Query; | ||
select(alias?: string): string; | ||
select(config: Config, alias?: string): string; | ||
cast(config: Config, col: Column): string; | ||
hook(name: keyof Hooks, fn: Hook): Model; | ||
@@ -202,3 +205,3 @@ imports(...descriptors: string[]): Model; | ||
name?: string; | ||
_cache: any; | ||
_cache?: any; | ||
}; | ||
@@ -205,0 +208,0 @@ export interface SchemaConfig { |
@@ -42,10 +42,17 @@ "use strict"; | ||
} | ||
select(alias = '') { | ||
select(config, alias = '') { | ||
return this.cols.map(c => { | ||
if (alias) | ||
return `"${alias}"."${c.name}"${c.cast ? `::${c.cast}` : ''} "${alias}__${c.name}"`; | ||
return `"${alias}"."${c.name}"${this.cast(config, c)} "${alias}__${c.name}"`; | ||
else | ||
return `"${c.name}"${c.cast ? `::${c.cast}` : ''}`; | ||
return `"${c.name}"${this.cast(config, c)}`; | ||
}).join(', '); | ||
} | ||
cast(config, col) { | ||
if (col.cast) | ||
return `::${col.cast}`; | ||
if (config.tzTimestamps && col.pgtype === 'timestamp') | ||
return `::timestamptz`; | ||
return ''; | ||
} | ||
hook(name, fn) { | ||
@@ -52,0 +59,0 @@ this.hooks[name] = fn; |
@@ -172,2 +172,5 @@ "use strict"; | ||
export default class ${model.name} { | ||
constructor(props?: Partial<${model.name}>) { | ||
if (props) Object.assign(this, props); | ||
} | ||
static get table() { return ${JSON.stringify(model.table)}; } | ||
@@ -202,3 +205,3 @@ `; | ||
if (${model.fields.filter(f => f.pkey || f.optlock).map(f => `model.${f.name} !== undefined`).join(' && ')}) {${updateMembers(model, ' ')} | ||
if (${model.fields.filter(f => f.pkey || f.optlock).map(f => `model.${f.name} !== undefined`).join(' && ')}) {${updateMembers(config, model, ' ')} | ||
@@ -235,3 +238,3 @@ const transact = !con.inTransaction; | ||
const params = [${model.fields.filter(f => f.pkey || f.optlock).map(f => `model.${f.name}`).join(', ')}]; | ||
const res = await con.query(\`delete from "${model.table}" where ${model.fields.filter(f => f.pkey || f.optlock).map((f, i) => `${f.optlock ? `$\{params[${i}] == null ? \`"${f.name}" is null\` : \`date_trunc('millisecond', "${f.name}")\`}` : `"${f.name}"`} = $${i + 1}`).join(' AND ')}\`, params); | ||
const res = await con.query(\`delete from "${model.table}" where ${model.fields.filter(f => f.pkey || f.optlock).map((f, i) => `${f.optlock ? `$\{params[${i}] == null ? \`"${f.name}" is null\` : \`date_trunc('millisecond', "${f.name}"${model.cast(config, f)})\`}` : `"${f.name}"`} = $${i + 1}`).join(' AND ')}\`, params); | ||
if (res.rowCount < 1) throw new Error('No matching row to delete for ${model.name}'); | ||
@@ -283,3 +286,3 @@ if (res.rowCount > 1) throw new Error('Too many matching rows deleted for ${model.name}'); | ||
static async findAll(con: dao.Connection, where: string = '', params: any[] = []): Promise<${model.name}[]> { | ||
const res = await con.query('select ${model.select()} from "${model.table}"' + (where ? ' WHERE ' + where : ''), params); | ||
const res = await con.query('select ${model.select(config)} from "${model.table}"' + (where ? ' WHERE ' + where : ''), params); | ||
return res.rows.map(r => ${model.name}.load(r, new ${model.name}())); | ||
@@ -369,2 +372,11 @@ } | ||
} | ||
function stringyDates(config, type) { | ||
if (!config.stringyDates) | ||
return type; | ||
if (type === 'Date') | ||
return 'Date|string'; | ||
if (type === 'Date[]') | ||
return 'Array<Date|string>'; | ||
return type; | ||
} | ||
function modelProps(config, model, client = false) { | ||
@@ -375,3 +387,3 @@ let tpl = ''; | ||
col = model.cols[c]; | ||
tpl += ` ${col.alias || col.name}${col.nullable ? '?' : ''}: ${col.enum ? col.enum.map(v => `'${v}'`).join('|') : (col.retype || col.type)}${col.default ? ` = ${col.default}` : ''};\n`; | ||
tpl += ` ${col.alias || col.name}${col.nullable ? '?' : ''}: ${col.enum ? col.enum.map(v => `'${v}'`).join('|') : stringyDates(config, col.retype || col.type)}${col.default ? ` = ${col.default}` : ''};\n`; | ||
} | ||
@@ -389,3 +401,3 @@ if (Object.keys(model._extras).length > 0) { | ||
} | ||
function updateMembers(model, prefix) { | ||
function updateMembers(config, model, prefix) { | ||
let res = `\n${prefix}const params = [];\n${prefix}const sets = [];\n${prefix}let sql = 'UPDATE ${model.table} SET ';`; | ||
@@ -408,3 +420,3 @@ const lock = model.fields.find(f => f.optlock); | ||
res += `\n${prefix}params.push(${where.map(f => `model.${f.alias || f.name}`).join(', ')});`; | ||
res += `\n${prefix}sql += \` WHERE ${where.map((f, i) => `${f.optlock ? `date_trunc('millisecond', "${f.name}")` : `"${f.name}"`} = $\${count + ${i + 1}}`).join(' AND ')}\`;`; | ||
res += `\n${prefix}sql += \` WHERE ${where.map((f, i) => `${f.optlock ? `date_trunc('millisecond', "${f.name}"${model.cast(config, f)})` : `"${f.name}"`} = $\${count + ${i + 1}}`).join(' AND ')}\`;`; | ||
return res; | ||
@@ -527,3 +539,3 @@ } | ||
if (col === '*') { | ||
return (entry.cols || entry.model.cols).map(c => `${alias}.${c.name}${c.cast ? `::${c.cast}` : ''} AS ${entry.prefix}${c.name}`).join(', '); | ||
return (entry.cols || entry.model.cols).map(c => `${alias}.${c.name}${mdl.cast(config, c)} AS ${entry.prefix}${c.name}`).join(', '); | ||
} | ||
@@ -538,3 +550,3 @@ else { | ||
else { | ||
return `${alias}.${c.name}${c.cast ? `::${c.cast}` : ''} AS ${entry.prefix}${c.name}`; | ||
return `${alias}.${c.name}${mdl.cast(config, c)} AS ${entry.prefix}${c.name}`; | ||
} | ||
@@ -541,0 +553,0 @@ } |
104605
0.69%2197
1.01%