Last active
April 9, 2018 14:41
-
-
Save kapouer/9a2119dd251506ff0baa6bb6dd5c8330 to your computer and use it in GitHub Desktop.
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
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