Skip to content

Instantly share code, notes, and snippets.

@kapouer
Last active April 9, 2018 14:41
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 kapouer/9a2119dd251506ff0baa6bb6dd5c8330 to your computer and use it in GitHub Desktop.
Save kapouer/9a2119dd251506ff0baa6bb6dd5c8330 to your computer and use it in GitHub Desktop.
const objection = require('.');
const Model = objection.Model;
const expect = require('expect.js');
const knex = require('knex')({
connection: {
user: 'objection',
host: 'localhost',
database: 'objection_test'
},
client: 'postgres'
});
Model.knex(knex);
class ModelSub extends objection.Model {
static get jsonSchema() {
return {
type: 'object',
properties: {
id: { type: ['number', 'null'] },
data: {
type: 'object',
properties: {
text: {type: "string"},
title: {type: "string"}
}
}
}
}
}
static get tableName() {
return 'modelsub';
}
static idColumn() {
return 'id';
}
}
function init(knex) {
return Promise.resolve()
.then(() => knex.schema.dropTableIfExists('modelsub'))
.then(() => {
return knex.schema.createTable('modelsub', table => {
table.increments('id').primary();
table.jsonb('data');
});
});
}
function run() {
return ModelSub.query().insert({id: 1, data: {title: 'mytitle', text: 'mytext'}}).then(() => {
console.log("inserted a row matching schema");
return ModelSub.query().patch({
'data:text': 100
}).catch(err => {
console.error("got error", err);
}).then(() => {
return ModelSub.query().where('id', 1).select('id', 'data').first().then(function(row) {
console.log("now row is", row);
});
});
});
}
init(knex).then(function() {
return run();
}).then(function() {
console.log("DONE");
}).catch(function(err) {
console.error(err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment