Created
July 26, 2016 02:10
-
-
Save FrancisMurillo/1dd88b7136745f4dc580a7c9229daa97 to your computer and use it in GitHub Desktop.
RethinkDB Schema Maker
This file contains 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 r = require('rethinkdb'); | |
const sampleSchema = { | |
name: 'mydb', | |
tables: [ | |
{ | |
name: 'users', | |
// Not really used but as a schema reference | |
fields: [ | |
'id', // Default | |
'username', | |
'password', | |
'displayName' | |
], | |
indices: [ | |
{ | |
name: 'username_password', | |
keys: [ | |
'username', | |
'password' | |
] | |
}, | |
'displayName' | |
] | |
} | |
] | |
}; | |
const getSampleConnection = () => { | |
return r | |
.connect({ | |
host: 'localhost', | |
port: 28015 | |
}); | |
}; | |
const schema = require('./schema'); | |
const getConnection = require('./connection'); | |
getConnection().then((conn) => { | |
const createSchema = (schema) => { | |
const execute = (query) => { | |
return query.run(conn).then(_ => null, _ => null); | |
}; | |
const call = (query) => { | |
return query.run(conn); | |
}; | |
const createDb = (schema) => { | |
const {name} = schema; | |
console.log('Creating database ' + name); | |
return execute(r.dbCreate(name)).then(_ => r.db(name)); | |
}; | |
const createIndex = (table, index) => { | |
if (typeof index === 'string') { | |
console.log('Creating index:' + index) | |
return execute(table.indexCreate(index)); | |
} else { | |
const {name, keys} = index; | |
console.log('Creating index:' + name); | |
const queryKeys = keys.map(key => r.row('key')) | |
return execute(table.indexCreate(name, queryKeys)) | |
} | |
}; | |
const createTable = (db, table) => { | |
const {name, indices} = table; | |
console.log('Creating table ' + name); | |
return execute(db.tableCreate(name)) | |
.then(_ => db.table(name)) | |
.then(table => { | |
console.log('With table:' + name); | |
return Promise.all(indices.map(index => { | |
return createIndex(table, index) | |
})); | |
}); | |
}; | |
return createDb(schema) | |
.then(db => { | |
const {tables} = schema; | |
console.log('With database:' + schema.name); | |
return Promise.all(tables.map(table => { | |
return createTable(db, table); | |
})); | |
}) | |
.then(_ => { | |
console.log('Schema created'); | |
}); | |
}; | |
return createSchema(schema) | |
.then(_ => process.exit()); | |
}); | |
const getConnection = () => { | |
return r | |
.connect({ | |
host: '192.168.1.161', | |
port: 38015 | |
}); | |
}; | |
getConnection().then((conn) => { | |
const createSchema = (schema) => { | |
const execute = (query) => { | |
return query.run(conn).then(_ => null, _ => null); | |
}; | |
const call = (query) => { | |
return query.run(conn); | |
}; | |
const createDb = (schema) => { | |
const {name} = schema; | |
console.log('Creating database ' + name); | |
return execute(r.dbCreate(name)).then(_ => r.db(name)); | |
}; | |
const createIndex = (table, index) => { | |
if (typeof index === 'string') { | |
console.log('Creating index:' + index) | |
return execute(table.indexCreate(index)); | |
} else { | |
const {name, keys} = index; | |
console.log('Creating index:' + name); | |
const queryKeys = keys.map(key => r.row('key')) | |
return execute(table.indexCreate(name, queryKeys)) | |
} | |
}; | |
const createTable = (db, table) => { | |
const {name, indices} = table; | |
console.log('Creating table ' + name); | |
return execute(db.tableCreate(name)) | |
.then(_ => db.table(name)) | |
.then(table => { | |
console.log('With table:' + name); | |
return Promise.all(indices.map(index => { | |
return createIndex(table, index) | |
})); | |
}); | |
}; | |
return createDb(schema) | |
.then(db => { | |
const {tables} = schema; | |
console.log('With database:' + schema.name); | |
return Promise.all(tables.map(table => { | |
return createTable(db, table); | |
})); | |
}) | |
.then(_ => { | |
console.log('Schema created'); | |
}); | |
}; | |
return createSchema(schema) | |
.then(_ => process.exit()); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment