lumen-framework 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +26 -0
- package/README.md +164 -0
- package/bin/lumen +275 -0
- package/dist/cli.cjs +3002 -0
- package/dist/cli.cjs.map +7 -0
- package/dist/index.js +6515 -0
- package/dist/index.js.map +7 -0
- package/dist/index.mjs +6480 -0
- package/dist/index.mjs.map +7 -0
- package/dist/types/constants.d.ts +7 -0
- package/dist/types/errors/controller-missing-error.d.ts +7 -0
- package/dist/types/errors/serializer-missing-error.d.ts +7 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/interfaces.d.ts +12 -0
- package/dist/types/packages/application/index.d.ts +97 -0
- package/dist/types/packages/application/initialize.d.ts +6 -0
- package/dist/types/packages/application/interfaces.d.ts +16 -0
- package/dist/types/packages/application/utils/create-controller.d.ts +12 -0
- package/dist/types/packages/application/utils/create-serializer.d.ts +4 -0
- package/dist/types/packages/application/utils/normalize-port.d.ts +4 -0
- package/dist/types/packages/cli/commands/build.d.ts +1 -0
- package/dist/types/packages/cli/commands/create.d.ts +4 -0
- package/dist/types/packages/cli/commands/dbcreate.d.ts +4 -0
- package/dist/types/packages/cli/commands/dbdrop.d.ts +4 -0
- package/dist/types/packages/cli/commands/dbmigrate.d.ts +4 -0
- package/dist/types/packages/cli/commands/dbrollback.d.ts +4 -0
- package/dist/types/packages/cli/commands/dbseed.d.ts +4 -0
- package/dist/types/packages/cli/commands/destroy.d.ts +11 -0
- package/dist/types/packages/cli/commands/generate.d.ts +10 -0
- package/dist/types/packages/cli/commands/index.d.ts +12 -0
- package/dist/types/packages/cli/commands/repl.d.ts +1 -0
- package/dist/types/packages/cli/commands/serve.d.ts +8 -0
- package/dist/types/packages/cli/commands/test.d.ts +4 -0
- package/dist/types/packages/cli/constants.d.ts +2 -0
- package/dist/types/packages/cli/errors/database-config-missing.d.ts +4 -0
- package/dist/types/packages/cli/generator/constants.d.ts +5 -0
- package/dist/types/packages/cli/generator/index.d.ts +11 -0
- package/dist/types/packages/cli/generator/interfaces.d.ts +9 -0
- package/dist/types/packages/cli/generator/utils/create-generator.d.ts +9 -0
- package/dist/types/packages/cli/generator/utils/generate-type.d.ts +33 -0
- package/dist/types/packages/cli/generator/utils/generator-for.d.ts +2 -0
- package/dist/types/packages/cli/generator/utils/log.d.ts +4 -0
- package/dist/types/packages/cli/generator/utils/migration-conflict.d.ts +6 -0
- package/dist/types/packages/cli/templates/application.d.ts +5 -0
- package/dist/types/packages/cli/templates/config.d.ts +5 -0
- package/dist/types/packages/cli/templates/controller.d.ts +5 -0
- package/dist/types/packages/cli/templates/database.d.ts +5 -0
- package/dist/types/packages/cli/templates/empty-migration.d.ts +5 -0
- package/dist/types/packages/cli/templates/eslint-config.d.ts +5 -0
- package/dist/types/packages/cli/templates/gitignore.d.ts +5 -0
- package/dist/types/packages/cli/templates/license.d.ts +5 -0
- package/dist/types/packages/cli/templates/middleware.d.ts +5 -0
- package/dist/types/packages/cli/templates/model-migration.d.ts +5 -0
- package/dist/types/packages/cli/templates/model.d.ts +5 -0
- package/dist/types/packages/cli/templates/package-json.d.ts +5 -0
- package/dist/types/packages/cli/templates/readme.d.ts +5 -0
- package/dist/types/packages/cli/templates/routes.d.ts +5 -0
- package/dist/types/packages/cli/templates/seed.d.ts +5 -0
- package/dist/types/packages/cli/templates/serializer.d.ts +5 -0
- package/dist/types/packages/cli/templates/util.d.ts +5 -0
- package/dist/types/packages/cli/utils/create-prompt.d.ts +4 -0
- package/dist/types/packages/cli/utils/driver-for.d.ts +1 -0
- package/dist/types/packages/cli/utils/indent.d.ts +1 -0
- package/dist/types/packages/compiler/index.d.ts +11 -0
- package/dist/types/packages/compiler/interfaces.d.ts +1 -0
- package/dist/types/packages/compiler/utils/create-boot-script.d.ts +6 -0
- package/dist/types/packages/compiler/utils/create-manifest.d.ts +6 -0
- package/dist/types/packages/compiler/utils/format-name.d.ts +5 -0
- package/dist/types/packages/config/index.d.ts +3 -0
- package/dist/types/packages/config/interfaces.d.ts +6 -0
- package/dist/types/packages/controller/constants.d.ts +2 -0
- package/dist/types/packages/controller/index.d.ts +591 -0
- package/dist/types/packages/controller/interfaces.d.ts +13 -0
- package/dist/types/packages/controller/utils/find-many.d.ts +6 -0
- package/dist/types/packages/controller/utils/find-one.d.ts +6 -0
- package/dist/types/packages/controller/utils/params-to-query.d.ts +16 -0
- package/dist/types/packages/controller/utils/resolve-relationships.d.ts +5 -0
- package/dist/types/packages/database/attribute/index.d.ts +6 -0
- package/dist/types/packages/database/attribute/interfaces.d.ts +6 -0
- package/dist/types/packages/database/attribute/utils/create-attribute.d.ts +5 -0
- package/dist/types/packages/database/attribute/utils/create-getter.d.ts +3 -0
- package/dist/types/packages/database/attribute/utils/create-normalizer.d.ts +1 -0
- package/dist/types/packages/database/attribute/utils/create-setter.d.ts +8 -0
- package/dist/types/packages/database/change-set/index.d.ts +10 -0
- package/dist/types/packages/database/constants.d.ts +3 -0
- package/dist/types/packages/database/errors/config-missing-error.d.ts +4 -0
- package/dist/types/packages/database/errors/index.d.ts +5 -0
- package/dist/types/packages/database/errors/invalid-driver-error.d.ts +7 -0
- package/dist/types/packages/database/errors/migrations-pending-error.d.ts +7 -0
- package/dist/types/packages/database/errors/model-missing-error.d.ts +7 -0
- package/dist/types/packages/database/errors/unique-constraint-error.d.ts +4 -0
- package/dist/types/packages/database/index.d.ts +26 -0
- package/dist/types/packages/database/initialize.d.ts +6 -0
- package/dist/types/packages/database/interfaces.d.ts +85 -0
- package/dist/types/packages/database/migration/index.d.ts +12 -0
- package/dist/types/packages/database/migration/interfaces.d.ts +4 -0
- package/dist/types/packages/database/migration/utils/generate-timestamp.d.ts +2 -0
- package/dist/types/packages/database/model/index.d.ts +1046 -0
- package/dist/types/packages/database/model/initialize-class.d.ts +10 -0
- package/dist/types/packages/database/model/interfaces.d.ts +14 -0
- package/dist/types/packages/database/model/utils/get-columns.d.ts +5 -0
- package/dist/types/packages/database/model/utils/persistence.d.ts +18 -0
- package/dist/types/packages/database/model/utils/process-write-error.d.ts +4 -0
- package/dist/types/packages/database/model/utils/run-hooks.d.ts +6 -0
- package/dist/types/packages/database/model/utils/validate.d.ts +5 -0
- package/dist/types/packages/database/query/errors/index.d.ts +1 -0
- package/dist/types/packages/database/query/errors/record-not-found-error.d.ts +6 -0
- package/dist/types/packages/database/query/index.d.ts +38 -0
- package/dist/types/packages/database/query/runner/constants.d.ts +2 -0
- package/dist/types/packages/database/query/runner/index.d.ts +9 -0
- package/dist/types/packages/database/query/runner/utils/build-results.d.ts +10 -0
- package/dist/types/packages/database/query/runner/utils/get-find-param.d.ts +2 -0
- package/dist/types/packages/database/query/utils/format-select.d.ts +5 -0
- package/dist/types/packages/database/query/utils/scopes-for.d.ts +2 -0
- package/dist/types/packages/database/relationship/index.d.ts +11 -0
- package/dist/types/packages/database/relationship/interfaces.d.ts +12 -0
- package/dist/types/packages/database/relationship/utils/getters.d.ts +14 -0
- package/dist/types/packages/database/relationship/utils/inverse-setters.d.ts +15 -0
- package/dist/types/packages/database/relationship/utils/setters.d.ts +14 -0
- package/dist/types/packages/database/relationship/utils/unassociate.d.ts +5 -0
- package/dist/types/packages/database/relationship/utils/update-relationship.d.ts +5 -0
- package/dist/types/packages/database/relationship/utils/validate-type.d.ts +2 -0
- package/dist/types/packages/database/transaction/index.d.ts +16 -0
- package/dist/types/packages/database/transaction/interfaces.d.ts +5 -0
- package/dist/types/packages/database/utils/connect.d.ts +6 -0
- package/dist/types/packages/database/utils/create-migrations.d.ts +5 -0
- package/dist/types/packages/database/utils/normalize-model-name.d.ts +5 -0
- package/dist/types/packages/database/utils/pending-migrations.d.ts +4 -0
- package/dist/types/packages/database/utils/type-for-column.d.ts +5 -0
- package/dist/types/packages/database/validation/errors/index.d.ts +1 -0
- package/dist/types/packages/database/validation/errors/validation-error.d.ts +7 -0
- package/dist/types/packages/database/validation/index.d.ts +13 -0
- package/dist/types/packages/database/validation/interfaces.d.ts +5 -0
- package/dist/types/packages/freezeable/constants.d.ts +1 -0
- package/dist/types/packages/freezeable/index.d.ts +3 -0
- package/dist/types/packages/freezeable/map/index.d.ts +11 -0
- package/dist/types/packages/freezeable/set/index.d.ts +11 -0
- package/dist/types/packages/freezeable/utils/freeze.d.ts +20 -0
- package/dist/types/packages/freezeable/utils/is-frozen.d.ts +4 -0
- package/dist/types/packages/fs/index.d.ts +52 -0
- package/dist/types/packages/fs/interfaces.d.ts +19 -0
- package/dist/types/packages/fs/utils/create-path-remover.d.ts +5 -0
- package/dist/types/packages/fs/utils/create-resolver.d.ts +4 -0
- package/dist/types/packages/fs/utils/exists.d.ts +4 -0
- package/dist/types/packages/fs/utils/is-js-file.d.ts +4 -0
- package/dist/types/packages/fs/utils/parse-path.d.ts +5 -0
- package/dist/types/packages/fs/utils/rmrf.d.ts +5 -0
- package/dist/types/packages/fs/watcher/index.d.ts +13 -0
- package/dist/types/packages/fs/watcher/initialize.d.ts +5 -0
- package/dist/types/packages/fs/watcher/interfaces.d.ts +3 -0
- package/dist/types/packages/jsonapi/constants.d.ts +2 -0
- package/dist/types/packages/jsonapi/errors/index.d.ts +3 -0
- package/dist/types/packages/jsonapi/errors/invalid-content-type-error.d.ts +8 -0
- package/dist/types/packages/jsonapi/errors/not-acceptable-error.d.ts +8 -0
- package/dist/types/packages/jsonapi/errors/unsupported-media-type-error.d.ts +8 -0
- package/dist/types/packages/jsonapi/index.d.ts +5 -0
- package/dist/types/packages/jsonapi/interfaces.d.ts +66 -0
- package/dist/types/packages/jsonapi/utils/has-media-type.d.ts +4 -0
- package/dist/types/packages/jsonapi/utils/is-jsonapi.d.ts +4 -0
- package/dist/types/packages/loader/builder/index.d.ts +6 -0
- package/dist/types/packages/loader/builder/interfaces.d.ts +10 -0
- package/dist/types/packages/loader/builder/utils/create-children-builder.d.ts +2 -0
- package/dist/types/packages/loader/builder/utils/create-parent-builder.d.ts +2 -0
- package/dist/types/packages/loader/builder/utils/sort-by-namespace.d.ts +5 -0
- package/dist/types/packages/loader/index.d.ts +8 -0
- package/dist/types/packages/loader/interfaces.d.ts +4 -0
- package/dist/types/packages/loader/resolver/index.d.ts +7 -0
- package/dist/types/packages/loader/resolver/utils/closest-ancestor.d.ts +2 -0
- package/dist/types/packages/loader/resolver/utils/closest-child.d.ts +2 -0
- package/dist/types/packages/loader/utils/bundle-for.d.ts +5 -0
- package/dist/types/packages/loader/utils/format-key.d.ts +4 -0
- package/dist/types/packages/logger/constants.d.ts +8 -0
- package/dist/types/packages/logger/index.d.ts +182 -0
- package/dist/types/packages/logger/interfaces.d.ts +17 -0
- package/dist/types/packages/logger/request-logger/index.d.ts +6 -0
- package/dist/types/packages/logger/request-logger/interfaces.d.ts +25 -0
- package/dist/types/packages/logger/request-logger/templates.d.ts +9 -0
- package/dist/types/packages/logger/request-logger/utils/filter-params.d.ts +4 -0
- package/dist/types/packages/logger/request-logger/utils/log-json.d.ts +10 -0
- package/dist/types/packages/logger/request-logger/utils/log-text.d.ts +10 -0
- package/dist/types/packages/logger/utils/line.d.ts +4 -0
- package/dist/types/packages/logger/utils/sql.d.ts +4 -0
- package/dist/types/packages/logger/writer/constants.d.ts +3 -0
- package/dist/types/packages/logger/writer/index.d.ts +6 -0
- package/dist/types/packages/logger/writer/interfaces.d.ts +2 -0
- package/dist/types/packages/logger/writer/utils/format-message.d.ts +9 -0
- package/dist/types/packages/lumenify/index.d.ts +11 -0
- package/dist/types/packages/lumenify/utils/create-response-proxy.d.ts +8 -0
- package/dist/types/packages/pm/cluster/index.d.ts +21 -0
- package/dist/types/packages/pm/cluster/interfaces.d.ts +7 -0
- package/dist/types/packages/pm/index.d.ts +6 -0
- package/dist/types/packages/router/definitions/context/index.d.ts +11 -0
- package/dist/types/packages/router/definitions/context/utils/create-definition-group.d.ts +7 -0
- package/dist/types/packages/router/definitions/context/utils/create-definition.d.ts +10 -0
- package/dist/types/packages/router/definitions/context/utils/normalize-resource-args.d.ts +12 -0
- package/dist/types/packages/router/definitions/index.d.ts +10 -0
- package/dist/types/packages/router/definitions/interfaces.d.ts +7 -0
- package/dist/types/packages/router/index.d.ts +18 -0
- package/dist/types/packages/router/interfaces.d.ts +18 -0
- package/dist/types/packages/router/namespace/index.d.ts +20 -0
- package/dist/types/packages/router/namespace/interfaces.d.ts +9 -0
- package/dist/types/packages/router/namespace/utils/normalize-name.d.ts +4 -0
- package/dist/types/packages/router/namespace/utils/normalize-path.d.ts +4 -0
- package/dist/types/packages/router/resource/index.d.ts +13 -0
- package/dist/types/packages/router/resource/interfaces.d.ts +5 -0
- package/dist/types/packages/router/resource/utils/normalize-only.d.ts +5 -0
- package/dist/types/packages/router/route/action/constants.d.ts +1 -0
- package/dist/types/packages/router/route/action/enhancers/resource.d.ts +5 -0
- package/dist/types/packages/router/route/action/enhancers/track-perf.d.ts +5 -0
- package/dist/types/packages/router/route/action/index.d.ts +9 -0
- package/dist/types/packages/router/route/action/interfaces.d.ts +2 -0
- package/dist/types/packages/router/route/action/utils/create-page-links.d.ts +12 -0
- package/dist/types/packages/router/route/action/utils/get-action-name.d.ts +5 -0
- package/dist/types/packages/router/route/action/utils/get-controller-name.d.ts +5 -0
- package/dist/types/packages/router/route/constants.d.ts +1 -0
- package/dist/types/packages/router/route/index.d.ts +28 -0
- package/dist/types/packages/router/route/interfaces.d.ts +10 -0
- package/dist/types/packages/router/route/params/errors/index.d.ts +5 -0
- package/dist/types/packages/router/route/params/errors/invalid-parameter-error.d.ts +8 -0
- package/dist/types/packages/router/route/params/errors/parameter-not-nullable-error.d.ts +9 -0
- package/dist/types/packages/router/route/params/errors/parameter-required-error.d.ts +8 -0
- package/dist/types/packages/router/route/params/errors/parameter-type-error.d.ts +9 -0
- package/dist/types/packages/router/route/params/errors/parameter-value-error.d.ts +9 -0
- package/dist/types/packages/router/route/params/errors/resource-mismatch-error.d.ts +8 -0
- package/dist/types/packages/router/route/params/index.d.ts +18 -0
- package/dist/types/packages/router/route/params/interfaces.d.ts +25 -0
- package/dist/types/packages/router/route/params/parameter/index.d.ts +14 -0
- package/dist/types/packages/router/route/params/parameter/interfaces.d.ts +4 -0
- package/dist/types/packages/router/route/params/parameter/utils/validate-value.d.ts +5 -0
- package/dist/types/packages/router/route/params/parameter-group/index.d.ts +14 -0
- package/dist/types/packages/router/route/params/parameter-group/utils/has-required-params.d.ts +5 -0
- package/dist/types/packages/router/route/params/utils/get-data-params.d.ts +6 -0
- package/dist/types/packages/router/route/params/utils/get-default-collection-params.d.ts +6 -0
- package/dist/types/packages/router/route/params/utils/get-default-member-params.d.ts +5 -0
- package/dist/types/packages/router/route/params/utils/get-query-params.d.ts +14 -0
- package/dist/types/packages/router/route/params/utils/get-url-params.d.ts +5 -0
- package/dist/types/packages/router/route/params/utils/validate-resource-id.d.ts +5 -0
- package/dist/types/packages/router/route/params/utils/validate-type.d.ts +5 -0
- package/dist/types/packages/router/route/utils/get-dynamic-segments.d.ts +4 -0
- package/dist/types/packages/router/route/utils/get-static-path.d.ts +4 -0
- package/dist/types/packages/router/utils/create-replacer.d.ts +5 -0
- package/dist/types/packages/serializer/index.d.ts +521 -0
- package/dist/types/packages/serializer/interfaces.d.ts +7 -0
- package/dist/types/packages/server/constants.d.ts +2 -0
- package/dist/types/packages/server/index.d.ts +27 -0
- package/dist/types/packages/server/interfaces.d.ts +18 -0
- package/dist/types/packages/server/request/constants.d.ts +2 -0
- package/dist/types/packages/server/request/index.d.ts +8 -0
- package/dist/types/packages/server/request/interfaces.d.ts +63 -0
- package/dist/types/packages/server/request/parser/constants.d.ts +6 -0
- package/dist/types/packages/server/request/parser/errors/index.d.ts +1 -0
- package/dist/types/packages/server/request/parser/errors/malformed-request-error.d.ts +8 -0
- package/dist/types/packages/server/request/parser/index.d.ts +5 -0
- package/dist/types/packages/server/request/parser/utils/format.d.ts +17 -0
- package/dist/types/packages/server/request/parser/utils/parse-nested-object.d.ts +4 -0
- package/dist/types/packages/server/request/parser/utils/parse-read.d.ts +5 -0
- package/dist/types/packages/server/request/parser/utils/parse-write.d.ts +5 -0
- package/dist/types/packages/server/request/utils/get-domain.d.ts +5 -0
- package/dist/types/packages/server/responder/index.d.ts +5 -0
- package/dist/types/packages/server/responder/utils/data-for.d.ts +5 -0
- package/dist/types/packages/server/responder/utils/has-content-type.d.ts +2 -0
- package/dist/types/packages/server/responder/utils/normalize.d.ts +7 -0
- package/dist/types/packages/server/response/index.d.ts +5 -0
- package/dist/types/packages/server/response/interfaces.d.ts +22 -0
- package/dist/types/packages/server/utils/create-server-error.d.ts +7 -0
- package/dist/types/packages/server/utils/set-cors-headers.d.ts +3 -0
- package/dist/types/packages/server/utils/validate-accept.d.ts +4 -0
- package/dist/types/packages/server/utils/validate-content-type.d.ts +4 -0
- package/dist/types/packages/template/index.d.ts +8 -0
- package/dist/types/packages/template/utils/insert-values.d.ts +4 -0
- package/dist/types/utils/chain.d.ts +5 -0
- package/dist/types/utils/compact.d.ts +11 -0
- package/dist/types/utils/compose.d.ts +19 -0
- package/dist/types/utils/create-query-string.d.ts +6 -0
- package/dist/types/utils/diff.d.ts +4 -0
- package/dist/types/utils/entries.d.ts +4 -0
- package/dist/types/utils/env.d.ts +3 -0
- package/dist/types/utils/exec.d.ts +5 -0
- package/dist/types/utils/github.d.ts +9 -0
- package/dist/types/utils/has-own-property.d.ts +1 -0
- package/dist/types/utils/insert.d.ts +4 -0
- package/dist/types/utils/is-buffer.d.ts +4 -0
- package/dist/types/utils/is-null.d.ts +4 -0
- package/dist/types/utils/is-object.d.ts +17 -0
- package/dist/types/utils/is-undefined.d.ts +4 -0
- package/dist/types/utils/k.d.ts +10 -0
- package/dist/types/utils/map-to-object.d.ts +1 -0
- package/dist/types/utils/merge.d.ts +4 -0
- package/dist/types/utils/omit.d.ts +9 -0
- package/dist/types/utils/pick.d.ts +11 -0
- package/dist/types/utils/present.d.ts +4 -0
- package/dist/types/utils/promise-hash.d.ts +9 -0
- package/dist/types/utils/proxy.d.ts +6 -0
- package/dist/types/utils/range.d.ts +4 -0
- package/dist/types/utils/set-type.d.ts +11 -0
- package/dist/types/utils/sleep.d.ts +4 -0
- package/dist/types/utils/stringify.d.ts +4 -0
- package/dist/types/utils/transform-keys.d.ts +29 -0
- package/dist/types/utils/try-catch.d.ts +55 -0
- package/dist/types/utils/underscore.d.ts +4 -0
- package/dist/types/utils/uniq.d.ts +4 -0
- package/package.json +87 -0
|
@@ -0,0 +1,1046 @@
|
|
|
1
|
+
import Query from '../query';
|
|
2
|
+
import ChangeSet from '../change-set';
|
|
3
|
+
import type Logger from '../../logger';
|
|
4
|
+
import type Database from '../../database';
|
|
5
|
+
import type Serializer from '../../serializer';
|
|
6
|
+
import type { Relationship$opts } from '../relationship';
|
|
7
|
+
import type { ModelClass, Database$column } from '../interfaces';
|
|
8
|
+
import type { Transaction$ResultProxy } from '../transaction';
|
|
9
|
+
import type { Model$Hooks } from './interfaces';
|
|
10
|
+
/**
|
|
11
|
+
* @class Model
|
|
12
|
+
* @public
|
|
13
|
+
*/
|
|
14
|
+
declare class Model {
|
|
15
|
+
['constructor']: ModelClass;
|
|
16
|
+
/**
|
|
17
|
+
* The name of the corresponding database table for a `Model` instance's
|
|
18
|
+
* constructor.
|
|
19
|
+
*
|
|
20
|
+
* @property tableName
|
|
21
|
+
* @type {String}
|
|
22
|
+
* @public
|
|
23
|
+
*/
|
|
24
|
+
tableName: string;
|
|
25
|
+
/**
|
|
26
|
+
* The canonical name of a `Model`'s constructor.
|
|
27
|
+
*
|
|
28
|
+
* @property modelName
|
|
29
|
+
* @type {String}
|
|
30
|
+
* @public
|
|
31
|
+
*/
|
|
32
|
+
modelName: string;
|
|
33
|
+
/**
|
|
34
|
+
* The name of the API resource a `Model` instance's constructor represents.
|
|
35
|
+
*
|
|
36
|
+
* @property resourceName
|
|
37
|
+
* @type {String}
|
|
38
|
+
* @public
|
|
39
|
+
*/
|
|
40
|
+
resourceName: string;
|
|
41
|
+
/**
|
|
42
|
+
* A timestamp representing when the Model instance was created.
|
|
43
|
+
*
|
|
44
|
+
* @property createdAt
|
|
45
|
+
* @type {Date}
|
|
46
|
+
* @public
|
|
47
|
+
*/
|
|
48
|
+
createdAt: Date;
|
|
49
|
+
/**
|
|
50
|
+
* A timestamp representing the last time the Model instance was updated.
|
|
51
|
+
*
|
|
52
|
+
* @property updatedAt
|
|
53
|
+
* @type {Date}
|
|
54
|
+
* @public
|
|
55
|
+
*/
|
|
56
|
+
updatedAt: Date;
|
|
57
|
+
/**
|
|
58
|
+
* @property initialized
|
|
59
|
+
* @type {Boolean}
|
|
60
|
+
* @private
|
|
61
|
+
*/
|
|
62
|
+
initialized: boolean;
|
|
63
|
+
/**
|
|
64
|
+
* @property rawColumnData
|
|
65
|
+
* @type {Object}
|
|
66
|
+
* @private
|
|
67
|
+
*/
|
|
68
|
+
rawColumnData: Record<string, unknown>;
|
|
69
|
+
/**
|
|
70
|
+
* @property isModelInstance
|
|
71
|
+
* @type {Boolean}
|
|
72
|
+
* @private
|
|
73
|
+
*/
|
|
74
|
+
isModelInstance: boolean;
|
|
75
|
+
/**
|
|
76
|
+
* @property prevAssociations
|
|
77
|
+
* @type {Set}
|
|
78
|
+
* @private
|
|
79
|
+
*/
|
|
80
|
+
prevAssociations: Set<Model>;
|
|
81
|
+
/**
|
|
82
|
+
* @property changeSets
|
|
83
|
+
* @type {Array}
|
|
84
|
+
* @private
|
|
85
|
+
*/
|
|
86
|
+
changeSets: Array<ChangeSet>;
|
|
87
|
+
/**
|
|
88
|
+
* An object where you declare `hasOne` relationships.
|
|
89
|
+
*
|
|
90
|
+
* When declaring a relationship you must specify the inverse of the
|
|
91
|
+
* relationship.
|
|
92
|
+
*
|
|
93
|
+
* ```javascript
|
|
94
|
+
* class User extends Model {
|
|
95
|
+
* static hasOne = {
|
|
96
|
+
* profile: {
|
|
97
|
+
* inverse: 'user'
|
|
98
|
+
* // The line above lets Lumen know that this relationship is accessible
|
|
99
|
+
* // on profile instances via `profile.user`.
|
|
100
|
+
* }
|
|
101
|
+
* };
|
|
102
|
+
* }
|
|
103
|
+
*
|
|
104
|
+
* class Profile extends Model {
|
|
105
|
+
* static belongsTo = {
|
|
106
|
+
* user: {
|
|
107
|
+
* inverse: 'profile'
|
|
108
|
+
* // The line above lets Lumen know that this relationship is accessible
|
|
109
|
+
* // on user instances via `user.profile`.
|
|
110
|
+
* }
|
|
111
|
+
* };
|
|
112
|
+
* }
|
|
113
|
+
* ```
|
|
114
|
+
*
|
|
115
|
+
* If the name of the model is different than the key of the relationship, you
|
|
116
|
+
* must specify it in the relationship object.
|
|
117
|
+
*
|
|
118
|
+
* ```javascript
|
|
119
|
+
* class Profile extends Model {
|
|
120
|
+
* static belongsTo = {
|
|
121
|
+
* owner: {
|
|
122
|
+
* inverse: 'profile',
|
|
123
|
+
* model: 'user'
|
|
124
|
+
* // The line above lets Lumen know that this is a relationship with the
|
|
125
|
+
* // `User` model and not a non-existent `Owner` model.
|
|
126
|
+
* }
|
|
127
|
+
* };
|
|
128
|
+
* }
|
|
129
|
+
* ```
|
|
130
|
+
*
|
|
131
|
+
* @property hasOne
|
|
132
|
+
* @type {Object}
|
|
133
|
+
* @default {}
|
|
134
|
+
* @static
|
|
135
|
+
* @public
|
|
136
|
+
*/
|
|
137
|
+
static hasOne: Record<string, unknown>;
|
|
138
|
+
/**
|
|
139
|
+
* An object where you declare `hasMany` relationships.
|
|
140
|
+
*
|
|
141
|
+
* When declaring a relationship you must specify the inverse of the
|
|
142
|
+
* relationship.
|
|
143
|
+
*
|
|
144
|
+
* ```javascript
|
|
145
|
+
* class Author extends Model {
|
|
146
|
+
* static hasMany = {
|
|
147
|
+
* books: {
|
|
148
|
+
* inverse: 'author'
|
|
149
|
+
* // The line above lets Lumen know that this relationship is accessible
|
|
150
|
+
* // on book instances via `book.author`.
|
|
151
|
+
* }
|
|
152
|
+
* };
|
|
153
|
+
* }
|
|
154
|
+
*
|
|
155
|
+
* class Book extends Model {
|
|
156
|
+
* static belongsTo = {
|
|
157
|
+
* author: {
|
|
158
|
+
* inverse: 'books'
|
|
159
|
+
* // The line above lets Lumen know that this relationship is accessible
|
|
160
|
+
* // on author instances via `author.books`.
|
|
161
|
+
* }
|
|
162
|
+
* };
|
|
163
|
+
* }
|
|
164
|
+
* ```
|
|
165
|
+
*
|
|
166
|
+
* If the name of the model is different than the key of the relationship, you
|
|
167
|
+
* must specify it in the relationship object.
|
|
168
|
+
*
|
|
169
|
+
* ```javascript
|
|
170
|
+
* class Author extends Model {
|
|
171
|
+
* static hasMany = {
|
|
172
|
+
* publications: {
|
|
173
|
+
* inverse: 'author',
|
|
174
|
+
* model: 'book'
|
|
175
|
+
* // The line above lets Lumen know that this is a relationship with the
|
|
176
|
+
* // `Book` model and not a non-existent `Publication` model.
|
|
177
|
+
* }
|
|
178
|
+
* };
|
|
179
|
+
* }
|
|
180
|
+
* ```
|
|
181
|
+
*
|
|
182
|
+
* ##### Many to Many
|
|
183
|
+
*
|
|
184
|
+
* In the examples above there is only one owner of relationship. Sometimes we
|
|
185
|
+
* need to express a many to many relationship. Typically in relational
|
|
186
|
+
* databases, this is done with a join table. When declaring a many to many
|
|
187
|
+
* relationship that uses a join table, you must specify the join model.
|
|
188
|
+
*
|
|
189
|
+
* ```javascript
|
|
190
|
+
* class Categorization extends Model {
|
|
191
|
+
* static belongsTo = {
|
|
192
|
+
* tag: {
|
|
193
|
+
* inverse: 'categorization'
|
|
194
|
+
* },
|
|
195
|
+
* post: {
|
|
196
|
+
* inverse: 'categorization'
|
|
197
|
+
* }
|
|
198
|
+
* }
|
|
199
|
+
* }
|
|
200
|
+
*
|
|
201
|
+
* class Tag extends Model {
|
|
202
|
+
* static hasMany = {
|
|
203
|
+
* posts: {
|
|
204
|
+
* inverse: 'tags',
|
|
205
|
+
* through: 'categorizations'
|
|
206
|
+
* }
|
|
207
|
+
* };
|
|
208
|
+
* }
|
|
209
|
+
*
|
|
210
|
+
* class Post extends Model {
|
|
211
|
+
* static hasMany = {
|
|
212
|
+
* tags: {
|
|
213
|
+
* inverse: 'posts',
|
|
214
|
+
* through: 'categorizations'
|
|
215
|
+
* }
|
|
216
|
+
* };
|
|
217
|
+
* }
|
|
218
|
+
* ```
|
|
219
|
+
*
|
|
220
|
+
* @property hasMany
|
|
221
|
+
* @type {Object}
|
|
222
|
+
* @default {}
|
|
223
|
+
* @static
|
|
224
|
+
* @public
|
|
225
|
+
*/
|
|
226
|
+
static hasMany: Record<string, unknown>;
|
|
227
|
+
/**
|
|
228
|
+
* An object where you declare `belongsTo` relationships.
|
|
229
|
+
*
|
|
230
|
+
* When declaring a relationship you must specify the inverse of the
|
|
231
|
+
* relationship.
|
|
232
|
+
*
|
|
233
|
+
* ```javascript
|
|
234
|
+
* class Book extends Model {
|
|
235
|
+
* static belongsTo = {
|
|
236
|
+
* author: {
|
|
237
|
+
* inverse: 'books'
|
|
238
|
+
* // The line above lets Lumen know that this relationship is accessible
|
|
239
|
+
* // on author instances via `author.books`.
|
|
240
|
+
* }
|
|
241
|
+
* };
|
|
242
|
+
* }
|
|
243
|
+
*
|
|
244
|
+
* class Author extends Model {
|
|
245
|
+
* static hasMany = {
|
|
246
|
+
* books: {
|
|
247
|
+
* inverse: 'book'
|
|
248
|
+
* // The line above lets Lumen know that this relationship is accessible
|
|
249
|
+
* // on book instances via `book.author`.
|
|
250
|
+
* }
|
|
251
|
+
* };
|
|
252
|
+
* }
|
|
253
|
+
* ```
|
|
254
|
+
*
|
|
255
|
+
* If the name of the model is different than the key of the relationship, you
|
|
256
|
+
* must specify it in the relationship object.
|
|
257
|
+
*
|
|
258
|
+
* ```javascript
|
|
259
|
+
* class Book extends Model {
|
|
260
|
+
* static belongsTo = {
|
|
261
|
+
* writer: {
|
|
262
|
+
* inverse: 'books',
|
|
263
|
+
* model: 'author'
|
|
264
|
+
* // The line above lets Lumen know that this is a relationship with the
|
|
265
|
+
* // `Author` model and not a non-existent `Writer` model.
|
|
266
|
+
* }
|
|
267
|
+
* };
|
|
268
|
+
* }
|
|
269
|
+
* ```
|
|
270
|
+
*
|
|
271
|
+
* Sometimes our foreign keys in the database do not follow conventions (i.e
|
|
272
|
+
* `author_id`). You have the option to manually specify foreign keys when a
|
|
273
|
+
* situation like this occurs.
|
|
274
|
+
*
|
|
275
|
+
* ```javascript
|
|
276
|
+
* class Book extends Model {
|
|
277
|
+
* static belongsTo = {
|
|
278
|
+
* author: {
|
|
279
|
+
* inverse: 'books',
|
|
280
|
+
* foreignKey: 'SoMe_UnCoNvEnTiOnAl_FoReIgN_KeY'
|
|
281
|
+
* }
|
|
282
|
+
* };
|
|
283
|
+
* }
|
|
284
|
+
* ```
|
|
285
|
+
*
|
|
286
|
+
* @property belongsTo
|
|
287
|
+
* @type {Object}
|
|
288
|
+
* @default {}
|
|
289
|
+
* @static
|
|
290
|
+
* @public
|
|
291
|
+
*/
|
|
292
|
+
static belongsTo: Record<string, unknown>;
|
|
293
|
+
/**
|
|
294
|
+
* An object where you declare validations for an instance's attributes.
|
|
295
|
+
*
|
|
296
|
+
* Before a model instance is saved, validations declared in this block are
|
|
297
|
+
* executed. To declare a validation for a model attribute, simply add the
|
|
298
|
+
* attribute name as a key to the validates object. The value for the
|
|
299
|
+
* attribute key should be a function that takes a single argument (the value
|
|
300
|
+
* to validate against) and return a boolean value represent whether or not
|
|
301
|
+
* the attribute is valid.
|
|
302
|
+
*
|
|
303
|
+
* ```javascript
|
|
304
|
+
* class User extends Model {
|
|
305
|
+
* static validates {
|
|
306
|
+
* username: value => /^\w{2,30}$/.test(value),
|
|
307
|
+
* password: value => String(value).length >= 8
|
|
308
|
+
* };
|
|
309
|
+
* }
|
|
310
|
+
* ```
|
|
311
|
+
*
|
|
312
|
+
* In the spirit of have a small api surface area, Lumen provides no validation
|
|
313
|
+
* helper functions. You can roll your own helpers with or use one of the many
|
|
314
|
+
* excellent validation libraries like [validator](https://goo.gl/LWaHBB).
|
|
315
|
+
*
|
|
316
|
+
* ```javascript
|
|
317
|
+
* import { isEmail } from 'validator';
|
|
318
|
+
*
|
|
319
|
+
* class User extends Model {
|
|
320
|
+
* static validates {
|
|
321
|
+
* email: isEmail
|
|
322
|
+
* };
|
|
323
|
+
* }
|
|
324
|
+
* ```
|
|
325
|
+
*
|
|
326
|
+
* @property validates
|
|
327
|
+
* @type {Object}
|
|
328
|
+
* @default {}
|
|
329
|
+
* @static
|
|
330
|
+
* @public
|
|
331
|
+
*/
|
|
332
|
+
static validates: Record<string, unknown>;
|
|
333
|
+
/**
|
|
334
|
+
* An object where you declare custom query scopes for the model.
|
|
335
|
+
*
|
|
336
|
+
* Scopes allow you to DRY up query logic by chaining custom set's of queries
|
|
337
|
+
* with built-in query method such as `where`, `not`, `page`, etc. To declare
|
|
338
|
+
* a scope, add it as a method on the scopes object.
|
|
339
|
+
*
|
|
340
|
+
* ```javascript
|
|
341
|
+
* class Post extends Model {
|
|
342
|
+
* static hasMany = {
|
|
343
|
+
* tags: {
|
|
344
|
+
* inverse: 'posts'
|
|
345
|
+
* },
|
|
346
|
+
* comments: {
|
|
347
|
+
* inverse: 'post'
|
|
348
|
+
* }
|
|
349
|
+
* };
|
|
350
|
+
*
|
|
351
|
+
* static belongsTo = {
|
|
352
|
+
* user: {
|
|
353
|
+
* inverse: 'posts'
|
|
354
|
+
* }
|
|
355
|
+
* };
|
|
356
|
+
*
|
|
357
|
+
* static scopes = {
|
|
358
|
+
* isPublic() {
|
|
359
|
+
* return this.where({
|
|
360
|
+
* isPublic: true
|
|
361
|
+
* });
|
|
362
|
+
* },
|
|
363
|
+
*
|
|
364
|
+
* byUser(user) {
|
|
365
|
+
* return this.where({
|
|
366
|
+
* userId: user.id
|
|
367
|
+
* });
|
|
368
|
+
* },
|
|
369
|
+
*
|
|
370
|
+
* withEverything() {
|
|
371
|
+
* return this.includes('tags', 'user', 'comments');
|
|
372
|
+
* }
|
|
373
|
+
* };
|
|
374
|
+
* }
|
|
375
|
+
* ```
|
|
376
|
+
*
|
|
377
|
+
* Given the scopes declared in the example above, here is how we could return
|
|
378
|
+
* all the public posts with relationships eager loaded for the user with the
|
|
379
|
+
* id of 1.
|
|
380
|
+
*
|
|
381
|
+
* ```javascript
|
|
382
|
+
* const user = await User.find(1);
|
|
383
|
+
*
|
|
384
|
+
* return Post
|
|
385
|
+
* .byUser(user)
|
|
386
|
+
* .isPublic()
|
|
387
|
+
* .withEverything();
|
|
388
|
+
* ```
|
|
389
|
+
*
|
|
390
|
+
* Since scopes can be chained with built-in query methods, we can easily
|
|
391
|
+
* paginate this collection.
|
|
392
|
+
*
|
|
393
|
+
* ```javascript
|
|
394
|
+
* const user = await User.find(1);
|
|
395
|
+
*
|
|
396
|
+
* return Post
|
|
397
|
+
* .byUser(user)
|
|
398
|
+
* .isPublic()
|
|
399
|
+
* .withEverything()
|
|
400
|
+
* .page(1);
|
|
401
|
+
* ```
|
|
402
|
+
*
|
|
403
|
+
* @property scopes
|
|
404
|
+
* @type {Object}
|
|
405
|
+
* @default {}
|
|
406
|
+
* @static
|
|
407
|
+
* @public
|
|
408
|
+
*/
|
|
409
|
+
static scopes: Record<string, unknown>;
|
|
410
|
+
/**
|
|
411
|
+
* An object where you declare hooks to execute at certain times in a model
|
|
412
|
+
* instance's lifecycle.
|
|
413
|
+
*
|
|
414
|
+
* There are many lifecycle hooks that are executed through out a model
|
|
415
|
+
* instance's lifetime. The have many use cases such as sanitization of
|
|
416
|
+
* attributes, creating dependent relationships, hashing passwords, and much
|
|
417
|
+
* more.
|
|
418
|
+
*
|
|
419
|
+
* ##### Execution Order
|
|
420
|
+
*
|
|
421
|
+
* When creating a record.
|
|
422
|
+
*
|
|
423
|
+
* 1. beforeValidation
|
|
424
|
+
* 2. afterValidation
|
|
425
|
+
* 3. beforeCreate
|
|
426
|
+
* 4. beforeSave
|
|
427
|
+
* 5. afterCreate
|
|
428
|
+
* 6. afterSave
|
|
429
|
+
*
|
|
430
|
+
* When updating a record.
|
|
431
|
+
*
|
|
432
|
+
* 1. beforeValidation
|
|
433
|
+
* 2. afterValidation
|
|
434
|
+
* 3. beforeUpdate
|
|
435
|
+
* 4. beforeSave
|
|
436
|
+
* 5. afterUpdate
|
|
437
|
+
* 6. afterSave
|
|
438
|
+
*
|
|
439
|
+
* When deleting a record.
|
|
440
|
+
*
|
|
441
|
+
* 1. beforeDestroy
|
|
442
|
+
* 2. afterDestroy
|
|
443
|
+
*
|
|
444
|
+
* ##### Anatomy
|
|
445
|
+
*
|
|
446
|
+
* Hooks are async functions that are called with two arguments. The first
|
|
447
|
+
* argument is the record that the hook applies to and the second argument is
|
|
448
|
+
* the transaction object relevant to the method from which the hook was
|
|
449
|
+
* called.
|
|
450
|
+
*
|
|
451
|
+
* The only time you will need to use the transaction object is if you are
|
|
452
|
+
* creating, updating, or deleting different record(s) within the hook. Using
|
|
453
|
+
* the transaction object when modifying the database in a hook ensures that
|
|
454
|
+
* any modifications made within the hook will be rolled back if the function
|
|
455
|
+
* that initiated the transaction fails.
|
|
456
|
+
*
|
|
457
|
+
* ```javascript
|
|
458
|
+
* import Notification from 'app/models/notification';
|
|
459
|
+
*
|
|
460
|
+
* class Comment extends Model {
|
|
461
|
+
* static belongsTo = {
|
|
462
|
+
* post: {
|
|
463
|
+
* inverse: 'comments'
|
|
464
|
+
* },
|
|
465
|
+
* user: {
|
|
466
|
+
* inverse: 'comments'
|
|
467
|
+
* }
|
|
468
|
+
* };
|
|
469
|
+
*
|
|
470
|
+
* static hooks = {
|
|
471
|
+
* async afterCreate(comment, trx) {
|
|
472
|
+
* let [post, commenter] = await Promise.all([
|
|
473
|
+
* comment.post,
|
|
474
|
+
* comment.user
|
|
475
|
+
* ]);
|
|
476
|
+
*
|
|
477
|
+
* const commentee = await post.user;
|
|
478
|
+
*
|
|
479
|
+
* post = post.title;
|
|
480
|
+
* commenter = commenter.name;
|
|
481
|
+
*
|
|
482
|
+
* // Calling .transacting(trx) prevents the commentee from getting a
|
|
483
|
+
* // notification if the comment fails to be persisted in the database.
|
|
484
|
+
* await Notification
|
|
485
|
+
* .transacting(trx)
|
|
486
|
+
* .create({
|
|
487
|
+
* user: commentee,
|
|
488
|
+
* message: `${commenter} commented on your post "${post}"`
|
|
489
|
+
* });
|
|
490
|
+
* },
|
|
491
|
+
*
|
|
492
|
+
* async afterSave() {
|
|
493
|
+
* // Good thing you called transacting in afterCreate.
|
|
494
|
+
* throw new Error('Fatal Error');
|
|
495
|
+
* }
|
|
496
|
+
* };
|
|
497
|
+
* }
|
|
498
|
+
* ```
|
|
499
|
+
*
|
|
500
|
+
* @property hooks
|
|
501
|
+
* @type {Object}
|
|
502
|
+
* @default {}
|
|
503
|
+
* @static
|
|
504
|
+
* @public
|
|
505
|
+
*/
|
|
506
|
+
static hooks: Model$Hooks;
|
|
507
|
+
/**
|
|
508
|
+
* A reference to the application's logger.
|
|
509
|
+
*
|
|
510
|
+
* @property logger
|
|
511
|
+
* @type {Logger}
|
|
512
|
+
* @static
|
|
513
|
+
* @public
|
|
514
|
+
*/
|
|
515
|
+
static logger: Logger;
|
|
516
|
+
/**
|
|
517
|
+
* The name of the corresponding database table for the model.
|
|
518
|
+
*
|
|
519
|
+
* @property tableName
|
|
520
|
+
* @type {String}
|
|
521
|
+
* @static
|
|
522
|
+
* @public
|
|
523
|
+
*/
|
|
524
|
+
static tableName: string;
|
|
525
|
+
/**
|
|
526
|
+
* The canonical name of the model.
|
|
527
|
+
*
|
|
528
|
+
* @property modelName
|
|
529
|
+
* @type {String}
|
|
530
|
+
* @static
|
|
531
|
+
* @public
|
|
532
|
+
*/
|
|
533
|
+
static modelName: string;
|
|
534
|
+
/**
|
|
535
|
+
* The name of the resource the model represents.
|
|
536
|
+
*
|
|
537
|
+
* @property resourceName
|
|
538
|
+
* @type {String}
|
|
539
|
+
* @static
|
|
540
|
+
* @public
|
|
541
|
+
*/
|
|
542
|
+
static resourceName: string;
|
|
543
|
+
/**
|
|
544
|
+
* The column name to use for a model's primary key.
|
|
545
|
+
*
|
|
546
|
+
* @property primaryKey
|
|
547
|
+
* @type {String}
|
|
548
|
+
* @default 'id'
|
|
549
|
+
* @static
|
|
550
|
+
* @public
|
|
551
|
+
*/
|
|
552
|
+
static primaryKey: string;
|
|
553
|
+
/**
|
|
554
|
+
* @property table
|
|
555
|
+
* @type {Function}
|
|
556
|
+
* @static
|
|
557
|
+
* @private
|
|
558
|
+
*/
|
|
559
|
+
static table: () => unknown;
|
|
560
|
+
/**
|
|
561
|
+
* @property store
|
|
562
|
+
* @type {Database}
|
|
563
|
+
* @static
|
|
564
|
+
* @private
|
|
565
|
+
*/
|
|
566
|
+
static store: Database;
|
|
567
|
+
/**
|
|
568
|
+
* @property initialized
|
|
569
|
+
* @type {Boolean}
|
|
570
|
+
* @static
|
|
571
|
+
* @private
|
|
572
|
+
*/
|
|
573
|
+
static initialized: boolean;
|
|
574
|
+
/**
|
|
575
|
+
* @property serializer
|
|
576
|
+
* @type {Serializer}
|
|
577
|
+
* @static
|
|
578
|
+
* @private
|
|
579
|
+
*/
|
|
580
|
+
static serializer: Serializer<Model>;
|
|
581
|
+
/**
|
|
582
|
+
* @property attributes
|
|
583
|
+
* @type {Object}
|
|
584
|
+
* @static
|
|
585
|
+
* @private
|
|
586
|
+
*/
|
|
587
|
+
static attributes: Record<string, unknown>;
|
|
588
|
+
/**
|
|
589
|
+
* @property attributeNames
|
|
590
|
+
* @type {Array}
|
|
591
|
+
* @static
|
|
592
|
+
* @private
|
|
593
|
+
*/
|
|
594
|
+
static attributeNames: Array<string>;
|
|
595
|
+
/**
|
|
596
|
+
* @property relationships
|
|
597
|
+
* @type {Object}
|
|
598
|
+
* @static
|
|
599
|
+
* @private
|
|
600
|
+
*/
|
|
601
|
+
static relationships: Record<string, Relationship$opts>;
|
|
602
|
+
/**
|
|
603
|
+
* @property relationshipNames
|
|
604
|
+
* @type {Array}
|
|
605
|
+
* @static
|
|
606
|
+
* @private
|
|
607
|
+
*/
|
|
608
|
+
static relationshipNames: Array<string>;
|
|
609
|
+
constructor(attrs?: Record<string, unknown>, initialize?: boolean);
|
|
610
|
+
/**
|
|
611
|
+
* Indicates if the model is new.
|
|
612
|
+
*
|
|
613
|
+
* ```javascript
|
|
614
|
+
* import Post from 'app/models/post';
|
|
615
|
+
*
|
|
616
|
+
* let post = new Post({
|
|
617
|
+
* body: '',
|
|
618
|
+
* title: 'New Post',
|
|
619
|
+
* isPublic: false
|
|
620
|
+
* });
|
|
621
|
+
*
|
|
622
|
+
* post.isNew;
|
|
623
|
+
* // => true
|
|
624
|
+
*
|
|
625
|
+
* Post.create({
|
|
626
|
+
* body: '',
|
|
627
|
+
* title: 'New Post',
|
|
628
|
+
* isPublic: false
|
|
629
|
+
* }).then(post => {
|
|
630
|
+
* post.isNew;
|
|
631
|
+
* // => false;
|
|
632
|
+
* });
|
|
633
|
+
* ```
|
|
634
|
+
*
|
|
635
|
+
* @property isNew
|
|
636
|
+
* @type {Boolean}
|
|
637
|
+
* @public
|
|
638
|
+
*/
|
|
639
|
+
get isNew(): boolean;
|
|
640
|
+
/**
|
|
641
|
+
* Indicates if the model is dirty.
|
|
642
|
+
*
|
|
643
|
+
* ```javascript
|
|
644
|
+
* import Post from 'app/models/post';
|
|
645
|
+
*
|
|
646
|
+
* Post
|
|
647
|
+
* .find(1)
|
|
648
|
+
* .then(post => {
|
|
649
|
+
* post.isDirty;
|
|
650
|
+
* // => false
|
|
651
|
+
*
|
|
652
|
+
* post.isPublic = true;
|
|
653
|
+
*
|
|
654
|
+
* post.isDirty;
|
|
655
|
+
* // => true
|
|
656
|
+
*
|
|
657
|
+
* return post.save();
|
|
658
|
+
* })
|
|
659
|
+
* .then(post => {
|
|
660
|
+
* post.isDirty;
|
|
661
|
+
* // => false
|
|
662
|
+
* });
|
|
663
|
+
* ```
|
|
664
|
+
*
|
|
665
|
+
* @property isDirty
|
|
666
|
+
* @type {Boolean}
|
|
667
|
+
* @public
|
|
668
|
+
*/
|
|
669
|
+
get isDirty(): boolean;
|
|
670
|
+
/**
|
|
671
|
+
* Indicates if the model is persisted.
|
|
672
|
+
*
|
|
673
|
+
* ```javascript
|
|
674
|
+
* import Post from 'app/models/post';
|
|
675
|
+
*
|
|
676
|
+
* Post
|
|
677
|
+
* .find(1)
|
|
678
|
+
* .then(post => {
|
|
679
|
+
* post.persisted;
|
|
680
|
+
* // => true
|
|
681
|
+
*
|
|
682
|
+
* post.isPublic = true;
|
|
683
|
+
*
|
|
684
|
+
* post.persisted;
|
|
685
|
+
* // => false
|
|
686
|
+
*
|
|
687
|
+
* return post.save();
|
|
688
|
+
* })
|
|
689
|
+
* .then(post => {
|
|
690
|
+
* post.persisted;
|
|
691
|
+
* // => true
|
|
692
|
+
* });
|
|
693
|
+
* ```
|
|
694
|
+
*
|
|
695
|
+
* @property persisted
|
|
696
|
+
* @type {Boolean}
|
|
697
|
+
* @public
|
|
698
|
+
*/
|
|
699
|
+
get persisted(): boolean;
|
|
700
|
+
/**
|
|
701
|
+
* @property dirtyAttributes
|
|
702
|
+
* @type {Map}
|
|
703
|
+
* @public
|
|
704
|
+
*/
|
|
705
|
+
get dirtyAttributes(): Map<string, unknown>;
|
|
706
|
+
/**
|
|
707
|
+
* @property dirtyRelationships
|
|
708
|
+
* @type {Map}
|
|
709
|
+
* @public
|
|
710
|
+
*/
|
|
711
|
+
get dirtyRelationships(): Map<string, unknown>;
|
|
712
|
+
/**
|
|
713
|
+
* @property dirtyProperties
|
|
714
|
+
* @type {Map}
|
|
715
|
+
* @private
|
|
716
|
+
*/
|
|
717
|
+
get dirtyProperties(): Map<string, unknown>;
|
|
718
|
+
/**
|
|
719
|
+
* @property currentChangeSet
|
|
720
|
+
* @type {ChangeSet}
|
|
721
|
+
* @private
|
|
722
|
+
*/
|
|
723
|
+
get currentChangeSet(): ChangeSet;
|
|
724
|
+
/**
|
|
725
|
+
* @property currentChangeSet
|
|
726
|
+
* @type {void | ChangeSet}
|
|
727
|
+
* @private
|
|
728
|
+
*/
|
|
729
|
+
get persistedChangeSet(): ChangeSet | undefined;
|
|
730
|
+
/**
|
|
731
|
+
* Specify the transaction object to use for following save, update, or
|
|
732
|
+
* destroy method calls.
|
|
733
|
+
*
|
|
734
|
+
* When you call a method like update or destroy, lumen will create a
|
|
735
|
+
* transaction and wrap the internals of the method and other downstream
|
|
736
|
+
* method calls like model hooks within. In some edge cases it can be more
|
|
737
|
+
* useful to manually initiate the transaction. Bulk updating or destroying
|
|
738
|
+
* are good examples of this. When you manually begin a transaction, you can
|
|
739
|
+
* call this method to specify the transaction object that you would like to
|
|
740
|
+
* use for subsequent mutation methods (save, update, destroy, etc.) so lumen
|
|
741
|
+
* knows not to automatically begin a new transaction if/when a mutation
|
|
742
|
+
* method is called.
|
|
743
|
+
*
|
|
744
|
+
* ```javascript
|
|
745
|
+
* const post = await Post.first();
|
|
746
|
+
*
|
|
747
|
+
* // This call to update uses the transaction that lumen will initiate.
|
|
748
|
+
* await post.update({
|
|
749
|
+
* // updates to post...
|
|
750
|
+
* });
|
|
751
|
+
*
|
|
752
|
+
* await post.transaction(trx => {
|
|
753
|
+
* // This call to update uses the transaction that we created with the
|
|
754
|
+
* // call to the transaction method.
|
|
755
|
+
* return post
|
|
756
|
+
* .transacting(trx)
|
|
757
|
+
* .update({
|
|
758
|
+
* // updates to post...
|
|
759
|
+
* });
|
|
760
|
+
* });
|
|
761
|
+
* ```
|
|
762
|
+
*
|
|
763
|
+
* @method transacting
|
|
764
|
+
* @param {Transaction} transaction - A transaction object to forward to save,
|
|
765
|
+
* update, or destroy method calls.
|
|
766
|
+
* @return {Model} - Returns a proxied version of `this` that delagates the
|
|
767
|
+
* transaction param to subsquent save, update, or destroy method calls.
|
|
768
|
+
* @public
|
|
769
|
+
*/
|
|
770
|
+
transacting(trx: unknown): this;
|
|
771
|
+
/**
|
|
772
|
+
* Manually begin a new transaction.
|
|
773
|
+
*
|
|
774
|
+
* Most of the time, you don't need to start transactions yourself. However,
|
|
775
|
+
* if you need to do something like implement bulk updating of related records
|
|
776
|
+
* the transaction method can be useful.
|
|
777
|
+
*
|
|
778
|
+
* ```javascript
|
|
779
|
+
* const post = await Post.first().include('user');
|
|
780
|
+
* const user = await post.user;
|
|
781
|
+
*
|
|
782
|
+
* await post.transaction(trx => {
|
|
783
|
+
* return Promise.all([
|
|
784
|
+
* post.transacting(trx).update({
|
|
785
|
+
* // updates to post...
|
|
786
|
+
* }),
|
|
787
|
+
* user.transacting(trx).update({
|
|
788
|
+
* // updates to user...
|
|
789
|
+
* })
|
|
790
|
+
* ]);
|
|
791
|
+
* });
|
|
792
|
+
* ```
|
|
793
|
+
*
|
|
794
|
+
* @method transaction
|
|
795
|
+
* @param {Function} fn - The function used for executing the tranasction.
|
|
796
|
+
* This function is called with a new transaction object as it's only argument
|
|
797
|
+
* and is expected to return a promise.
|
|
798
|
+
* @return {Promise} Resolves with the resolved value of the fn param.
|
|
799
|
+
* @public
|
|
800
|
+
*/
|
|
801
|
+
transaction<T>(fn: (...args: Array<unknown>) => Promise<T>): Promise<T>;
|
|
802
|
+
/**
|
|
803
|
+
* Persist any unsaved changes to the database.
|
|
804
|
+
*
|
|
805
|
+
* ```javascript
|
|
806
|
+
* const post = await Post.first();
|
|
807
|
+
*
|
|
808
|
+
* console.log(post.title, post.isDirty);
|
|
809
|
+
* // => 'New Post' false
|
|
810
|
+
*
|
|
811
|
+
* post.title = 'How to Save a Lumen Model';
|
|
812
|
+
*
|
|
813
|
+
* console.log(post.title, post.isDirty);
|
|
814
|
+
* // => 'How to Update a Lumen Model' true
|
|
815
|
+
*
|
|
816
|
+
* await post.save();
|
|
817
|
+
*
|
|
818
|
+
* console.log(post.title, post.isDirty);
|
|
819
|
+
* // => 'How to Save a Lumen Model' false
|
|
820
|
+
* ```
|
|
821
|
+
*
|
|
822
|
+
* @method save
|
|
823
|
+
* @return {Promise} Resolves with `this`.
|
|
824
|
+
* @public
|
|
825
|
+
*/
|
|
826
|
+
save(transaction?: unknown): Promise<Transaction$ResultProxy<this, boolean>>;
|
|
827
|
+
/**
|
|
828
|
+
* Assign values to the instance and persist any changes to the database.
|
|
829
|
+
*
|
|
830
|
+
* ```javascript
|
|
831
|
+
* const post = await Post.first();
|
|
832
|
+
*
|
|
833
|
+
* console.log(post.title, post.isPublic, post.isDirty);
|
|
834
|
+
* // => 'New Post' false false
|
|
835
|
+
*
|
|
836
|
+
* await post.update({
|
|
837
|
+
* title: 'How to Update a Lumen Model',
|
|
838
|
+
* isPublic: true
|
|
839
|
+
* });
|
|
840
|
+
*
|
|
841
|
+
* console.log(post.title, post.isPublic, post.isDirty);
|
|
842
|
+
* // => 'How to Update a Lumen Model' true false
|
|
843
|
+
* ```
|
|
844
|
+
*
|
|
845
|
+
* @method update
|
|
846
|
+
* @param {Object} properties - An object containing key, value pairs of the
|
|
847
|
+
* attributes and/or relationships you would like to assign to the instance.
|
|
848
|
+
* @return {Promise} Resolves with `this`.
|
|
849
|
+
* @public
|
|
850
|
+
*/
|
|
851
|
+
update(props?: Record<string, unknown>, transaction?: unknown): Promise<Transaction$ResultProxy<this, boolean>>;
|
|
852
|
+
/**
|
|
853
|
+
* Permanently delete the instance from the database.
|
|
854
|
+
*
|
|
855
|
+
* @method destroy
|
|
856
|
+
* @return {Promise} Resolves with `this`.
|
|
857
|
+
* @public
|
|
858
|
+
*/
|
|
859
|
+
destroy(transaction?: unknown): Promise<Transaction$ResultProxy<this, true>>;
|
|
860
|
+
/**
|
|
861
|
+
* Reload the record from the database.
|
|
862
|
+
*
|
|
863
|
+
* @method reload
|
|
864
|
+
* @return {Promise} Resolves with `this`.
|
|
865
|
+
* @public
|
|
866
|
+
*/
|
|
867
|
+
reload(): Promise<Model>;
|
|
868
|
+
/**
|
|
869
|
+
* Rollback attributes and relationships to the last known persisted set of
|
|
870
|
+
* values.
|
|
871
|
+
*
|
|
872
|
+
* @method rollback
|
|
873
|
+
* @return {Model} Returns `this`.
|
|
874
|
+
* @public
|
|
875
|
+
*/
|
|
876
|
+
rollback(): this;
|
|
877
|
+
/**
|
|
878
|
+
* @method getAttributes
|
|
879
|
+
* @param {String} [...keys] - The keys of the properties to return.
|
|
880
|
+
* @return {Object} An object containing keys that were passed in as agruments
|
|
881
|
+
* and their associated values.
|
|
882
|
+
* @private
|
|
883
|
+
*/
|
|
884
|
+
getAttributes(...keys: Array<string>): Record<string, unknown>;
|
|
885
|
+
/**
|
|
886
|
+
* @method getPrimaryKey
|
|
887
|
+
* @return {Number} The value of the primary key for the instance.
|
|
888
|
+
* @private
|
|
889
|
+
*/
|
|
890
|
+
getPrimaryKey(): number;
|
|
891
|
+
/**
|
|
892
|
+
* Create and persist a new instance of the model.
|
|
893
|
+
*
|
|
894
|
+
* @method create
|
|
895
|
+
* @param {Object} properties - An object containing key, value pairs of the
|
|
896
|
+
* attributes and/or relationships you would like to assign to the instance.
|
|
897
|
+
* @return {Promise} Resolves with the newly created model.
|
|
898
|
+
* @static
|
|
899
|
+
* @public
|
|
900
|
+
*/
|
|
901
|
+
static create(props?: Record<string, unknown>, transaction?: unknown): Promise<Transaction$ResultProxy<Model, true>>;
|
|
902
|
+
/**
|
|
903
|
+
* Specify the transaction object to use for following save, update, or
|
|
904
|
+
* destroy method calls.
|
|
905
|
+
*
|
|
906
|
+
* When you call a method like update or destroy, lumen will create a
|
|
907
|
+
* transaction and wrap the internals of the method and other downstream
|
|
908
|
+
* method calls like model hooks within. In some edge cases it can be more
|
|
909
|
+
* useful to manually initiate the transaction. Bulk updating or destroying
|
|
910
|
+
* are good examples of this. When you manually begin a transaction, you can
|
|
911
|
+
* call this method to specify the transaction object that you would like to
|
|
912
|
+
* use for calls to the static create method so lumen knows not to automatically
|
|
913
|
+
* begin a new transaction if/when the static create method is called.
|
|
914
|
+
*
|
|
915
|
+
* ```javascript
|
|
916
|
+
* // This call to create uses the transaction that lumen will initiate.
|
|
917
|
+
* await Post.create();
|
|
918
|
+
*
|
|
919
|
+
* await Post.transaction(trx => {
|
|
920
|
+
* // This call to create uses the transaction that we created with the
|
|
921
|
+
* // call to the transaction method.
|
|
922
|
+
* return Post
|
|
923
|
+
* .transacting(trx)
|
|
924
|
+
* .create();
|
|
925
|
+
* });
|
|
926
|
+
* ```
|
|
927
|
+
*
|
|
928
|
+
* @method transacting
|
|
929
|
+
* @param {Transaction} transaction - A transaction object to forward to
|
|
930
|
+
* create method calls.
|
|
931
|
+
* @return {Model} - Returns a proxied version of `this` that delagates the
|
|
932
|
+
* transaction param to subsquent create method calls.
|
|
933
|
+
* @static
|
|
934
|
+
* @public
|
|
935
|
+
*/
|
|
936
|
+
static transacting(trx: unknown): ModelClass;
|
|
937
|
+
/**
|
|
938
|
+
* Manually begin a new transaction.
|
|
939
|
+
*
|
|
940
|
+
* Most of the time, you don't need to start transactions yourself. However,
|
|
941
|
+
* the transaction method can be useful if you need to do something like
|
|
942
|
+
* bulk creating records.
|
|
943
|
+
*
|
|
944
|
+
* ```javascript
|
|
945
|
+
* await Post.transaction(trx => {
|
|
946
|
+
* return Promise.all([
|
|
947
|
+
* Post.transacting(trx).create({
|
|
948
|
+
* // ...props
|
|
949
|
+
* }),
|
|
950
|
+
* Post.transacting(trx).create({
|
|
951
|
+
* // ...props
|
|
952
|
+
* })
|
|
953
|
+
* ]);
|
|
954
|
+
* });
|
|
955
|
+
* ```
|
|
956
|
+
*
|
|
957
|
+
* @method transaction
|
|
958
|
+
* @param {Function} fn - The function used for executing the tranasction.
|
|
959
|
+
* This function is called with a new transaction object as it's only argument
|
|
960
|
+
* and is expected to return a promise.
|
|
961
|
+
* @return {Promise} Resolves with the resolved value of the fn param.
|
|
962
|
+
* @static
|
|
963
|
+
* @public
|
|
964
|
+
*/
|
|
965
|
+
static transaction<T>(fn: (...args: Array<unknown>) => Promise<T>): Promise<T>;
|
|
966
|
+
static all(): Query<Array<Model>>;
|
|
967
|
+
static find(primaryKey: unknown): Query<Model>;
|
|
968
|
+
static page(num: number): Query<Array<Model>>;
|
|
969
|
+
static limit(amount: number): Query<Array<Model>>;
|
|
970
|
+
static offset(amount: number): Query<Array<Model>>;
|
|
971
|
+
static count(): Query<number>;
|
|
972
|
+
static order(attr: string, direction?: string): Query<Array<Model>>;
|
|
973
|
+
static where(conditions: Record<string, unknown>): Query<Array<Model>>;
|
|
974
|
+
static whereBetween(conditions: Record<string, unknown>): Query<Array<Model>>;
|
|
975
|
+
static whereRaw(query: string, bindings?: Array<unknown>): Query<Array<Model>>;
|
|
976
|
+
static not(conditions: Record<string, unknown>): Query<Array<Model>>;
|
|
977
|
+
static first(): Query<Model>;
|
|
978
|
+
static last(): Query<Model>;
|
|
979
|
+
static select(...params: Array<string>): Query<Array<Model>>;
|
|
980
|
+
static distinct(...params: Array<string>): Query<Array<Model>>;
|
|
981
|
+
static include(...relationships: Array<string | Record<string, unknown>>): Query<Array<Model>>;
|
|
982
|
+
static unscope(...scopes: Array<string>): Query<Array<Model>>;
|
|
983
|
+
/**
|
|
984
|
+
* Check if a model has a scope.
|
|
985
|
+
*
|
|
986
|
+
* @method hasScope
|
|
987
|
+
* @param {String} name - The name of the scope to look for.
|
|
988
|
+
* @return {Boolean}
|
|
989
|
+
* @static
|
|
990
|
+
* @public
|
|
991
|
+
*/
|
|
992
|
+
static hasScope(name: string): boolean;
|
|
993
|
+
/**
|
|
994
|
+
* Check if a value is an instance of a model.
|
|
995
|
+
*
|
|
996
|
+
* @method isInstance
|
|
997
|
+
* @param {any} value - The value in question.
|
|
998
|
+
* @return {Boolean}
|
|
999
|
+
* @static
|
|
1000
|
+
* @public
|
|
1001
|
+
*/
|
|
1002
|
+
static isInstance(value: unknown): boolean;
|
|
1003
|
+
/**
|
|
1004
|
+
* Bind the model's connection to the database and get inferred data from the
|
|
1005
|
+
* schema upon application boot.
|
|
1006
|
+
*
|
|
1007
|
+
* @method initialize
|
|
1008
|
+
* @param {Database} store - A reference of the applications database
|
|
1009
|
+
* instance.
|
|
1010
|
+
* @param {Table} table - A function that returns a knex query builder bound
|
|
1011
|
+
* to the model's table name.
|
|
1012
|
+
* @return {Promise} Resolves with the model class.
|
|
1013
|
+
* @static
|
|
1014
|
+
* @private
|
|
1015
|
+
*/
|
|
1016
|
+
static initialize(store: Database, table: () => unknown): Promise<ModelClass>;
|
|
1017
|
+
/**
|
|
1018
|
+
* @method columnFor
|
|
1019
|
+
* @param {String} key - The respective attribute name of the column.
|
|
1020
|
+
* @return {void | Object} An object containing metadata about the column if a
|
|
1021
|
+
* match is found.
|
|
1022
|
+
* @static
|
|
1023
|
+
* @private
|
|
1024
|
+
*/
|
|
1025
|
+
static columnFor(key: string): Database$column | undefined;
|
|
1026
|
+
/**
|
|
1027
|
+
* @method columnNameFor
|
|
1028
|
+
* @param {String} key - The respective attribute name of the column.
|
|
1029
|
+
* @return {void | String} The name of the column in the database if a match
|
|
1030
|
+
* is found.
|
|
1031
|
+
* @static
|
|
1032
|
+
* @private
|
|
1033
|
+
*/
|
|
1034
|
+
static columnNameFor(key: string): string | undefined;
|
|
1035
|
+
/**
|
|
1036
|
+
* @method relationshipFor
|
|
1037
|
+
* @param {String} key - The name of the relationship to match against.
|
|
1038
|
+
* @return {void | Object} An object containing relationship metadata if a
|
|
1039
|
+
* match is found.
|
|
1040
|
+
* @static
|
|
1041
|
+
* @private
|
|
1042
|
+
*/
|
|
1043
|
+
static relationshipFor(key: string): Relationship$opts | undefined;
|
|
1044
|
+
}
|
|
1045
|
+
export default Model;
|
|
1046
|
+
export type { Model$Hook, Model$Hooks } from './interfaces';
|