Skip to content

Commit

Permalink
Merge pull request #533 from coagmano/add-static-schema-getter
Browse files Browse the repository at this point in the history
Add static schema getter
  • Loading branch information
dr-dimitru authored Dec 9, 2017
2 parents bc4557f + 4b9260c commit 72c81cd
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 139 deletions.
67 changes: 1 addition & 66 deletions client.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,72 +158,7 @@ export class FilesCollection extends FilesCollectionCore {
}

if (!this.schema) {
this.schema = {
size: {
type: Number
},
name: {
type: String
},
type: {
type: String
},
path: {
type: String
},
isVideo: {
type: Boolean
},
isAudio: {
type: Boolean
},
isImage: {
type: Boolean
},
isText: {
type: Boolean
},
isJSON: {
type: Boolean
},
isPDF: {
type: Boolean
},
extension: {
type: String,
optional: true
},
_storagePath: {
type: String
},
_downloadRoute: {
type: String
},
_collectionName: {
type: String
},
public: {
type: Boolean,
optional: true
},
meta: {
type: Object,
blackbox: true,
optional: true
},
userId: {
type: String,
optional: true
},
updatedAt: {
type: Date,
optional: true
},
versions: {
type: Object,
blackbox: true
}
};
this.schema = FilesCollectionCore.schema;
}

check(this.debug, Boolean);
Expand Down
67 changes: 67 additions & 0 deletions core.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,73 @@ export default class FilesCollectionCore extends EventEmitter {
super();
}

static schema = {
size: {
type: Number
},
name: {
type: String
},
type: {
type: String
},
path: {
type: String
},
isVideo: {
type: Boolean
},
isAudio: {
type: Boolean
},
isImage: {
type: Boolean
},
isText: {
type: Boolean
},
isJSON: {
type: Boolean
},
isPDF: {
type: Boolean
},
extension: {
type: String,
optional: true
},
_storagePath: {
type: String
},
_downloadRoute: {
type: String
},
_collectionName: {
type: String
},
public: {
type: Boolean,
optional: true
},
meta: {
type: Object,
blackbox: true,
optional: true
},
userId: {
type: String,
optional: true
},
updatedAt: {
type: Date,
optional: true
},
versions: {
type: Object,
blackbox: true
}
};

/*
* @locus Anywhere
* @memberOf FilesCollectionCore
Expand Down
19 changes: 17 additions & 2 deletions docs/constructor.md
Original file line number Diff line number Diff line change
Expand Up @@ -821,16 +821,31 @@ export default Images;
### Add extra security:

#### Attach schema [*Isomorphic*]:
*Default schema is stored under* `FilesCollection.schema` *object.*

*To attach schema, use/install [aldeed:collection2](https://github.com/aldeed/meteor-collection2) and [simple-schema](https://atmospherejs.com/aldeed/simple-schema) packages.*

*You're free to modify/overwrite* `FilesCollection.schema` *object.*
```jsx
const Images = new FilesCollection({/* ... */});
Images.collection.attachSchema(new SimpleSchema(Images.schema));
```

*You're free to extend the schema to include your own properties. The default schema is stored under* `FilesCollection.schema` *object.*

```jsx
const mySchema = {
...FilesCollection.schema,
myProp: String,
myOtherProp: {
type: Array
}
};
const Images = new FilesCollection({
/* ... */
schema: mySchema
});
Images.collection.attachSchema(new SimpleSchema(mySchema));
```

#### Deny collection interaction on client [*Server*]:
*Deny insert/update/remove from client*
```jsx
Expand Down
30 changes: 25 additions & 5 deletions docs/schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,42 @@ var defaultSchema = {
#### Attach schema (*Recommended*):
*Although this package comes with schema it isn't enabled (attached) by default (since v1.5.0), you're free to use it or not. To attach schema you need to install [Collection2](https://github.com/aldeed/meteor-collection2) and [simple-schema](https://atmospherejs.com/aldeed/simple-schema) packages separately.*
```javascript
this.Images = new FilesCollection({ collectionName: 'Images'});
Images = new FilesCollection({ collectionName: 'Images'});
Images.collection.attachSchema(new SimpleSchema(Images.schema));
```

#### Extend default schema:

The default schema is available as a static property of `FilesCollection`, so you can extend it using a library like underscore:
```javascript
var mySchema = _.extend(defaultSchema, {
var mySchema = _.extend(FilesCollection.schema, {
myProp: {
type: String
}
});

this.Images = new FilesCollection({
Images = new FilesCollection({
collectionName: 'Images',
schema: mySchema
});
Images.collection.attachSchema(new SimpleSchema(mySchema));
```

Or with ES6 object spread syntax:

```javascript
const mySchema = {
...FilesCollection.schema
myProp: {
type: String
}
});

export default Images = new FilesCollection({
collectionName: 'Images',
schema: new SimpleSchema(mySchema)
schema: mySchema
});
Images.collection.attachSchema(new SimpleSchema(mySchema));
```

#### Pass your own schema (*not recommended*):
Expand All @@ -100,6 +120,6 @@ var mySchema = { /* Your schema here */ };

this.Images = new FilesCollection({
collectionName: 'Images',
schema: new SimpleSchema(mySchema)
schema: mySchema
});
```
67 changes: 1 addition & 66 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,72 +346,7 @@ export class FilesCollection extends FilesCollectionCore {
}

if (!this.schema) {
this.schema = {
size: {
type: Number
},
name: {
type: String
},
type: {
type: String
},
path: {
type: String
},
isVideo: {
type: Boolean
},
isAudio: {
type: Boolean
},
isImage: {
type: Boolean
},
isText: {
type: Boolean
},
isJSON: {
type: Boolean
},
isPDF: {
type: Boolean
},
extension: {
type: String,
optional: true
},
_storagePath: {
type: String
},
_downloadRoute: {
type: String
},
_collectionName: {
type: String
},
public: {
type: Boolean,
optional: true
},
meta: {
type: Object,
blackbox: true,
optional: true
},
userId: {
type: String,
optional: true
},
updatedAt: {
type: Date,
optional: true
},
versions: {
type: Object,
blackbox: true
}
};
this.schema = FilesCollectionCore.schema;
}

check(this.debug, Boolean);
Expand Down

0 comments on commit 72c81cd

Please sign in to comment.