Skip to content

Commit

Permalink
Updated typings
Browse files Browse the repository at this point in the history
  • Loading branch information
artus9033 committed Nov 23, 2020
1 parent 054f6a8 commit 272353d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 21 deletions.
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.2",
"version": "1.0.3",
"description": "TypeScript disk-based JSON database for storing small structures with a Mongo-like interface",
"main": "dist/index.js",
"scripts": {
Expand Down
48 changes: 33 additions & 15 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import uuid from "uuid";
import util, { CollectionType, EnrichModelType, ModelTypeBase, QueryType } from "./util";
import DiskDB from ".";

export default class Collection<ModelType extends ModelTypeBase> {
export default class Collection<CollectionModelType> {
private _f: string;

constructor(private db: DiskDB<any>, private collectionName: string) {
this._f = path.join(this.db._db.path, this.collectionName + ".json");
}

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

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

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

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

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>;
data: Omit<CollectionModelType & ModelTypeBase, "_id">
): EnrichModelType<CollectionModelType & ModelTypeBase>;
save(
data: Omit<CollectionModelType & ModelTypeBase, "_id">[]
): EnrichModelType<CollectionModelType & ModelTypeBase>[];
save(
data:
| Omit<CollectionModelType & ModelTypeBase, "_id">
| Omit<CollectionModelType & ModelTypeBase, "_id">[]
):
| EnrichModelType<CollectionModelType & ModelTypeBase>
| EnrichModelType<CollectionModelType & ModelTypeBase>[] {
var collection = JSON.parse(util.readFromFile(this._f)) as CollectionType<
CollectionModelType & ModelTypeBase
>;

if (isArray(data)) {
/*
Expand All @@ -56,7 +70,7 @@ export default class Collection<ModelType extends ModelTypeBase> {
let d = {
...data[i],
_id: uuid.v4().replace(/-/g, ""),
} as ModelType;
} as CollectionModelType & ModelTypeBase;
collection.push(d);
retCollection.push(d);
}
Expand All @@ -69,7 +83,7 @@ export default class Collection<ModelType extends ModelTypeBase> {
let newData = {
...data,
_id: uuid.v4().replace(/-/g, ""),
} as ModelType;
} as CollectionModelType & ModelTypeBase;

collection.push(newData);

Expand All @@ -79,8 +93,8 @@ export default class Collection<ModelType extends ModelTypeBase> {
}

update(
query: QueryType<ModelType>,
data: ModelType,
query: QueryType<CollectionModelType & ModelTypeBase>,
data: CollectionModelType & ModelTypeBase,
options?: {
multi?: boolean;
upsert?: boolean;
Expand All @@ -90,7 +104,9 @@ export default class Collection<ModelType extends ModelTypeBase> {
updated: number;
inserted: number;
} = { updated: 0, inserted: 0 },
collection = JSON.parse(util.readFromFile(this._f)) as CollectionType<ModelType>; // update
collection = JSON.parse(util.readFromFile(this._f)) as CollectionType<
CollectionModelType & ModelTypeBase
>; // update

var records = util.finder(collection, query, true);

Expand Down Expand Up @@ -121,7 +137,7 @@ export default class Collection<ModelType extends ModelTypeBase> {
return ret;
}

remove(query?: QueryType<ModelType>, multi?: boolean) {
remove(query?: QueryType<CollectionModelType & ModelTypeBase>, multi?: boolean) {
if (query) {
var collection = JSON.parse(util.readFromFile(this._f));
if (typeof multi === "undefined") {
Expand All @@ -140,6 +156,8 @@ export default class Collection<ModelType extends ModelTypeBase> {
}

count() {
return (JSON.parse(util.readFromFile(this._f)) as CollectionType<ModelType>).length;
return (JSON.parse(util.readFromFile(this._f)) as CollectionType<
CollectionModelType & ModelTypeBase
>).length;
}
}
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import path from "path";
import { red as e, green as s } from "chalk";

import Collection from "./collection";
import util, { ModelTypeBase } from "./util";
import util from "./util";

type ValuesOf<T extends any[]> = T[number];

class DiskDB<
CollectionTypes extends {
[key: string]: ModelTypeBase;
[key: string]: object;
}
> {
public _db = {
Expand Down
6 changes: 3 additions & 3 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ export class util {
return fs.unlinkSync(file);
}

static updateFiltered<ModelType extends ModelTypeBase>(
collection: CollectionType<ModelType>,
query: QueryType<ModelType> = {},
static updateFiltered<CollectionModelType>(
collection: CollectionType<CollectionModelType & ModelTypeBase>,
query: QueryType<CollectionModelType & ModelTypeBase> = {},
data: Object,
multi?: boolean
) {
Expand Down

0 comments on commit 272353d

Please sign in to comment.