Skip to content

Commit

Permalink
refactor: refactor types
Browse files Browse the repository at this point in the history
  • Loading branch information
D-Sketon committed Nov 4, 2023
1 parent b700b98 commit 99d1370
Show file tree
Hide file tree
Showing 34 changed files with 463 additions and 409 deletions.
12 changes: 6 additions & 6 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ if (typeof writev === 'function') {
};
}

async function exportAsync(database: Database, path: string) {
async function exportAsync(database: Database, path: string): Promise<void> {
const handle = await open(path, 'w');

try {
Expand Down Expand Up @@ -79,7 +79,7 @@ interface DatabaseOptions {

class Database {
options: DatabaseOptions;
_models: any;
_models: Record<string, Model>;
Model: typeof Model;

/**
Expand Down Expand Up @@ -117,7 +117,7 @@ class Database {
* @param {Schema|object} [schema]
* @return {Model}
*/
model(name: string, schema?: any) {
model(name: string, schema?: Schema | object): Model {
if (this._models[name]) {
return this._models[name];
}
Expand All @@ -133,7 +133,7 @@ class Database {
* @param {function} [callback]
* @return {Promise}
*/
load(callback?) {
load(callback?: NodeJSLikeCallback<any>): Bluebird<any> {
const { path, onUpgrade, onDowngrade, version: newVersion } = this.options;

if (!path) throw new WarehouseError('options.path is required');
Expand Down Expand Up @@ -173,14 +173,14 @@ class Database {
* @param {function} [callback]
* @return {Promise}
*/
save(callback?) {
save(callback?: NodeJSLikeCallback<any>): Bluebird<void>{

Check failure on line 176 in src/database.ts

View workflow job for this annotation

GitHub Actions / linter

Missing space before opening brace
const { path } = this.options;

if (!path) throw new WarehouseError('options.path is required');
return Bluebird.resolve(exportAsync(this, path)).asCallback(callback);
}

toJSON() {
toJSON(): { meta: { version: number, warehouse: string }, models: Record<string, Model> } {
const models = Object.keys(this._models)
.reduce((obj, key) => {
const value = this._models[key];
Expand Down
27 changes: 15 additions & 12 deletions src/document.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import rfdc from 'rfdc';
import type Model from './model';
import type Schema from './schema';
const cloneDeep = rfdc();

abstract class Document {
abstract _model;
_id!: any;
abstract _schema;
abstract _model: Model;
_id!: string | number | undefined;
abstract _schema: Schema;
[key: PropertyKey]: any;

/**
* Document constructor.
*
* @param {object} data
*/
constructor(data) {
constructor(data?: object) {
if (data) {
Object.assign(this, data);
}
Expand All @@ -23,7 +26,7 @@ abstract class Document {
* @param {function} [callback]
* @return {Promise}
*/
save(callback) {
save(callback?: NodeJSLikeCallback<any>): Promise<any> {
return this._model.save(this, callback);
}

Expand All @@ -34,7 +37,7 @@ abstract class Document {
* @param {function} [callback]
* @return {Promise}
*/
update(data, callback) {
update(data: Record<string, any>, callback?: NodeJSLikeCallback<any>): Promise<any> {
return this._model.updateById(this._id, data, callback);
}

Expand All @@ -45,7 +48,7 @@ abstract class Document {
* @param {function} [callback]
* @return {Promise}
*/
replace(data, callback) {
replace(data: object | Document, callback?: NodeJSLikeCallback<any>): Promise<any> {
return this._model.replaceById(this._id, data, callback);
}

Expand All @@ -55,7 +58,7 @@ abstract class Document {
* @param {function} [callback]
* @return {Promise}
*/
remove(callback) {
remove(callback?: NodeJSLikeCallback<any>): Promise<any> {
return this._model.removeById(this._id, callback);
}

Expand All @@ -64,7 +67,7 @@ abstract class Document {
*
* @return {object}
*/
toObject() {
toObject(): object {
const keys = Object.keys(this);
const obj = {};

Expand All @@ -83,7 +86,7 @@ abstract class Document {
*
* @return {String}
*/
toString() {
toString(): string {
return JSON.stringify(this);
}

Expand All @@ -93,13 +96,13 @@ abstract class Document {
* @param {String|Object} expr
* @return {Document}
*/
populate(expr) {
populate(expr: string | any[] | { path?: string; model?: any; [key: PropertyKey]: any }): Document {
const stack = this._schema._parsePopulate(expr);
return this._model._populate(this, stack);
}
}

function isGetter(obj, key) {
function isGetter(obj: any, key: PropertyKey): any {
return Object.getOwnPropertyDescriptor(obj, key).get;
}

Expand Down
4 changes: 2 additions & 2 deletions src/lib/jsonstream/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Parser from 'jsonparse';
* @param {*} y
* @returns {boolean}
*/
const check = (x, y) => {
const check = (x, y): boolean => {
if (typeof x === 'string') {
return y === x;
}
Expand All @@ -27,7 +27,7 @@ const check = (x, y) => {
return false;
};

export function parse(path, map = null) {
export function parse(path: string | any[], map = null) {
let header, footer;

const parser = new Parser();
Expand Down
Loading

0 comments on commit 99d1370

Please sign in to comment.