Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
a042a589a0 | |||
75b5f6af08 | |||
e6a36f22ac | |||
246e3486f0 | |||
c1c4a29415 | |||
6448516aa0 | |||
24a7b2dbd3 | |||
afa7c2fe61 | |||
b768896fbe | |||
8d7d9adef1 |
38
README.md
38
README.md
@ -66,16 +66,19 @@ So to get to get access to a specific collection you document
|
|||||||
@smartdata.Collection(smartdataDb)
|
@smartdata.Collection(smartdataDb)
|
||||||
class MyObject extends smartdata.DbDoc<MyObject> {
|
class MyObject extends smartdata.DbDoc<MyObject> {
|
||||||
// read the next block about DbDoc
|
// read the next block about DbDoc
|
||||||
@smartdata.svDb() property1: string; // @smartdata.svDb() marks the property for db save
|
@smartdata.svDb()
|
||||||
|
property1: string; // @smartdata.svDb() marks the property for db save
|
||||||
|
|
||||||
property2: number; // this one is not marked, so it won't be save upon calling this.save()
|
property2: number; // this one is not marked, so it won't be save upon calling this.save()
|
||||||
constructor(optionsArg: { property1: string; property2: number }) {
|
|
||||||
super();
|
constructor() {
|
||||||
|
super(); // the super call is important ;) But you probably know that.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// start to instantiate instances of classes from scratch or database
|
// start to instantiate instances of classes from scratch or database
|
||||||
|
|
||||||
let localObject = new MyObject({
|
const localObject = new MyObject({
|
||||||
property1: 'hi',
|
property1: 'hi',
|
||||||
property2: 2
|
property2: 2
|
||||||
});
|
});
|
||||||
@ -93,27 +96,38 @@ MyObject.getInstance<MyObject>({
|
|||||||
represents a individual document in a collection
|
represents a individual document in a collection
|
||||||
and thereby is ideally suited to extend the class you want to actually store.
|
and thereby is ideally suited to extend the class you want to actually store.
|
||||||
|
|
||||||
**sStore** instances of classes to Db:
|
### CRUD operations
|
||||||
|
smartdata supports full CRUD operations
|
||||||
|
|
||||||
|
**Store** or **Update** instances of classes to MongoDB:
|
||||||
DbDoc extends your class with the following methods:
|
DbDoc extends your class with the following methods:
|
||||||
|
|
||||||
- `.save()` will save (or update) the object you call it on only. Any referenced non-savable objects will not get stored.
|
- async `.save()` will save (or update) the object you call it on only. Any referenced non-savable objects will not get stored.
|
||||||
- `.saveDeep()` does the same like `.save()`.
|
- async `.saveDeep()` does the same like `.save()`.
|
||||||
In addition it will look for properties that reference an object
|
In addition it will look for properties that reference an object
|
||||||
that extends DbDoc as well and call .saveDeep() on them as well.
|
that extends DbDoc as well and call .saveDeep() on them as well.
|
||||||
Loops are prevented
|
Loops are prevented
|
||||||
|
|
||||||
**Get** a new class instance from a Doc in the DB:
|
**Get** a new class instance from MongoDB:
|
||||||
DbDoc exposes a static method that allows you specify a filter to retrieve a cloned class of the one you used to that doc at some point later in time. Yes, let that sink in a minute :)
|
DbDoc exposes a static method that allows you specify a filter to retrieve a cloned class of the one you used to that doc at some point later in time:
|
||||||
|
|
||||||
|
* static async `.getInstance({ /* filter props here */ })` gets you an instance that has the data of the first matched document as properties.
|
||||||
|
* static async `getInstances({ /* filter props here */ })` get you an array instances (one instance for every matched document).
|
||||||
|
|
||||||
|
**Delete** instances from MongoDb:
|
||||||
|
smartdata extends your class with a method to easily delete the doucment from DB:
|
||||||
|
|
||||||
|
* async `.delete()`will delete the document from DB.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
So you can just call `.getInstance({ /* filter props here */ })`.
|
|
||||||
|
|
||||||
## TypeScript
|
## TypeScript
|
||||||
|
|
||||||
How does TypeScript play into this?
|
How does TypeScript play into this?
|
||||||
Since you define your classes in TypeScript and types flow through smartdata in a generic way
|
Since you define your classes in TypeScript and types flow through smartdata in a generic way
|
||||||
you should get all the Intellisense and type checking you love when using smartdata.
|
you should get all the Intellisense and type checking you love when using smartdata.
|
||||||
smartdata itself also bundles typings.
|
smartdata itself also bundles typings. You don't need to install any additional types for smartdata.
|
||||||
So you don't need to install any additional types when importing smartdata.
|
|
||||||
|
|
||||||
For further information read the linked docs at the top of this readme.
|
For further information read the linked docs at the top of this readme.
|
||||||
|
|
||||||
|
2
package-lock.json
generated
2
package-lock.json
generated
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartdata",
|
"name": "@pushrocks/smartdata",
|
||||||
"version": "3.1.18",
|
"version": "3.1.23",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartdata",
|
"name": "@pushrocks/smartdata",
|
||||||
"version": "3.1.18",
|
"version": "3.1.23",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "do more with data",
|
"description": "do more with data",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
|
35
test/test.ts
35
test/test.ts
@ -109,6 +109,41 @@ tap.test('should be able to delete an instance of car', async () => {
|
|||||||
expect(myCar2.color).to.equal('red');
|
expect(myCar2.color).to.equal('red');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// tslint:disable-next-line: max-classes-per-file
|
||||||
|
@smartdata.Collection(() => {
|
||||||
|
return testDb;
|
||||||
|
})
|
||||||
|
class Truck extends smartdata.SmartDataDbDoc<Car> {
|
||||||
|
@smartdata.unI()
|
||||||
|
public id: string = smartunique.shortId();
|
||||||
|
|
||||||
|
@smartdata.svDb()
|
||||||
|
public color: string;
|
||||||
|
|
||||||
|
@smartdata.svDb()
|
||||||
|
public brand: string;
|
||||||
|
|
||||||
|
constructor(colorArg: string, brandArg: string) {
|
||||||
|
super();
|
||||||
|
this.color = colorArg;
|
||||||
|
this.brand = brandArg;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tap.test('should store a new Truck', async () => {
|
||||||
|
const truck = new Truck('blue', 'MAN');
|
||||||
|
await truck.save();
|
||||||
|
const myTruck = await Truck.getInstance<Truck>({color: 'blue'});
|
||||||
|
myTruck.id = 'foo';
|
||||||
|
await myTruck.save();
|
||||||
|
const myTruck2 = await Truck.getInstance<Truck>({color: 'blue'});
|
||||||
|
console.log(myTruck2);
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
// =======================================
|
// =======================================
|
||||||
// close the database connection
|
// close the database connection
|
||||||
// =======================================
|
// =======================================
|
||||||
|
@ -120,8 +120,6 @@ export class SmartdataCollection<T> {
|
|||||||
await this.checkDoc(dbDocArg);
|
await this.checkDoc(dbDocArg);
|
||||||
const identifiableObject = await dbDocArg.createIdentifiableObject();
|
const identifiableObject = await dbDocArg.createIdentifiableObject();
|
||||||
const saveableObject = await dbDocArg.createSavableObject();
|
const saveableObject = await dbDocArg.createSavableObject();
|
||||||
console.log(identifiableObject);
|
|
||||||
console.log(saveableObject);
|
|
||||||
const updateableObject: any = {};
|
const updateableObject: any = {};
|
||||||
for (const key of Object.keys(saveableObject)) {
|
for (const key of Object.keys(saveableObject)) {
|
||||||
if (identifiableObject[key]) {
|
if (identifiableObject[key]) {
|
||||||
@ -129,7 +127,6 @@ export class SmartdataCollection<T> {
|
|||||||
}
|
}
|
||||||
updateableObject[key] = saveableObject[key];
|
updateableObject[key] = saveableObject[key];
|
||||||
}
|
}
|
||||||
console.log(updateableObject);
|
|
||||||
this.mongoDbCollection.updateOne(
|
this.mongoDbCollection.updateOne(
|
||||||
identifiableObject,
|
identifiableObject,
|
||||||
{ $set: updateableObject },
|
{ $set: updateableObject },
|
||||||
@ -141,7 +138,9 @@ export class SmartdataCollection<T> {
|
|||||||
await this.init();
|
await this.init();
|
||||||
await this.checkDoc(dbDocArg);
|
await this.checkDoc(dbDocArg);
|
||||||
const identifiableObject = await dbDocArg.createIdentifiableObject();
|
const identifiableObject = await dbDocArg.createIdentifiableObject();
|
||||||
this.mongoDbCollection.deleteOne(identifiableObject);
|
await this.mongoDbCollection.deleteOne(identifiableObject, {
|
||||||
|
w: 1
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -103,17 +103,15 @@ export class SmartDataDbDoc<T> {
|
|||||||
for (const item of foundDocs) {
|
for (const item of foundDocs) {
|
||||||
const newInstance = new this();
|
const newInstance = new this();
|
||||||
newInstance.creationStatus = 'db';
|
newInstance.creationStatus = 'db';
|
||||||
for (const key in item) {
|
for (const key of Object.keys(item)) {
|
||||||
if (key !== 'id') {
|
newInstance[key] = item[key];
|
||||||
newInstance[key] = item[key];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
returnArray.push(newInstance);
|
returnArray.push(newInstance);
|
||||||
}
|
}
|
||||||
return returnArray;
|
return returnArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
static async getInstance<T>(filterArg): Promise<T> {
|
public static async getInstance<T>(filterArg): Promise<T> {
|
||||||
const result = await this.getInstances<T>(filterArg);
|
const result = await this.getInstances<T>(filterArg);
|
||||||
if (result && result.length > 0) {
|
if (result && result.length > 0) {
|
||||||
return result[0];
|
return result[0];
|
||||||
|
Reference in New Issue
Block a user