Skip to content

Commit

Permalink
prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
wmertens committed May 15, 2024
1 parent 40a55e7 commit ed54ba9
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 66 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ coverage/
package-lock.json
package.json
API.md
flake.lock
2 changes: 1 addition & 1 deletion src/DB/DB.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class DBImpl extends SQLite {
if (onBeforeMigrations) await onBeforeMigrations(db)
await this.runMigrations(db)
if (options.onDidOpen) await options.onDidOpen(db)
}
}
super({...options, onDidOpen})
this.options.migrations = migrations
}
Expand Down
11 changes: 6 additions & 5 deletions src/DB/SQLite.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,10 @@ class SQLiteImpl extends EventEmitter {
return _sqlite[method](...(args || []), cb)
})
.catch(cb)
}
}
: () => {
fnResult = _sqlite[method](...(args || []), cb)
}
}
let busyRetry = RETRY_COUNT
// We need to consume `this` from sqlite3 callback
// eslint-disable-next-line unicorn/no-this-assignment, @typescript-eslint/no-this-alias -- we need it
Expand All @@ -400,8 +400,8 @@ class SQLiteImpl extends EventEmitter {
returnFn
? fnResult
: returnThis
? {lastID: this.lastID, changes: this.changes}
: out
? {lastID: this.lastID, changes: this.changes}
: out
)
}
if (!_sqlite[method])
Expand Down Expand Up @@ -510,7 +510,8 @@ class SQLiteImpl extends EventEmitter {
* - the variables to be bound to the statement.
* @param {function(Object): Promise<void>} cb
* - the function to call on each row.
* @returns {Promise<void>} - a promise for execution completion.
* @returns {Promise<void>}
* - a promise for execution completion.
*/
each(...args) {
const lastIdx = args.length - 1
Expand Down
4 changes: 2 additions & 2 deletions src/EventQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class EventQueueImpl extends JsonModel {
`)
// Recalculate size
await db.exec(`UPDATE history SET size=0`)
}
}
: null,
},
})
Expand Down Expand Up @@ -234,7 +234,7 @@ class EventQueueImpl extends JsonModel {
? await this.searchOne(null, {
where: {'v > ?': [Number(v)]},
sort: {v: 1},
})
})
: null
if (event || noWait) break
// Wait for next one from this process
Expand Down
40 changes: 22 additions & 18 deletions src/EventSourcingDB/ESModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,16 @@ class ESModel extends JsonModel {
* Create an event that will insert or replace the given object into the
* database.
*
* @param {Object} obj - the object to store. If there is no `id`
* value (or whatever the `id` column is
* named), one is assigned automatically.
* @param {boolean} [insertOnly] - don't allow replacing existing objects.
* @param {*} [meta] - extra metadata to store in the event but
* not in the object.
* @returns {Pick<ESEvent, 'type' | 'data'>} - args to pass to
* addEvent/dispatch.
* @param {Object} obj
* - the object to store. If there is no `id`
* value (or whatever the `id` column is named), one is assigned
* automatically.
* @param {boolean} [insertOnly]
* - don't allow replacing existing objects.
* @param {*} [meta]
* - extra metadata to store in the event but not in the object.
* @returns {Pick<ESEvent, 'type' | 'data'>}
* - args to pass to addEvent/dispatch.
*/
set: (obj, insertOnly, meta) => {
const data = [insertOnly ? ESModel.INSERT : ESModel.SET, null, obj]
Expand All @@ -157,13 +159,14 @@ class ESModel extends JsonModel {
/**
* Create an event that will update an existing object.
*
* @param {Object} obj - the data to store.
* @param {boolean} [upsert] - if `true`, allow inserting if the object
* doesn't exist.
* @param {*} [meta] - extra metadata to store in the event at
* `data[3]` but not in the object.
* @returns {Pick<ESEvent, 'type' | 'data'>} - args to pass to
* addEvent/dispatch.
* @param {Object} obj
* - the data to store.
* @param {boolean} [upsert]
* - if `true`, allow inserting if the object doesn't exist.
* @param {*} [meta]
* - extra metadata to store in the event at `data[3]` but not in the object.
* @returns {Pick<ESEvent, 'type' | 'data'>}
* - args to pass to addEvent/dispatch.
*/
update: (obj, upsert, meta) => {
const id = obj[this.idCol]
Expand All @@ -185,8 +188,8 @@ class ESModel extends JsonModel {
* - the id or the object itself.
* @param {*} meta
* - metadata, attached to the event only, at `data[3]`
* @returns {Pick<ESEvent, 'type' | 'data'>} - args to pass to
* addEvent/dispatch.
* @returns {Pick<ESEvent, 'type' | 'data'>}
* - args to pass to addEvent/dispatch.
*/
remove: (idOrObj, meta) => {
const id = typeof idOrObj === 'object' ? idOrObj[this.idCol] : idOrObj
Expand Down Expand Up @@ -282,7 +285,8 @@ class ESModel extends JsonModel {
* - the id or the object itself.
* @param {*} meta
* - metadata, attached to the event only, at `data[3]`
* @returns {Promise<boolean>} - always returns true.
* @returns {Promise<boolean>}
* - always returns true.
*/
async remove(idOrObj, meta) {
if (this.writable) return super.remove(idOrObj)
Expand Down
7 changes: 4 additions & 3 deletions src/EventSourcingDB/EventSourcingDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class EventSourcingDB extends EventEmitter {
// Make sure migrations happened before opening
await this.rwDb.open()
},
})
})

if (queue) {
this.queue = queue
Expand Down Expand Up @@ -344,7 +344,7 @@ class EventSourcingDB extends EventEmitter {
...rest,
dispatch,
emitter: this,
})
})
model.preprocessor = preprocessor || Model.preprocessor
model.reducer = fixupOldReducer(name, reducer || Model.reducer)
if (!model.transact) model.transact = transact || Model.transact
Expand Down Expand Up @@ -461,7 +461,8 @@ class EventSourcingDB extends EventEmitter {
* Event data, can be anything.
* @param {number} [ts]
* The timestamp of the event.
* @returns {Promise<Event>} The processed event.
* @returns {Promise<Event>}
* The processed event.
*/
dispatch = makeDispatcher('dispatch', async (type, data, ts) => {
const event = await this.queue.add(type, data, ts)
Expand Down
12 changes: 7 additions & 5 deletions src/JsonModel/JsonModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ class JsonModelImpl {
set(obj, path, {...get(obj, path)})
}
return obj
}
}
: obj => ({...obj})
const colSqls = realCols.map(col => col.quoted)
const setSql = `INTO ${quoted}(${colSqls.join(',')}) VALUES(${colSqls
Expand Down Expand Up @@ -280,7 +280,7 @@ class JsonModelImpl {
newObj[this.idCol] = result.lastID
}
return newObj
})
})
}
}

Expand Down Expand Up @@ -724,8 +724,9 @@ class JsonModelImpl {
* @param {IDValue[]} ids - the values for the column.
* @param {string} [colName=this.idCol] - the columnname, defaults to the
* ID column.
* @returns {Promise<(Item | null)[]>} - the objects, or null where they don't
* exist, in order of their requested ID.
* @returns {Promise<(Item | null)[]>} - the objects, or null where they
* don't exist, in order of their
* requested ID.
*/
async getAll(ids, colName = this.idCol) {
let {path, _getAllSql} = this.columns[colName]
Expand Down Expand Up @@ -798,7 +799,8 @@ class JsonModelImpl {
* @param {JMSearchAttrs | RowCallback} attrsOrFn
* @param {RowCallback | JMSearchOptions} [optionsOrFn]
* @param {RowCallback} [fn]
* @returns {Promise<void>} Table iteration completed.
* @returns {Promise<void>}
* Table iteration completed.
*/
async each(attrsOrFn, optionsOrFn, fn) {
if (!fn) {
Expand Down
2 changes: 1 addition & 1 deletion src/JsonModel/makeDefaultIdValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const makeIdValue = (idCol, {value, slugValue, type} = {}) => {
: o => {
const id = o[idCol]
return id || id === 0 ? id : null
}
}
}
// do not bind the value functions, they must be able to use other db during migrations
if (slugValue) {
Expand Down
8 changes: 4 additions & 4 deletions src/JsonModel/makeMigrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const makeMigrations = ({
? `${type} NOT NULL`
: `${type} PRIMARY KEY ${
isIntegerId && autoIncrement ? 'AUTOINCREMENT' : ''
} NOT NULL`
} NOT NULL`

await db.exec(
`CREATE TABLE ${tableQuoted}(${rowIdCol}${quoted} ${keySql}, json JSON);`
Expand Down Expand Up @@ -61,11 +61,11 @@ export const makeMigrations = ({
col.index
? `CREATE ${
col.unique ? 'UNIQUE ' : ''
}INDEX IF NOT EXISTS ${sql.quoteId(
}INDEX IF NOT EXISTS ${sql.quoteId(
`${tableName}_${name}`
)} ON ${tableQuoted}(${expr}) ${
)} ON ${tableQuoted}(${expr}) ${
col.ignoreNull ? `WHERE ${expr} IS NOT NULL` : ''
};`
};`
: ''
}`
)
Expand Down
8 changes: 4 additions & 4 deletions src/JsonModel/normalizeColumn.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,24 @@ export const normalizeColumn = (col, name) => {
const r = await prev.call(this, o)
if (r == null) throw new Error(`${name}: value is required`)
return r
}
}
: o => {
const v = get(o, col.path)
if (v == null) throw new Error(`${name}: value is required`)
return v
}
}
}
if (col.falsyBool) {
const prev = col.value
col.value = prev
? async function (o) {
const r = await prev.call(this, o)
return r ? true : undefined
}
}
: o => {
const v = get(o, col.path)
return v ? true : undefined
}
}
if (col.real) {
if (col.parse) throw new TypeError(`${name}: falsyBool can't have parse`)
col.parse = v => (v ? true : undefined)
Expand Down
4 changes: 2 additions & 2 deletions src/JsonModel/verifyOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export const columnPropType =
isAnyOfArray: PropTypes.bool,
isArray: PropTypes.bool,
textSearch: PropTypes.bool,
})
})

export const verifyColumn = (name, column) => {
if (process.env.NODE_ENV !== 'production') {
Expand Down Expand Up @@ -172,7 +172,7 @@ const jmPropTypes =
dispatch: PropTypes.any,
emitter: PropTypes.any,
}),
}
}

