rethink-struct
Advanced tools
+48
| (async () => { | ||
| const { RethinkStruct } = require('./index') | ||
| const schema = { | ||
| databases: [ | ||
| 'db1', | ||
| 'db2', | ||
| { | ||
| name: 'db3', | ||
| tables: [ | ||
| { | ||
| name: 'table3', | ||
| indexes: ['index1'] | ||
| }, | ||
| { | ||
| name: 'table4', | ||
| indexes: [ | ||
| { | ||
| name: 'index2' | ||
| }, { | ||
| name: 'index3', | ||
| type: 'simple' | ||
| }, { | ||
| name: 'index4', | ||
| type: 'compound', | ||
| compound: ['x', 'y'] | ||
| }, { | ||
| name: 'index5', | ||
| type: 'multi' | ||
| }, { | ||
| name: 'index6', | ||
| type: 'geo' | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| name: 'db4', | ||
| tables: ['table1', 'table2'] | ||
| } | ||
| ] | ||
| } | ||
| const rethink = require('rethinkdb') | ||
| const rethinkConn = await rethink.connect() | ||
| const x = new RethinkStruct(schema, rethinkConn, true) | ||
| x.assert() | ||
| })() |
| class DatabaseElem { | ||
| constructor(name, schema) { | ||
| this.name = name | ||
| this.schema = schema | ||
| this.tables = [] | ||
| } | ||
| } | ||
| module.exports = { DatabaseElem } |
| class IndexElem { | ||
| constructor(name, schema) { | ||
| this.name = name | ||
| this.schema = schema | ||
| } | ||
| } | ||
| module.exports = { IndexElem } |
| class TableElem { | ||
| constructor(name, schema) { | ||
| this.name = name | ||
| this.schema = schema | ||
| this.indexes = [] | ||
| } | ||
| } | ||
| module.exports = { TableElem } |
+168
| const r = require('rethinkdb') | ||
| const { DatabaseElem } = require('./elems/database') | ||
| const { TableElem } = require('./elems/table') | ||
| const { IndexElem } = require('./elems/index') | ||
| class RethinkStruct { | ||
| constructor(schema, conn, debug = false) { | ||
| this.schema = schema | ||
| this.conn = conn | ||
| this.debug = debug | ||
| this.databases = [] | ||
| } | ||
| async assert() { | ||
| await this._parseDatabases() | ||
| this.debug('Databases parsed') | ||
| await this._buildDatabases() | ||
| this.debug('Databases built') | ||
| await this._parseTables() | ||
| this.debug('Tables parsed') | ||
| await this._buidlTables() | ||
| this.debug('Tables built') | ||
| await this._parseIndexes() | ||
| this.debug('Indexes parsed') | ||
| await this._buildIndexes() | ||
| this.debug('Indexes built') | ||
| this.debug('Struct asserted') | ||
| } | ||
| _parseDatabases() { | ||
| return new Promise(async (resolve, reject) => { | ||
| const errors = [] | ||
| if (this.schema.databases === undefined) | ||
| errors.push('Can\'t find databases definition') | ||
| if (Array.isArray(this.schema.databases) === false) | ||
| errors.push('Databases definition should be an array of string/object') | ||
| this.schema.databases.forEach(database => { | ||
| if (typeof database !== 'string' && database.name === undefined) | ||
| errors.push('Database should be a string or an object with a name') | ||
| this.databases.push(new DatabaseElem(...(typeof database === 'string' ? ([database, null]) : ([database.name, database])))) | ||
| }) | ||
| return errors.length > 0 ? reject(errors) : resolve() | ||
| }) | ||
| } | ||
| _parseTables() { | ||
| return new Promise(resolve => { | ||
| const databases = this.databases | ||
| .filter(database => database.schema != null) | ||
| .filter(database => database.schema.tables !== undefined) | ||
| databases.forEach(database => { | ||
| database.schema.tables.forEach(table => { | ||
| if (typeof table !== 'string' && table.name === undefined) | ||
| errors.push('Table should be a string or an object with a name') | ||
| database.tables.push(new TableElem(...(typeof table === 'string' ? ([table, null]) : ([table.name, table])))) | ||
| }) | ||
| }) | ||
| resolve() | ||
| }) | ||
| } | ||
| _parseIndexes() { | ||
| return new Promise(resolve => { | ||
| const databases = this.databases | ||
| .filter(database => database.schema != null) | ||
| .filter(database => database.schema.tables !== undefined) | ||
| databases.forEach(database => { | ||
| database.tables | ||
| .map(table => table) | ||
| .filter(table => table.schema != null) | ||
| .filter(table => table.schema.indexes !== undefined) | ||
| .forEach(table => { | ||
| table.schema.indexes.forEach(index => { | ||
| if (typeof index !== 'string' && index.name === undefined) | ||
| errors.push('Indexes should be a string or an object with a name') | ||
| table.indexes.push(new IndexElem(...(typeof index === 'string' ? ([index, null]) : ([index.name, index])))) | ||
| }) | ||
| }) | ||
| }) | ||
| resolve() | ||
| }) | ||
| } | ||
| async _buildDatabases() { | ||
| const dbList = await r.dbList().run(this.conn) | ||
| return Promise.all( | ||
| this.databases | ||
| .filter(database => dbList.indexOf(typeof database === 'string' ? database : database.name) === -1) | ||
| .map(database => r.dbCreate(database.name).run(this.conn)) | ||
| ) | ||
| } | ||
| _buidlTables() { | ||
| const tablesBuilders = [] | ||
| this.databases.forEach(async database => { | ||
| const tableList = await r.db(database.name).tableList().run(this.conn) | ||
| tablesBuilders.concat( | ||
| database.tables | ||
| .filter(table => tableList.indexOf(typeof table === 'string' ? table : table.name) === -1) | ||
| .map(table => r.db(database.name).tableCreate(table.name).run(this.conn)) | ||
| ) | ||
| }) | ||
| return Promise.all(tablesBuilders) | ||
| } | ||
| _buildIndexes() { | ||
| const indexesBuilders = [] | ||
| this.databases.forEach(database => { | ||
| database.tables.forEach(async table => { | ||
| const indexesList = await r.db(database.name).table(table.name).indexList().run(this.conn) | ||
| table.indexes | ||
| .filter(index => indexesList.indexOf(typeof index === 'string' ? index : index.name) === -1) | ||
| .forEach(index => { | ||
| const type = index.schema == null ? 'simple' : (index.schema.type === undefined ? 'simple' : index.schema.type) | ||
| let indexBuilder | ||
| switch (type) { | ||
| case 'simple': | ||
| indexBuilder = r | ||
| .db(database.name) | ||
| .table(table.name) | ||
| .indexCreate(index.name) | ||
| .run(this.conn) | ||
| break | ||
| case 'compound': | ||
| if (Array.isArray(index.schema.compound) === true) | ||
| indexBuilder = r | ||
| .db(database.name) | ||
| .table(table.name) | ||
| .indexCreate(index.name, index.schema.compound.map(fieldName => r.row(fieldName))) | ||
| .run(this.conn) | ||
| else | ||
| indexBuilder = Promise.reject('Compound index should have a compound field') | ||
| break | ||
| case 'multi': | ||
| indexBuilder = r | ||
| .db(database.name) | ||
| .table(table.name) | ||
| .indexCreate(index.name, { multi: true }) | ||
| .run(this.conn) | ||
| break | ||
| case 'geo': | ||
| indexBuilder = r | ||
| .db(database.name) | ||
| .table(table.name) | ||
| .indexCreate(index.name, { geo: true }) | ||
| .run(this.conn) | ||
| break | ||
| } | ||
| indexesBuilders.push(indexBuilder) | ||
| }) | ||
| }) | ||
| }) | ||
| return Promise.all(indexesBuilders) | ||
| } | ||
| _debug(message) { | ||
| if (this.debug === true) | ||
| console.log(`[Rethink-Struct] ${message}`) | ||
| } | ||
| } | ||
| module.exports = { RethinkStruct } |
+3
-15
@@ -5,18 +5,6 @@ MIT License | ||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
| Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
| The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. | ||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
+11
-9
| { | ||
| "name": "rethink-struct", | ||
| "version": "1.0.2", | ||
| "description": "Assert that databases, tables, indexes exists on your Rethink server and create it if they don't", | ||
| "main": "src/structurate.js", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/Xstoudi/rethink-struct.git" | ||
| }, | ||
| "version": "2.0.0", | ||
| "description": "", | ||
| "main": "index.js", | ||
| "scripts": { | ||
@@ -14,7 +10,13 @@ "test": "echo \"Error: no test specified\" && exit 1" | ||
| "keywords": [ | ||
| "rethinkdb", | ||
| "rethink", | ||
| "rethinkdb", | ||
| "rdb", | ||
| "structure", | ||
| "init", | ||
| "assert", | ||
| "ensure" | ||
| "ensure", | ||
| "index", | ||
| "simple", | ||
| "multi", | ||
| "compound" | ||
| ], | ||
@@ -21,0 +23,0 @@ "author": "Xavier Stouder <xavier@stouder.io>", |
+19
-44
@@ -1,49 +0,24 @@ | ||
| # RethinkStruct | ||
| [](https://github.com/Xstoudi/rethink-struct/blob/master/LICENSE) | ||
| [](https://npmjs.com/package/rethink-struct) | ||
| # rethink-struct | ||
| ## Overview | ||
| Assert that databases, tables, indexes exists on your Rethink server and create it if they don't. | ||
| Assert that databases, tables and indexes exists on you RethinkDB server and create it if they don't. | ||
| ## Support | ||
| If you like this package and you like what you see in the ad, please click on it to support `rethink-struct`! | ||
| <a target='_blank' rel='nofollow' href='https://app.codesponsor.io/link/RFZm26J558vLyi6jH9gt7X9F/Xstoudi/rethink-struct'> | ||
| <img alt='Sponsor' width='888' height='68' src='https://app.codesponsor.io/embed/RFZm26J558vLyi6jH9gt7X9F/Xstoudi/rethink-struct.svg' /> | ||
| </a> | ||
| ## Requirements | ||
| * NodeJS 7.x.x or higher | ||
| * Harmony flag `--harmony_async_await` | ||
| * Async/await support (Node 8 or Node 7 with harmony flag) | ||
| ## Index support | ||
| Currently, `rethink-struct` is the only RethinkDB initialisation package that supports all indexes: | ||
| * Simple | ||
| * Compound | ||
| * Multi | ||
| * Geo | ||
| ## How to assert ? | ||
| Assertion is made by reading a JSON file. | ||
| The JSON must be structurated like this: | ||
| ```json | ||
| { | ||
| "databases": [ | ||
| { | ||
| "name": "database_name", | ||
| "tables": [ | ||
| { | ||
| "name": "table_name", | ||
| "indexes": ["field_to_index", "second_field_to_index"] | ||
| }, | ||
| { | ||
| "name": "another_table_name" | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| "name": "another_database_name", | ||
| "tables": [ | ||
| "a_table" | ||
| ] | ||
| } | ||
| ] | ||
| } | ||
| ``` | ||
| Then: | ||
| ```javascript | ||
| const rethink = require('rethinkdb') | ||
| const rethinkStruct = require('rethink-struct') | ||
| const rethinkConn = await rethink.connect() | ||
| await rethinkStruct(require('your.json'), rethinkConn) | ||
| ``` | ||
| ## Example | ||
| You can find an example in the `example` folder. | ||
| The package parse your schema and build the database as it should be. | ||
| You can find an example of use in the `example.js` file. |
Sorry, the diff of this file is not supported yet
| (async () => { | ||
| const rethinkStruct = require('../src/structurate') | ||
| const rethink = require('rethinkdb') | ||
| try { | ||
| const conn = await rethink.connect({ hostname: 'localhost' }) | ||
| await rethinkStruct(require('./sample.json'), conn) | ||
| console.log('Done') | ||
| } catch (e) { | ||
| console.error(e) | ||
| } | ||
| })() |
| { | ||
| "databases": [ | ||
| { | ||
| "name": "mygame", | ||
| "tables": [ | ||
| { | ||
| "name": "users", | ||
| "indexes": ["username"] | ||
| }, | ||
| { | ||
| "name": "scores", | ||
| "indexes": ["value"] | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| "name": "films", | ||
| "tables": [ | ||
| { | ||
| "name": "film", | ||
| "indexes": ["name", "year"] | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| } |
| const assert = require('assert') | ||
| const rethink = require('rethinkdb') | ||
| function structurate(model = ensureArg('model'), connection = ensureArg('connection')) { | ||
| return new Promise(async (resolve, reject) => { | ||
| try { | ||
| const dbList = await rethink.dbList().run(connection) | ||
| assert(model.databases, "Can't find 'databases'") | ||
| assert(Array.isArray(model.databases), "'databases' should be an array") | ||
| for (const database of model.databases) { | ||
| assert(database.name, 'A database has no name') | ||
| if (!~dbList.indexOf(database.name)) | ||
| await rethink.dbCreate(database.name).run(connection) | ||
| const tbList = await rethink.db(database.name).tableList().run(connection) | ||
| assert(database.tables, "Can't find 'tables'") | ||
| assert(Array.isArray(database.tables), "'tables' should be an array") | ||
| for (const table of database.tables) { | ||
| assert(table.name, 'A table has no name') | ||
| if (!~tbList.indexOf(table.name)) | ||
| await rethink.db(database.name).tableCreate(table.name).run(connection) | ||
| const idxList = await rethink.db(database.name).table(table.name).indexList().run(connection) | ||
| if(table.indexes) | ||
| for (const index of table.indexes) { | ||
| if (!~idxList.indexOf(index)) | ||
| await rethink.db(database.name).table(table.name).indexCreate(index).run(connection) | ||
| } | ||
| } | ||
| } | ||
| } catch (err) { | ||
| return reject(err) | ||
| } | ||
| console.log('Rethink structure asserted') | ||
| resolve() | ||
| }) | ||
| } | ||
| function ensureArg(argName) { | ||
| throw new Error(`Argument '${argName}' is missing...`) | ||
| } | ||
| module.exports = structurate |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
9781
74.54%8
14.29%217
201.39%24
-52%1
Infinity%1
Infinity%