Created
April 5, 2019 13:23
-
-
Save delijati/6e4f43937c9864dbcaffd5b8a8c620f4 to your computer and use it in GitHub Desktop.
Knex objection ORM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
// $ npm install knex objection sqlite3 | |
const {Model} = require('objection'); | |
const knex = require('knex')({ | |
client: 'sqlite3', | |
connection: { | |
filename: './db.sqlite' | |
}, | |
useNullAsDefault: true | |
}); | |
class Person extends Model { | |
static get tableName() { | |
return 'person'; | |
} | |
} | |
async function main() { | |
await knex.schema.createTableIfNotExists('person', table => { | |
table.increments(); | |
table.string('firstname'); | |
table.string('lastname'); | |
table.timestamps(); | |
}); | |
Model.knex(knex); | |
const jennifer = await Person.query().insert({firstname: 'Jennifer', lastname: 'Lawrence'}); | |
console.log(jennifer); | |
const entries = await Person.query(); | |
entries.forEach(row => { | |
console.log(row); | |
}); | |
// destroy to stop script execution | |
await knex.destroy(); | |
} | |
main(); | |
// (async () => { | |
// await main(); | |
// })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment