Compare commits

...

7 Commits

Author SHA1 Message Date
Nikita Dezzpil Orlov
8b62947466 chore(release): 1.4.0 2022-07-13 13:27:17 +03:00
Nikita Dezzpil Orlov
3a684a177c feat: доработка ридми 2022-07-13 13:27:09 +03:00
Nikita Dezzpil Orlov
def5b75101 fix: обновил ts 2022-01-31 18:00:28 +03:00
Nikita Dezzpil Orlov
aa38fae8cc fix: Исправил ошибку метода Entity.isPersisted 2022-01-31 17:40:17 +03:00
Nikita Dezzpil Orlov
41aa5d951a fix: Исправил ошибку установки внутреного айди для записи при создании сущности 2022-01-31 17:38:53 +03:00
Nikita Dezzpil Orlov
79cdca410e feat: Принес класс хранилища SQLite и зависимости 2022-01-31 16:11:08 +03:00
Nikita Dezzpil Orlov
e799da6a48 fix: Перенес модули монги в optionalDependencies 2022-01-31 15:34:34 +03:00
16 changed files with 2146 additions and 224 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
node_modules
dist
*.db

17
.idea/dataSources.xml Normal file
View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
<data-source source="LOCAL" name="sqlite.test" uuid="4938db2d-685c-43b9-a8f6-d56ef172daf6">
<driver-ref>sqlite.xerial</driver-ref>
<synchronize>true</synchronize>
<jdbc-driver>org.sqlite.JDBC</jdbc-driver>
<jdbc-url>jdbc:sqlite:$PROJECT_DIR$/src/tests/repo/sqlite.test.db</jdbc-url>
<working-dir>$ProjectFileDir$</working-dir>
<libraries>
<library>
<url>file://$APPLICATION_CONFIG_DIR$/jdbc-drivers/Xerial SQLiteJDBC/3.34.0/sqlite-jdbc-3.34.0.jar</url>
</library>
</libraries>
</data-source>
</component>
</project>

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RunConfigurationProducerService">
<option name="ignoredProducers">
<set>
<option value="com.android.tools.idea.compose.preview.runconfiguration.ComposePreviewRunConfigurationProducer" />
</set>
</option>
</component>
</project>

View File

