Comparing version 0.1.4 to 0.1.5-alpha.4
@@ -15,8 +15,5 @@ let currentConfig = { | ||
const { Database } = require('./db/Database') | ||
const { connect } = require('./db/DatabasePool') | ||
await Promise.all( | ||
Object.entries(settings.databases).map(async ([dbName, dbConfig]) => { | ||
const db = Database.resolveDriver(dbConfig) | ||
await connect(db, dbName) | ||
}) | ||
const { configDb } = require('./db/DatabasePool') | ||
Object.entries(settings.databases).map(([dbName, dbConfig]) => | ||
configDb(Database.resolveDriver(dbConfig), dbName) | ||
) | ||
@@ -26,5 +23,3 @@ } | ||
const shutdown = async () => { | ||
require('./models/ModelRegistry').clearModels() | ||
} | ||
const shutdown = async () => {} | ||
@@ -31,0 +26,0 @@ module.exports = { |
@@ -7,4 +7,10 @@ const { DatabaseMapper } = require('./DatabaseMapper') | ||
connected = false | ||
connecting = false | ||
Mapper = DatabaseMapper | ||
constructor (...args) { | ||
super(...args) | ||
this.queue = [] | ||
} | ||
static resolveDriver (dbConfig) { | ||
@@ -15,6 +21,21 @@ const Model = require(dbConfig.driver) | ||
async connect () { | ||
async connectDb () { | ||
throw new NotImplemented() | ||
} | ||
async connect () { | ||
this.connecting = true | ||
await this.connectDb() | ||
this.connecting = false | ||
this.resolveQueue() | ||
} | ||
async resolveQueue () { | ||
await new Promise(resolve => setTimeout(resolve, 0)) | ||
let callback | ||
while ((callback = this.queue.shift())) { | ||
callback() | ||
} | ||
} | ||
async disconnect () { | ||
@@ -24,3 +45,16 @@ throw new NotImplemented() | ||
async waitForConnection () { | ||
if (!this.connected) { | ||
if (this.connecting) { | ||
await new Promise(resolve => { | ||
this.queue.push(resolve) | ||
}) | ||
} else { | ||
await this.connect() | ||
} | ||
} | ||
} | ||
async query (str) { | ||
await this.waitForConnection() | ||
return await this.queryDb(str) | ||
@@ -30,2 +64,3 @@ } | ||
async exec (str) { | ||
await this.waitForConnection() | ||
return await this.execDb(str) | ||
@@ -42,3 +77,8 @@ } | ||
stream (query) { | ||
async stream (qs) { | ||
await this.waitForConnection() | ||
return this.streamDb(qs) | ||
} | ||
streamDb (qs) { | ||
throw new NotImplemented() | ||
@@ -45,0 +85,0 @@ } |
@@ -13,11 +13,10 @@ const { getModelName } = require('../models/ModelRegistry') | ||
new Model( | ||
Object.entries(item) | ||
.filter(([fieldValue]) => fieldValue.startsWith(prefix)) | ||
.reduce( | ||
(aggr, [fieldName, fieldValue]) => ({ | ||
...aggr, | ||
[fieldName.substr(prefixLength)]: fieldValue | ||
}), | ||
{} | ||
) | ||
Object.entries(item).reduce((aggr, [fieldName, fieldValue]) => { | ||
if (fieldName.startsWith(prefix)) { | ||
aggr[fieldName.substr(prefixLength)] = fieldValue | ||
} else if (!fieldName.includes('__')) { | ||
aggr[fieldName] = fieldValue | ||
} | ||
return aggr | ||
}, {}) | ||
) | ||
@@ -24,0 +23,0 @@ } |
const { Database } = require('./Database') | ||
const { DatabaseError, NotConnected } = require('./errors') | ||
const { DatabaseError } = require('./errors') | ||
@@ -7,6 +7,5 @@ class DatabasePool { | ||
async connect (db, dbName = 'default') { | ||
configDb (db, dbName = 'default') { | ||
if (db instanceof Database) { | ||
this.databases[dbName] = db | ||
await db.connect() | ||
} else { | ||
@@ -19,2 +18,17 @@ throw new DatabaseError( | ||
async connectDb (db, dbName = 'default') { | ||
this.configDb(db, dbName) | ||
await this.connectDbInstance(dbName) | ||
} | ||
async connectDbInstance (dbName) { | ||
await this.databases[dbName].connect() | ||
} | ||
async connectAll () { | ||
await Promise.all( | ||
Object.keys(this.databases).map(dbName => this.connectDbInstance(dbName)) | ||
) | ||
} | ||
async disconnect () { | ||
@@ -30,5 +44,2 @@ await Promise.all(Object.values(this.databases).map(db => db.disconnect())) | ||
} | ||
if (!db.connected) { | ||
throw new NotConnected(`Database "${dbName}" is not connected`) | ||
} | ||
return db | ||
@@ -42,5 +53,6 @@ } | ||
DatabasePool, | ||
connect: async (...args) => await poolSingleton.connect(...args), | ||
connect: async (...args) => await poolSingleton.connectDb(...args), | ||
disconnect: async () => await poolSingleton.disconnect(), | ||
getDb: name => poolSingleton.getDb(name), | ||
configDb: (db, dbName) => poolSingleton.configDb(db, dbName), | ||
get instance () { | ||
@@ -47,0 +59,0 @@ return poolSingleton |
@@ -9,2 +9,3 @@ const { DjormError } = require('../errors') | ||
class QueryFormatterError extends DjormError {} | ||
class UnknownType extends QueryError {} | ||
@@ -16,3 +17,4 @@ module.exports = { | ||
QueryError, | ||
QueryFormatterError | ||
QueryFormatterError, | ||
UnknownType | ||
} |
@@ -10,2 +10,3 @@ module.exports = { | ||
...require('./Query'), | ||
...require('./QueryAllRecords'), | ||
...require('./QueryColumn'), | ||
@@ -15,2 +16,3 @@ ...require('./QueryColumnGroup'), | ||
...require('./QueryFormatter'), | ||
...require('./QueryFunc'), | ||
...require('./QueryIdentifier'), | ||
@@ -17,0 +19,0 @@ ...require('./QueryJoin'), |
@@ -9,2 +9,3 @@ const { And } = require('./And') | ||
const { QueryColumnGroup } = require('./QueryColumnGroup') | ||
const { QueryError } = require('./errors') | ||
const { QueryTable } = require('./QueryTable') | ||
@@ -27,2 +28,6 @@ | ||
mapModel (value) { | ||
return this.setProp('model', value) | ||
} | ||
filter (props) { | ||
@@ -42,5 +47,23 @@ return this.initProp('conditions', defaultConditions).appendProp( | ||
} | ||
return this.setProp('target', value) | ||
return this.setProp( | ||
'target', | ||
typeof value === 'string' ? value : new QueryTable(value) | ||
) | ||
} | ||
parseTargetName (targetName) { | ||
return targetName.split('.').reverse() | ||
} | ||
parseTarget () { | ||
const target = this.props.target | ||
if (typeof target === 'string') { | ||
return this.parseTargetName(target) | ||
} | ||
if (target instanceof QueryTable) { | ||
return this.parseTargetName(target.name) | ||
} | ||
throw new QueryError(`Unknown target type: "${target}"`) | ||
} | ||
getModelFields (model) { | ||
@@ -97,4 +120,4 @@ const selection = [] | ||
createReadStream () { | ||
return this.db.stream(this.db.formatQuery(this)) | ||
async stream () { | ||
return await this.db.stream(this) | ||
} | ||
@@ -101,0 +124,0 @@ } |
@@ -18,10 +18,14 @@ const { QueryColumn } = require('./QueryColumn') | ||
breakdown () { | ||
return this.columns.map( | ||
name => | ||
new QueryColumn({ | ||
name, | ||
source: this.source, | ||
prefix: this.prefix | ||
}) | ||
) | ||
return this.columns.map(name => { | ||
if (name instanceof QueryColumn) { | ||
return name | ||
.setProp('source', this.source) | ||
.setProp('prefix', this.prefix) | ||
} | ||
return new QueryColumn({ | ||
name, | ||
source: this.source, | ||
prefix: this.prefix | ||
}) | ||
}) | ||
} | ||
@@ -28,0 +32,0 @@ } |
@@ -1,4 +0,4 @@ | ||
const { PropModel } = require('./props') | ||
const { ImmutablePropModel } = require('./props') | ||
class QueryIdentifier extends PropModel { | ||
class QueryIdentifier extends ImmutablePropModel { | ||
get name () { | ||
@@ -5,0 +5,0 @@ return this.props.name |
@@ -1,5 +0,5 @@ | ||
const { PropModel } = require('./props') | ||
const { ImmutablePropModel } = require('./props') | ||
class QueryShortcut extends PropModel {} | ||
class QueryShortcut extends ImmutablePropModel {} | ||
module.exports = { QueryShortcut } |
const { And } = require('./And') | ||
const { getModelName } = require('../models/ModelRegistry') | ||
const { ObjectNotFound } = require('../errors') | ||
const { Q } = require('./QueryCondition') | ||
const { QueryAllRecords } = require('./QueryAllRecords') | ||
const { QueryColumn } = require('./QueryColumn') | ||
const { QueryFunc } = require('./QueryFunc') | ||
const { QueryIdentifier } = require('./QueryIdentifier') | ||
const { QueryJoin } = require('./QueryJoin') | ||
const { Query } = require('./Query') | ||
const { QueryShortcut } = require('./QueryShortcut') | ||
const { UnknownType } = require('./errors') | ||
@@ -15,3 +21,3 @@ const defaultSelection = () => [] | ||
parseSelectionValue (value) { | ||
if (typeof value instanceof QueryIdentifier) { | ||
if (value instanceof QueryIdentifier || value instanceof QueryShortcut) { | ||
return value | ||
@@ -22,2 +28,3 @@ } | ||
} | ||
throw new UnknownType(`Value "${value}" is not a valid selection type`) | ||
} | ||
@@ -82,2 +89,14 @@ | ||
async get () { | ||
const obj = await this.first() | ||
if (!obj) { | ||
throw new ObjectNotFound( | ||
this.model | ||
? `Could not find specified "${getModelName(this.model)}"` | ||
: 'Query did not return a result' | ||
) | ||
} | ||
return obj | ||
} | ||
async last () { | ||
@@ -88,2 +107,16 @@ const items = await this.fetch() | ||
async count () { | ||
const result = await this.appendProp( | ||
'selection', | ||
new QueryFunc({ | ||
name: 'COUNT', | ||
args: [new QueryAllRecords()], | ||
alias: '__djorm_cnt' | ||
}) | ||
) | ||
.mapModel(null) | ||
.first() | ||
return result.__djorm_cnt | ||
} | ||
getMapper () { | ||
@@ -93,4 +126,4 @@ return new this.db.Mapper(this) | ||
createReadStream (query) { | ||
return super.createReadStream(query).pipe(this.getMapper()) | ||
async stream () { | ||
return (await super.stream()).pipe(this.getMapper()) | ||
} | ||
@@ -97,0 +130,0 @@ } |
@@ -29,6 +29,2 @@ const camelCase = require('camelcase') | ||
getDefault (inst) { | ||
return this.queryTargetModel(inst) | ||
} | ||
expand () { | ||
@@ -68,3 +64,3 @@ return this.expandedField | ||
const model = getModel(this.model) | ||
return await model.objects.requireOne({ | ||
return await model.objects.get({ | ||
[model.pkName]: inst.get(this.keyField) | ||
@@ -71,0 +67,0 @@ }) |
const { NestedValidationError } = require('./errors') | ||
const serialize = obj => (obj.serializeValues ? obj.serializeValues() : obj) | ||
const serialize = obj => { | ||
if (obj instanceof Array) { | ||
return obj.map(serialize) | ||
} | ||
return obj && obj.serializeValues ? obj.serializeValues() : obj | ||
} | ||
@@ -5,0 +10,0 @@ const filterUnique = (item, index, src) => src.indexOf(item) === index |
const { FieldError } = require('../errors') | ||
const { concatValidators, filterUnique } = require('../filters') | ||
const { getModelName } = require('./ModelRegistry') | ||
const { getModelName, registerModel } = require('./ModelRegistry') | ||
@@ -78,8 +78,16 @@ let FieldModel = null | ||
static getField (fieldName) { | ||
const [, field] = this.fieldObjects.find( | ||
const fieldAttrs = this.fieldObjects.find( | ||
([name, field]) => name === fieldName | ||
) | ||
if (!fieldAttrs) { | ||
return null | ||
} | ||
const [, field] = fieldAttrs | ||
return field | ||
} | ||
static register () { | ||
return registerModel(this) | ||
} | ||
get (fieldName) { | ||
@@ -86,0 +94,0 @@ const value = this[fieldName] |
@@ -11,7 +11,3 @@ const { DatabaseModelBase } = require('./DatabaseModelBase') | ||
const { Update } = require('../db/Update') | ||
const { | ||
getModelName, | ||
getRelationship, | ||
registerModel | ||
} = require('./ModelRegistry') | ||
const { getModelName, getRelationship } = require('./ModelRegistry') | ||
@@ -53,6 +49,2 @@ class DatabaseModel extends DatabaseModelBase { | ||
static register () { | ||
return registerModel(this) | ||
} | ||
get pk () { | ||
@@ -63,2 +55,6 @@ return this[this.constructor.pkName] | ||
rel (relatedName) { | ||
const field = this.constructor.getField(relatedName) | ||
if (field) { | ||
return field.queryTargetModel(this) | ||
} | ||
return getRelationship(this.constructor, relatedName).queryParentModel(this) | ||
@@ -65,0 +61,0 @@ } |
@@ -25,9 +25,13 @@ const { Select } = require('../db/Select') | ||
async get (filter) { | ||
return await this.query.filter(filter).first() | ||
return await this.query.filter(filter).get() | ||
} | ||
createReadStream () { | ||
return this.query.createReadStream() | ||
async count () { | ||
return await this.query.count() | ||
} | ||
stream () { | ||
return this.query.stream() | ||
} | ||
filter (...args) { | ||
@@ -34,0 +38,0 @@ return this.query.filter(...args) |
{ | ||
"name": "djorm", | ||
"version": "0.1.4", | ||
"version": "0.1.5-alpha.4", | ||
"description": "> TODO: description", | ||
@@ -35,3 +35,3 @@ "author": "Pavel Žák <pavel@zak.global>", | ||
}, | ||
"gitHead": "7487deff2226242039074607d933f862baf5e302" | ||
"gitHead": "6367b21e84590d40d2ea9b14bf25f19ec7d4ce85" | ||
} |
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
47997
53
1703