export const verifyOptions = options => {
if (process.env.NODE_ENV !== 'production') {
Expand Down
8 changes: 4 additions & 4 deletions src/lib/slugify.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ export const slugifyString = (name, alwaysResult) => {
typeof name === 'string'
? name
: typeof name === 'number'
? name.toString()
: name && typeof name === 'object'
? Object.values(name).find(v => typeof v === 'string' && v)
: null
? name.toString()
: name && typeof name === 'object'
? Object.values(name).find(v => typeof v === 'string' && v)
: null
if (!t) {
if (alwaysResult) return randomString(12)
throw new Error(`Cannot slugify ${name}`)
Expand Down
35 changes: 18 additions & 17 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,16 +406,16 @@ interface JsonModel<
IDCol extends string = ConfigOrID extends {idCol: string}
? ConfigOrID['idCol']
: ConfigOrID extends string
? ConfigOrID
: 'id',
? ConfigOrID
: 'id',
Item extends {[x: string]: any} = RealItem extends {[id in IDCol]?: unknown}
? RealItem
: RealItem & {[id in IDCol]: IDValue},
Config = ConfigOrID extends string ? object : ConfigOrID,
Columns extends JMColums<IDCol> = Config extends {columns: object}
? Config['columns']
: // If we didn't get a config, assume all keys are columns
{[colName in keyof Item]: object},
{[colName in keyof Item]: object},
SearchAttrs = JMSearchAttrs<Columns>,
SearchOptions = JMSearchOptions<Columns>,
> {
Expand Down Expand Up @@ -460,8 +460,8 @@ interface JsonModel<
/**
* Search the all matching objects.
*
* @returns - `{items[], cursor}`. If no cursor, you got all the results. If
* `options.itemsOnly`, returns only the items array.
* @returns - `{items[], cursor}`. If no cursor, you got all the results.
* If `options.itemsOnly`, returns only the items array.
*/
search(
/** Simple value attributes. */
Expand Down Expand Up @@ -563,8 +563,8 @@ interface JsonModel<
/**
* Get several objects by their unique value, like their ID.
*
* @returns - the objects, or undefined where they don't exist, in order of
* their requested ID.
* @returns - the objects, or undefined where they don't exist, in order
* of their requested ID.
*/
getAll(
/** The values for the column */
Expand Down Expand Up @@ -625,8 +625,8 @@ interface JsonModel<
* automatically.
* @param [insertOnly] - don't allow replacing existing objects.
* @param [noReturn] - do not return the stored object; an optimization.
* @returns - if `noReturn` is false, the stored object is fetched from the
* DB.
* @returns - if `noReturn` is false, the stored object is fetched from
* the DB.
*/
set(
obj: Partial<Item>,
Expand Down Expand Up @@ -779,8 +779,9 @@ type DispatchFn = (
| [arg: {type: string; data?: any; ts?: number}]
) => Promise<ESEvent>

type AddEventFn =
(...args: | [type: string, data?: any ]| [arg: {type: string; data?: any}]) => void
type AddEventFn = (
...args: [type: string, data?: any] | [arg: {type: string; data?: any}]
) => void

// TODO get from models config
type ESDBModelArgs = {
Expand Down Expand Up @@ -876,8 +877,8 @@ interface ESModel<
IDCol extends string = ConfigOrID extends {idCol: string}
? ConfigOrID['idCol']
: ConfigOrID extends string
? ConfigOrID
: 'id',
? ConfigOrID
: 'id',
Item extends {[x: string]: any} = RealItem extends {[id in IDCol]: unknown}
? RealItem
: RealItem & {[id in IDCol]: IDValue},
Expand Down Expand Up @@ -934,8 +935,8 @@ interface ESModel<
* @param [noReturn] - do not return the stored object; an optimization.
* @param [meta] - extra metadata to store in the event but not in
* the object.
* @returns - if `noReturn` is false, the stored object is fetched from the
* DB.
* @returns - if `noReturn` is false, the stored object is fetched from
* the DB.
*/
set(
obj: Partial<Item>,
Expand All @@ -952,8 +953,8 @@ interface ESModel<
* @param [noReturn] - do not return the stored object; an optimization.
* @param [meta] - extra metadata to store in the event at `data[3]`
* but not in the object.
* @returns - if `noReturn` is false, the stored object is fetched from the
* DB.
* @returns - if `noReturn` is false, the stored object is fetched from
* the DB.
*/
update(
o: Partial<Item>,
Expand Down

0 comments on commit ed54ba9

Please sign in to comment.