-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ottoman): add support for nested modelKey
Closes: "#638"
- Loading branch information
1 parent
208f88a
commit 5f9898e
Showing
10 changed files
with
170 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { getDefaultInstance, model } from '../src'; | ||
import { startInTest } from './testData'; | ||
|
||
const accessDoc = { | ||
type: 'airlineR', | ||
isActive: false, | ||
name: 'Ottoman Access', | ||
}; | ||
|
||
const accessDoc2 = { | ||
type: 'airlineNested', | ||
isActive: false, | ||
name: 'Ottoman Access Nested', | ||
}; | ||
|
||
const updateDoc = { | ||
isActive: true, | ||
}; | ||
|
||
const replaceDoc = { | ||
type: 'airlineNested Replace', | ||
isActive: false, | ||
}; | ||
|
||
const schema = { | ||
type: String, | ||
isActive: Boolean, | ||
name: String, | ||
}; | ||
|
||
interface IUser { | ||
type: string; | ||
name?: string; | ||
isActive?: boolean; | ||
} | ||
|
||
describe('nested model key', function () { | ||
test('UserModel.create Creating a document', async () => { | ||
const UserModel = model<IUser>('UserNested', schema, { modelKey: 'metadata.doc_type' }); | ||
await startInTest(getDefaultInstance()); | ||
const result = await UserModel.create(accessDoc); | ||
expect(result.id).toBeDefined(); | ||
}); | ||
|
||
test('UserModel.findById Get a document', async () => { | ||
const UserModel = model('UserNested', schema, { modelKey: 'metadata.doc_type' }); | ||
await startInTest(getDefaultInstance()); | ||
const result = await UserModel.create(accessDoc); | ||
const user = await UserModel.findById(result.id); | ||
expect(user.name).toBeDefined(); | ||
}); | ||
|
||
test('UserModel.update -> Update a document', async () => { | ||
const UserModel = model<IUser>('UserNested', schema, { modelKey: 'metadata.doc_type' }); | ||
await startInTest(getDefaultInstance()); | ||
const result = await UserModel.create(accessDoc); | ||
await UserModel.updateById(result.id, updateDoc); | ||
const user = await UserModel.findById(result.id); | ||
expect(user.isActive).toBe(true); | ||
}); | ||
|
||
test('UserModel.replace Replace a document', async () => { | ||
const UserModel = model<IUser>('UserNested', schema, { modelKey: 'metadata.doc_type' }); | ||
await startInTest(getDefaultInstance()); | ||
const result = await UserModel.create(accessDoc); | ||
await UserModel.replaceById(result.id, replaceDoc); | ||
const user = await UserModel.findById(result.id); | ||
expect(user.type).toBe('airlineNested Replace'); | ||
expect(user.name).toBeUndefined(); | ||
}); | ||
|
||
test('Document.save Save and update a document', async () => { | ||
const UserModel = model('UserNested', schema, { modelKey: 'metadata.doc_type' }); | ||
await startInTest(getDefaultInstance()); | ||
const user = new UserModel(accessDoc2); | ||
const result = await user.save(); | ||
expect(user.id).toBeDefined(); | ||
user.name = 'Instance Edited'; | ||
user.id = result.id; | ||
const updated = await user.save(); | ||
expect(updated.name).toBe('Instance Edited'); | ||
}); | ||
|
||
test('Remove saved document from Model instance', async () => { | ||
const UserModel = model('UserNested', schema, { modelKey: 'metadata.doc_type' }); | ||
await startInTest(getDefaultInstance()); | ||
const user = new UserModel(accessDoc2); | ||
await user.save(); | ||
const removed = await user.remove(); | ||
expect(user.id).toBeDefined(); | ||
expect(removed.cas).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const getValueByPath = <T = Record<string, any>>(obj: T, path: string, separator = '.') => | ||
path.split(separator).reduce((prev, curr) => prev && prev[curr], obj); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export const setValueByPath = <T = Record<string, any>>(object: T, path: string, value: any) => { | ||
path = path.replace(/[\[]/gm, '.').replace(/[\]]/gm, ''); //to accept [index] | ||
const keys = path.split('.'); | ||
const last = keys.pop(); | ||
if (!last) { | ||
return; | ||
} | ||
|
||
keys.reduce(function (o, k) { | ||
return (o[k] = o[k] || {}); | ||
}, object)[last] = value; | ||
}; |