Skip to content

Instantly share code, notes, and snippets.

@delijati
Created April 5, 2019 13:23
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 delijati/6e4f43937c9864dbcaffd5b8a8c620f4 to your computer and use it in GitHub Desktop.
Save delijati/6e4f43937c9864dbcaffd5b8a8c620f4 to your computer and use it in GitHub Desktop.
Knex objection ORM
'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