Skip to content

Commit

Permalink
fix(update): trans '' to null
Browse files Browse the repository at this point in the history
fix: return result if call then() without arguments

fix: throw error if call catch() without arguments
  • Loading branch information
cncolder committed Jun 5, 2018
1 parent b3d582b commit bf37bb2
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 26 deletions.
10 changes: 7 additions & 3 deletions src/batchGet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,21 @@ export class BatchGet<M extends Model> extends ReadOperate<M> implements AsyncIt
}

async then<TRes>(
onfulfilled?: (value?: M[]) => TRes | PromiseLike<TRes>,
onfulfilled: (value?: M[]) => TRes | PromiseLike<TRes> = (r => r) as any,
onrejected?: (reason: any) => TRes | PromiseLike<TRes>,
) {
try {
let result = [] as M[]
for await (let m of this) {
result.push(m)
}
onfulfilled(result)
return onfulfilled(result)
} catch (err) {
onrejected(err)
if (onrejected) {
onrejected(err)
} else {
throw err
}
}
}

Expand Down
10 changes: 7 additions & 3 deletions src/batchWrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class BatchWrite<M extends Model> extends WriteOperate<M> implements Asyn
}

async then<TRes>(
onfulfilled?: (value?: number) => TRes | PromiseLike<TRes>,
onfulfilled: (value?: number) => TRes | PromiseLike<TRes> = (r => r) as any,
onrejected?: (reason: any) => TRes | PromiseLike<TRes>,
) {
try {
Expand All @@ -72,9 +72,13 @@ export class BatchWrite<M extends Model> extends WriteOperate<M> implements Asyn
for await (let m of this) {
time++
}
onfulfilled(time)
return onfulfilled(time)
} catch (err) {
onrejected(err)
if (onrejected) {
onrejected(err)
} else {
throw err
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ export class Database extends Schema {
// return this._documentClient
// || (this._documentClient = new DocumentClient())
// }
static client = new DocumentClient()
static client = new DocumentClient({
convertEmptyValues: true
})

/**
* Configure tiamo to use a DynamoDB local endpoint for testing.
Expand Down
2 changes: 1 addition & 1 deletion src/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class Delete<M extends Model> extends ConditionWriteOperate<M> {
}

then<TRes>(
onfulfilled?: (value?: M) => TRes | PromiseLike<TRes>,
onfulfilled: (value?: M) => TRes | PromiseLike<TRes> = (r => r) as any,
onrejected?: (reason: any) => TRes | PromiseLike<TRes>,
) {
const params = this.toJSON()
Expand Down
2 changes: 1 addition & 1 deletion src/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class Get<M extends Model> extends ReadOperate<M> {
}

then<TRes>(
onfulfilled?: (value?: M) => TRes | PromiseLike<TRes>,
onfulfilled: (value?: M) => TRes | PromiseLike<TRes> = (r => r) as any,
onrejected?: (reason: any) => TRes | PromiseLike<TRes>,
) {
const { Model } = this.options
Expand Down
8 changes: 4 additions & 4 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,12 @@ export class Model extends Database {
this.pre('save', this.validate.bind(this))
}

get isNew() {
isNew() {
return !!Reflect.getOwnMetadata('tiamo:cache:new', this)
}
set isNew(value: boolean) {
Reflect.defineMetadata('tiamo:cache:new', value, this)
}
// set isNew(value: boolean) {
// Reflect.defineMetadata('tiamo:cache:new', value, this)
// }

/**
* Hook instance
Expand Down
2 changes: 1 addition & 1 deletion src/put.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class Put<M extends Model> extends ConditionWriteOperate<M> {
}

then<TRes>(
onfulfilled?: (value?: M) => TRes | PromiseLike<TRes>,
onfulfilled: (value?: M) => TRes | PromiseLike<TRes> = (r => r) as any,
onrejected?: (reason: any) => TRes | PromiseLike<TRes>,
) {
const params = this.toJSON()
Expand Down
4 changes: 2 additions & 2 deletions src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class Query<M extends Model, R extends M | M[]> extends MultiReadOperate<
}

async then<TRes>(
onfulfilled?: (value?: R) => TRes | PromiseLike<TRes>,
onfulfilled: (value?: R) => TRes | PromiseLike<TRes> = (r => r) as any,
onrejected?: (reason: any) => TRes | PromiseLike<TRes>,
) {
try {
Expand All @@ -105,7 +105,7 @@ export class Query<M extends Model, R extends M | M[]> extends MultiReadOperate<
}
result.push(res)
}
onfulfilled(this.options.one ? first : result)
return onfulfilled(this.options.one ? first : result)
} catch (err) {
onrejected(err)
}
Expand Down
5 changes: 3 additions & 2 deletions src/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,16 @@ export class Scan<M extends Model> extends MultiReadOperate<M> implements AsyncI
}

async then<TRes>(
onfulfilled?: (value?: M[]) => TRes | PromiseLike<TRes>,
onfulfilled: (value?: M[]) => TRes | PromiseLike<TRes> = (r => r) as any,
onrejected?: (reason: any) => TRes | PromiseLike<TRes>,
) {
try {
let result = []
for await (let res of this) {
result.push(res)
}
onfulfilled(result)

return onfulfilled(result)
} catch (err) {
onrejected(err)
}
Expand Down
9 changes: 7 additions & 2 deletions src/update.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DynamoDB } from 'aws-sdk'
import { DocumentClient } from 'aws-sdk/clients/dynamodb'
import { Model, $update } from './model'
import { expression, ExpressionLogic } from './expression'
import { expression } from './expression'
import { ConditionWriteOperate, OperateOptions } from './operate'

export class Update<M extends Model> extends ConditionWriteOperate<M> {
Expand All @@ -28,6 +28,11 @@ export class Update<M extends Model> extends ConditionWriteOperate<M> {
set(key: string) {
const { options } = this
const f = <V>(op: string, op2?: string) => (val?: V) => {
if (op === '=' && (val as any) === '') {
// return this.remove(key)
val = null
}

const { exprs, names, values } = expression(key)(op, op2)(val)
exprs.forEach(e => options.setExprs.add(e))
Object.assign(options.names, names)
Expand Down Expand Up @@ -86,7 +91,7 @@ export class Update<M extends Model> extends ConditionWriteOperate<M> {
}

then<TRes>(
onfulfilled?: (value?: M) => TRes | PromiseLike<TRes>,
onfulfilled: (value?: M) => TRes | PromiseLike<TRes> = (r => r) as any,
onrejected?: (reason: any) => TRes | PromiseLike<TRes>,
) {
const params = this.toJSON()
Expand Down
52 changes: 46 additions & 6 deletions test/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ describe('Model', () => {
it('default is new', () => {
let foo1 = new Foo()

expect(foo1.isNew).toBe(true)
expect(foo1.isNew()).toBe(true)

let foo2 = new Foo({}, { isNew: false })

expect(foo2.isNew).toBe(false)
expect(foo1.isNew).toBe(true)
expect(foo2.isNew()).toBe(false)
expect(foo1.isNew()).toBe(true)
})
})

Expand Down Expand Up @@ -53,9 +53,9 @@ describe('Model', () => {
expect(value.unknown).toBeUndefined()
})

it('validate before create', async () => {
await expect(Foo.create({ id: 42 as any })).rejects.toThrow('"id" must be a string')
})
// it('validate before create', async () => {
// await expect(Foo.create({ id: 42 as any })).rejects.toThrow('"id" must be a string')
// })

it('validate before create', async () => {
await expect(new Foo({ id: 42 as any }).save()).rejects.toThrow('"id" must be a string')
Expand Down Expand Up @@ -325,6 +325,10 @@ describe('Model', () => {
expect(e.name).toBeUndefined()
expect(e.arr[0]).toBe('1')
})

it('allow call then() without arguments', async () => {
await expect(GetExample.get({ id: '1' }).then().then()).resolves.toBeDefined()
})
})

describe('query', () => {
Expand Down Expand Up @@ -462,6 +466,14 @@ describe('Model', () => {

expect(m.id).toBe('1')
})

it('throw if query without cond', async () => {
await expect(Example.query()).rejects.toThrow('Either the KeyConditions or KeyConditionExpression parameter must be specified in the request.')
})

it('allow call then() without arguments', async () => {
await expect(Example.query().where('id').eq('1').then().then()).resolves.toBeDefined()
})
})

describe('scan', () => {
Expand Down Expand Up @@ -531,6 +543,10 @@ describe('Model', () => {

expect(c).toBe(2)
})

it('allow call then() without arguments', async () => {
await expect(ScanExample.scan().then().then()).resolves.toBeDefined()
})
})

describe('update', () => {
Expand Down Expand Up @@ -752,6 +768,18 @@ describe('Model', () => {
it('update id = 1 nothing', async () => {
await expect(Example.update({ id: '1' })).rejects.toThrow('Update expression is empty')
})

it('update id = 1 set name to empty string', async () => {
let u = Example.update({ id: '1' })
.set('name').to('')

expect(u.toJSON().ExpressionAttributeValues[':name']).toBeNull()
await expect(u.then()).resolves.not.toThrow()
})

it('allow call then() without arguments', async () => {
await expect(Example.update({id: '1'}).remove('name').then().then()).resolves.toBeDefined()
})
})

describe('delete', () => {
Expand Down Expand Up @@ -870,6 +898,10 @@ describe('Model', () => {

expect(a.length).toBe(0)
})

it('allow call then() without arguments', async () => {
await expect(Example.delete({ id: '1' }).then().then()).resolves.toBeDefined()
})
})

describe('batch', () => {
Expand Down Expand Up @@ -1069,6 +1101,14 @@ describe('Model', () => {

await expect(p).resolves.toHaveLength(2)
})

it('allow call then() without arguments', async () => {
await expect(BatchExample.batch().put([{ id: 1 }]).get([{ id: 1 }]).then().then()).resolves.toBeDefined()
})

it('throw if call catch() without arguments', async () => {
await expect(BatchExample.batch().get([]).catch().catch()).rejects.toThrow('Member must have length greater than or equal to 1')
})
})

xit('', async () => {
Expand Down

0 comments on commit bf37bb2

Please sign in to comment.