Skip to content

Commit

Permalink
feat(core): convert to esm module
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicbartl committed Jan 15, 2024
1 parent 336a3ca commit 8ee5545
Show file tree
Hide file tree
Showing 17 changed files with 297 additions and 131 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"license": "MIT",
"homepage": "https://github.com/freshfox/firestore-storage",
"devDependencies": {
"esbuild": "^0.19.11",
"firebase-tools": "^12.8.0",
"lerna": "^4.0.0",
"mocha-junit-reporter": "^2.2.1",
Expand Down
10 changes: 7 additions & 3 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
"version": "6.0.11",
"description": "Core classes, types and utilities for firestore-storage",
"license": "MIT",
"main": "dist/lib/index.js",
"types": "dist/lib/index.d.ts",
"main": "dist/cjs/index.js",
"module": "dist/esm2015/index.js",
"types": "dist/types/index.d.ts",
"engines": {
"node": ">=10.10.0"
},
Expand All @@ -16,7 +17,10 @@
],
"scripts": {
"test": "NODE_ENV=test mocha './src/**/*.test.ts' --reporter mocha-junit-reporter",
"build": "pnpm clean && tsc --build",
"build": "pnpm build:cjs && pnpm build:esm && pnpm build:types",
"build:cjs": "tsc --project tsconfig.json",
"build:esm": "tsc --project tsconfig.esm2015.json",
"build:types": "tsc --project tsconfig.types.json",
"clean": "rm -rf dist tsconfig.tsbuildinfo",
"fss": "fss"
},
Expand Down
File renamed without changes.
64 changes: 0 additions & 64 deletions packages/core/src/lib/storage/model.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as should from 'should';
import should from 'should';
import { CollectionPath, DocumentIds } from './collections';

describe('CollectionPath', function () {
Expand Down
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions packages/core/src/storage/model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface ModelMetaInternal {
id?: string;
rawPath?: string;
}

export type ModelMeta<R extends boolean = false> = R extends true ? Required<ModelMetaInternal> : ModelMetaInternal;
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ export abstract class BaseQuery<T extends BaseModel, Op extends string, R> {
return this.applyWhere(this.getWhereProp(prop), op, value);
}

whereAll(attributes: ModelQuery<T> | null) {
whereAll<K extends ModelQuery<T>>(attributes: K | null) {
if (attributes) {
return Object.keys(attributes).reduce((query, key) => {
return query.applyWhere(key, '==' as any, attributes[key]);
const keys = Object.keys(attributes) as (keyof K)[];
return keys.reduce((query, key) => {
return query.applyWhere(String(key), '==' as any, attributes[key]);
}, this);
}
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ export abstract class BaseRepository<T extends BaseModel, Path extends Collectio
id: undefined;
data: ModelDataOnly<T>;
};
toFirestoreDocument(data: T | ModelDataOnly<T> | PatchUpdate<ModelDataWithId<T>>) {
toFirestoreDocument(data: T | ModelDataOnly<T> | PatchUpdate<ModelDataWithId<T>>): {
id: string | undefined;
data: ModelDataOnly<T>;
} {
return this.getTransformer().toFirestoreDocument(data);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { BaseModelClass, ModelMeta } from './model';
import { ModelMeta } from './model';
import { cloneDeep } from 'lodash';
import { BaseModel, ModelDataOnly, ModelDataWithId, PatchUpdate } from './types';

export interface IDocumentTransformer<T extends BaseModel> {
fromFirestoreToObject(data: ModelDataOnly<T>, meta: ModelMeta): T;
fromFirestoreToObject(data: ModelDataOnly<T>, meta: ModelMeta<true>): T;

toFirestoreDocument(doc: T): { id: string; data: ModelDataOnly<T> };
toFirestoreDocument(doc: ModelDataOnly<T> | PatchUpdate<ModelDataWithId<T>>): {
Expand All @@ -16,24 +16,6 @@ export interface IDocumentTransformer<T extends BaseModel> {
};
}

export class ModelClassTransformer<T extends BaseModelClass<T>> implements IDocumentTransformer<T> {
constructor(private TypeClass: new (...args: any[]) => T) {}

fromFirestoreToObject(data, meta) {
return new this.TypeClass(data, meta);
}

toFirestoreDocument(doc) {
if (doc instanceof BaseModelClass) {
return {
id: doc.id,
data: doc.getData(),
};
}
return DEFAULT_DOCUMENT_TRANSFORMER.toFirestoreDocument(doc) as any;
}
}

export const DEFAULT_DOCUMENT_TRANSFORMER: IDocumentTransformer<BaseModel> = {
fromFirestoreToObject(data, meta) {
const base: BaseModel = {
Expand All @@ -43,13 +25,13 @@ export const DEFAULT_DOCUMENT_TRANSFORMER: IDocumentTransformer<BaseModel> = {
return Object.assign({}, data, base);
},
toFirestoreDocument(doc) {
const clone = cloneDeep(doc);
const clone = cloneDeep(doc) as any;
delete clone.id;
delete clone.createdAt;
delete clone.updatedAt;

return {
id: doc.id,
id: (doc as any)['id'] || undefined,
data: clone,
};
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,6 @@ export type ModelDataWithId<T extends BaseModel> = Pick<T, 'id'> & ModelDataOnly

export type ModelQuery<T extends BaseModel> = Partial<ModelDataOnly<T>>;

export interface ReferenceMap {
[id: string]: boolean;
}

export function toReferenceMap(...entities: BaseModel[]): ReferenceMap {
return entities.reduce((map: ReferenceMap, entity: BaseModel) => {
map[entity.id] = true;
return map;
}, {});
}

export function toReferenceMapFromIds(ids: string[], value: any = true): ReferenceMap {
return ids.reduce((map, id) => {
map[id] = value;
return map;
}, {});
}

export function isSameReferenceMap(r1: ReferenceMap, r2: ReferenceMap) {
const ids1 = Object.keys(r1);
const ids2 = Object.keys(r2);
if (ids1.length !== ids2.length) {
return false;
}
for (const id of ids1) {
if (ids2.indexOf(id) === -1) {
return false;
}
}
return true;
}

type NestedPartial<T> = {
[K in keyof T]?: T[K] extends Array<infer R> ? Array<R> : NestedPartial<T[K]>;
};
Expand Down
7 changes: 7 additions & 0 deletions packages/core/tsconfig.esm2015.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "ES2015",
"outDir": "dist/esm2015",
}
}
3 changes: 2 additions & 1 deletion packages/core/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"extends": "../tsconfig-base.json",
"compilerOptions": {
"outDir": "./dist",
"rootDir": "./src",
"outDir": "./dist/cjs",
"baseUrl": "./",
"module": "commonjs",
"types": [
"node",
"mocha",
Expand Down
8 changes: 8 additions & 0 deletions packages/core/tsconfig.types.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"declaration": true,
"emitDeclarationOnly": true,
"outDir": "dist/types",
}
}
12 changes: 10 additions & 2 deletions packages/tsconfig-base.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,17 @@
"sourceMap": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"composite": true
"composite": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"moduleResolution": "node",
"outDir": "build/node",
"rootDir": "./src",
"strict": true,
"inlineSources": true,
"removeComments": false,
},
"exclude": [
"exclude": [
"node_modules",
"dist"
],
Expand Down
Loading

0 comments on commit 8ee5545

Please sign in to comment.