Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/deptno/dynalee
🍳 DynamoDB Typescript ORM
https://github.com/deptno/dynalee
dynamodb dynamodb-orm dynamodb-typescript dynamodb-typescript-orm orm typescript
Last synced: about 2 months ago
JSON representation
🍳 DynamoDB Typescript ORM
- Host: GitHub
- URL: https://github.com/deptno/dynalee
- Owner: deptno
- License: mit
- Created: 2018-10-26T13:17:51.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2019-01-07T07:43:16.000Z (almost 6 years ago)
- Last Synced: 2024-10-18T06:25:40.385Z (2 months ago)
- Topics: dynamodb, dynamodb-orm, dynamodb-typescript, dynamodb-typescript-orm, orm, typescript
- Language: TypeScript
- Homepage:
- Size: 714 KB
- Stars: 9
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# dynalee
**dyanlee** is **DML** only **DynamoDB ORM**
Strongly typed.
Define model with way to follow DynamoDB philosophy.
## usage
### Model
#### define hash model
```typescript
import {HashModel} from 'dynalee'interface Cat {
name: string
birth: number
color: string
}const CatModel = new HashModel({
table: 'TableName',
hashKey: 'name'
})const aCat = CatModel.of({
name: 'first cat',
birth: 2018,
color: 'white'
})const docCat = await aCat.put() // save
console.log(docCat.head())
/*
{
name: 'first cat'
birth: 2018
color: 'white'
}
*/
```#### define multiple model in one table
```typescript
import {RangeModel} from 'dynalee'interface Index {
name: string
birth: number
}
interface Cat extends Index {
color: string
age?: number,
doYouWantSet?: string[]|Set // use union for DynamoDB type and javascript type
doYouWantList?: string[]
}
interface Dog extends Index {
longLegs: boolean
}const model = new RangeModel({
table: 'TableName',
hashKey: 'name',
rangeKey: 'birth',
})
const CatModel = model as RangeModel
const DogModel = model as RangeModel
```#### update
```typescript
CatModel
.update('deptno cat', '1985')
.update(setter => {
setter
.of({
age: 10,
color: 'white'
}) // set multiple props
})
condition(setter => {
setter.attributeNotExists('ID')
})
.returnValue('NONE')
.run()
```#### update 2
```typescript
CatModel
.update('deptno cat', '1985')
.update(setter => {
setter
.set('color', 'white')
.add('age', 10)
})
condition((and, or, not) => {
and.attributeNotExists('ID')
})
.returnValue('NONE')
.run()
```#### delete
```typescript
CatModel.delete('deptno cat', '1985')
```#### get item
```typescript
CatModel.get('deptno cat', '1985')
```### Document
#### create
```typescript
const document = await CatModel
.of({
name: 'son of deptno cat',
birth: '2006',
age: 10
})
.put() // save
```#### get raw data
```typescript
const document = await CatModel.get('deptno cat', '1985')
const rawdata = document.head()
```#### delete
```typescript
const document = await CatModel.get('deptno cat', '1985')
document.delete()
```#### put
```typescript
const document = await CatModel.get('deptno cat', '1985')
await document
.set(setter => {
setter.age = 10
setter.color = 'white'
})
.put()
```### SecondaryIndex
#### define secondary index
```typescript
import {SecondaryIndex} from 'dynalee'interface Index {
name: string
birth: number
}
interface Cat extends Index {
color: string
}
interface Dog extends Index {
longLegs: boolean
}const model = new SecondaryIndex({
table: 'TableName',
index: 'IndexTable',
hashKey: 'species',
rangeKey: 'birth'
})
const CatModel = model as SecondaryIndex
const DogModel = model as SecondaryIndex
```#### query on secondary index
```typescript
const documents = await CatModel
.query('Turkish Angora')
.run()
const items = documents.Items.map(item => item.head())
```#### query on secondary index
```typescript
const documents = await CatModel
.rangeQuery('Turkish Angora')
.eq('2006')
.run()
const items = documents.Items.map(item => item.head())
```#### more query options
```typescript
const nextToken = null
const documents = await CatModel
.rangeQuery('Turkish Angora')
.eq('2006')
.desc()
.consistent()
.limit(10)
.startAt(nextToken)
.run()
const items = documents.Items.map(item => item.head())
```#### scan on secondary index
```typescript
const documents = await CatModel
.scan()
.filter(setter => {
setter.attributeNotExists('specis')
})
.run()
const items = documents.Items.map(item => item.head())
```### advanced usage
#### Set
```typescript
const cat = await CatModel
.of({
name: 'my cat',
birth: '2018',
age: 10,
doYouWantSet: new Set(['string set 1', 'string set 2'])
})
.update(setter => { // example code, `of` method can include below props
setter
.plus('age', 1)
.add('doYouWantSet', new Set(['string set 3']))
})
.put()
const document = cat.head()
console.log(document.doYouWantSet instanceof Set) // trueconst jsDocument = cat.head(true)
console.log(jsDocument.doYouWantSet instanceof Set) // false
console.log(Array.isArray(jsDocument.doYouWantSet)) // true
```#### List
```typescript
const cat = await CatModel
.of({
name: 'my cat',
birth: '2018',
age: 10,
doYouWantList: ['string 1', 'string 2']
})
.update(setter => { // example code, `of` method can include below props
setter
.plus('age', 1)
.add('doYouWantSet', ['string 3'])
})
.put()
```#### Trigger
likes CreatedAt, UpdatedAt
```typescript
import {HashModel, DocumentOptions} from 'dynalee'
const document: DocumentOptions = {
onCreate: [
{
attributeName: 'CreatedAt',
handler(prevVal?) {
return new Date().toISOString()
}
},
],
onUpdate: [
{
attributeName: 'UpdatedAt',
handler(prevVal?) {
return new Date().toISOString()
}
}
],
}
interface Cat {
name: string
}
const CatModel = new HashModel({
table: 'TableName',
hashKey: 'name',
options: {
document
}
})
```#### use with [dynamon](https://github.com/deptno/dynamon)
```typescript
import {HashModel} from 'dynalee'
interface Cat {
name: string
}
const CatModel = new HashModel({
table: 'TableName',
hashKey: 'name',
options: {
aws: {
region : 'dynamon',
endpoint: 'http://localhost:8000'
},
}
})
```## link
[#dynamon](https://github.com/deptno/dynamon)
## license
MIT