Skip to content

Commit

Permalink
Merge pull request #685 from toa-io/dev
Browse files Browse the repository at this point in the history
Alpha release
  • Loading branch information
temich committed Jul 1, 2024
2 parents 51b3896 + e95ea74 commit 7c6bc52
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 24 deletions.
8 changes: 5 additions & 3 deletions connectors/storages.mongodb/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const { MongoClient } = require('mongodb')
const INSTANCES = {}

class Client extends Connector {
name

/**
* @public
* @type {import('mongodb').Collection}
Expand Down Expand Up @@ -58,8 +60,8 @@ class Client extends Connector {
async open () {
const urls = await this.resolveURLs()
const dbname = this.resolveDB()
const collname = this.locator.lowercase

this.name = this.locator.lowercase
this.key = getKey(dbname, urls)

INSTANCES[this.key] ??= this.createInstance(urls)
Expand All @@ -70,13 +72,13 @@ class Client extends Connector {
const db = this.instance.client.db(dbname)

try {
this.collection = await db.createCollection(collname)
this.collection = await db.createCollection(this.name)
} catch (e) {
if (e.code !== ALREADY_EXISTS) {
throw e
}

this.collection = db.collection(collname)
this.collection = db.collection(this.name)
}
}

Expand Down
2 changes: 1 addition & 1 deletion connectors/storages.mongodb/src/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class Storage extends Connector {
if (id)
return false
else
throw new exceptions.DuplicateException()
throw new exceptions.DuplicateException(this.#client.name, entity)
} else if (error.cause?.code === 'ECONNREFUSED') {
// This is temporary and should be replaced with a class decorator.
if (attempt > 10)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ entity:
operations:
transit:
concurrency: retry
input:
name: string
email: string
8 changes: 6 additions & 2 deletions features/storages/mongodb.indexes.feature
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ Feature: MongoDB indexes
Then the following exception is thrown:
"""yaml
code: 306
message: DuplicateException
message: 'DuplicateException: mongo_indexed'
cause:
email: john@example.com
"""
# see collection indexes in the database

Expand All @@ -33,5 +35,7 @@ Feature: MongoDB indexes
Then the following exception is thrown:
"""yaml
code: 306
message: DuplicateException
message: 'DuplicateException: mongo_indexed'
cause:
email: john@example.com
"""
21 changes: 17 additions & 4 deletions libraries/schemas/source/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ class Schema {
/** @type {import('ajv').ValidateFunction} */
#validateOptional

/** @type {import('ajv').ValidateFunction} */
#match

/**
* @param {import('ajv').ValidateFunction} validate
* @param {import('ajv').ValidateFunction} validateOptional
*/
constructor (validate, validateOptional) {
constructor (validate, validateOptional, match) {
this.id = validate.schema.$id
this.#validate = validate
this.#validateOptional = validateOptional
this.#match = match
}

fit (value, validate = this.#validate) {
Expand All @@ -40,6 +44,13 @@ class Schema {
return this.fit(value, this.#validateOptional)
}

match (value) {
if (this.#match() === undefined)
throw new Error('Matching schema is not defined')

return this.fit(value, this.#match)
}

validate (value, message) {
const valid = this.#validate(value)

Expand Down Expand Up @@ -82,14 +93,16 @@ const schema = (cos, options) => {
const validate = create(schema, options)

let validateOptional
let match

if (schema.type === 'object') {
const { required, ...rest } = schema
const { required, ...optional } = schema

validateOptional = ajv().compile(rest)
validateOptional = create(optional)
match = create(optional, { useDefaults: false })
}

return new Schema(validate, validateOptional)
return new Schema(validate, validateOptional, match)
}

exports.Schema = Schema
Expand Down
4 changes: 2 additions & 2 deletions libraries/schemas/source/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ function is (schema) {
* @param {object[]} [schemas]
* @param {object} [additional]
*/
function ajv (schemas, additional = {}) {
const options = Object.assign({ schemas }, additional, OPTIONS)
function ajv (schemas, override = {}) {
const options = Object.assign({ schemas }, OPTIONS, override)
const ajv = new Ajv(options)

formats(ajv)
Expand Down
2 changes: 1 addition & 1 deletion runtime/core/src/contract/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Contract {
const error = this.schema.fit(value)

if (error !== null)
throw new this.constructor.Exception(error)
throw new this.constructor.Exception(error, value)
}

static Exception = SystemException
Expand Down
4 changes: 2 additions & 2 deletions runtime/core/src/entities/changeset.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ class Changeset {
}

set (value) {
const error = this.#schema.fitOptional(value)
const error = this.#schema.match(value)

if (error !== null)
throw new EntityContractException(error)
throw new EntityContractException(error, value)

delete value._version
value._updated = Date.now()
Expand Down
2 changes: 1 addition & 1 deletion runtime/core/src/entities/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Entity {
const error = optional ? this.#schema.fitOptional(value) : this.#schema.fit(value)

if (error !== null)
throw new EntityContractException(error)
throw new EntityContractException(error, value)

this.#set(value)
}
Expand Down
23 changes: 15 additions & 8 deletions runtime/core/src/exceptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ class Exception {
code
message

constructor (code, message) {
constructor (code, message, cause) {
this.code = code
this.message = message

if (cause !== undefined)
this.cause = cause
}
}

Expand All @@ -48,8 +51,8 @@ class SystemException extends Exception {
}

class ContractException extends Exception {
constructor (code, error) {
super(code || codes.Contract, typeof error === 'string' ? error : error?.message)
constructor (code, error, cause) {
super(code || codes.Contract, typeof error === 'string' ? error : error?.message, cause)

if (typeof error === 'object' && error !== null)
for (const k of ['keyword', 'property', 'schema', 'path', 'params'])
Expand All @@ -59,15 +62,15 @@ class ContractException extends Exception {
}

class RequestContractException extends ContractException {
constructor (error) { super(codes.RequestContract, error) }
constructor (error, cause) { super(codes.RequestContract, error, cause) }
}

class ResponseContractException extends ContractException {
constructor (error) { super(codes.ResponseContract, error) }
constructor (error, cause) { super(codes.ResponseContract, error, cause) }
}

class EntityContractException extends ContractException {
constructor (error) { super(codes.EntityContract, error) }
constructor (error, cause) { super(codes.EntityContract, error, cause) }
}

// #region exports
Expand All @@ -82,8 +85,12 @@ for (const [name, code] of Object.entries(codes)) {

if (exports[classname] === undefined) {
exports[classname] = class extends Exception {
constructor (message) {
super(code, message ?? classname)
constructor (message, cause) {
message = message
? `${classname}: ${message}`
: classname

super(code, message ?? classname, cause)
}
}
}
Expand Down

0 comments on commit 7c6bc52

Please sign in to comment.