Skip to content

Instantly share code, notes, and snippets.

@heisian
Last active October 19, 2018 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heisian/751f9b2ff337c29d5c6d3740adfe6456 to your computer and use it in GitHub Desktop.
Save heisian/751f9b2ff337c29d5c6d3740adfe6456 to your computer and use it in GitHub Desktop.
returning('*')
const { DB } = require('db')
const db = new DB()
//
;(async () => {
await db.connect()
await db.transaction(async (trx) => {
const user = await db.models.User.query(trx)
.insert({})
.returning('*')
console.log({ user })
const identityDirectInsert = await db.models.Identity.query(trx)
.insert({
userId: user.id,
nameFirst: 'W',
nameLast: 'X',
})
.returning('*')
console.log({ identityDirectInsert })
const identityDirectPatch = await identityDirectInsert
.$query(trx)
.patch({
nameFirst: 'Y',
nameLast: 'Z',
})
.returning('*')
console.log({ identityDirectPatch })
const identityRelatedInsert = await user
.$relatedQuery('identities', trx)
.insert({
nameFirst: 'A',
nameLast: 'B',
})
.returning('*')
console.log({ identityRelatedInsert })
const identityRelatedPatch = await user
.$relatedQuery('identities', trx)
.where({ nameFirst: 'A' })
.patch({
nameFirst: 'C',
nameLast: 'D',
})
.returning('*')
console.log({ identityRelatedPatch })
const ssn = await identityDirectInsert
.$relatedQuery('ssn', trx)
.insert({})
.returning('*')
console.log({ ssn })
const ssnRelatedPatch = await identityDirectInsert
.$relatedQuery('ssn', trx)
.patch({
encryptedValue: 123456789,
})
.returning('*')
console.log({ ssnRelatedPatch })
const profile = await identityDirectInsert
.$relatedQuery('profiles', trx)
.insert({})
.returning('*')
console.log({ profile })
const medicalHistory = await profile
.$relatedQuery('medicalHistory', trx)
.insert({})
.returning('*')
console.log({ medicalHistory })
const medicalHistoryRelatedPatch = await profile
.$relatedQuery('medicalHistory', trx)
.patch({ hivAids: true })
.returning('*')
console.log({ medicalHistoryRelatedPatch })
})
})()
{ user:
User {
createdAt: 2018-10-19T15:51:09.431Z,
updatedAt: 2018-10-19T15:51:09.431Z,
leadId: 'ba7RSp5DkzuDpetJ',
password:
'$2a$10$StH5W0srOmpt7gsQwhIw6ujwMZ1d9xWUOeOpoWY6Sok2rpCWzfe16',
id: 103,
objectId: null,
leadObjectId: null,
referrerObjectId: null,
deletedAt: null,
referrer:
Referrer {
entityId: 103,
entityType: 'User',
createdAt: '2018-10-19T15:51:09.541Z',
updatedAt: '2018-10-19T15:51:09.541Z',
code: '49ogvMmiXZI580Q5',
active: true,
allowLeadShare: true,
id: 83,
referralPrograms: [Array] } } }
{ identityDirectInsert:
Identity {
userId: 108,
nameFirst: 'W',
nameLast: 'X',
createdAt: 2018-10-19T16:19:21.573Z,
updatedAt: 2018-10-19T16:19:21.573Z,
id: 111,
primary: null,
nameMiddle: null,
gender: null,
birthDate: null,
birthCountry: null,
deletedAt: null,
allowLeadShare: true,
birthState: null } }
{ identityDirectPatch:
Identity {
id: 111,
userId: 108,
primary: null,
nameFirst: 'Y',
nameMiddle: null,
nameLast: 'Z',
gender: null,
birthDate: null,
birthCountry: null,
createdAt: 2018-10-19T16:19:21.573Z,
updatedAt: 2018-10-19T16:19:21.577Z,
deletedAt: null,
allowLeadShare: true,
birthState: null } }
{ identityRelatedInsert:
Identity {
nameFirst: 'A',
nameLast: 'B',
userId: 108,
createdAt: 2018-10-19T16:19:21.581Z,
updatedAt: 2018-10-19T16:19:21.581Z,
id: 112,
primary: null,
nameMiddle: null,
gender: null,
birthDate: null,
birthCountry: null,
deletedAt: null,
allowLeadShare: true,
birthState: null } }
{ identityRelatedPatch:
[ Identity {
id: 112,
userId: 108,
primary: null,
nameFirst: 'C',
nameMiddle: null,
nameLast: 'D',
gender: null,
birthDate: null,
birthCountry: null,
createdAt: 2018-10-19T16:19:21.581Z,
updatedAt: 2018-10-19T16:19:21.583Z,
deletedAt: null,
allowLeadShare: true,
birthState: null } ] }
{ ssn:
SSN {
identityId: 103,
createdAt: 2018-10-19T15:51:09.569Z,
updatedAt: 2018-10-19T15:51:09.569Z,
id: 2,
encryptedValue: null,
verifyAttempts: null,
lastFourAttempts: null,
tuRiskGroup: null,
lnPrefill: null,
details: null,
deletedAt: null,
lastFourVerified: null } }
{ ssnRelatedPatch:
[ SSN {
id: 2,
identityId: 103,
encryptedValue: '123456789',
verifyAttempts: null,
lastFourAttempts: null,
tuRiskGroup: null,
lnPrefill: null,
details: null,
createdAt: 2018-10-19T15:51:09.569Z,
updatedAt: 2018-10-19T15:51:09.572Z,
deletedAt: null,
lastFourVerified: null } ] }
{ profile:
Profile {
identityId: 103,
createdAt: 2018-10-19T15:51:09.582Z,
updatedAt: 2018-10-19T15:51:09.582Z,
id: 102,
height: null,
weight: null,
statusCitizen: null,
statusPermanentResident: null,
statusGreenCardNumber: null,
statusMonthsInUs: null,
driversLicenseState: null,
driversLicenseNumber: null,
driversLicensePrefill: null,
driversLicenseStateVerified: null,
adverseRisks: null,
deletedAt: null } }
{ medicalHistory:
MedicalHistory {
profileId: 102,
...
hivAids: null,
hivAidsDetails: null,
...
tobaccoAmount: null,
tobaccoFrequency: null } }
{ medicalHistoryRelatedPatch:
[ MedicalHistory {
profileId: 102,
...
hivAids: true,
hivAidsDetails: null,
...
tobaccoAmount: null,
tobaccoFrequency: null } ] }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment