Skip to content

Commit

Permalink
Fixed tsc compilation errors in specific cases; added better return
Browse files Browse the repository at this point in the history
typings to find* functions
  • Loading branch information
artus9033 committed Nov 23, 2020
1 parent 9db23d5 commit 778c9af
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 102 deletions.
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
docs
test
317 changes: 224 additions & 93 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tsdiskdb",
"version": "1.0.1",
"version": "1.0.2",
"description": "TypeScript disk-based JSON database for storing small structures with a Mongo-like interface",
"main": "dist/index.js",
"scripts": {
Expand Down
16 changes: 10 additions & 6 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from "path";
import { isArray } from "lodash";
import uuid from "uuid";

import util, { CollectionType, ModelTypeBase, QueryType } from "./util";
import util, { CollectionType, EnrichModelType, ModelTypeBase, QueryType } from "./util";
import DiskDB from ".";

export default class Collection<ModelType extends ModelTypeBase> {
Expand All @@ -13,7 +13,7 @@ export default class Collection<ModelType extends ModelTypeBase> {
this._f = path.join(this.db._db.path, this.collectionName + ".json");
}

find(query?: QueryType<ModelType>) {
find(query?: QueryType<ModelType>): EnrichModelType<ModelType>[] {
var collection = JSON.parse(util.readFromFile(this._f));

if (!query || Object.keys(query).length === 0) {
Expand All @@ -24,7 +24,7 @@ export default class Collection<ModelType extends ModelTypeBase> {
}
}

findOne(query?: QueryType<ModelType>) {
findOne(query?: QueryType<ModelType>): EnrichModelType<ModelType> {
var collection = JSON.parse(util.readFromFile(this._f));

if (!query) {
Expand All @@ -35,9 +35,11 @@ export default class Collection<ModelType extends ModelTypeBase> {
}
}

save(data: Omit<ModelType, "_id">): ModelType;
save(data: Omit<ModelType, "_id">[]): ModelType[];
save(data: Omit<ModelType, "_id"> | Omit<ModelType, "_id">[]): ModelType | ModelType[] {
save(data: Omit<ModelType, "_id">): EnrichModelType<ModelType>;
save(data: Omit<ModelType, "_id">[]): EnrichModelType<ModelType>[];
save(
data: Omit<ModelType, "_id"> | Omit<ModelType, "_id">[]
): EnrichModelType<ModelType> | EnrichModelType<ModelType>[] {
var collection = JSON.parse(util.readFromFile(this._f)) as CollectionType<ModelType>;

if (isArray(data)) {
Expand Down Expand Up @@ -68,7 +70,9 @@ export default class Collection<ModelType extends ModelTypeBase> {
...data,
_id: uuid.v4().replace(/-/g, ""),
} as ModelType;

collection.push(newData);

util.writeToFile(this._f, collection);

return newData;
Expand Down
1 change: 1 addition & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var merge = require("merge");
export type ModelTypeBase = { _id: string };
export type CollectionType<ModelType extends ModelTypeBase = ModelTypeBase> = Array<ModelType>;
export type QueryType<ModelType extends ModelTypeBase> = Partial<ModelType> | undefined | null;
export type EnrichModelType<ModelType> = ModelType & ModelTypeBase;

export class util {
static isValidPath(path: string) {
Expand Down
Loading

0 comments on commit 778c9af

Please sign in to comment.