Socket
Socket
Sign inDemoInstall

constructs

Package Overview
Dependencies
0
Maintainers
3
Versions
1334
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    constructs

A programming model for composable configuration


Version published
Weekly downloads
2.3M
increased by0.22%
Maintainers
3
Created
Weekly downloads
 

Package description

What is constructs?

The 'constructs' npm package provides a framework for defining cloud infrastructure in code by composing and sharing reusable components known as constructs. Constructs can represent low-level resources such as a virtual machine, a database, or a piece of code, as well as higher-level abstractions that are composed of these resources. The package is designed to be used with the AWS Cloud Development Kit (CDK) but is also general enough to be used in other contexts.

What are constructs's main functionalities?

Defining Constructs

This feature allows developers to define their own constructs by extending the Construct class. Constructs can encapsulate any number of child constructs and resources, providing a way to create reusable and composable infrastructure components.

{"const { Construct } = require('constructs');\n\nclass MyConstruct extends Construct {\n  constructor(scope, id) {\n    super(scope, id);\n    // Define resources and other constructs here\n  }\n}\n\nconst app = new Construct();\nconst myConstruct = new MyConstruct(app, 'MyConstructId');"}

Composing Constructs

This feature demonstrates how to compose constructs within other constructs, allowing for the creation of complex infrastructure systems by nesting constructs.

{"const { Construct } = require('constructs');\n\nclass MyConstruct extends Construct {\n  // ...\n}\n\nclass MyCompositeConstruct extends Construct {\n  constructor(scope, id) {\n    super(scope, id);\n    new MyConstruct(this, 'NestedConstruct');\n  }\n}\n\nconst app = new Construct();\nconst myComposite = new MyCompositeConstruct(app, 'MyCompositeConstructId');"}

Synthesizing Constructs

This feature shows how to synthesize a construct, which means to generate a representation of the construct's state, typically for the purpose of deploying infrastructure. The 'synthesize' method is part of the 'Node' class, which provides context and lifecycle methods for constructs.

{"const { Construct, Node } = require('constructs');\n\nclass MyConstruct extends Construct {\n  // ...\n}\n\nconst app = new Construct();\nconst myConstruct = new MyConstruct(app, 'MyConstructId');\n\nconst synthesized = Node.of(myConstruct).synthesize();\nconsole.log(synthesized);"}

Other packages similar to constructs

Readme

Source

Constructs Programming Model

Define composable configuration models through code

Release npm version PyPI version NuGet version Maven Central

User Manual

Scope Relocation

The path of a scope is used as a seed for all names generated within that scope such as logical names in the AWS CDK or node.uniqueId.

There are use cases in which an entire construct subtree (scope) needs to be "relocated" to a different path. The main use case is in order to allow refactoring while preserving old names.

Be careful: relocating a scope may result in duplicate names. Use at your own risk.

To relocate a scope, use the node.relocate(root) method. This method can only be called before any children are added to the scope and it will impact the value of node.path and node.uniqueId.

If implementing name generation, make sure to refer to node.path as the root of the scope's path instead of node.scopes.

Let's say we had a scope foo that was originally under the foo path and it has a child called childOfFoo:

const foo = new Construct(root, 'foo');
const childOfFoo = new Construct(foo, 'childOfFoo');
expect(Node.of(foo).path).toBe('foo');
expect(Node.of(childOfFoo).path).toBe('foo/childOfFoo');

Now, we want to refactor our code and hoist it under a new root, say baz:

const baz = new Construct(root, 'baz');
const foo = new Construct(baz, 'foo');
const childOfFoo = new Construct(foo, 'childOfFoo');

// now the paths (andu uniqudIds) are different
expect(Node.of(foo).path).toBe('bar/foo');
expect(Node.of(childOfFoo).path).toBe('baz/foo/childOfFoo');

If we relocate foo to foo, the original paths are preserved:

const baz = new Construct(root, 'baz');
const foo = new Construct(baz, 'foo');
Node.of(construct).relocate('foo'); // must be done before adding children

const childOfFoo = new Construct(foo, 'childOfFoo');

// now the paths (andu uniqudIds) are different
expect(Node.of(foo).path).toBe('foo');
expect(Node.of(childOfFoo).path).toBe('foo/childOfFoo');

Similarly, the uniqueId of a construct will be derived from this path.

Contributing

This project has adopted the Amazon Open Source Code of Conduct.

We welcome community contributions and pull requests. See our contribution guide for more information on how to report issues, set up a development environment and submit code.

License

This project is distributed under the Apache License, Version 2.0.

Keywords

FAQs

Last updated on 15 Jul 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc