Compare commits

...

2 Commits

Author SHA1 Message Date
Nikita Dezzpil Orlov
8e619112cd chore(release): 1.2.0 2021-10-04 18:20:04 +03:00
Nikita Dezzpil Orlov
d0942e59d9 feat: добавил Entity Manager 2021-10-04 18:19:54 +03:00
6 changed files with 89 additions and 3 deletions

View File

@ -2,6 +2,13 @@
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
## [1.2.0](https://git.archive.systems/Dezzpil/ivanovna.orm/compare/v1.1.3...v1.2.0) (2021-10-04)
### Features
* добавил Entity Manager ([d0942e5](https://git.archive.systems/Dezzpil/ivanovna.orm/commit/d0942e59d97d861aa3368ee96a3a15f0418263e4))
### [1.1.3](https://git.archive.systems/Dezzpil/ivanovna.orm/compare/v1.1.2...v1.1.3) (2021-10-04)

14
orm.d.ts vendored
View File

@ -26,6 +26,20 @@ export declare abstract class Entity<T extends Data> {
/** @public */
export declare type EntityConstructor<T extends Data> = new (data?: T) => Entity<T>;
/** @public */
export declare class EntityManager {
private readonly _storage;
private _saveMap;
private _removeMap;
constructor(storage: Storage_2);
persist(entity: Entity<any>): this;
persistMany(entities: Entity<any>[]): this;
remove(entity: Entity<any>): this;
forget(): this;
flush(): Promise<this>;
refresh(entity: Entity<any>): Promise<Entity<any>>;
}
/** @public */
export declare class ErrEntityHasNoUniqKeyValue extends Error {
}

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{
"name": "ivna-orm",
"version": "1.1.3",
"version": "1.2.0",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "ivna-orm",
"version": "1.1.3",
"version": "1.2.0",
"license": "ISC",
"devDependencies": {
"@jest/test-sequencer": "^27.2.3",

View File

@ -1,6 +1,6 @@
{
"name": "ivna-orm",
"version": "1.1.3",
"version": "1.2.0",
"description": "Mini ORM for convenience",
"main": "dist/index.js",
"files": [

64
src/app/entity/manager.ts Normal file
View File

@ -0,0 +1,64 @@
import { Entity } from '../entity'
import { Storage } from '../storage'
/** @public */
export class EntityManager {
private readonly _storage: Storage
private _saveMap: Map<string, Entity<any>>
private _removeMap: Map<string, Entity<any>>
constructor(storage: Storage) {
this._storage = storage
this._saveMap = new Map()
this._removeMap = new Map()
}
persist(entity: Entity<any>): this {
const key = [entity.constructor.name, entity.getUniqKey()].join('&')
this._saveMap.set(key, entity)
return this
}
persistMany(entities: Entity<any>[]): this {
for (let entity of entities) {
this.persist(entity)
}
return this
}
remove(entity: Entity<any>): this {
const key = [entity.constructor.name, entity.getUniqKey()].join('&')
this._removeMap.set(key, entity)
return this
}
forget(): this {
this._saveMap = new Map()
return this
}
async flush() {
if (!this._saveMap.size && !this._removeMap.size) return this
const session = this._storage.createSession()
await session.start()
await session.commit(async () => {
for (let en of this._saveMap.values()) {
await en._getRepo(this._storage).save(en)
}
this._saveMap = new Map()
for (let en of this._removeMap.values()) {
await en._getRepo(this._storage).remove(en)
}
this._removeMap = new Map()
})
return this
}
async refresh(entity: Entity<any>) {
const repo = entity._getRepo(this._storage)
return repo.findById(entity.getUniqKey())
}
}

View File

@ -6,3 +6,4 @@ export { Data, ValuesObject } from './app/data'
export { Entity, EntityConstructor, ErrEntityHasNoUniqKeyValue } from './app/entity'
export { Repo, ErrFoundNotUniqEntity, ErrEntityNotFound } from './app/repo'
export { Storage, StorageCursor, ErrStorage, ErrNoSession, StorageSession } from './app/storage'
export { EntityManager } from './app/entity/manager'