-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.mjs
510 lines (438 loc) · 15.3 KB
/
model.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/**
* **Created on 29/01/2023**
*
* src/apps/inventory/models/inventory.model.mjs
* @author André Timermann <andre@timermann.com.br>
*
* Modelo foi criado para ser usado pela API e não pelo banco de dados, então as proprieadades devem estar pronta para
* consumo pela api.
*
* exemplo: console.log(this) retorna os dados já no formato usado pela API
*
* Dados devem ser formatados para o banco no momento da gravação na base (MODEM => SERVICE)
* Utilizar o método:
* getFormattedDataForDatabase()
*
* === Transaction with Prisma ===
* https://www.prisma.io/docs/guides/performance-and-optimization/prisma-client-transactions-guide#interactive-transactions
*
* TODO: Criar opção para Habilitar Decimal do Prisma, quebra validação
* TODO: Error: 'inventory'.tax should be number (Esta validando NULL)
*
*
*/
/**
* @typedef {object} Options
* @description Objeto de configuração
*
* @property {boolean} [validate=true] - Indica se a validação deve ser feita ao criar a instância.
* @property {boolean} [ignoreRequired=false] - Se true, ignora validação de campo obrigatório (útil para update parcial)
* @property {boolean} [validateDeep=false] - Se true, Realiza validação profunda, ou seja, das subinstancias relacionada
*/
import isObject from 'lodash/isObject.js'
import cloneDeep from 'lodash/cloneDeep.js'
import Ajv from 'ajv'
import Decimal from 'decimal.js'
export default class NodeModel {
/**
* Instancia de PrismaClient, obrigatório ser definodo pelo usuário
* @type {PrismaClient<Prisma.PrismaClientOptions, "log" extends keyof Prisma.PrismaClientOptions ? (Prisma.PrismaClientOptions["log"] extends Array<Prisma.LogLevel|Prisma.LogDefinition> ? Prisma.GetEvents<Prisma.PrismaClientOptions["log"]> : never) : never, "rejectOnNotFound" extends keyof Prisma.PrismaClientOptions ? Prisma.PrismaClientOptions["rejectOnNotFound"] : false>}
*/
static prismaClient = null
/**
* Prisma schema model relacionada a este model
* Normalmente defino em schama.prisma
* Ref: https://www.prisma.io/docs/concepts/components/prisma-client/working-with-prismaclient/use-custom-model-and-field-names
*
* **Nota:** Anterormente era tableName, porém o prisma já realiza o mapeamento com banco de dados no schamea.prisma
* devemos agora definor o nome do model prisma relacionado com este model
*
* @type {String}
*/
static prismaModel = null
/**
* Lista de atributos da instância da classe que não são visíveis por padrão.
*
* @type {Array}
*/
static hidden = []
static relations = {}
/**
* Schema de validação usado pelo ADV
* REF: https://ajv.js.org/
* @type {null}
*/
static schema = null
/**
* Objeto de validação, gerado da primeira vez que o validator é chamado
* @type {import("ajv").ValidateFunction}
* @private
*/
static _validatorCache
/**
* Validador ignorando se é obrigatório (Útil para update)
* @private
*/
static _validatorIgnoreRequiredCache
/**
* Dicionario cacheado que mapeia as chaves estrangeiras para o nome da relação usado pelo nestResult
*
* @type {*}
* @private
*/
static _nestedConvertionDictCache = undefined
static _flattenConvertionDictCache = undefined
/// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Métodos estáticos
/// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Retorna instancia de prisma client já com model definido, chamando de service
*
* exemplo para model user:
*
* this.service.findMany()
*
* é equivalente a:
* - prisma.user.findMany()
* - this.prisma.user.findMany()
* - this.prisma[this.prismaModel].findMany()
*
* REFS:
* https://www.prisma.io/docs/concepts/components/prisma-client/custom-models
* https://www.prisma.io/docs/getting-started/quickstart#41-create-a-new-user-record
*
* @returns {*}
*/
static get Service () {
if (!this.prismaModel) throw new Error('Static property prismaModel not defined.')
return this.prisma[this.prismaModel]
}
/**
* Retorna objeto prismaClient para montagem de consultas
*
* REF: https://www.prisma.io/docs/concepts/components/prisma-client
*
* @returns {PrismaClient<Prisma.PrismaClientOptions, "log" extends keyof Prisma.PrismaClientOptions ? (Prisma.PrismaClientOptions["log"] extends Array<Prisma.LogLevel|Prisma.LogDefinition> ? Prisma.GetEvents<Prisma.PrismaClientOptions["log"]> : never) : never, "rejectOnNotFound" extends keyof Prisma.PrismaClientOptions ? Prisma.PrismaClientOptions["rejectOnNotFound"] : false>}
*/
static get prisma () {
const errorExampleText = `
Create a base class by extending PrismaModel and define prismaClient like this:
import { PrismaClient } from '@prisma/client'
export default class Model extends PrismaModel {
static prismaClient = new PrismaClient()
}
`
if (!this.prismaClient) {
throw new Error(`Static property "prismaClient" not defined. ${errorExampleText}`)
}
if (!this.prismaClient._clientVersion) {
throw new Error(`Static property "prismaClient" is invalid. ${errorExampleText}`)
}
return this.prismaClient
}
/**
* Cria uma instância da classe.
*
* @param {object} data - Os dados para definir na instância.
* @param {Options} [options] - As opções para criar a instância.
* @returns {PrismaModel} - A nova instância da classe.
*/
static create (data, options) {
const instance = new this()
instance.setValues(data, options)
instance._makeGettersAndSettersEnumerable()
return instance
}
/**
* Atalho para método "create" ajustado para atualização
*
* - ignoreRequired definido true
* - Id pode ser enviado pro argumento separadamente
*
* @param id
* @param data
* @param {Options} [options] - As opções para criar a instância.
* @returns {Model}
*/
static createForUpdate (id, data, options) {
return this.create({ ...data, id }, { ...options, ignoreRequired: true })
}
/**
* Cria uma coleção de instâncias
* @param collectionData
* @param {Options} [options] - As opções para criar a instância.
* @returns {[Model]}
*/
static createCollection (collectionData, options) {
if (!Array.isArray(collectionData)) {
throw new Error('Collection data must be an array')
}
return collectionData.map(data => this.create(data, options))
}
static flattenData (rowData) {
if (!rowData === null || rowData === undefined) return rowData
if (Array.isArray(rowData)) {
return rowData.map(data => this.flattenData(data))
}
const flattenData = {}
for (const [columnName, columnValue] of Object.entries(rowData)) {
const foreignKeyName = this.flattenConvertionDict[columnName]
if (typeof columnValue === 'object' && foreignKeyName) {
flattenData[foreignKeyName] = columnValue.id
} else {
flattenData[columnName] = columnValue
}
}
return flattenData
}
/**
* Altera resultado convertendo chave estrangeira em objeto aninhado com ‘id’
* Deve ser usado com nestTables = false. Para nestTable configurar o select corretamente e utilizarformatNestedResult
*
* @param rowData
* @returns {{}}
*/
static nestData (rowData) {
if (!rowData === null || rowData === undefined) return rowData
if (Array.isArray(rowData)) {
return rowData.map(data => this.nestData(data))
}
const nestedData = {}
for (const [columnName, columnValue] of Object.entries(rowData)) {
const relation = this.nestedConvertionDict[`${this.prismaModel}.${columnName}`]
if (relation) {
nestedData[relation] = {
id: columnValue
}
} else {
nestedData[columnName] = columnValue
}
}
return nestedData
}
/**
* Retorna um objeto validator, criando um cache na primeira execução
* @returns {import("ajv").ValidateFunction} - o objeto validator
*/
static get validator () {
// Validation disabled
if (!this.schema) {
return () => true
}
const ajv = new Ajv({
allErrors: true,
verbose: true,
// coerceTypes: true,
// $data: true,
// nullable: true
})
if (!this._validatorCache) {
this._validatorCache = ajv.compile(this.schema)
this._validatorIgnoreRequiredCache = ajv.compile({
...this.schema, required: []
})
}
return (data, ignoreRequired = false) => {
const valid = ignoreRequired ? this._validatorIgnoreRequiredCache(data) : this._validatorCache(data)
if (!valid) {
throw new Error(ajv.errorsText(this._validatorCache.errors, { dataVar: `'${this.prismaModel}'` }))
}
return true
}
}
/// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Métodos de consulta
/// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Retorna primeiro registro
*
* @param trx
* @returns {Promise<Model|*>}
*/
static async getFirst (trx) {
const data = await this.Service.findFirst()
if (!data) return data
return this.create(data)
}
/**
* Retorna um registro
*
* @param id
* @param trx
* @returns {Promise<Model|*>}
*/
static async getOne (id, trx) {
const data = await this.Service.findUnique({ where: { id } })
if (!data) return data
return this.create(data)
}
/**
* Retorna todos os registros
*
* @param trx
* @returns {Promise<Model[]>}
*/
static async getAll (trx) {
const collectionData = await this.Service.findMany()
return this.createCollection(collectionData)
}
/**
* Remove registro unico
*
* @returns {Promise<number>}
*/
static async deleteById (id, trx) {
try {
return await this.Service.delete({ where: { id } })
} catch (e) {
if (e.code !== 'P2025') {
throw e
}
return false
}
}
async save (trx) {
const Class = this.constructor
const id = this.id
const data = this.getFormattedDataForDatabase()
const updateData = id
? await Class.Service.update({ where: { id }, data })
: await Class.Service.create({ data })
await this.setValues(updateData)
return this
}
/**
* Remove registro unico
*
* @returns {Promise<number>}
*/
async delete (trx) {
if (!this.id) {
throw new Error('Property "id" not defined!')
}
return await this.constructor.deleteById(this.id, trx)
}
/// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Métodos de Instancia
/// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
getFormattedDataForDatabase () {
// TODO: Verificar necessidade, apenas para o caso do método nestData
return this.constructor.flattenData(this)
}
/**
* Adiciona varias propriedades de uma vez ao modelo.
*
* @param {Object} data - Um objeto contendo as propriedades a serem definidas no modelo.
* @param {Options} [options] - As opções para criar a instância.
*/
setValues (data, options = {}) {
options = { ...{ validate: true, ignoreRequired: false, validateDeep: false }, ...options }
if (!data) {
throw new Error('The argument \'data\' was not provided or is null or undefined.')
}
if (!isObject(data)) {
throw new TypeError(`The 'data' argument must be a plain object. Received: ${JSON.stringify(data)}`)
}
if (Array.isArray(data)) {
throw new Error('Array is not allowed. To create collections use createCollection.')
}
// TODO: Verificar nestdata é util
// const nestData = options.nestTables ? this.constructor.nestResultWithNestTables(data) : this.constructor.nestData(data)
for (const [attrName, value] of Object.entries(data)) {
if (value === undefined) continue
this.setValue(attrName, value, options)
}
if (options.validate) {
this.validate(options.ignoreRequired)
}
}
/**
* Define o valor de um atributo da instância do modelo.
*
* @param {string} attrName - O nome do atributo que deve receber o valor.
* @param {*} value - O valor a ser atribuído ao atributo.
* @param {Options} [options] - As opções para criar a instância.
*/
setValue (attrName, value, options) {
options = { ...{ validate: true, ignoreRequired: false, validateDeep: false }, ...options }
const Class = this.constructor
const SubClass = Class.relations[attrName]
if (isObject(value) && SubClass) {
this._createSubModelProperty(attrName, value, options)
} else if (isObject(value) && Decimal.isDecimal(value)) {
// Disable Decimal TODO: Adicionar opção para habilitar,
this._createSimpleProperty(attrName, value.toNumber())
} else {
this._createSimpleProperty(attrName, value)
}
}
/**
* Valida propriedades
*
* @param {Boolean} ignoreRequired
*/
validate (ignoreRequired) {
this.constructor.validator(this, ignoreRequired)
}
/**
* Habilitada o modo enumerable para todos os Getters e Setters definido na instancia
* - Exibir atributos do getter e Setter no template
*
* @private
*/
_makeGettersAndSettersEnumerable () {
const Class = this.constructor
for (const [propName, propDesc] of Object.entries(Object.getOwnPropertyDescriptors(Object.getPrototypeOf(this)))) {
if (!propDesc.enumerable && (propDesc.get || propDesc.set)) {
Object.defineProperty(this, propName, {
enumerable: !Class.hidden.includes(propName), // Se for hidden mantém oculto, não altera
configurable: false,
set: propDesc.set,
get: propDesc.get
})
}
}
}
/**
* Cria uma instância de uma Model relacionado
*
* @private
* @param {string} attrName - O nome do atributo a ser criado
* @param {*} value - O valor inicial do atributo
* @param options
* @private
*/
_createSubModelProperty (attrName, value, options) {
const Class = this.constructor
const SubClass = Class.relations[attrName]
if (!options.validateDeep) {
options = { ignoreRequired: true, options }
}
Object.defineProperty(this, attrName, {
enumerable: !Class.hidden.includes(attrName),
configurable: false,
writable: true,
value: Array.isArray(value) ? SubClass.createCollection(value, options) : value === null ? null : SubClass.create(value, options)
})
}
/**
* Cria uma propriedade simples do modelo
*
* @private
* @param {string} attrName - O nome do atributo a ser criado
* @param {*} value - O valor inicial do atributo
* @returns {void}
*/
_createSimpleProperty (attrName, value) {
const Class = this.constructor
const ownPropertyDescriptors = Object.getOwnPropertyDescriptors(Object.getPrototypeOf(this))
// Se propriedade ainda não estiver definida (exemplo já existe getter/setter) cria
if (ownPropertyDescriptors[attrName]) {
this[attrName] = cloneDeep(value)
} else {
Object.defineProperty(this, attrName, {
enumerable: !Class.hidden.includes(attrName), configurable: false, writable: true, value: cloneDeep(value)
})
}
}
}