Skip to content

Commit

Permalink
normalized nested expand items
Browse files Browse the repository at this point in the history
  • Loading branch information
ganigeorgiev committed Dec 24, 2022
1 parent 6c10409 commit e6fa0e1
Show file tree
Hide file tree
Showing 18 changed files with 92 additions and 12 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.9.1

- Normalized nested `expand` items to `Record|Array<Record>` instances.


## 0.9.0

- Added `pb.health.check()` that checks the health status of the API service (_available in PocketBase v0.10.0_)
Expand Down
5 changes: 5 additions & 0 deletions dist/pocketbase.cjs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ declare class Record extends BaseModel {
load(data: {
[key: string]: any;
}): void;
/**
* Loads the provided expand items and recursively normalizes each
* item to a `Record|Array<Record>`.
*/
private loadExpand;
}
declare class Admin extends BaseModel {
avatar: number;
Expand Down
2 changes: 1 addition & 1 deletion dist/pocketbase.cjs.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pocketbase.cjs.js.map

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions dist/pocketbase.es.d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ declare class Record extends BaseModel {
load(data: {
[key: string]: any;
}): void;
/**
* Loads the provided expand items and recursively normalizes each
* item to a `Record|Array<Record>`.
*/
private loadExpand;
}
declare class Admin extends BaseModel {
avatar: number;
Expand Down
2 changes: 1 addition & 1 deletion dist/pocketbase.es.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pocketbase.es.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pocketbase.es.mjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pocketbase.es.mjs.map

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions dist/pocketbase.iife.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ declare class Record extends BaseModel {
load(data: {
[key: string]: any;
}): void;
/**
* Loads the provided expand items and recursively normalizes each
* item to a `Record|Array<Record>`.
*/
private loadExpand;
}
declare class Admin extends BaseModel {
avatar: number;
Expand Down
2 changes: 1 addition & 1 deletion dist/pocketbase.iife.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pocketbase.iife.js.map

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions dist/pocketbase.umd.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ declare class Record extends BaseModel {
load(data: {
[key: string]: any;
}): void;
/**
* Loads the provided expand items and recursively normalizes each
* item to a `Record|Array<Record>`.
*/
private loadExpand;
}
declare class Admin extends BaseModel {
avatar: number;
Expand Down
2 changes: 1 addition & 1 deletion dist/pocketbase.umd.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pocketbase.umd.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.9.0",
"version": "0.9.1",
"name": "pocketbase",
"description": "PocketBase JavaScript SDK",
"author": "Gani Georgiev",
Expand Down
21 changes: 20 additions & 1 deletion src/models/Record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@ export default class Record extends BaseModel {
// normalize common fields
this.collectionId = typeof data.collectionId === 'string' ? data.collectionId : '';
this.collectionName = typeof data.collectionName === 'string' ? data.collectionName : '';
this.expand = typeof data.expand === 'object' && data.expand !== null ? data.expand : {};

// normalize expand items
this.loadExpand(data.expand);
}

/**
* Loads the provided expand items and recursively normalizes each
* item to a `Record|Array<Record>`.
*/
private loadExpand(expand: { [key: string]: any }) {
expand = expand || {};
this.expand = {};

for (const key in expand) {
if (Array.isArray(expand[key])) {
this.expand[key] = expand[key].map((data: any) => new Record(data || {}));
} else {
this.expand[key] = new Record(expand[key] || {});
}
}
}
}
36 changes: 36 additions & 0 deletions tests/models/Record.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { assert } from 'chai';
import Record from '@/models/Record';

describe('Record', function() {
describe('test Record data load and expand initialization', function () {
const data = {
id: "test_id",
collectionId: "test_collectionId",
collectionName: "test_collectionName",
custom: "test_custom",
expand: {
one: {
id: "one_id",
expand: {
sub: [{id: "sub.a"}, {id: "sub.b"}]
},
},
many: [{id: "multi_id"}],
}
};

const model = new Record(data);

assert.equal(model.id, data.id);
assert.equal(model.collectionId, data.collectionId);
assert.equal(model.collectionName, data.collectionName);
assert.equal(model.custom, data.custom);
assert.instanceOf(model.expand?.one, Record);
assert.equal((model.expand.one as any).id, data.expand.one.id);
assert.equal((model.expand.one as any).expand.sub.length, 2);
assert.instanceOf((model.expand.one as any).expand.sub[0], Record);
assert.instanceOf((model.expand.one as any).expand.sub[1], Record);
assert.equal((model.expand.many as any).length, 1);
assert.instanceOf((model.expand.many as any)[0], Record);
});
});

0 comments on commit e6fa0e1

Please sign in to comment.