Skip to content
This repository was archived by the owner on Dec 27, 2024. It is now read-only.

Commit 2177ec7

Browse files
committed
resolves #242 and refactors fromDb
1 parent 4de95c5 commit 2177ec7

File tree

14 files changed

+335
-117
lines changed

14 files changed

+335
-117
lines changed

.idea/runConfigurations/Debug_Server_Tests.xml

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sakuraapi/core",
3-
"version": "0.19.5",
3+
"version": "0.19.6",
44
"description": "MongoDB and TypeScript MEAN Stack Framework for NodeJS",
55
"main": "lib/index.js",
66
"typings": "lib/index.d.ts",
@@ -15,7 +15,6 @@
1515
"docs:generate": "./scripts/docs:generate.sh",
1616
"docs:publish": "./scripts/docs:publish.sh",
1717
"docs:serve": "./scripts/docs:serve.sh",
18-
"docker:compose-test": "./scripts/docker:compose-test.sh",
1918
"install": "chmod +x scripts/*.* || true",
2019
"lint": "./scripts/lint.sh",
2120
"prepublish": "npm test && npx nsp check",
@@ -70,7 +69,7 @@
7069
"supertest": "^3.1.0",
7170
"tslint": "^5.11.0",
7271
"typedoc": "^0.11.1",
73-
"typescript": "^2.9.2"
72+
"typescript": "^3.0.3"
7473
},
7574
"dependencies": {
7675
"bcrypt": "^3.0.0",

scripts/_functions.sh

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env bash
2+
3+
PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
4+
PROJECT_NAME="$(basename ${PROJECT_DIR})"
5+
6+
function compose {
7+
docker-compose --project-name ${PROJECT_NAME} --file ${COMPOSE_FILE} $@
8+
}
9+
10+
function down {
11+
compose down --remove-orphans
12+
docker volume prune -f || echo "skipped docker volume prune"
13+
}
14+
15+
function pull {
16+
local services
17+
local remoteServices
18+
services=$(compose config --services)
19+
remoteServices=""
20+
for service in ${services[@]}; do
21+
if [[ ${service} != local-* ]]; then
22+
remoteServices+=" $service"
23+
fi
24+
done
25+
26+
compose pull --parallel ${remoteServices}
27+
}
28+
29+
30+
function saveVersion {
31+
cat package.json \
32+
| grep version \
33+
| head -1 \
34+
| awk -F: '{ print $2 }' \
35+
| sed 's/[",]//g' \
36+
| tr -d '[[:space:]]' > ./lib/version
37+
}
38+
39+
function updateStart {
40+
echo
41+
echo "🚀 starting $0"
42+
}
43+
44+
function update {
45+
echo "$1"
46+
}
47+
48+
function updateDone {
49+
echo "$0 done"
50+
echo
51+
}

scripts/build.sh

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
#!/usr/bin/env bash
22

3-
set -ex
3+
set -e
44

5+
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6+
. ${DIR}/_functions.sh
7+
8+
updateStart
9+
10+
update "removing lib"
511
rm -rf lib/
12+
13+
update "compiling typescript"
614
npx tsc
15+
16+
update "syncing assets"
717
rsync -r --exclude=*.ts spec/ lib/spec
18+
19+
update "saving version"
20+
saveVersion

scripts/coverage.sh

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
#!/usr/bin/env bash
22

3-
set -ex
4-
npm run docker:compose-test
5-
npm run build
3+
set -e
4+
5+
COMPOSE_FILE="./docker-compose.yml"
6+
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
7+
. ${DIR}/_functions.sh
8+
9+
updateStart
10+
11+
update "starting mongodb"
12+
trap down EXIT
13+
compose up -d
14+
15+
npm run build test
16+
17+
update "starting istanbul"
618
npx istanbul cover --include-all-sources node_modules/jasmine/bin/jasmine.js
7-
docker-compose down
19+
20+
update "opening report"
821
(open coverage/lcov-report/index.html || echo '')
22+
23+
updateDone

scripts/docker:compose-test.sh

Lines changed: 0 additions & 5 deletions
This file was deleted.

scripts/lint.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
#!/usr/bin/env bash
22

3+
set -e
4+
5+
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6+
. ${DIR}/_functions.sh
7+
8+
updateStart
9+
10+
update "starting tslint"
311
npx tslint -t stylish -c tslint.json -p tsconfig.json
12+
13+
updateDone

scripts/test.sh

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,34 @@
11
#!/usr/bin/env bash
22

3-
set -ex
3+
set -e
44

5-
TEST_TYPE=${1:-clearDb}
5+
TEST_TYPE=${1:-"clearDb"}
66

7-
npm run docker:compose-test
8-
npm run build
9-
npx jasmine || ((say 'fail' || echo 'fail') ; exit 1)
7+
COMPOSE_FILE="./docker-compose.yml"
8+
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
9+
. ${DIR}/_functions.sh
10+
11+
updateStart
12+
13+
npm run build test
14+
15+
if [ "$1" == "webstorm-debug" ]; then
16+
17+
update "starting MongoDB"
18+
down
19+
compose up -d
20+
21+
else
22+
23+
update "starting MongoDB"
24+
if [ "$1" != "saveDb" ]; then
25+
trap down EXIT
26+
fi
27+
compose up -d
28+
29+
update "starting jasmine"
30+
npx jasmine
1031

11-
if [ $TEST_TYPE != "saveDb" ]; then
12-
docker-compose down
1332
fi
33+
34+
updateDone

src/core/@model/model-operators/from-db.spec.ts

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,20 @@ describe('@Model.fromDb', () => {
526526

527527
describe('with dbOptions.promiscuous mode', () => {
528528

529+
@Model({promiscuous: true})
530+
class PromiscuousOrder {
531+
@Db()
532+
orderId: string = 'a123';
533+
534+
@Db()
535+
total: number = 100;
536+
537+
@Db({field: 'addr', model: Address})
538+
address = new Address();
539+
540+
itemName = 'Cherry Tree Axe';
541+
}
542+
529543
@Model({
530544
dbConfig: {
531545
collection: 'users',
@@ -540,8 +554,8 @@ describe('@Model.fromDb', () => {
540554
@Db({field: 'ph'})
541555
phone: string;
542556

543-
@Db({model: Order})
544-
order = new Order();
557+
@Db({model: PromiscuousOrder})
558+
order = new PromiscuousOrder();
545559
}
546560

547561
it('promiscuously includes fields not mapped with @Db', () => {
@@ -581,4 +595,63 @@ describe('@Model.fromDb', () => {
581595
});
582596
});
583597
});
598+
599+
describe('bugs', () => {
600+
601+
describe('issue 242', () => {
602+
603+
@Model()
604+
class Versions extends SapiModelMixin() {
605+
@Id() @Json({type: 'id'})
606+
id: ObjectID;
607+
}
608+
609+
@Model()
610+
class Child extends SapiModelMixin() {
611+
@Id() @Json({type: 'id'})
612+
id: ObjectID;
613+
614+
@Db({model: Versions}) @Json()
615+
versions: Versions[];
616+
}
617+
618+
@Model()
619+
class Details extends SapiModelMixin() {
620+
@Id() @Json({type: 'id'})
621+
id: ObjectID;
622+
623+
@Db({model: Child}) @Json()
624+
children: Child[];
625+
}
626+
627+
it('assigns _id in sub documents', async () => {
628+
629+
const dbo = {
630+
_id: '5b897fb0c5d0fc0a59e2f498',
631+
children: [
632+
{
633+
_id: '5b897fb0c5d0fc0a59e2f49a',
634+
versions: [
635+
{
636+
_id: '5b897fb0c5d0fc0a59e2f499'
637+
}
638+
]
639+
}
640+
]
641+
};
642+
643+
const result = Details.fromDb(dbo);
644+
645+
const detailsId = (result._id) ? result._id.toHexString() : undefined;
646+
const childId = (result.children[0]._id) ? result.children[0]._id.toHexString() : undefined;
647+
const versionId = (result.children[0].versions[0]._id) ? result.children[0].versions[0]._id.toHexString() : undefined;
648+
649+
expect(detailsId).toBe(dbo._id);
650+
expect(childId).toBe(dbo.children[0]._id);
651+
expect(versionId).toBe(dbo.children[0].versions[0]._id);
652+
653+
});
654+
655+
});
656+
});
584657
});

0 commit comments

Comments
 (0)