@strapi/database
Advanced tools
Comparing version 4.1.11 to 4.1.12
@@ -116,3 +116,3 @@ 'use strict'; | ||
async findOne(uid, params) { | ||
await db.lifecycles.run('beforeFindOne', uid, { params }); | ||
const states = await db.lifecycles.run('beforeFindOne', uid, { params }); | ||
@@ -124,3 +124,3 @@ const result = await this.createQueryBuilder(uid) | ||
await db.lifecycles.run('afterFindOne', uid, { params, result }); | ||
await db.lifecycles.run('afterFindOne', uid, { params, result }, states); | ||
@@ -132,3 +132,3 @@ return result; | ||
async findMany(uid, params) { | ||
await db.lifecycles.run('beforeFindMany', uid, { params }); | ||
const states = await db.lifecycles.run('beforeFindMany', uid, { params }); | ||
@@ -139,3 +139,3 @@ const result = await this.createQueryBuilder(uid) | ||
await db.lifecycles.run('afterFindMany', uid, { params, result }); | ||
await db.lifecycles.run('afterFindMany', uid, { params, result }, states); | ||
@@ -145,4 +145,4 @@ return result; | ||
async count(uid, params = {}) { | ||
await db.lifecycles.run('beforeCount', uid, { params }); | ||
async count(uid, params) { | ||
const states = await db.lifecycles.run('beforeCount', uid, { params }); | ||
@@ -157,3 +157,3 @@ const res = await this.createQueryBuilder(uid) | ||
await db.lifecycles.run('afterCount', uid, { params, result }); | ||
await db.lifecycles.run('afterCount', uid, { params, result }, states); | ||
@@ -164,3 +164,3 @@ return result; | ||
async create(uid, params = {}) { | ||
await db.lifecycles.run('beforeCreate', uid, { params }); | ||
const states = await db.lifecycles.run('beforeCreate', uid, { params }); | ||
@@ -192,3 +192,3 @@ const metadata = db.metadata.get(uid); | ||
await db.lifecycles.run('afterCreate', uid, { params, result }); | ||
await db.lifecycles.run('afterCreate', uid, { params, result }, states); | ||
@@ -200,3 +200,3 @@ return result; | ||
async createMany(uid, params = {}) { | ||
await db.lifecycles.run('beforeCreateMany', uid, { params }); | ||
const states = await db.lifecycles.run('beforeCreateMany', uid, { params }); | ||
@@ -222,3 +222,3 @@ const metadata = db.metadata.get(uid); | ||
await db.lifecycles.run('afterCreateMany', uid, { params, result }); | ||
await db.lifecycles.run('afterCreateMany', uid, { params, result }, states); | ||
@@ -229,3 +229,3 @@ return result; | ||
async update(uid, params = {}) { | ||
await db.lifecycles.run('beforeUpdate', uid, { params }); | ||
const states = await db.lifecycles.run('beforeUpdate', uid, { params }); | ||
@@ -273,3 +273,3 @@ const metadata = db.metadata.get(uid); | ||
await db.lifecycles.run('afterUpdate', uid, { params, result }); | ||
await db.lifecycles.run('afterUpdate', uid, { params, result }, states); | ||
@@ -281,3 +281,3 @@ return result; | ||
async updateMany(uid, params = {}) { | ||
await db.lifecycles.run('beforeUpdateMany', uid, { params }); | ||
const states = await db.lifecycles.run('beforeUpdateMany', uid, { params }); | ||
@@ -300,3 +300,3 @@ const metadata = db.metadata.get(uid); | ||
await db.lifecycles.run('afterUpdateMany', uid, { params, result }); | ||
await db.lifecycles.run('afterUpdateMany', uid, { params, result }, states); | ||
@@ -307,3 +307,3 @@ return result; | ||
async delete(uid, params = {}) { | ||
await db.lifecycles.run('beforeDelete', uid, { params }); | ||
const states = await db.lifecycles.run('beforeDelete', uid, { params }); | ||
@@ -336,3 +336,3 @@ const { where, select, populate } = params; | ||
await db.lifecycles.run('afterDelete', uid, { params, result: entity }); | ||
await db.lifecycles.run('afterDelete', uid, { params, result: entity }, states); | ||
@@ -344,3 +344,3 @@ return entity; | ||
async deleteMany(uid, params = {}) { | ||
await db.lifecycles.run('beforeDeleteMany', uid, { params }); | ||
const states = await db.lifecycles.run('beforeDeleteMany', uid, { params }); | ||
@@ -356,3 +356,3 @@ const { where } = params; | ||
await db.lifecycles.run('afterDelete', uid, { params, result }); | ||
await db.lifecycles.run('afterDeleteMany', uid, { params, result }, states); | ||
@@ -359,0 +359,0 @@ return result; |
@@ -46,3 +46,4 @@ import { Database } from '../'; | ||
clear(): void; | ||
run(action: Action, uid: string, properties: any): Promise<void>; | ||
run(action: Action, uid: string, properties: any): Promise<Map<any, any>>; | ||
run(action: Action, uid: string, properties: any, states: Map<any, any>): Promise<Map<any, any>>; | ||
createEvent(action: Action, uid: string, properties: any): Event; | ||
@@ -49,0 +50,0 @@ } |
@@ -33,3 +33,9 @@ 'use strict'; | ||
createEvent(action, uid, properties) { | ||
/** | ||
* @param {string} action | ||
* @param {string} uid | ||
* @param {{ params?: any, result?: any }} properties | ||
* @param {Map<any, any>} state | ||
*/ | ||
createEvent(action, uid, properties, state) { | ||
const model = db.metadata.get(uid); | ||
@@ -40,2 +46,3 @@ | ||
model, | ||
state, | ||
...properties, | ||
@@ -45,7 +52,18 @@ }; | ||
async run(action, uid, properties) { | ||
for (const subscriber of subscribers) { | ||
/** | ||
* @param {string} action | ||
* @param {string} uid | ||
* @param {{ params?: any, result?: any }} properties | ||
* @param {Map<any, any>} states | ||
*/ | ||
async run(action, uid, properties, states = new Map()) { | ||
for (let i = 0; i < subscribers.length; i++) { | ||
const subscriber = subscribers[i]; | ||
if (typeof subscriber === 'function') { | ||
const event = this.createEvent(action, uid, properties); | ||
const state = states.get(subscriber) || {}; | ||
const event = this.createEvent(action, uid, properties, state); | ||
await subscriber(event); | ||
if (event.state) { | ||
states.set(subscriber, event.state || state); | ||
} | ||
continue; | ||
@@ -58,7 +76,13 @@ } | ||
if (hasAction && hasModel) { | ||
const event = this.createEvent(action, uid, properties); | ||
const state = states.get(subscriber) || {}; | ||
const event = this.createEvent(action, uid, properties, state); | ||
await subscriber[action](event); | ||
if (event.state) { | ||
states.set(subscriber, event.state); | ||
} | ||
} | ||
} | ||
return states; | ||
}, | ||
@@ -65,0 +89,0 @@ }; |
@@ -5,8 +5,13 @@ 'use strict'; | ||
const fse = require('fs-extra'); | ||
const Umzug = require('umzug'); | ||
const { Umzug } = require('umzug'); | ||
const createStorage = require('./storage'); | ||
const wrapTransaction = db => fn => () => | ||
db.getConnection().transaction(trx => Promise.resolve(fn(trx))); | ||
// TODO: check multiple commands in one sql statement | ||
const migrationResolver = path => { | ||
const migrationResolver = ({ name, path, context }) => { | ||
const { db } = context; | ||
// if sql file run with knex raw | ||
@@ -17,3 +22,4 @@ if (path.match(/\.sql$/)) { | ||
return { | ||
up: knex => knex.raw(sql), | ||
name, | ||
up: wrapTransaction(db)(knex => knex.raw(sql)), | ||
down() {}, | ||
@@ -24,3 +30,8 @@ }; | ||
// NOTE: we can add some ts register if we want to handle ts migration files at some point | ||
return require(path); | ||
const migration = require(path); | ||
return { | ||
name, | ||
up: wrapTransaction(db)(migration.up), | ||
down: wrapTransaction(db)(migration.down), | ||
}; | ||
}; | ||
@@ -33,13 +44,8 @@ | ||
const wrapFn = fn => db => db.getConnection().transaction(trx => Promise.resolve(fn(trx))); | ||
const storage = createStorage({ db, tableName: 'strapi_migrations' }); | ||
return new Umzug({ | ||
storage, | ||
storage: createStorage({ db, tableName: 'strapi_migrations' }), | ||
context: { db }, | ||
migrations: { | ||
path: migrationDir, | ||
pattern: /\.(js|sql)$/, | ||
params: [db], | ||
wrap: wrapFn, | ||
customResolver: migrationResolver, | ||
glob: ['*.{js,sql}', { cwd: migrationDir }], | ||
resolve: migrationResolver, | ||
}, | ||
@@ -46,0 +52,0 @@ }); |
@@ -17,7 +17,7 @@ 'use strict'; | ||
return { | ||
async logMigration(migrationName) { | ||
async logMigration({ name }) { | ||
await db | ||
.getConnection() | ||
.insert({ | ||
name: migrationName, | ||
name, | ||
time: new Date(), | ||
@@ -28,7 +28,7 @@ }) | ||
async unlogMigration(migrationName) { | ||
async unlogMigration({ name }) { | ||
await db | ||
.getConnection(tableName) | ||
.del() | ||
.where({ name: migrationName }); | ||
.where({ name }); | ||
}, | ||
@@ -35,0 +35,0 @@ |
{ | ||
"name": "@strapi/database", | ||
"version": "4.1.11", | ||
"version": "4.1.12", | ||
"description": "Strapi's database layer", | ||
@@ -39,3 +39,3 @@ "homepage": "https://strapi.io", | ||
"lodash": "4.17.21", | ||
"umzug": "2.3.0" | ||
"umzug": "3.1.1" | ||
}, | ||
@@ -46,3 +46,3 @@ "engines": { | ||
}, | ||
"gitHead": "698ae33e0007811ce24b568cc5d873d7a4d2a832" | ||
"gitHead": "ab698015cfc4f43fa0ce2ae94ec59e00a67e4cda" | ||
} |
172630
49
5567
+ Added@rushstack/node-core-library@5.11.0(transitive)
+ Added@rushstack/terminal@0.14.6(transitive)
+ Added@rushstack/ts-command-line@4.23.4(transitive)
+ Added@types/argparse@1.0.38(transitive)
+ Addedajv@8.13.0(transitive)
+ Addedajv-draft-04@1.0.0(transitive)
+ Addedajv-formats@3.0.1(transitive)
+ Addedargparse@1.0.10(transitive)
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addedemittery@0.10.2(transitive)
+ Addedfast-deep-equal@3.1.3(transitive)
+ Addedfs-extra@11.3.0(transitive)
+ Addedfs-jetpack@4.3.1(transitive)
+ Addedfs.realpath@1.0.0(transitive)
+ Addedglob@7.2.3(transitive)
+ Addedhas-flag@4.0.0(transitive)
+ Addedimport-lazy@4.0.0(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedjju@1.4.0(transitive)
+ Addedjson-schema-traverse@1.0.0(transitive)
+ Addedlru-cache@6.0.0(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedpath-is-absolute@1.0.1(transitive)
+ Addedpony-cause@1.1.1(transitive)
+ Addedpunycode@2.3.1(transitive)
+ Addedrequire-from-string@2.0.2(transitive)
+ Addedrimraf@2.7.1(transitive)
+ Addedsemver@7.5.4(transitive)
+ Addedsprintf-js@1.0.3(transitive)
+ Addedstring-argv@0.3.2(transitive)
+ Addedsupports-color@8.1.1(transitive)
+ Addedtype-fest@2.19.0(transitive)
+ Addedumzug@3.1.1(transitive)
+ Addeduri-js@4.4.1(transitive)
+ Addedwrappy@1.0.2(transitive)
+ Addedyallist@4.0.0(transitive)
- Removedbluebird@3.7.2(transitive)
- Removedumzug@2.3.0(transitive)
Updatedumzug@3.1.1