Skip to content

Commit

Permalink
bundle typescript declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
kilbot committed Mar 1, 2019
1 parent c2f07a3 commit ee459fa
Show file tree
Hide file tree
Showing 54 changed files with 412 additions and 268 deletions.
10 changes: 9 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
{
"parser": "babylon",
"printWidth": 100,
"trailingComma": "all"
"trailingComma": "all",
"overrides": [
{
"files": "*.ts",
"options": {
"parser": "typescript"
}
}
]
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"name": "@nozbe/watermelondb",
"description": "Build powerful React Native and React web apps that scale from hundreds to tens of thousands of records and remain fast",
"version": "0.10.1",
"types": "./types/index.d.ts",
"scripts": {
"build": "NODE_ENV=production node ./scripts/make.js",
"dev": "NODE_ENV=development node ./scripts/make.js",
Expand Down Expand Up @@ -88,6 +87,7 @@
"@babel/plugin-transform-sticky-regex": "^7.2.0",
"@babel/plugin-transform-template-literals": "^7.2.0",
"@babel/plugin-transform-unicode-regex": "^7.2.0",
"@types/react": "^16.8.6",
"anymatch": "^2.0.0",
"babel-core": "^7.0.0-0",
"babel-eslint": "10.0.1",
Expand All @@ -103,6 +103,7 @@
"cavy": "git+https://github.com/Nozbe/cavy.git",
"chokidar": "^2.0.4",
"concurrently": "^4.0.1",
"dts-generator": "^3.0.0",
"eslint": "^5.3.0",
"eslint-config-airbnb": "^17.1.0",
"eslint-config-prettier": "^4.0.0",
Expand Down
10 changes: 9 additions & 1 deletion scripts/make.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,15 @@ const compileTypescriptDefinitions = () => {
// eslint-disable-next-line
console.log(`✓ TypeScript definitions`)

execFile('./node_modules/.bin/tsc', ['--project', '.', '--outDir', path.join(DIR_PATH, 'types')])
execFile(
'./node_modules/.bin/dts-generator',
// eslint-disable-next-line
['--project', '.', '--out', path.resolve(DIST_PATH, 'index.d.ts')],
(error, stdout) => {
// eslint-disable-next-line
console.log(stdout)
},
)
}

if (isDevelopment) {
Expand Down
41 changes: 41 additions & 0 deletions src/Collection/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
declare module '@nozbe/watermelondb/Collection' {
import { Database, Model, Query, RecordId, TableName, TableSchema } from '@nozbe/watermelondb'
import { Condition } from '@nozbe/watermelondb/QueryDescription'
import { Class } from '@nozbe/watermelondb/utils/common'
import { Observable, Subject } from 'rxjs'

export interface CollectionChange<Record extends Model> {
record: Record
isDestroyed: boolean
}

export default class Collection<Record extends Model> {
public database: Database

public modelClass: Class<Record>

public changes: Subject<CollectionChange<Record>>

public table: TableName<Record>

public schema: TableSchema

public constructor(database: Database, ModelClass: Class<Record>)

public find(id: RecordId): Promise<Record>

public findAndObserve(id: RecordId): Observable<Record>

public query(...conditions: Condition[]): Query<Record>

public create(recordBuilder?: (record: Record) => void): Promise<Record>

public prepareCreate(recordBuilder?: (record: Record) => void): Record

public fetchQuery(query: Query<Record>): Promise<Record[]>

public fetchCount(query: Query<Record>): Promise<number>

public unsafeClearCache(): void
}
}
43 changes: 0 additions & 43 deletions src/Collection/index.ts

This file was deleted.

12 changes: 0 additions & 12 deletions src/CollectionMap/index.ts

This file was deleted.

12 changes: 12 additions & 0 deletions src/Database/CollectionMap.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
declare module '@nozbe/watermelondb/Database/CollectionMap' {
import { Collection, Database, Model, TableName } from '@nozbe/watermelondb'
import { Class } from '@nozbe/watermelondb/utils/common'

export default class CollectionMap {
public map: { [tableName: string]: Collection<any> }

public constructor(database: Database, modelClasses: Array<Class<Model>>)

public get<T extends Model>(tableName: TableName<T>): Collection<T>
}
}
31 changes: 31 additions & 0 deletions src/Database/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
declare module '@nozbe/watermelondb/Database' {
import { AppSchema, CollectionMap, DatabaseAdapter, Model, TableName } from '@nozbe/watermelondb'
import { CollectionChange } from '@nozbe/watermelondb/Collection'
import { Class } from '@nozbe/watermelondb/utils/common'
import { Observable } from 'rxjs'

export interface ActionInterface {
subAction<T>(action: () => Promise<T>): Promise<T>
}

export default class Database {
public adapter: DatabaseAdapter

public schema: AppSchema

public collections: CollectionMap

public constructor(options: { adapter: DatabaseAdapter; modelClasses: Array<Class<Model>> })

public batch(...records: Model[]): Promise<void>

// FIXME: action<T>(work: ActionInterface => Promise<T>, description?: string): Promise<T>
public action<T>(work: any, description?: string): Promise<T>

public withChangesForTables(
tables: Array<TableName<any>>,
): Observable<CollectionChange<any> | null>

public unsafeResetDatabase(): Promise<void>
}
}
29 changes: 0 additions & 29 deletions src/Database/index.ts

This file was deleted.

22 changes: 22 additions & 0 deletions src/DatabaseProvider/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
declare module '@nozbe/watermelondb/DatabaseProvider' {
import * as React from 'react'
import Database from '@nozbe/watermelondb/Database'

export interface DatabaseProviderProps {
children?: React.ReactChild // only one child is allowed, goes through React.Children.only
database: Database
}

export const DatabaseProviderComponent: React.ComponentClass<DatabaseProviderProps>

/**
* HOC
* https://gist.github.com/thehappybug/88342c122cfb1df9f14c9a10fb4926e4
*/
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
export function withDatabase<P extends { database?: Database }, R = Omit<P, 'database'>>(
Component: React.ComponentType<P> | React.FunctionComponent<P>,
): React.FunctionComponent<R>

export default DatabaseProviderComponent
}
14 changes: 14 additions & 0 deletions src/Model/helpers.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
declare module '@nozbe/watermelondb/Model/helper' {
import { Model } from '@nozbe/watermelondb'

export const hasUpdatedAt: (obj: Object) => boolean

export const createTimestampsFor: (
model: Model,
) => {
created_at: Date
updated_at: Date
}

export function addToRawSet(rawSet: string | void, value: string): string
}
12 changes: 0 additions & 12 deletions src/Model/helpers.ts

This file was deleted.

52 changes: 52 additions & 0 deletions src/Model/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
declare module '@nozbe/watermelondb/Model' {
import { ColumnName, TableName, Collection } from '@nozbe/watermelondb'
import { Observable } from 'rxjs'

export type RecordId = string

export type SyncStatus = 'synced' | 'created' | 'updated' | 'deleted'

export interface BelongsToAssociation {
type: 'belongs_to'
key: ColumnName
}
export interface HasManyAssociation {
type: 'has_many'
foreignKey: ColumnName
}
export type AssociationInfo = BelongsToAssociation | HasManyAssociation
export interface Associations {
[tableName: string]: AssociationInfo
}

export function associations(
...associationList: Array<[TableName<any>, AssociationInfo]>
): Associations

export default class Model {
// FIXME: How to correctly point to a static this?
public static table: TableName<Model>

public static associations: Associations

public id: RecordId

public syncStatus: SyncStatus

public update(recordUpdater?: (this: this) => void): Promise<void>

public prepareUpdate(recordUpdater?: (this: this) => void): this

public markAsDeleted(): Promise<void>

public destroyPermanently(): Promise<void>

public observe(): Observable<this>

public batch(...records: Readonly<[Model]>): Promise<void>

public subAction<T>(action: () => Promise<T>): Promise<T>

public collection: Collection<this>
}
}
38 changes: 0 additions & 38 deletions src/Model/index.ts

This file was deleted.

File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ declare module '@nozbe/watermelondb/QueryDescription' {
export function where(left: ColumnName, valueOrComparison: Value | Comparison): WhereDescription
export function and(...conditions: Where[]): And
export function or(...conditions: Where[]): Or
export function like(value: string): Comparison
export function sanitizeLikeString(value: string): string

type _OnFunctionColumnValue = (
table: TableName<any>,
Expand Down
Loading

0 comments on commit ee459fa

Please sign in to comment.