Skip to content

Commit

Permalink
1.1.29 - add originalId when inserting entity
Browse files Browse the repository at this point in the history
  • Loading branch information
ganoro committed Aug 10, 2019
1 parent 0393e0f commit 30ed579
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "pouchable",
"version": "1.1.28",
"version": "1.1.29",
"description": "PouchDB wrapped for TypeScript lovers",
"main": "./dist/src/index.js",
"scripts": {
Expand Down
8 changes: 4 additions & 4 deletions src/Collection.ts
Expand Up @@ -31,12 +31,12 @@ export abstract class Collection<T extends Entity> {
*/
public abstract getPrefix(): string;

public insert(data, created_at: number = null) : Promise<T> {
public insert(data, created_at: number = null, originalId: string = null) : Promise<T> {
return new Promise<T>((resolved, rejected)=> {
let c = this._resolveCore(data, created_at);
let b = values(this._resolveBuckets(data));
let sks = this._resolveSearchKeys(data);
this._collectionBase.insert(c, b, sks).then((eb) => {
this._collectionBase.insert(c, b, sks, originalId).then((eb) => {
return resolved(new this._ctor(eb));
}).catch((m) => {
return rejected(m)
Expand All @@ -50,7 +50,7 @@ export abstract class Collection<T extends Entity> {
*/
public get(id, options?) : Promise<T> {
return new Promise<T>((resolved, rejected)=> {
this._collectionBase.getById(id, options).then((eb) => {
this._collectionBase.getById(id).then((eb) => {
return resolved(new this._ctor(eb));
}).catch((m) => {
return rejected(m)
Expand Down Expand Up @@ -152,7 +152,7 @@ export abstract class Collection<T extends Entity> {
let core = this._resolveCoreFields(data);

// do the change all together
return entity.updateBuckets(bs, skc, core).then((t) => {
return entity.updateBuckets(bs, skc, core).then((t: any) => {
return resolved(t)
}).catch((m) => {
return rejected(m);
Expand Down
4 changes: 2 additions & 2 deletions src/EntityCollectionBase.ts
Expand Up @@ -49,11 +49,11 @@ export class EntityCollectionBase {
/**
* Insert a new entity
*/
insert(core, buckets?: any[], keys? : any[]) : Promise<EntityBase> {
insert(core, buckets?: any[], keys? : any[], originalId?: string) : Promise<EntityBase> {

return new Promise<EntityBase>((resolved, rejected) => {
// generate id
let id = this._idGenerator.get();
let id = originalId || this._idGenerator.get();
let e_core = this._resolveCore(core, id);
let e_buckets = buckets ? this._resolveBuckets(buckets, id) : {};
let e_search_keys_ref = keys ? this. _resolveSearchKeysRef(keys, id) : {};
Expand Down
16 changes: 16 additions & 0 deletions tests/CollectionTest.ts
Expand Up @@ -48,6 +48,22 @@ import { Promise } from 'ts-promise';
}).catch(_.noop);
}

@test ("insert doc with created_at and original id should return entity")
testInsertDoc2WithId(done: Function) {
let posts = new Posts(CollectionTest.db, Post);
const created_at = new Date().getTime()
const originalId = created_at + '-' + Math.random();
posts.insert({ title: "New Two"}, created_at, originalId).then((p) => {
if (p.title != "New Two") {
throw new Error("missing data");
}
if (p.id != originalId) {
throw new Error("error assignining original id");
}
done();
}).catch(_.noop);
}

@test ("insert doc with created_at in the previous should return entity")
testInsertDoc3(done: Function) {
let posts = new Posts(CollectionTest.db, Post);
Expand Down

0 comments on commit 30ed579

Please sign in to comment.