@ -2,6 +2,22 @@
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.4.0](https://git.archive.systems/Dezzpil/ivanovna.orm/compare/v1.3.3...v1.4.0) (2022-07-13)
### Features
* доработка ридми ([3a684a1](https://git.archive.systems/Dezzpil/ivanovna.orm/commit/3a684a177c28b1682ad776bd62d84488fa0924b7))
* Принес класс хранилища SQLite и зависимости ([79cdca4](https://git.archive.systems/Dezzpil/ivanovna.orm/commit/79cdca410e9c8316845c5373fe4c0449d2eb26c4))
### Bug Fixes
* Исправил ошибку метода Entity.isPersisted ([aa38fae](https://git.archive.systems/Dezzpil/ivanovna.orm/commit/aa38fae8cc52b84837d91d7e244d7be1efb119da))
* Исправил ошибку установки внутреного айди для записи при создании сущности ([41aa5d9](https://git.archive.systems/Dezzpil/ivanovna.orm/commit/41aa5d951ae1e5ddcca822621363c605c2fcf08b))
* обновил ts ([def5b75](https://git.archive.systems/Dezzpil/ivanovna.orm/commit/def5b7510137f0da0a7432e5a62498c6a115c6a9))
* Перенес модули монги в optionalDependencies ([e799da6](https://git.archive.systems/Dezzpil/ivanovna.orm/commit/e799da6a48ed975f9aa1847dbca49a14ea3e05bf))
### [1.3.3](https://git.archive.systems/Dezzpil/ivanovna.orm/compare/v1.3.2...v1.3.3) (2021-10-13)

View File

@ -4,3 +4,19 @@
Позволяет задать модели для таблиц или коллекций и предоставляет менеджер сущностей для манипуляций с ними.
## Тестирование
```shell
npm test
```
## Процедура обновления версии
Пример процедуры:
```shell
git add .
git commit -m'fix: исправлен баг'
```

View File

@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
exports.default = {
roots: ['<rootDir>/src/tests/'],
transform: {
'^.+\\.(ts|tsx)$': 'ts-jest',
'^.+\\.(ts|tsx|js)$': 'ts-jest',
},
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],

1783
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{
"name": "ivna-orm",
"version": "1.3.3",
"version": "1.4.0",
"description": "Mini ORM for convenience",
"main": "dist/index.js",
"files": [
@ -16,8 +16,7 @@
"test": "node --no-warnings node_modules/.bin/jest --runInBand --forceExit",
"build": "rimraf dist && tsc -b -v",
"build:api": "npm run build && api-extractor run && rimraf 'dist/**/*.d.ts*'",
"release": "standard-version -i HISTORY.md",
"prepare": "npm test && npm run build:api"
"release": "standard-version -i HISTORY.md"
},
"repository": {
"type": "git",
@ -46,11 +45,13 @@
"standard-version": "^9.3.1",
"ts-jest": "^27.0.5",
"ts-node": "^10.2.1",
"typescript": "^4.4.3"
"typescript": "^4.5.5"
},
"peerDependencies": {
"optionalDependencies": {
"mongodb": "^4.1.2",
"mongodb-client-encryption": "^1.2.7"
"mongodb-client-encryption": "^1.2.7",
"sqlite": "^4.0.23",
"sqlite3": "^5.0.2"
},
"prettier": {
"arrowParens": "avoid",

View File

@ -48,7 +48,7 @@ export abstract class Entity<T extends Data> {
}
isPersisted(): boolean {
return this.__id !== ''
return this.__id !== undefined && this.__id !== ''
}
toString() {

35
src/app/repo/sqlite.ts Normal file
View File

@ -0,0 +1,35 @@
import { Entity } from '../entity'
import { ErrEntityNotFound, ErrFoundNotUniqEntity, Repo } from '../repo'
import { StorageSQLite } from '../storage/sqlite'
export abstract class RepoSQLite<T extends Entity<any>> extends Repo<T> {
abstract Create(): Promise<void>
async Drop(): Promise<void> {
const st = this._storage as StorageSQLite
await st?.db?.run(`DROP TABLE IF EXISTS ${this.Name()}`)
}
async Recreate(): Promise<void> {
await this.Drop()
await this.Create()
}
async findById(id: string): Promise<T> {
const uniqKey = this.Entity().data.uniqKey()
const cond = `${uniqKey} = ?`
const cursor = await this._storage.find(this.Name(), { [cond]: [id] })
const entryList = await cursor.toArray()
if (entryList.length > 1) {
throw new ErrFoundNotUniqEntity(
`found few (${entryList.length}) entries in ${this.Name()} for id ${id}`
)
}
if (entryList.length === 0) {
throw new ErrEntityNotFound(`not found entry in [${this.Name()}] for id [${id}]`)
}
return this._transformer(entryList[0])
}
}

207
src/app/storage/sqlite.ts Normal file
View File

@ -0,0 +1,207 @@
import { Database, open } from 'sqlite'
import sqlite3 from 'sqlite3'
import { ErrStorage, Storage, StorageCursor, StorageSession } from '../storage'
import { ErrEntityHasNoUniqKeyValue } from '../entity'
export type SQLiteCriteria = Record<string, Array<string | number>>
function parseCriteria(criteria?: SQLiteCriteria): { where: string | null; params: Array<string | number> } {
if (criteria) {
const conditions = Object.entries(criteria)
const params = []
const where = []
if (conditions.length) {
for (const condition of conditions) {
where.push(condition[0].trim())
params.push(...condition[1])
}
return { where: where.join(' AND '), params }
}
}
return { where: null, params: [] }
}
function quoteStr(val: string | number | unknown) {
if (typeof val === 'string') {
return `'${val.replace(/'/g, `"`)}'`
}
return val
}
function transformDate(val: Date | unknown) {
return val instanceof Date ? `'${val.getTime()}'` : val
}
export interface StorageSQLiteParams {
dsn: string
}
export class StorageSQLite extends Storage {
private _db: Database | null = null
constructor(params: StorageSQLiteParams) {
super(params.dsn)
}
async connect(): Promise<Database> {
try {
sqlite3.verbose()
this._db = await open({
filename: this._dsn,
driver: sqlite3.Database,
})
return this._db
} catch (e) {
throw new ErrStorage(`error on connect to db [${this._dsn}]`)
}
}
async count(name: string, criteria?: SQLiteCriteria): Promise<number> {
if (!this._db) await this.connect()
const query = [`SELECT COUNT(*) as cnt FROM ${name}`]
const { where, params } = parseCriteria(criteria)
if (where) {
query.push(`WHERE ${where}`)
}
const result = await this._db?.get(query.join(' '), params)
return parseInt(result.cnt, 10)
}
async find(name: string, criteria: SQLiteCriteria): Promise<SQLiteCursor> {
if (!this._db) this._db = await this.connect()
return new SQLiteCursor(this._db, name, criteria)
}
/**
* Вернет тру, даже если записи не было
* @param name
* @param uniqKey
* @param uniqValue
*/
async remove(name: string, uniqKey: string, uniqValue: string): Promise<boolean> {
if (!this._db) this._db = await this.connect()
const sql = `DELETE FROM ${name} WHERE ${uniqKey}=${quoteStr(uniqValue)}`
const result = await this._db.run(sql)
return !!(result && result.changes)
}
async save(name: string, uniqKey: string, data: Record<string, unknown>): Promise<string> {
if (!this._db) this._db = await this.connect()
const keys = []
const values = []
const assigns = []
for (const [k, v] of Object.entries(data)) {
if (v === undefined) continue
keys.push(k)
let vp
if (v === null) {
vp = 'NULL'
} else {
vp = quoteStr(v)
vp = transformDate(vp)
}
values.push(vp)
assigns.push(`${k}=${vp}`)
}
const id = data[uniqKey]
if (id !== null && id !== undefined) {
const sql = `INSERT INTO ${name}(${keys.join(', ')})
VALUES (${values.join(', ')})
ON CONFLICT(${uniqKey}) DO UPDATE SET ${assigns.join(', ')}
WHERE ${uniqKey} = ${quoteStr(id)}`
try {
const result = await this._db.run(sql)
return result.lastID + ''
} catch (e) {
throw new ErrStorage(`can not save data to ${name} by sql:\n"${sql}"\nwith error: ${e}`)
}
} else {
throw new ErrEntityHasNoUniqKeyValue()
}
}
createSession(): StorageSession {
return new SQLiteSession(this._db)
}
get db(): Database | null {
return this._db
}
}
/**
*
*/
export class SQLiteCursor implements StorageCursor {
private _limit = 0
private _offset = 0
private _order: Record<string, string> = {}
constructor(private _db: Database, private _tableName: string, private _criteria?: SQLiteCriteria) {}
limit(number: number): this {
this._limit = number
return this
}
skip(offset: number): this {
this._offset = offset
return this
}
sort(parameters: Record<string, string>): this {
this._order = parameters
return this
}
_buildSQL(): { query: string[]; params: (string | number)[] } {
const query = [`SELECT ROWID as '_id', * FROM ${this._tableName}`]
const { where, params } = parseCriteria(this._criteria)
if (where) {
query.push(`WHERE ${where}`)
}
const orders = Object.entries(this._order)
if (orders.length) {
const orderStr = orders.map(e => `${e[0]} ${e[1]}`).join(', ')
query.push(`ORDER BY ${orderStr}`)
}
if (this._limit) query.push(`LIMIT ${this._limit}`)
if (this._offset) query.push(`OFFSET ${this._offset}`)
return { query, params }
}
async toArray(): Promise<Record<string, any>[]> {
const { query, params } = this._buildSQL()
return this._db.all(query.join(' '), params.length ? params : [])
}
}
/**
*
*/
export class SQLiteSession extends StorageSession {
constructor(private _db: Database | null) {
super()
}
async start(): Promise<void> {
await this._db?.run('BEGIN TRANSACTION')
return
}
async commit(fn: () => void): Promise<void> {
try {
await fn()
} catch (e) {
await this._db?.run('ROLLBACK')
throw e
}
await this._db?.run('COMMIT')
return
}
}

View File

@ -21,12 +21,12 @@ class Item extends Entity<ItemData> {
return this._data.c
}
_getRepo(storage: Storage): ItemRepository {
return new ItemRepository(storage)
_getRepo(storage: Storage): ItemRepo {
return new ItemRepo(storage)
}
}
class ItemRepository extends Repo<Item> {
class ItemRepo extends Repo<Item> {
Name() {
return 'items'
}
@ -36,10 +36,10 @@ class ItemRepository extends Repo<Item> {
}
}
describe('ItemRepository', () => {
describe('ItemRepo', () => {
describe('map storage save', () => {
const storage = new MapStorage('')
const repo = new ItemRepository(storage)
const repo = new ItemRepo(storage)
it('should return entity', async () => {
const dt = Data.assign(new ItemData(), { a: 1, b: 2, c: { d: 3 } })

42
src/tests/repo/helper.ts Normal file
View File

@ -0,0 +1,42 @@
import { Data } from '../../app/data'
import { Entity } from '../../app/entity'
import { RepoSQLite } from '../../app/repo/sqlite'
import { StorageSQLite } from '../../app/storage/sqlite'
import { Storage } from '../../app/storage'
export class ItemData extends Data {
uniqKey(): string {
return 'uid'
}
uid: string
title: string
}
export class ItemEntity extends Entity<ItemData> {
_getRepo(storage: Storage): ItemsRepo {
return new ItemsRepo(storage)
}
_getVO(): ItemData {
return new ItemData()
}
}
export class ItemsRepo extends RepoSQLite<ItemEntity> {
Entity(): ItemEntity {
return new ItemEntity()
}
Name(): string {
return 'items'
}
async Create(): Promise<void> {
const st = this._storage as StorageSQLite
await st?.db?.run(`
create table if not exists ${this.Name()} (
uid varchar(255) not null unique,
title text null
)
`)
}
}

View File

@ -0,0 +1,33 @@
import { StorageSQLite } from '../../app/storage/sqlite'
import { assert } from 'chai'
import { ItemData, ItemEntity, ItemsRepo } from './helper'
describe('ItemsRepoSQLite', () => {
const dsn = __dirname + `/sqlite.test.db`
let storage: StorageSQLite
let repo: ItemsRepo
beforeAll(async () => {
storage = new StorageSQLite({ dsn })
await storage.connect()
storage.db.on('trace', (data: string) => {
console.log(data)
})
repo = new ItemsRepo(storage)
await repo.Recreate()
for (const uid of ['foo', 'bar', 'pew', 'baz', 'fox']) {
const d = new ItemData()
d.uid = uid
const e = new ItemEntity(d)
await repo.save(e)
}
})
describe('#findById', () => {
it('should return persisted entity', async () => {
const e = await repo.findById('foo')
assert.isTrue(e.isPersisted())
assert.isDefined(e._id)
})
})
})

View File

@ -0,0 +1,137 @@
import { assert } from 'chai'
import { StorageSQLite } from '../../../app/storage/sqlite'
const abbrList = [
['bar', '{}', 2],
['pew', '{}', 2],
['baz', '{}', 2],
['xyz', '{}', 1],
['yes', '{}', 1],
['lol', JSON.stringify({ hello: 'world' }), 1],
['idk', JSON.stringify({ pew: 1 }), 1],
['gg', JSON.stringify({ pew: 100 }), 1],
]
const dsn = __dirname + `/sqlite.test.db`
describe('StorageSQLite', () => {
interface Item {
abbr: string
foo: string
n: number
}
let storage: StorageSQLite
beforeAll(async () => {
storage = new StorageSQLite({ dsn })
await storage.connect()
storage.db.on('trace', (data: string) => {
console.log(data)
})
await storage.db.run('drop table if exists items')
// UNIQUE нужно, чтобы работал UPSERT в методе save()
await storage.db.run(
'create table items (abbr varchar(4) not null unique, foo text null, num integer not null default 0)'
)
const stmt = await storage.db.prepare('insert into "items" (abbr, foo, num) values (?, ?, ?)')
for (const values of abbrList) {
await stmt.run(values)
}
})
describe('#find', () => {
it('should find all', async () => {
const cursor = await storage.find('items', {})
const items = await cursor.toArray()
assert.equal(items.length, abbrList.length)
})
it('should find only one', async () => {
const cursor = await storage.find('items', { 'abbr = ?': ['bar'] })
const items = await cursor.toArray()
assert.equal(items.length, 1)
})
it('should find 2 with offset', async () => {
const cursor = await storage.find('items', {})
const items = await cursor.limit(2).toArray()
assert.equal(items.length, 2)
const nextItems = await cursor.skip(2).toArray()
assert.equal(nextItems.length, 2)
assert.notDeepEqual(items[0], nextItems[0])
assert.notDeepEqual(items[1], nextItems[1])
})
it('should find reversed order ', async () => {
const cursor = await storage.find('items', {})
const items = await cursor.sort({ abbr: 'DESC' }).toArray()
assert.equal(items.length, abbrList.length)
assert.equal(items[0].abbr, 'yes')
})
it('should find and get string from string field for json', async () => {
const cursor = await storage.find('items', { 'abbr = ?': ['gg'] })
const items = await cursor.toArray()
assert.lengthOf(items, 1)
const item = items[0] as Item
assert.equal(typeof item.foo, 'string')
assert.deepEqual(JSON.parse(item.foo), { pew: 100 })
})
it('should find with criteria with many params 1', async () => {
const cursor = await storage.find('items', { 'num = ?': [1], 'foo != ?': ['{}'] })
const items = await cursor.toArray()
assert.lengthOf(items, 3)
})
it('should find with criteria with many params 2', async () => {
const cursor = await storage.find('items', { 'num = ?': [2], 'foo = ?': ['{}'] })
const items = await cursor.toArray()
assert.lengthOf(items, 3)
})
})
describe('#count', () => {
it('should return total count', async () => {
const count = await storage.count('items', {})
assert.equal(count, abbrList.length)
})
it('should return count with criteria', async () => {
const count1 = await storage.count('items', { "abbr LIKE '%'": [] })
assert.equal(count1, abbrList.length)
const count2 = await storage.count('items', { 'abbr = ? OR abbr = ?': ['bar', 'pew'] })
assert.equal(count2, 2)
})
})
describe('#save', () => {
it('should save new item', async () => {
let lastId = await storage.save('items', 'abbr', { abbr: 'banzay' })
assert.equal(lastId, abbrList.length + 1 + '')
abbrList.push(['banzay', '{}'])
lastId = await storage.save('items', 'abbr', { abbr: 'babam' })
assert.equal(lastId, abbrList.length + 1 + '')
abbrList.push(['babam', '{}'])
})
it('should update item', async () => {
let lastId = await storage.save('items', 'abbr', { abbr: 'kish' })
assert.equal(lastId, abbrList.length + 1 + '')
abbrList.push(['kish', '{}'])
lastId = await storage.save('items', 'abbr', { abbr: 'kish' })
assert.equal(lastId, abbrList.length + '')
})
})
describe('#remove', () => {
it('should remove item', async () => {
const lastId = await storage.save('items', 'abbr', { abbr: 'thx' })
assert.equal(lastId, abbrList.length + 1 + '')
const deleted = await storage.remove('items', 'abbr', 'thx')
assert.isTrue(deleted)
const count = await storage.count('items')
assert.equal(abbrList.length, count)
})
})
})

View File

@ -0,0 +1,46 @@
import { assert } from 'chai'
import { StorageSQLite } from '../../../app/storage/sqlite'
const itemsData = ['zero', 'one', 'two', 'three', 'four', 'five']
const dsn = __dirname + '/sqlsession.test.db'
describe('SQLiteSession', () => {
let storage: StorageSQLite
beforeAll(async () => {
storage = new StorageSQLite({ dsn })
await storage.connect()
storage.db.on('trace', (data: string) => {
console.log(data)
})
await storage.db.run('DROP TABLE IF EXISTS items')
// UNIQUE нужно, чтобы работал UPSERT в методе save()
await storage.db.run(
'CREATE TABLE items (key varchar(8) NOT NULL UNIQUE, val integer NOT NULL DEFAULT 0)'
)
const stmt = await storage.db.prepare('INSERT INTO items (key, val) VALUES (?, ?)')
for (let k = 0; k < itemsData.length; k++) {
await stmt.run([k, itemsData[k]])
}
})
describe('#commit', () => {
it('should commit save', async () => {
const session = storage.createSession()
await session.start()
await session.commit(async () => {
await storage.save('items', 'key', { key: 'ten', val: 10 })
await storage.save('items', 'key', { key: 'eleven', val: 11 })
await storage.remove('items', 'key', 'ten')
})
const cursor = await storage.find('items', {})
const items = await cursor.toArray()
assert.equal(itemsData.length + 1, items.length)
for (const item of items) {
if (item['key'] === 'ten') assert.fail('ten had to be removed in session')
}
})
})
})