Skip to content

Complete Example

Branden Horiuchi edited this page Jul 7, 2015 · 1 revision

Complete Example

var schemer = require('knex-schemer')(knex);
var c       = schemer.constants;

// schema definition
var schema = {

	user: {
		id: {type: c.type.integer, primary: true, increments: true},
		name: {type: c.type.string, size: 255},
		username: {type: c.type.string, size: 100},
		email: {type: c.type.string, size: 200}
	},
	credential: {
		id: {type: c.type.integer, primary: true, increments: true},
		name: {type: c.type.string, size: 255},
		username: {type: c.type.string, size: 100},
		encryptedKey: {type: c.type.string, size: 255}
	}
};

// data to load
var data = {
    user: [
        {
            id: 1,
            name: 'Jack Shepard',
            username: 'jshepard',
            email: '[email protected]'
        },
        {
            id: 2,
            name: 'Kate Austen',
            username: 'kausten',
            email: '[email protected]'
        }
    ],
    credential: [
        {
            id: 1,
            name: 'swan',
            username: 'operator',
            encryptedKey: '4815162342'
        }
    ]
};


// drop all tables
schemer.drop(schema).then(function() {

    // create or update all tables
    return schemer.sync(schema).then(function() {
	    
        // convert and load the data
        return schemer.convertAndLoad(data, schema).then(function() {
            // post load operations
        });
    });
});