1.0.13 • Published 2 years ago

@zsnout/core v1.0.13

Weekly downloads
-
License
GNU
Repository
github
Last release
2 years ago

This module creates a Fastify server with a Socket.IO server attatched. Additionally, it exports simple functions for serving directories and adding database/mail providers.

JavaScript API

server: FastifyInstance

The main Fastify server.

server.database: DatabaseProvider

The server's database provider. See interface DatabaseProvider for more information.

NOTICE: This property may not exist. To check if a database provider has been added using addProvider(), use hasProvider() or onProvider().

server.mailer: MailProvider

The server's mail provider. See interface MailProvider for more information.

NOTICE: This property may not exist. To check if a mail provider has been added using addProvider(), use hasProvider() or onProvider().

server.io: SocketIOInstance<IOEvents, IOEvents, IOServerEvents>

The server's Socket.IO instance.

listen(port: number, host?: string, backlog?: number): Promise<void>

Starts listening for connections on the server instance. Returns a promise that resolves once the server accepts connections.

  • port: number - The port to listen on.
  • host?: string - The host to listen on.
  • backlog?: number - The number of connections to backlog before refusing new connections.

addProvider<T extends keyof Providers>(type: T, provider: ProvidersT): void

This function is also added as a server instance method as server.addProvider.

Adds a database or mail provider to the server instance.

  • type: T - The type of provider to add.
  • provider: Providers[T] - The provider to add.

hasProvider(type: keyof Providers): boolean

This function is also added as a server instance method as server.hasProvider.

Checks if a provider has been added to the server instance. Returns a boolean indicating if the server has the provider.

  • type: keyof Providers - The type of provider to check for.

onProvider(type: keyof Providers): boolean

This function is also added as a server instance method as server.onProvider.

Waits for a provider to be added to the server instance. A boolean indicating if the server has the provider.

  • type: keyof Providers - The type of provider to wait for.

serveDirectory(root: string, prefix?: string): void

This function is also added as a server instance method as server.serveDirectory.

Serves a directory of static files.

  • root: string - A path to the directory containing files to send to the client.
  • prefix?: string - An optional prefix to serve files under.

TypeScript API

interface Collection<T extends Table>

An interface all database collections must implement.

Collection.find(query: Partial<T>): Promise<readonly Readonly[]>

Find documents in the collection. Returns a promise resolving to an array of documents.

  • query: Partial<T> - A filter to match documents against.

Collection.insert(...documents: T[]): Promise<void>

Inserts documents into the collection. Returns a promise resolving once the documents have been inserted.

  • ...documents: T[] - The documents to insert.

Collection.update(query: Partial<T>, data: Partial<T>): Promise<void>

Updates documents in the collection. Returns a promise resolving once the documents have been updated.

  • query: Partial<T> - A filter to match documents against.
  • data: Partial<T> - The data to update the documents with.

Collection.delete(query: Partial<T>): Promise<void>

Deletes documents from the collection. Returns a promise resolving once the documents have been deleted.

  • query: Partial<T> - A filter to match documents against.

interface Database extends StructuredDatabase

A list of collections in the main database. Used for autocomplete.

interface DatabaseProvider

The interface a database provider must implement.

DatabaseProvider.collection<T extends keyof Database>(table: T): Promise<Collection<DatabaseT>>

Gets a collection from the database. Returns a promise resolving with a collection instance for the given table.

  • table: T - The name of the collection to get.

interface IOEvents

A collection of client events that can be used by the socket.io instance.

interface IOServerEvents

A collection of server events that can be used by the socket.io instance.

interface MailProvider

An interface all mail providers must implement.

interface Providers

A list of providers that can be added to the server instance.

interface Table

A type alias for a database table.

type StructuredDatabase = { table: string: Table }

The base structure for a database.

1.0.13

2 years ago

1.0.12

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.0

2 years ago

1.2.6

2 years ago

1.2.5

2 years ago

1.2.4

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.0

2 years ago