Skip to content

Instantly share code, notes, and snippets.

@heisian
Last active October 19, 2018 16:21
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/e4bb1cc9f3b45bef7c143cfe49a93b1c to your computer and use it in GitHub Desktop.
Save heisian/e4bb1cc9f3b45bef7c143cfe49a93b1c to your computer and use it in GitHub Desktop.
no 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({})
console.log({ user })
const identityDirectInsert = await db.models.Identity.query(trx)
.insert({
userId: user.id,
nameFirst: 'W',
nameLast: 'X',
})
console.log({ identityDirectInsert })
const identityDirectPatch = await identityDirectInsert
.$query(trx)
.patch({
nameFirst: 'Y',
nameLast: 'Z',
})
console.log({ identityDirectPatch })
const identityRelatedInsert = await user
.$relatedQuery('identities', trx)
.insert({
nameFirst: 'A',
nameLast: 'B',
})
console.log({ identityRelatedInsert })
const identityRelatedPatch = await user
.$relatedQuery('identities', trx)
.where({ nameFirst: 'A' })
.patch({
nameFirst: 'C',
nameLast: 'D',
})
console.log({ identityRelatedPatch })
const ssn = await identityDirectInsert
.$relatedQuery('ssn', trx)
.insert({})
console.log({ ssn })
const ssnRelatedPatch = await identityDirectInsert
.$relatedQuery('ssn', trx)
.patch({
encryptedValue: 123456789,
})
console.log({ ssnRelatedPatch })
const profile = await identityDirectInsert
.$relatedQuery('profiles', trx)
.insert({})
console.log({ profile })
const medicalHistory = await profile
.$relatedQuery('medicalHistory', trx)
.insert({})
console.log({ medicalHistory })
const medicalHistoryRelatedPatch = await profile
.$relatedQuery('medicalHistory', trx)
.patch({ hivAids: true })
console.log({ medicalHistoryRelatedPatch })
})
})()
{ user:
User {
createdAt: '2018-10-19T15:54:56.694Z',
updatedAt: '2018-10-19T15:54:56.694Z',
leadId: 'IejXjjdHiRg1QW9r',
password:
'$2a$10$kwnR0SiDKKNf37HOa3f.Ye1uLamPYV/f93O3WinEt8CnUUhjyAzgC',
id: 104,
referrer:
Referrer {
entityId: 104,
entityType: 'User',
createdAt: '2018-10-19T15:54:56.806Z',
updatedAt: '2018-10-19T15:54:56.806Z',
code: 'XJRD625sQLj4dTJF',
active: true,
allowLeadShare: true,
id: 84,
referralPrograms: [Array] } } }
{ identityDirectInsert:
Identity {
userId: 105,
nameFirst: 'W',
nameLast: 'X',
createdAt: '2018-10-19T16:12:19.112Z',
updatedAt: '2018-10-19T16:12:19.112Z',
id: 106 } }
{ identityDirectPatch: 1 }
{ identityRelatedInsert:
Identity {
nameFirst: 'A',
nameLast: 'B',
userId: 104,
createdAt: '2018-10-19T15:54:56.820Z',
updatedAt: '2018-10-19T15:54:56.820Z',
id: 104 } }
{ identityRelatedPatch: 1 }
{ ssn:
SSN {
identityId: 104,
createdAt: '2018-10-19T15:54:56.831Z',
updatedAt: '2018-10-19T15:54:56.831Z',
id: 3 } }
{ ssnRelatedPatch: 1 }
{ profile:
Profile {
identityId: 104,
createdAt: '2018-10-19T15:54:56.841Z',
updatedAt: '2018-10-19T15:54:56.841Z',
id: 103 } }
{ medicalHistory: MedicalHistory { profileId: 103 } }
{ medicalHistoryRelatedPatch: 1 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment