feathers-knex-modeler
Advanced tools
Comparing version 1.4.2 to 1.4.3
{ | ||
"name": "feathers-knex-modeler", | ||
"version": "1.4.2", | ||
"version": "1.4.3", | ||
"description": "This package allows you to easily extend a table while you are developing it without requiring you to drop tables.", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -28,33 +28,33 @@ 'use strict' | ||
columns: [ | ||
{ 'name': 'id', 'type': 'increments' }, | ||
{ name: 'id', type: 'increments' }, | ||
{ | ||
'name': 'name', | ||
'type': 'text', | ||
'options': [{ 'type': 'notNullable' }] | ||
name: 'name', | ||
type: 'text', | ||
options: [{ type: 'notNullable' }] | ||
}, | ||
{ | ||
'name': 'random_text_array', | ||
'specificType': true, | ||
'type': 'text[]', | ||
'options': [{ 'type': 'notNullable' }] | ||
name: 'random_text_array', | ||
specificType: true, | ||
type: 'text[]', | ||
options: [{ type: 'notNullable' }] | ||
}, | ||
{ | ||
'name': 'test_id', | ||
'type': 'integer', | ||
'options': [{ 'type': 'references', 'argument': 'test.id' }, { 'type': 'onDelete', 'argument': 'CASCADE' }] | ||
name: 'test_id', | ||
type: 'integer', | ||
options: [{ type: 'references', argument: 'test.id' }, { type: 'onDelete', argument: 'CASCADE' }] | ||
}, | ||
{ | ||
'name': 'schema_type', | ||
'type': 'text', | ||
'options': [{ 'type': 'notNullable' }] | ||
name: 'schema_type', | ||
type: 'text', | ||
options: [{ type: 'notNullable' }] | ||
}, | ||
{ | ||
'name': 'status', | ||
'type': 'text', | ||
'options': [{ 'type': 'notNullable' }] | ||
name: 'status', | ||
type: 'text', | ||
options: [{ type: 'notNullable' }] | ||
}, | ||
{ | ||
'name': 'shared', | ||
'type': 'bool', | ||
'options': [{ 'type': 'notNullable' }] | ||
name: 'shared', | ||
type: 'bool', | ||
options: [{ type: 'notNullable' }] | ||
} | ||
@@ -69,6 +69,6 @@ ], | ||
await db.insert({ name: 'test1', shared: true, schema_type: 'something', status: 'pending' }).into('test') | ||
let testId = (await db.select().first('id').from('test')).id | ||
const testId = (await db.select().first('id').from('test')).id | ||
await db.insert({ 'random_text_array': ['test', 'test', 'test2'], test_id: testId, name: 'test', shared: true, status: 'test', schema_type: 'test' }).into('dependent') | ||
let result = await db.select().from('dependent') | ||
await db.insert({ random_text_array: ['test', 'test', 'test2'], test_id: testId, name: 'test', shared: true, status: 'test', schema_type: 'test' }).into('dependent') | ||
const result = await db.select().from('dependent') | ||
console.log(result) | ||
@@ -75,0 +75,0 @@ }) |
@@ -8,5 +8,6 @@ /* eslint-disable no-console */ | ||
const { default: PQueue } = require('p-queue') | ||
const MAX_RETRIES = 5 | ||
class Model extends EventEmitter { | ||
constructor (options) { | ||
_.defaultsDeep(options, { name: '', depends: [], columns: [], db: {} }) | ||
_.defaultsDeep(options, { name: '', depends: [], columns: [], db: {}, retries: MAX_RETRIES }) | ||
super(options) | ||
@@ -20,2 +21,3 @@ const self = this | ||
Object.defineProperty(self._, 'default', { enumerable: false, value: options.default }) | ||
Object.defineProperty(self._, 'retries', { enumerable: false, value: options.retries }) | ||
const tableName = _.get(self, '_.name') | ||
@@ -46,28 +48,38 @@ self.debug = debug(`feathers-knex-modeler:${tableName}`) | ||
async init () { | ||
async init (options, retries = 0) { | ||
const self = this | ||
const db = self.db | ||
const tableName = self.name | ||
self.debug(`Starting initialization of model for table: ${tableName}`) | ||
let encounteredErrors = false | ||
let errors | ||
try { | ||
self.emit({ source: 'initialization', type: 'database', value: { message: `Initializing database: ${self.name}. Waiting on dbs: ${self.depends.join(', ')}` } }) | ||
self.debug(`Waiting for dependent tables for table: ${tableName}`) | ||
await self.waitForTables() | ||
self.debug(`Creating table: ${tableName}`) | ||
await self.createTable() | ||
self.debug(`Creating Columns for table: ${tableName}`) | ||
await self.createColumns() | ||
self.debug(`Starting initialization of model for table: ${tableName}`) | ||
let encounteredErrors = false | ||
let errors | ||
try { | ||
self.emit({ source: 'initialization', type: 'database', value: { message: `Initializing database: ${self.name}. Waiting on dbs: ${self.depends.join(', ')}` } }) | ||
self.debug(`Waiting for dependent tables for table: ${tableName}`) | ||
await self.waitForTables() | ||
self.debug(`Creating table: ${tableName}`) | ||
await self.createTable() | ||
self.debug(`Creating Columns for table: ${tableName}`) | ||
await self.createColumns() | ||
} catch (err) { | ||
errors = err | ||
encounteredErrors = true | ||
self.emit({ source: 'initialization', type: 'error', value: err }) | ||
} | ||
if (encounteredErrors === true) { | ||
self.debug(`Failed initialization of model for table: ${tableName}`) | ||
throw new Error(errors) | ||
} else { | ||
self.debug(`Finished initialization of model for table: ${tableName}`) | ||
} | ||
} catch (err) { | ||
errors = err | ||
encounteredErrors = true | ||
self.emit({ source: 'initialization', type: 'error', value: err }) | ||
self.debug(err) | ||
retries++ | ||
if (retries < self._.retries) { | ||
return self.init(options, retries) | ||
} else { | ||
throw err | ||
} | ||
} | ||
if (encounteredErrors === true) { | ||
self.debug(`Failed initialization of model for table: ${tableName}`) | ||
throw new Error(errors) | ||
} else { | ||
self.debug(`Finished initialization of model for table: ${tableName}`) | ||
} | ||
return db | ||
@@ -74,0 +86,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21267
544