-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make @wq/model ORM a plugin & add reset()
- Loading branch information
Showing
10 changed files
with
77 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { Model, model as createModel } from './model.js'; | ||
|
||
const orm = { | ||
// Plugin attributes | ||
name: 'orm', | ||
type: 'orm', | ||
init() { | ||
for (const conf of Object.values(this.app.config.pages)) { | ||
if (!conf.list) { | ||
continue; | ||
} | ||
const model = createModel(conf); | ||
this.models[conf.name] = model; | ||
if (!this._orm) { | ||
this._orm = model.orm; | ||
} | ||
} | ||
this.app.models = this.models; | ||
this.app.prefetchAll = message => this.prefetchAll(message); | ||
this.app.resetAll = () => this.reset(); | ||
}, | ||
actions: { | ||
reset() { | ||
return { type: 'ORM_RESET' }; | ||
} | ||
}, | ||
reducer(state, action) { | ||
if (!this._orm) { | ||
return state; | ||
} | ||
return this._orm.reducer(state, action); | ||
}, | ||
persist: true, | ||
|
||
// Custom attributes | ||
models: {}, | ||
async prefetchAll(message) { | ||
if (message) { | ||
this.app.spin.start(message); | ||
} | ||
const result = await Promise.all( | ||
Object.values(this.models).map(model => model.prefetch()) | ||
); | ||
if (message) { | ||
this.app.spin.stop(message); | ||
} | ||
return result; | ||
} | ||
}; | ||
|
||
export default orm; | ||
|
||
export { Model, createModel, createModel as model }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters